【问题标题】:OpenGL Widget on QT doesn't display primitivesQT 上的 OpenGL Widget 不显示基元
【发布时间】:2016-07-23 21:02:03
【问题描述】:

下面复制的类用于在 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);

}

【问题讨论】:

    标签: c++ qt opengl glew


    【解决方案1】:

    我相信根据您的数学计算,所有顶点都是在 (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)。

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多