2

我正在尝试学习使用 OpenGL GLSL 着色器。我编写了一个非常简单的程序来简单地创建一个着色器并编译它。但是,每当我进入编译步骤时,我都会收到错误消息:

错误:预处理器错误错误:无法预处理源。

这是我非常简单的代码:

#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_PROJECTION);
    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;
}

我哪里错了?如果有帮助,我会尝试在 Acer Aspire One 上执行此操作,该 Aspire One 配备 atom 处理器和集成英特尔视频,运行最新的 Ubuntu。它不是很强大,但话又说回来,这是一个非常简单的着色器。非常感谢您的观看!

4

2 回答 2

7

我怀疑这是您正在寻找的那些非常基本的问题之一。在 C++ 编译器执行字符串连接之后,着色器源代码的开头将如下所示:

"#version 140uniform float t;"

我很确定它在抱怨,因为“140uniform”不符合它的数字概念。

于 2010-03-31T18:50:38.903 回答
2

如果你刚刚开始,你可能会更好地运行Mesa软件渲染器,它应该提供完整的(虽然很慢)OpenGL 2.1 支持。

于 2010-03-31T18:34:08.507 回答