Undeclared identifier 'gl_Position'
I am practicing to do opengl with cpp but there is an error in my program: / (vertexShader)
Here is the code for the vertexShader:
void main(void)
{
gl_Position = gl_Vertex;
}
There is a main cpp file here 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 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;
}
Error: undeclared id 'gl_Position' in vertexShader I am using visual studio 2015, Windows 8, intel cpu, amd gpu.
+3
source to share
1 answer
This is very similar to the question I answered last week ( GLSL Bug # 132 Syntax error: "gl_position error") and probably the same solution, override the builtins that need to be predefined:
out gl_PerVertex { vec4 gl_Position; };
In this situation (or in this), it was not clear why this is required. This is only needed under certain circumstances, such as the GL_ARB_separate_shader_objects extension . But maybe it's an AMD driver issue as they also had an AMD graphics card.
0
source to share