簡體   English   中英

未聲明的標識符'gl_Position'

[英]Undeclared identifier 'gl_Position'

我正在訓練用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。

這與我上周回答的問題非常類似( GLSL錯誤#132語法錯誤:“gl_position”解析錯誤 ),並且可能是相同的解決方案,重新聲明應該預定義的內置函數

out gl_PerVertex { vec4 gl_Position; };

在那種情況下(或在這種情況下),不清楚為什么需要這樣做。 只應在某些情況下才需要它,例如GL_ARB_separate_shader_objects擴展名。 但是,它可能是AMD驅動程序的問題,因為他們也有AMD顯卡。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM