2016-03-26 2 views
0

Je suis assez nouveau avec OpenGL et Qt et j'ai été si loin avec ça. Rendu d'un triangle simple avec OpenGL 3.3 n'était pas si difficile, mais l'intégration d'une caméra est. Pour une raison quelconque, mon triangle disparaît !? Ai-je mal calculé les mathématiques pour calculer les matrices? J'ai utilisé ces 2 tutoriels comme point de départ: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/Création d'une caméra avec OpenGL 3.3 et Qt 5.6

https://wiki.qt.io/How_to_use_OpenGL_Core_Profile_with_Qt

Mon code (les parties les plus importantes seulement):

void GLWidget::initializeGL() 
{ 
    QGLFormat glFormat = QGLWidget::format(); 
    if (!glFormat.sampleBuffers()) 
     qWarning() << "Could not enable sample buffers"; 

    // Set the clear color to black 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 

    // Prepare a complete shader program… 
    if (!prepareShaderProgram("A:/Projekte/Qt Workspace/Projects/CGOpenGL/simple.vert", "A:/Projekte/Qt Workspace/Projects/CGOpenGL/simple.frag")) 
     return; 

    /////Matrix calculations///// 
    projection.perspective(45.0f, 4.0f/3.0f, 0.1f, 100.0f); 
    view.lookAt(QVector3D(4,3,3),QVector3D(0,0,0),QVector3D(0,1,0)); 
    model = QMatrix4x4(); 
    ////////// 


    // We need us some vertex data. Start simple with a triangle ;-) 
    GLfloat points[] = {-0.5f, -0.5f, 0.0f, 
         1.0f, 0.5f, -0.5f, 
         0.0f, 1.0f, 0.0f, 
         0.5f, 0.0f, 1.0f}; 
    vertexBuffer.create(); 
    vertexBuffer.setUsagePattern(QGLBuffer::StaticDraw); 
    if (!vertexBuffer.bind()) 
    { 
     qWarning() << "Could not bind vertex buffer to the context"; 
     return; 
    } 
    vertexBuffer.allocate(points, 3 * 4 * sizeof(float)); 

    // Bind the shader program so that we can associate variables from 
    // our application to the shaders 
    if (!shader.bind()) 
    { 
     qWarning() << "Could not bind shader program to context"; 
     return; 
    } 

    // Enable the "vertex" attribute to bind it to our currently bound 
    // vertex buffer. 
    shader.setAttributeBuffer("vertex", GL_FLOAT, 0, 4); 
    shader.enableAttributeArray("vertex"); 
} 

void GLWidget::paintGL() 
{ 
    // Clear the buffer with the current clearing color 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // Set the MVP variable in the shader 
    shader.setUniformValue("MVP",projection * view * model); 

    // Draw stuff 
    glDrawArrays(GL_TRIANGLES, 0, 3); 
} 

Vertex Shader:

#version 330 

layout(location = 0) in vec3 vertexPosition_modelspace; 
uniform mat4 MVP; 

void main(void) 
{ 
    gl_Position = MVP * vec4(vertexPosition_modelspace,1); 
} 

Répondre

2

vous avez fait une appelez au

shader.enableAttributeArray("vertex"); 

encore nommaient:

vertexPosition_modelspace 

dans le shader, vous avez besoin de changer les noms pour être cohérent.

Renommez la variable dans le shader à « sommet »

+0

Mon Dieu ... Je cherchais 2 heures d'affilée pour toute erreur mathématique et il était un nom de variable ... Je me sens stupide maintenant>.