我正在关注arcsynthesis的OpenGL教程。第一部分没有提供使演示工作所需的所有代码,所以我不得不即兴发挥。无论如何,看起来一切正常,除了我的顶点和片段着色器没有编译。这些是我用glGetShaderInfoLog获得的错误:
ERROR: 0:1: error(#132) Syntax error: "test" parse error
ERROR: error(#273) 1 compilation errors. No code generated
这是我的OpenGL代码:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const float vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f
};
const string strVertexShader = "test.vert";
const string strFragmentShader = "test.frag";
GLuint positionBufferObject;
GLuint theProgram;
GLuint create_shader(GLenum, const string &);
GLuint create_program(const vector<GLuint>&);
void init_program()
{
vector<GLuint> shaderList;
shaderList.push_back(create_shader(GL_VERTEX_SHADER, strVertexShader));
shaderList.push_back(create_shader(GL_FRAGMENT_SHADER, strFragmentShader));
theProgram = create_program(shaderList);
for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
}
GLuint create_shader(GLenum eShaderType, const std::string &strShaderFile)
{
GLuint shader = glCreateShader(eShaderType);
const char *strFileData = strShaderFile.c_str();
glShaderSource(shader, 1, &strFileData, NULL);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar *strInfoLog = new GLchar[infoLogLength + 1];
glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
const char *strShaderType = NULL;
switch(eShaderType)
{
case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
}
fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
delete[] strInfoLog;
}
return shader;
}
GLuint create_program(const std::vector<GLuint> &shaderList)
{
GLuint program = glCreateProgram();
for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
glAttachShader(program, shaderList[iLoop]);
glLinkProgram(program);
GLint status;
glGetProgramiv (program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
GLint infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar *strInfoLog = new GLchar[infoLogLength + 1];
glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
fprintf(stderr, "Linker failure: %s\n", strInfoLog);
delete[] strInfoLog;
}
for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
glDetachShader(program, shaderList[iLoop]);
return program;
}
void init(void)
{
init_program();
glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void display(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(theProgram);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glUseProgram(0);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutMainLoop();
}
我的顶点着色器“test.vert”:
#version 330
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
我的片段着色为“test.frag”:
#version 330
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
答案 0 :(得分:3)
OpenGL不会为您加载文件,它根本不知道文件的概念。 glShaderSource
期望源代码为字符串(或一系列单独的字符串)。您只需提供一个文件名,现在解释为GLSL源代码,当然不是有效的程序。
请注意,arcsynthesis教程附带full source code,因此无需即兴创作。