c++ - GLSL 错误 : failed to preprocess the source. 我该如何解决这个问题?

标签 c++ opengl glsl

我正在尝试学习使用 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;
}

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

最佳答案

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

"#version 140uniform float t;"

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

关于c++ - GLSL 错误 : failed to preprocess the source. 我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2555389/

相关文章:

c++ - 从应用程序中获取应用程序版本

opengl - 问题包括C++程序中的GLFW header

opengl - OpenGL 有预着色器吗?

c - 更好地控制 OpenGL 中的曲面 segmentation ?

qt - 如何在 QT OpenGL Widget 上使用 OpenGL 函数?

opengl - 为什么除法产生的结果与 float 乘以分数的结果截然不同

c++ - 如何解决多线程代码中for循环的依赖关系?

c++ - 简单的指针初始化

c++ - 插入实现 'Linear Hashing'

c++ - “swizzle”不是 'glm' 的成员