Talesseed Talesseed - 2 months ago 15
C++ Question

Undeclared identifier 'gl_Position'

I am training to do opengl stuff with cpp, but there is an error in my programm :/ (vertexShader)

Here is the code for the vertexShader:

void main(void)
{
gl_Position = gl_Vertex;
}


Here, there is the main cpp file that calls the shader:

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


The error: Undeclared identifier 'gl_Position' in the vertexShader
I'm using visual studio 2015, Windows 8, intel cpu, amd gpu.

Answer

This is very similar to a question I answered last week (GLSL Error #132 Syntax error: "gl_position" parse error), and likely the same solution, redeclare the built-ins that are supposed to be predefined:

out gl_PerVertex { vec4 gl_Position; };

In that situation (or in this one), it wasn't clear in that why this is required. It should only be required under certain circumstances, such as the of the GL_ARB_separate_shader_objects extension. But, it is perhaps an issue with the AMD driver, as they also had an AMD video card.

Comments