1

我正在使用 Eclipse、MinGW,并且我已经编译了 freeglut 和 glew,并将它们的库、DLL 和头文件复制到了正确的位置(MinGW/include/GL、MinGW/lib、Windows/SysWOW64)。我还在 MinGW C++ 链接器下链接了 Eclipse 中的库(freeglut64、opengl32、glew32、glu32)。这是我的代码...

--CharGame.cpp--
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "CharGame.h"
#define WINDOW_TITLE "CharGame"

int CurrentWidth = 800,
    CurrentHeight = 600,
    WindowHandle = 0;
unsigned FrameCount = 0;
GLuint VertexShaderId,
    FragmentShaderId,
    ProgramId,
    VaoId,
    VboId,
    ColorBufferId;

int main(int argc, char* argv[]) {
    Initialize(argc, argv);
    glutMainLoop();
    exit(EXIT_SUCCESS);
}
const GLchar* VertexShader =
{
    "#version 400\n"\

    "layout(location=0) in vec4 in_Position;\n"\
    "layout(location=1) in vec4 in_Color;\n"\
    "out vec4 ex_Color;\n"\

    "void main(void)\n"\
    "{\n"\
    "   gl_Position = in_Position;\n"\
    "   ex_Color = in_Color;\n"\
    "}\n"
};
const GLchar* FragmentShader =
{
    "#version 400\n"\

    "in vec4 ex_Color;\n"\
    "out vec4 out_Color;\n"\

    "void main(void)\n"\
    "{\n"\
    "   out_Color = ex_Color;\n"\
    "}\n"
};

void Initialize(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitContextVersion(4, 0);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
    glutInitWindowSize(CurrentWidth, CurrentHeight);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    WindowHandle = glutCreateWindow(WINDOW_TITLE);
    if (WindowHandle < 1) {
        fprintf(stderr, "Error: Could not create a render window.\n");
        exit(EXIT_FAILURE);
    }
    glutReshapeFunc(Resize);
    glutDisplayFunc(Render);
    glutIdleFunc(Idle);
    glutTimerFunc(0, Timer, 0);

    fprintf(stdout, "Info: OpenGL Version: %s\n", glGetString(GL_VERSION));
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

}

void Resize(int width, int height) {
    CurrentHeight = height;
    CurrentWidth = width;
    glViewport(0, 0, CurrentWidth, CurrentHeight);
}

void Render(void) {
    ++FrameCount;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glutSwapBuffers();
    glutPostRedisplay();
}

void Idle(void) {
    glutPostRedisplay();
}

void Timer(int value) {
    if (0 != value) {
        char* TempString = (char*) malloc(512 + strlen(WINDOW_TITLE));
        sprintf(TempString, "%s: %d Frames Per Second @ %d x %d", WINDOW_TITLE, FrameCount*4, CurrentWidth, CurrentHeight);
        glutSetWindowTitle(TempString);
        free(TempString);
    }
    FrameCount = 0;
    glutTimerFunc(250, Timer, 1);
}
--End CharGame.cpp--

--CharGame.h--
#ifndef CHARGAME_H_
#define CHARGAME_H_

void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void Resize(int,int);
void Render(void);
void Timer(int);
void Idle(void);

#endif
--End CharGame.h--

我相信我所做的一切都是正确的,但是,Eclipse 在 VertexShader 和 FragmentShader 上抛出错误“无法解析类型 'GLchar'”。我是否在代码中犯了错误或错过了所需的步骤?

4

2 回答 2

2

您没有以正确的方式声明字符串常量。那里不需要花括号。并注意字符串之间的空行 - 删除它们(见下文)或在那里添加“\”。

在“glew.h”之前包含“GL/GL.h”文件,这也可能有帮助。

将其与普通字符一起使用:

const char* VertexShader =
"#version 400\n"\
"layout(location=0) in vec4 in_Position;\n"\
"layout(location=1) in vec4 in_Color;\n"\
"out vec4 ex_Color;\n"\
"void main(void)\n"\
"{\n"\
"   gl_Position = in_Position;\n"\
"   ex_Color = in_Color;\n"\
"}\n";

const char* FragmentShader =
"#version 400\n"\
"in vec4 ex_Color;\n"\
"out vec4 out_Color;\n"\
"void main(void)\n"\
"{\n"\
"   out_Color = ex_Color;\n"\
"}\n";
于 2012-06-04T18:14:43.637 回答
1

这只是在扩展我对另一个答案的评论:

const char* VertexShader =
R"(#version 400
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;
void main(void)
{
   gl_Position = in_Position;
   ex_Color = in_Color;
}
)";
于 2012-06-04T18:41:45.970 回答