c - 三角形未在 GLFW3 中渲染

标签 c opengl glfw

我最近下载了 GLFW3,因为据我所知它比 GLUT 更好。我设法获得一个窗口来显示并更改清晰的颜色,但我无法弄清楚为什么我没有在绘制调用中渲染任何内容。在本例中,它是一个三角形。我在 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 使用绑定(bind)到指定目标的命名缓冲区对象:

glBufferData(
    GL_ARRAY_BUFFER,            // GL_ARRAY_BUFFER instead of VertexBufferID
    sizeof(vertex_buffer_data),
    vertex_buffer_data,
    GL_STATIC_DRAW); 
<小时/>

如果您想使用OpenGL Core profile context ,那么你必须使用 shader program ,这不是可选的。
此外,您必须创建一个名为 Vertex Array Object ,因为默认顶点数组对象 (0) 不存在于核心配置文件上下文中。

OpenGL 中渲染的现代方法是使用 Shader程序。

如果您不想使用着色器程序,则必须使用兼容性上下文,并且必须使用 glVertexPointer 不推荐的方式定义顶点数据数组。并且您必须通过 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 );

关于c - 三角形未在 GLFW3 中渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51941602/

相关文章:

c - 如何为此结构分配内存

c++ - OSX Lion 上的 makefile 中的 GLFW 链接问题

c++ - 显示列表最适合这个吗? (OpenGL)

c++ - GLFW 设置回调函数

c - SWIG:结构字段的 "out"类型映射需要访问相同结构的另一个字段

c - 这段翻译好的汇编代码还有什么更大的意义吗?

c - 使用 while 循环 scanf !=EOF 后如何使用 scanf

c - OpenGL 照明斗争

c++ - 如何在 OSX 上将静态 C++ 函数声明为友元

c++ - CMake - get_filename_component 未知组件目录