【发布时间】:2013-12-07 20:44:58
【问题描述】:
我目前正在通过tutorial 学习 OpenGL(和 GLSL)。到目前为止,我完成了教程 2:第一个三角形。我的代码有点不同,因为我更喜欢编写 C,而不是更常见的 C++。另外,我使用 glfw3 打开了我的窗口,我相信教程使用 glfw。就目前而言,我已经能够用 C 编写所有内容,并且一切正常,直到我使用了我的第一个着色器。每当我使用它们时,我的三角形都不会渲染。我在 Mac 上,没有时间升级到 OSX 10.9 (Mavericks),因此我不得不使用 OpenGL 3.2 和 GLSL 1.2。
tl;dr: 问题:每当我使用着色器时,我的三角形都不会渲染。
这是我的代码:
int main(void) {
GLFWwindow* window;
/* Initialize the library */
if(!glfwInit()) {
return -1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Tutorial 1", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Initialize GLEW */
glewExperimental = GL_TRUE; // Needed in core profile
if(glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
/* Triangle's vertices */
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("VertexShader.txt", "FragmentShader.txt");
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
// 1rst attribute buffer : vertices
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Use our shader
glUseProgram(programID);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
这是我的顶点着色器:
#version 120
vec3 vertexPosition_modelspace;
void main() {
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
我的 FragmentShader:
#version 120
vec3 color;
void main() {
color = vec3(1, 0, 0);
}
我认为这可能与着色器文件有关,我想我可能在教程中将它们翻译错了(他使用 GLSL 3.3)。
【问题讨论】:
-
@AntonD:实际上,因为他使用的是 1.20 版本,所以应该是
attribute。此外,片段着色器的输出未正确声明。 -
@derhass 那么我的片段着色器的输出实际上有什么问题,我知道我不能使用 vec3 颜色,因为根据我去过的一些网站,这是一个更高版本.
-
Andon M. Coleman 的回答解释了所有必要的内容