I'm trying to learn to play with OpenGL GLSL shaders. I've written a very simple program to simply create a shader and compile it. However, whenever I get to the compile step, I get the error:
Error: Preprocessor error Error: failed to preprocess the source.
Here's my very simple code:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
const GLchar* gravity_shader[] = {
"#version 140"
"uniform float t;"
"uniform mat4 MVP;"
"in vec4 pos;"
"in vec4 vel;"
"const vec4 g = vec4(0.0, 0.0, -9.80, 0.0);"
"void main() {"
" vec4 position = pos;"
" position += t*vel + t*t*g;"
" gl_Position = MVP * position;"
"}"
};
double pointX = (double)screenWidth/2.0;
double pointY = (double)screenWidth/2.0;
void initShader() {
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader, 1, gravity_shader, NULL);
glCompileShader(shader);
GLint compiled = true;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if(!compiled) {
GLint length;
GLchar* log;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
log = (GLchar*)malloc(length);
glGetShaderInfoLog(shader, length, &length, log);
std::cout << log <<std::endl;
free(log);
}
exit(0);
}
bool myInit() {
initShader();
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(1.0);
glLineWidth(1.0f);
glMatrixMode(GL_开发者_如何学JAVAPROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) screenWidth, 0.0, (GLdouble) screenHeight);
glEnable(GL_DEPTH_TEST);
return true;
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 150);
glutCreateWindow("Mouse Interaction Display");
myInit();
glutMainLoop();
return 0;
}
Where am I going wrong? If it helps, I am trying to do this on a Acer Aspire One with an atom processor and integrated Intel video running the latest Ubuntu. It's not very powerful, but then again, this is a very simple shader. Thanks a lot for taking a look!
I suspect this is one of those problems that's so basic you're looking right past it. After the C++ compiler does string concatenation, the beginning of your shader source code will look like this:
"#version 140uniform float t;"
I'm pretty sure it's complaining because "140uniform" doesn't fit its idea of a number.
If you're just starting out you may be better off by running against the Mesa software renderer, which should provide full (though slow) OpenGL 2.1 support.
精彩评论