使用OpenGL和GLFW的简单三角形 [英] Simple triangle using OpenGL and GLFW

查看:316
本文介绍了使用OpenGL和GLFW的简单三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小程序,使用顶点缓冲区显示一个简单的三角形。对于窗口我使用glfw,我的环境是Mac 10.9,XCode 5。

I wrote a small program to display a simple triangle using vertex buffer. For the windowing i'm using glfw, my environment is Mac 10.9, XCode 5.

窗口显示为黑色,但三角形不是油漆。

The window appears black but the triangle isn't paint.

这里的代码:

#include <GLFW/glfw3.h>
#include <OpenGL/gl.h>
#include <iostream>

int main(int argc, const char * argv[])
{
    GLFWwindow* window;
    if (!glfwInit())
    {
        return -1;
    }

    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
    if (!window) 
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    GLfloat verts[] =
    {
        0.0f,  0.5f,  0.0f,
        0.5f, -0.5f,  0.0f,
        -0.5f, -0.5f,  0.0f
    };

    //Generate a buffer id
    GLuint vboID;

    //Create a buffer on GPU memory
    glGenBuffers(1, &vboID);

    //Bind an arraybuffer to the ID
    glBindBuffer(GL_ARRAY_BUFFER, vboID);

    // Fill that buffer with the client vertex
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

    //Enable attributes
    glEnableVertexAttribArray(0);

    // Setup a pointer to the attributes
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    while (!glfwWindowShouldClose(window))
    {
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwPollEvents();
        glfwSwapBuffers(window);
    }

    glfwTerminate();
    return 0;
}


推荐答案

渲染的核心配置文件:

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

您的代码缺少一些符合Core配置文件的要求:

Your code is missing a number of things to be Core Profile compliant:


  • 您需要实现着色器程序。 Core Profile不再支持旧的固定管道,并且需要在GLSL中实现自己的着色器。详细解释如何做到这一点超出了答案的范围,但你会使用 glCreateProgram ,glCreateShader glShaderSource glCompileShader glAttachShader glLinkProgram 。您应该可以在线上和书本中找到材料。

  • 您需要使用顶点数组对象(VAO)。查找 glGenVertexArrays glBindVertexArray

  • You need to implement shader programs. The Core Profile does not support the old fixed pipeline anymore, and requires you to implement your own shaders in GLSL. Explaining how to do this in detail is beyond the scope of an answer, but you will be using calls like glCreateProgram, glCreateShader, glShaderSource, glCompileShader, glAttachShader, glLinkProgram. You should be able to find material online and in books.
  • You need to use Vertex Array Objects (VAO). Look up glGenVertexArrays and glBindVertexArray.

这篇关于使用OpenGL和GLFW的简单三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆