简体   繁体   English

树莓派上的openGL,索引顶点

[英]openGL on raspberry pi, indexed vertices

I'm trying to learn some basic opengl on raspberry pi. 我正在尝试学习有关树莓派的一些基本的opengl。 I folow these tutorials https://solarianprogrammer.com/2013/05/13/opengl-101-drawing-primitives/ http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/ 我遵循这些教程https://solarianprogrammer.com/2013/05/13/opengl-101-drawing-primitives/ http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/

I bumped in to a prbolem I can't solve. 我碰到了我无法解决的prbolem。 I succesfully made it draw a triangle by listing vertices in an array, then I tried to do the same but using indexes, it doesn't work and I don't know why. 我通过在数组中列出顶点成功使它绘制了三角形,然后我尝试执行相同的操作,但使用索引,它不起作用,我也不知道为什么。 it compiles succesfully and runs, but it doesn't display the triangle. 它可以成功编译并运行,但是不显示三角形。

both tutorials make it so simple, add a buffer and replace "glDrawArrays" with "glDrawElements". 这两个教程都使它变得如此简单,添加了一个缓冲区并将“ glDrawArrays”替换为“ glDrawElements”。 but I must have missed something.. 但我一定错过了一些东西。

#include <stdio.h>
#include <unistd.h>
#include "../common/startScreen.h"
#include "../common/LoadShaders.h"

// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

using namespace glm;

int main(int argc, const char **argv)
{
    float r=0.0;
    InitGraphics();
    printf("Screen started\n");
    // Create and compile our GLSL program from the shaders
    GLuint programID = LoadShaders( "simpletransformvertshader.glsl", "simplefragshader.glsl" );
    printf("Shaders loaded\n");

// Get a handle for our "MVP" uniform
    GLuint MatrixID = glGetUniformLocation(programID, "MVP");

    // Get a handle for our buffers
    GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");

    // Projection matrix 
    glm::mat4 Projection = glm::perspective(45.0f, 16.0f / 9.0f, 0.1f, 100.0f);



    // Camera matrix
    glm::mat4 View       = glm::lookAt(
                                        glm::vec3(0,0,5),
                                        glm::vec3(0,0,0),
                                        glm::vec3(0,1,0),
                                               );

    //glm::mat4 myMatrix = glm::translate(10.0f, 0.0f, 0.0f);

   // Model matrix : an identity matrix (model will be at the origin)
    glm::mat4 Model      = glm::mat4(1.0f);
    // Our ModelViewProjection : multiplication of our 3 matrices
    glm::mat4 MVP        = Projection * View * Model; // Remember, matrix$

    //glm:mat4 MVP = Prpkection * View * Model;

GLfloat g_vertex_buffer_data[] = {
    -1.0f, -1.0f, 0.0f,
     1.0f, -1.0f, 0.0f,
     1.0f,  1.0f, 0.0f,
//      -1.0,   1.0f, 0.0f,
    };



GLuint indices[3] = {
    0,1,2,
//      2,3,0
    };



   // Set the viewport

    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_bu$


    GLuint eab;
    glGenBuffers(1, &eab);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STAT$



    do{
   // Model = glm::rotate(Model, r, glm::vec3(0.0f, 0.0f, 1.0f));

           glClear( GL_COLOR_BUFFER_BIT );

            // Use our shader
            glUseProgram(programID);

            // Send our transformation to the currently bound shader,
            // in the "MVP" uniform
            glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

            // 1rst attribute buffer : vertices
            glEnableVertexAttribArray(vertexPosition_modelspaceID);
            glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);


            glVertexAttribPointer(
                    vertexPosition_modelspaceID, 

                    3,                  // size
                    GL_FLOAT,           // type
                    GL_FALSE,           // normalized?
                    0,                  // stride
                    (void*)0            // array buffer offset
            );

// see above glEnableVertexAttribArray(vertexPosition_modelspaceID);

            // Draw the triangle !
     //      glDrawArrays(GL_TRIANGLES, 0, 3); 
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab);
            glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);




            glDisableVertexAttribArray(vertexPosition_modelspaceID);

    updateScreen();
    }
    while(1);

    // Cleanup VBO
    glDeleteBuffers(1, &vertexbuffer);
    glDeleteBuffers(1, &eab);
    glDeleteProgram(programID);


}

GLES 1.x/2.x does not support 32 bit indices by default. 默认情况下,GLES 1.x / 2.x不支持32位索引。 You can use at most 16 bit indices, so you should change your code to: 您最多可以使用16位索引,因此应将代码更改为:

GLushort indices[3] = {...}
[...]
glDrawElements(..., 3, GL_UNSIGNED_SHORT, ...);

Note that trying to use GL_UNSIGNED_INT as the type parameter on an implementation without support for GL_OES_element_index_uint should just result in an GL_INVALID_ENUM error. 请注意,尝试在不支持GL_OES_element_index_uint的实现上使用GL_UNSIGNED_INT作为类型参数,只会导致GL_INVALID_ENUM错误。 You should add some error checks at least for debugging. 您至少应添加一些错误检查以进行调试。

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

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