2016-07-23 10 views
0

以下にコピーされるクラスは、QTウィジェットでOpenGLコンテキストを作成するためのものです。QTのOpenGLウィジェットにプリミティブが表示されない

私はGLFWを使用する場合、それがうまく機能している間、それは任意のポイントを表示しません。しかし...

#include "glwidget.h" 
#include "shader.hpp" 

GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) { 

} 

GLWidget::~GLWidget() { 
    // Cleanup VBO 
    glDeleteBuffers(1, &vertexbuffer); 
    glDeleteBuffers(1, &colorbuffer); 
    glDeleteBuffers(1, &elementbuffer); 
    glDeleteProgram(programID); 
    glDeleteVertexArrays(1, &VertexArrayID); 
} 

void GLWidget::initializeGL() 
{ 
    glewInit(); 
    // Dark blue background 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

    // Enable depth test 
    glEnable(GL_DEPTH_TEST); 

    // Accept fragment if it closer to the camera than the former one 
    glDepthFunc(GL_LESS); 

    // Cull triangles which normal is not towards the camera 
    glEnable(GL_CULL_FACE); 

    // Create and compile our GLSL program from the shaders 
    programID = LoadShaders("SimpleVertexShader.vertexshader", "ColorFragmentShader.fragmentshader"); 

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

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

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


    // Temporary code for vertices and their color generation 
    vertices.push_back(glm::vec3(0,0,0)); // A point situated at 0,0,0 for test 
    color.push_back(glm::vec3(1.0, 0.0, 0.0)); 
    index.push_back(0); 

    int size = 1000; 
    for (int i = 1; i < size; ++i) { 
     vertices.push_back(glm::vec3((rand() % 100)/10, (rand() % 100)/10, (rand() % 100)/10)); 
     color.push_back(glm::vec3(1.0, 0.0, 0.0)); 
     index.push_back(i); 
    } 

    /*Computing the points centroid */ 
    for (int i = 0; i < vertices.size(); i++) 
    { 
     mx += vertices[i][0]; 
     my += vertices[i][1]; 
     mz += vertices[i][2]; 
    } 

    mx = mx/vertices.size(); my = my/vertices.size(); mz = mz/vertices.size(); 

    for (int i = 0; i < vertices.size(); i++) 
    { 
     vertices[i][0] = vertices[i][0] - mx; 
     vertices[i][1] = vertices[i][1] - my; 
     vertices[i][2] = vertices[i][2] - mz; 
    } 

    glGenBuffers(1, &vertexbuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); 

    glGenBuffers(1, &colorbuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 
    glBufferData(GL_ARRAY_BUFFER, color.size() * sizeof(glm::vec3), &color[0], GL_STATIC_DRAW); 


    glGenBuffers(1, &elementbuffer); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, index.size() * sizeof(unsigned int), &index[0], GL_STATIC_DRAW); 

    /* Line indexing, not in use actually.... 
    unsigned int indexL[] = { 1550, vertices.size() - 1300 }; 

    GLuint elementbufferLine; 
    glGenBuffers(1, &elementbufferLine); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbufferLine); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2 * sizeof(unsigned int), &indexL[0], GL_STATIC_DRAW); */ 
} 

void GLWidget::resizeGL(int w, int h) 
{ 
    //glViewport(0, 0, w, h); 

    ViewMatrix = glm::lookAt(
     glm::vec3(-5, -5, -5),     // Camera is here 
     glm::vec3(mx, my, mz), // and looks here 
     glm::vec3(0, 1, 0)      // Head is up (set to 0,-1,0 to look upside-down) 
    ); 

    ModelMatrix   = glm::mat4(1.0f); 
    ProjectionMatrix = glm::perspective(glm::radians(80.0f), float(w) /float(h), 0.1f, 100.0f); 
    MVP     = ProjectionMatrix * ViewMatrix * ModelMatrix; 
} 

void GLWidget::paintGL() 
{ 
    // Clear the screen 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 


    /* Using shaders */ 
    glUseProgram(programID); 

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

    // 1rst attribute buffer : vertices 
    glEnableVertexAttribArray(0); 
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
    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 
    ); 

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

    // Index buffer 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); 

    // Draw the point cloud ! 
    glPointSize(5); 
    glDrawElements(
     GL_POINTS,    // mode 
     index.size(),   // count 
     GL_UNSIGNED_INT,  // type 
     (void*)0    // element array buffer offset 
    ); 

    glDisableVertexAttribArray(0); 
    glDisableVertexAttribArray(1); 

} 

答えて

0

私はすべての頂点が(0,0,0)で作成され、あなたの数学と信じています - (9.9,9.9,9.9)の範囲である。次に、重心を計算して減算します。あなたのmx、my、mzが(5.0、5.0、5.0)であると言うことができます。今度は重心を引くと、あなたのポイントはすべて(-5.0、-5.0、-5.0)と(4.9,4.9、4.9)の範囲になります。

つまり、すべての点を原点に合わせるだけです。私はあなたが起源(0、0、0)ではなく、(5、5、5)であるべきだと信じています。

関連する問題