2016-07-23 2 views
0

아래에 복사 된 클래스는 QT 위젯에 OpenGL 컨텍스트를 만드는 역할을합니다. 내가 GLFW를 사용할 때 잘 작동하는 동안 어떤 지점을 표시하지 않습니다 그러나QT의 OpenGL 위젯에 프리미티브가 표시되지 않습니다.

...

#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)를보아야한다고 생각한다.

관련 문제