#[SOLVED] OpenGL Program occasionally does not render the model

20 messages · Page 1 of 1 (latest)

charred bolt
#

I have come across an unusual bug in my program. I have a simple 3D renderer program I have been working on that loads an OBJ file and renders it, with some controls such as zoom, pan and rotate the camera. However, occasionally when the program is run (either through VS Code or simply by clicking the EXE file) the view comes up blank. The outputs are the same even when nothing is rendered and when trying to run it through RenderDoc it always seems to work. In the attached picture, the blank view happened after 3 working attempts (I just closed 2 of them). A shortened version of my rendering code looks like this:


    unsigned int VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    
    unsigned int VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*18*(numinds), newVertArr, GL_STATIC_DRAW);

    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)0);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void*)(9*sizeof(float)));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glUseProgram(shaderProgram);
    glBindVertexArray(VAO);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

while(!glfwWindowShouldClose(window)){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        formYRotMat(camRotHorMat, cameraRotation[0]);
        formXRotMat(camRotVertMat, cameraRotation[1]);

        glUniformMatrix4fv(projMatUniform, 1, GL_TRUE, projMat);
        glUniformMatrix4fv(camRotHor, 1, GL_TRUE, camRotHorMat);
        glUniformMatrix4fv(camRotVert, 1, GL_TRUE, camRotVertMat);
        glUniform3f(originShader,origin[0],origin[1],origin[2]);
        glUniform3f(camDirShader, camVector[0],camVector[1], camVector[2]);
        glUniform1f(distanceShader,distance);
        glDrawArrays(GL_TRIANGLES, 0, 18*numinds);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
#

I'm sorry that it is in the form of a file, and for the messy code. I'm still learning OpenGL and C++ and the code is for a school project, so I couldn't use Github (Can't install programs)

#

In the console output, it states the compilation of the the shaders, the number of vertices/faces, the projection matrix and the first few vertex points in the array in that order

noble eagle
#

125000 triangles in one model seems like a lot, are you sure that's right?

gray field
#

its likely a standard model

#

anyway if its random per-run, then it sounds like it might be some UB in your code

static cape
#

XYZ Dragon is heavy yeah

gray field
#

undefined behaviour

#

like, you probably have uninitialized data somewhere

charred bolt
#

Ah alright

#

I'll have a look around

gray field
#

alternatively, you might be relying on some unspecified or explicitly undefined behaviour in opengl

#

e.g., reading and writing from the same textures during a draw call

charred bolt
#

I honestly have no clue what undefined behaviour I may be using

#

I feel like its something to do with my shader loading function, it is giving me very wierd values

#

However the same happens when I don't use my file loading function and just have it embedded

charred bolt
#

Alright, after getting Dr. Memory I found out it was some wierd stuff with uninitialised memory, and I ended up fixing it!

#

[SOLVED] OpenGL Program occasionally does not render the model