问题
I'm using Eclipse, MinGW, and I have compiled freeglut and glew and copied their libraries, DLLs, and header files into the proper locations (MinGW/include/GL, MinGW/lib, Windows/SysWOW64). I have also linked the libraries (freeglut64, opengl32, glew32, glu32) in Eclipse under the MinGW C++ Linker. Here is my code...
--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--
I believe I have done everything right, however, Eclipse throws the error "Type 'GLchar' could not be resolved" on VertexShader and FragmentShader. Did I make a mistake in my code or miss a required step?
回答1:
You're not declaring string constants the right way. You don't need curly braces there. And watch out for empty lines betwreen the strings - either remove them (see below) or add the "\" there.
Include the "GL/GL.h" file before "glew.h", this might help also.
Use this with ordinary chars:
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";
回答2:
This is just expanding on my comment on another answer:
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;
}
)";
来源:https://stackoverflow.com/questions/10886056/type-glchar-could-not-be-resolved