未声明的标识符'gl_Position'

时间:2015-06-05 19:37:38

标签: c++ opengl shader

我正在训练用cpp做opengl的东西,但我的程序中有一个错误:/(vertexShader)

这是vertexShader的代码:

void main(void)
{
    gl_Position = gl_Vertex; 
}

这里有一个调用着色器的主cpp文件:

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>

static char* readFile(const char* filename) {
    // Open the file
    FILE* fp = fopen(filename, "rb");
    // Move the file pointer to the end of the file and determing the length
    fseek(fp, 0, SEEK_END);
    long file_length = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char* contents = new char[file_length + 1];
    // zero out memory
    for (int i = 0; i < file_length + 1; i++) {
        contents[i] = 0;
    }
    // Here's the actual read
    fread(contents, 1, file_length, fp);
    // This is how you denote the end of a string in C
    contents[file_length + 1] = '\0';
    fclose(fp);
    return contents;
}

GLuint makeVertexShader(const char* shaderSource) {
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(vertexShaderID);
    return vertexShaderID;
}

GLuint makeFragmentShader(const char* shaderSource) {
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(fragmentShaderID);
    return fragmentShaderID;
}

GLuint makeShaderProgram(GLuint vertexShaderID, GLuint fragmentShaderID) {
    GLuint shaderID = glCreateProgram();
    glAttachShader(shaderID, vertexShaderID);
    glAttachShader(shaderID, fragmentShaderID);
    glLinkProgram(shaderID);
    return shaderID;
}

int main(int argc, char** argv) {
    // Standard stuff...
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Shaders");
    glewInit();

    char* fragmentShaderSourceCode = readFile("fragmentShader.txt");
    char* vertexShaderSourceCode = readFile("vertexShader.txt");
    GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
    GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
    GLuint shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);
    glUseProgram(shaderProgramID);
    printf("vertShaderID is %d\n", vertShaderID);
    printf("fragShaderID is %d\n", fragShaderID);
    printf("shaderProgramID is %d\n", shaderProgramID);
    glDeleteProgram(shaderProgramID);
    int temp;
    scanf("%d", &temp);
    return 0;
}

错误:vertexShader中未声明的标识符'gl_Position' 我正在使用visual studio 2015,Windows 8,intel cpu,amd gpu。

1 个答案:

答案 0 :(得分:0)

这与我上周回答的一个问题(GLSL Error #132 Syntax error: "gl_position" parse error)非常相似,可能是相同的解决方案,重新声明应该预定义的内置插件:

out gl_PerVertex { vec4 gl_Position; };

在那种情况下(或在这种情况下),不清楚为什么需要这样做。只应在某些情况下才需要它,例如GL_ARB_separate_shader_objects扩展名。但是,它可能是AMD驱动程序的问题,因为他们也有AMD显卡。