三角形不在 GLFW3 中渲染
Triangle not rendering in GLFW3
我最近下载了 GLFW3,因为据我所知它比 GLUT 好。我设法得到一个 window 来显示和更改清晰的颜色,但我不明白为什么我没有在我的绘制调用中渲染任何东西。在这种情况下,它是一个三角形。我在 XCode 9.2 上 运行 这是我现在拥有的代码:
#define GLFW_INCLUDE_GLCOREARB
#include <stdio.h>
#include <stdlib.h>
#include <GLFW/glfw3.h>
static const GLfloat vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
int main(int argc, const char * argv[]) {
GLuint VertexBufferID;
GLFWwindow* window;
/* Initialize the library */
if ( !glfwInit() )
{
return -1;
}
#ifdef __APPLE__
/* We need to explicitly ask for a 3.2 context on OS X */
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow( 400 , 400, "Hello World", NULL, NULL );
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glGenBuffers(1, &VertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
//Edit in
**program = initShaders(VSHADER_SOURCE, FSHADER_SOURCE);**
while (!glfwWindowShouldClose(window))
{
/* Render here */
//set clear color
glClearColor(0.0, 0.0, 0.0, 1.0);
//clear window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers
//Draw
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
//got error 0x502 on line below
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
//Edit in
**glUseProgram(program);**
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
这可能是一个小错误,但我看不到。
编辑:好的着色器是必需的,据我所知。我不知道我是如何在 GLUT 中摆脱它的。我猜这是一个旧版本。所以这是我正在使用的着色器程序。
"#version 330 core\n"
"layout(location = 0) in vec3 vertexPosition_modelspace;\n"
"void main()\n"
"{\n"
" gl_Position.xyz = vertexPosition_modelspace;\n"
" gl_Position.w = 1.0;\n"
"}\n";
"#version 330 core\n"
"out vec3 color;\n"
"void main()\n"
"{\n"
" color = vec3(1, 0, 0);\n"
"}\n";
我还应该提到,我也一直在按照本教程寻求帮助。 http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/
至于错误,我在glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
中发现一个错误代码502,它显然意味着GL_INVALID_OPERATION,我不知道在这种情况下是什么意思。
glBufferData
的第二个参数是缓冲区的目标类型,而不是命名缓冲区对象本身。 glBufferData
使用绑定到指定目标的命名缓冲区对象:
glBufferData(
GL_ARRAY_BUFFER, // GL_ARRAY_BUFFER instead of VertexBufferID
sizeof(vertex_buffer_data),
vertex_buffer_data,
GL_STATIC_DRAW);
如果您想使用 OpenGL Core profile context, then you have to use a shader program,这不是可选的。
此外 您必须创建一个命名的 Vertex Array Object,因为默认顶点数组对象 (0) 不存在于核心配置文件上下文中。
现代的 OpenGL 渲染方式是使用 Shader 程序。
如果您不想使用着色器程序,则必须使用兼容性上下文,并且必须使用 glVertexPointer
and you have to enable the client-side capability for vertex coordinates by glEnableClientState( GL_VERTEX_ARRAY )
所反对的方式定义顶点数据数组。
glfwWindowHint (GLFW_OPENGL_PROFILE,
GLFW_OPENGL_COMPAT_PROFILE); // instead of GLFW_OPENGL_CORE_PROFILE
.....
glGenBuffers(1, &VertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glBufferData(GL_ARRAY_BUFFER,
sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
.....
glEnableClientState( GL_VERTEX_ARRAY );
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState( GL_VERTEX_ARRAY );
我最近下载了 GLFW3,因为据我所知它比 GLUT 好。我设法得到一个 window 来显示和更改清晰的颜色,但我不明白为什么我没有在我的绘制调用中渲染任何东西。在这种情况下,它是一个三角形。我在 XCode 9.2 上 运行 这是我现在拥有的代码:
#define GLFW_INCLUDE_GLCOREARB
#include <stdio.h>
#include <stdlib.h>
#include <GLFW/glfw3.h>
static const GLfloat vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
int main(int argc, const char * argv[]) {
GLuint VertexBufferID;
GLFWwindow* window;
/* Initialize the library */
if ( !glfwInit() )
{
return -1;
}
#ifdef __APPLE__
/* We need to explicitly ask for a 3.2 context on OS X */
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow( 400 , 400, "Hello World", NULL, NULL );
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glGenBuffers(1, &VertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
//Edit in
**program = initShaders(VSHADER_SOURCE, FSHADER_SOURCE);**
while (!glfwWindowShouldClose(window))
{
/* Render here */
//set clear color
glClearColor(0.0, 0.0, 0.0, 1.0);
//clear window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers
//Draw
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
//got error 0x502 on line below
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
//Edit in
**glUseProgram(program);**
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
这可能是一个小错误,但我看不到。
编辑:好的着色器是必需的,据我所知。我不知道我是如何在 GLUT 中摆脱它的。我猜这是一个旧版本。所以这是我正在使用的着色器程序。
"#version 330 core\n"
"layout(location = 0) in vec3 vertexPosition_modelspace;\n"
"void main()\n"
"{\n"
" gl_Position.xyz = vertexPosition_modelspace;\n"
" gl_Position.w = 1.0;\n"
"}\n";
"#version 330 core\n"
"out vec3 color;\n"
"void main()\n"
"{\n"
" color = vec3(1, 0, 0);\n"
"}\n";
我还应该提到,我也一直在按照本教程寻求帮助。 http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/
至于错误,我在glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
中发现一个错误代码502,它显然意味着GL_INVALID_OPERATION,我不知道在这种情况下是什么意思。
glBufferData
的第二个参数是缓冲区的目标类型,而不是命名缓冲区对象本身。 glBufferData
使用绑定到指定目标的命名缓冲区对象:
glBufferData(
GL_ARRAY_BUFFER, // GL_ARRAY_BUFFER instead of VertexBufferID
sizeof(vertex_buffer_data),
vertex_buffer_data,
GL_STATIC_DRAW);
如果您想使用 OpenGL Core profile context, then you have to use a shader program,这不是可选的。
此外 您必须创建一个命名的 Vertex Array Object,因为默认顶点数组对象 (0) 不存在于核心配置文件上下文中。
现代的 OpenGL 渲染方式是使用 Shader 程序。
如果您不想使用着色器程序,则必须使用兼容性上下文,并且必须使用 glVertexPointer
and you have to enable the client-side capability for vertex coordinates by glEnableClientState( GL_VERTEX_ARRAY )
所反对的方式定义顶点数据数组。
glfwWindowHint (GLFW_OPENGL_PROFILE,
GLFW_OPENGL_COMPAT_PROFILE); // instead of GLFW_OPENGL_CORE_PROFILE
.....
glGenBuffers(1, &VertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glBufferData(GL_ARRAY_BUFFER,
sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
.....
glEnableClientState( GL_VERTEX_ARRAY );
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState( GL_VERTEX_ARRAY );