简体   繁体   English

使用GLEW(mac,c ++)无法在openGL中绘制任何内容

[英]Can't draw anything in openGL using GLEW (mac,c++)

I am trying to learn OpenGL programming in c++, using This tutorial . 我正在尝试使用本教程学习c ++中的OpenGL编程。 i am using a macbook pro, running OSX-Yosemite version 10.10.3. 我正在使用运行OSX-Yosemite版本10.10.3的Macbook Pro。 My openGL version is 3.3. 我的openGL版本是3.3。

I am using the libraries GLFW, and GLEW. 我正在使用库GLFW和GLEW。

I am compiling (and linking) my code from my terminal using this command: g++ triangleTest.cpp -o triangleTest -lglfw3 -lglew -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo (i am not writing in Xcode). 我正在使用以下命令从终端编译(并链接)我的代码: g++ triangleTest.cpp -o triangleTest -lglfw3 -lglew -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo (我不是用Xcode编写)。

The goal of this program is to make a white triangle on a black background, the code looks like this: 该程序的目标是在黑色背景上制作一个白色三角形,代码如下所示:

// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include <GL/glew.h>
#include<GLFW/glfw3.h>
GLFWwindow* window;

// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
   -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f,  1.0f, 1.0f,
};

int main(){

    // Initialise GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }

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


    std::cout<<"GLFW3 initialized\n";


    window = glfwCreateWindow(640, 480, "I am a triangle", NULL, NULL);

     if (!window)
     {
         glfwTerminate();
         std::cout<<"Window initialization failed\n";
        return 1;   
     }

    std::cout<<"Window initialized\n";

    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) {
        std::cout<<"Failed to initialize GLEW try again\n";
        return 1;
    }


    // This will identify our vertex buffer
    GLuint vertexbuffer;
    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer); 
    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);






    glfwSwapInterval(1);

     while (!glfwWindowShouldClose(window)){

        // Clear the screen
        glClear( GL_COLOR_BUFFER_BIT );

        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
            1,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_TRUE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );

        // Draw the triangle !
        glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
     }


    glfwTerminate();    
}

The code is compiled and run (from the terminal), and a black window with no triangle, but the correct name and size apears. 该代码被编译并运行(从终端),一个黑色的窗口没有三角形,但是名称和大小正确。 The window works perfectly, and can be moved around and closed like it is suposed to, but the triangle is not drawn. 窗口可以完美地工作,并且可以像其所希望的那样移动和关闭,但是未绘制三角形。

Looking at the tutorial, it seems that you're missing the VAO. 查看教程,您似乎缺少了VAO。 Specifically, you should add these lines after you initialize GLEW. 具体来说,您应该在初始化GLEW之后添加这些行。

GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

Then, in your call to VertexAttribPointer, your attribute should be 0, not 1. So the call shoud look like this 然后,在调用VertexAttribPointer时,您的属性应为0,而不是1。因此,调用应如下所示

glVertexAttribPointer(
        0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        (void*)0            // array buffer offset
);

That makes it match the index of he attribute, since there is no attribute at index 1 这使其与he属性的索引匹配,因为索引1没有属性

Forget that site, very ugly and dark. 忘记那个站点,非常丑陋和黑暗。 There are one guy from Reddit who compiled a small list of VERY useful tutorials for beginners. Reddit中有一个人为初学者编写了一小部分非常有用的教程。 Just go there, choose one and I promise you that for 8 hours per day, in about a week you will have a cube rotating with texture and maybe some light! 只要去那里,选择一个,我保证每天运行8个小时,大约一周后,您将拥有一个旋转的立方体,具有纹理并且可能还有些光线! =) So, give it a try: List of cool Modern Opengl tutorials And this tutorials work on Mac. =)因此,请尝试一下: 很棒的Modern Opengl教程列表而且这些教程在Mac上也可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM