在小牛队的简单的Opengl三角

时间:2014-06-01 07:50:23

标签: macos opengl

我是opengl的新手。我开始使用GLFW的简单三角形,但我没有得到任何东西。 我的系统是: OSX Mavericks 10.9.2 Intel HD 4000 编译cmd行: g ++ main.cpp -o测试-I / usr / local / include -L / usr / local / lib -lglfw -framework Opengl

我注意到奇怪的第一件事是,在编译时,它没有识别
glGenVertexArrays,并建议:glGenVertexArraysAPPLE同样适用于:glBindVertexArray并建议:glBindVertexArrayAPPLE 在使用APPLE ext时,没有画任何东西。

我在这里缺少什么?

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

GLuint vao=0;
GLuint shader_programme;

void CheckGLErrors(std::string str)
{
    int errCount = 0;
    GLenum currError = glGetError();
    if(currError != GL_NO_ERROR)
    {
        //Do something with `currError`.
        std::cout<<"\n"<<str<<" Error raised:"<<currError<<"\n";
    }
}

void initVertex()
{
    float points[] = {
        0.0f,  0.5f,  0.0f,
        0.5f, -0.5f,  0.0f,
       -0.5f, -0.5f,  0.0f
    };

static const float colors[] =
{
    1.0, 0.0, 0.0, 1.0,
    0.0, 1.0, 0.0, 1.0,
    0.0, 0.0, 1.0, 1.0
};

    glGenVertexArraysAPPLE(1, &vao);    
    glBindVertexArrayAPPLE (vao);    

    GLuint vbo = 0;
    glGenBuffers (1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);

    glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);  
    glEnableVertexAttribArray (0);    
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    const char* vertex_shader =
    "#version 400\n"
    "in vec3 vp;"
    "void main () {"
    "  gl_Position = vec4 (vp, 1.0);"
    "}";

    const char* fragment_shader =
    "#version 400\n"
    "out vec4 frag_colour;"
    "void main () {"
    "  frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
    "}";

    GLuint vs = glCreateShader (GL_VERTEX_SHADER);
    glShaderSource (vs, 1, &vertex_shader, NULL);
    glCompileShader (vs);

GLint status = GL_TRUE;
glGetShaderiv(vs, GL_COMPILE_STATUS, &status);

    if(status != GL_TRUE) std::cout<<"VS FAILED\n";

    GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
    glShaderSource (fs, 1, &fragment_shader, NULL);
    glCompileShader (fs);

status = GL_TRUE;
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
    if(status != GL_TRUE) std::cout<<"PS FAILED\n";

    shader_programme = glCreateProgram ();
    glAttachShader (shader_programme, fs);
    glAttachShader (shader_programme, vs);
    glLinkProgram (shader_programme);
}


int main(int argc,  char * argv[])
{

    GLFWwindow* window;    
    int ver[3];
    glfwGetVersion(&ver[0], &ver[1], &ver[2]);
    std::cout << "major " <<ver[0] <<"minor: "<<ver[1]<<" ver: "<<ver[2];

    /* Initialize the library */
    if (!glfwInit())
    {
        std::cout << "Error";
        return -1;
    }

    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);


    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        std::cout << "Create window Error";
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);    
    std::cout<<"Version: " << glGetString(GL_VERSION);;

    initVertex();

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glPointSize(40.0f);
        glClearColor(0.0f,1.0f,0.0f,0.0f);

        glUseProgram(shader_programme);
        glBindVertexArrayAPPLE(vao);

        glDrawArrays(GL_TRIANGLE, 0, 3);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

glGenVertexArrays()缺少的声明中,您需要包含gl3.h而不是gl.h才能获得GL3级功能:

#include <OpenGL/gl3.h>

我在代码中看到的一个问题是您使用顶点属性位置0来设置坐标。属性位置是glEnableVertexAttribArray()glVertexAttribPointer()的第一个参数。但您真的不知道着色器程序中vp的位置是0。确保这一点的最简单方法是通过扩展vp的声明直接在着色器代码中指定它:

layout(location = 0) in vec3 vp;

这适用于GL 3.3及更高版本。旧版GL版本的替代方案是在glLinkProgram()调用之前进行以下API调用

glBindAttribLocation(shader_programme, 0, "vp");