C++ 使用OpenGL和GLFW的简单三角形

C++ 使用OpenGL和GLFW的简单三角形,c++,xcode,opengl,glfw,C++,Xcode,Opengl,Glfw,我写了一个小程序,用顶点缓冲区显示一个简单的三角形。对于我使用glfw的窗口,我的环境是Mac10.9,XCode 5 窗口显示为黑色,但三角形未绘制 代码如下: #include <GLFW/glfw3.h> #include <OpenGL/gl.h> #include <iostream> int main(int argc, const char * argv[]) { GLFWwindow* window; if (!glfwIni

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

窗口显示为黑色,但三角形未绘制

代码如下:

#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;
}
#包括
#包括
#包括
int main(int argc,const char*argv[]
{
GLFWwindow*窗口;
如果(!glfwInit())
{
返回-1;
}
glfwWindowHint(GLFW_上下文_版本_专业,4);
glfwWindowHint(GLFW_上下文_版本_小调,1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwWindowHint(GLFW_OPENGL_配置文件、GLFW_OPENGL_核心配置文件);
window=glfwCreateWindow(640480,“Hello三角形”,NULL,NULL);
如果(!窗口)
{
glfwTerminate();
返回-1;
}
glfwMakeContextCurrent(窗口);
GLfloat verts[]=
{
0.0f,0.5f,0.0f,
0.5f,-0.5f,0.0f,
-0.5f,-0.5f,0.0f
};
//生成缓冲区id
胶合vboID;
//在GPU内存上创建缓冲区
glGenBuffers(1,&vboID);
//将arraybuffer绑定到ID
glBindBuffer(GL_数组_BUFFER,vboID);
//用客户端顶点填充该缓冲区
glBufferData(GLU数组缓冲区、大小(顶点)、顶点、GLU静态绘制);
//启用属性
GlenableVertexAttributeArray(0);
//设置指向属性的指针
glvertexattributepointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
而(!glfwWindowShouldClose(窗口))
{
gldrawArray(GL_三角形,0,3);
glfwPollEvents();
glfwSwapBuffers(窗口);
}
glfwTerminate();
返回0;
}

您正在为渲染选择OpenGL核心配置文件:

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
您的代码缺少一些与核心配置文件兼容的内容:

  • 您需要实现着色器程序。核心配置文件不再支持旧的固定管道,并且要求您在GLSL中实现自己的着色器。详细解释如何执行此操作超出了回答的范围,但您将使用调用,如
    glCreateProgram
    glCreateShader
    glShaderSource
    glCompileShader
    glAttachShader
    glLinkProgram
    。你应该能够在网上和书里找到材料
  • 您需要使用顶点数组对象(VAO)。查找
    glgenvertexarray
    glbinvertexarray

您正在为渲染选择OpenGL核心配置文件:

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
您的代码缺少一些与核心配置文件兼容的内容:

  • 您需要实现着色器程序。核心配置文件不再支持旧的固定管道,并且要求您在GLSL中实现自己的着色器。详细解释如何执行此操作超出了回答的范围,但您将使用调用,如
    glCreateProgram
    glCreateShader
    glShaderSource
    glCompileShader
    glAttachShader
    glLinkProgram
    。你应该能够在网上和书里找到材料
  • 您需要使用顶点数组对象(VAO)。查找
    glgenvertexarray
    glbinvertexarray

你好,雷托,非常感谢。很有效。你好,雷托,非常感谢。它起作用了。