未声明的标识符 'gl_Position'
Undeclared identifier 'gl_Position'
我正在训练用 cpp 做 opengl 的东西,但是我的程序有一个错误:/ (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] = '[=12=]';
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;
}
错误:顶点着色器中未声明的标识符 'gl_Position'
我正在使用 visual studio 2015,Windows 8,intel cpu,amd gpu。
这与我上周回答的问题 () 非常相似,并且可能是相同的解决方案,重新声明应该预定义的内置函数:
out gl_PerVertex { vec4 gl_Position; };
在那种情况下(或在这种情况下),不清楚为什么需要这样做。它应该只在某些情况下才需要,例如 GL_ARB_separate_shader_objects 扩展名。但是,这可能是 AMD 驱动程序的问题,因为他们也有 AMD 显卡。
我正在训练用 cpp 做 opengl 的东西,但是我的程序有一个错误:/ (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] = '[=12=]';
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;
}
错误:顶点着色器中未声明的标识符 'gl_Position' 我正在使用 visual studio 2015,Windows 8,intel cpu,amd gpu。
这与我上周回答的问题 (
out gl_PerVertex { vec4 gl_Position; };
在那种情况下(或在这种情况下),不清楚为什么需要这样做。它应该只在某些情况下才需要,例如 GL_ARB_separate_shader_objects 扩展名。但是,这可能是 AMD 驱动程序的问题,因为他们也有 AMD 显卡。