我正在使用 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'”。我是否在代码中犯了错误或错过了所需的步骤?