Andy Rama
Andy Rama

Reputation: 43

OpenGL not rendering, likely due to a few simple mistakes I couldn't find

So this code is supposed to render a rectangle (eventually I'd like it to render a cube, but there's no sense in trying to do that without being able to draw a rectangle first).

I have code in place to check for GL errors, and while there is one, the answer to this question leads me to beleive that it is not relevant (it's GL_INVALID_ENUM, being thrown immediatley after glewInit();).

Here is my code:

Main:

#include <glm/glm.hpp>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <thread>

#define GLEW_STATIC
#include <GL/glew.h>

#include <engine.hpp>

GLuint VertexBuffer, VertexArray, ElementBuffer, ShaderProgram;

GLfloat vertices[] = {
         -0.5, 0.5, 0.5, // Front Top Left      - 0
         0.5,  0.5, 0.5, // Front Top Right     - 1
         0.5, -0.5, 0.5, // Front Bottom Right  - 2
         -0.5,-0.5, 0.5, // Front Bottom Left   - 3
         -0.5, 0.5,-0.5, // Back Top Left       - 4
         0.5,  0.5,-0.5, // Back Top Right      - 5
         0.5, -0.5,-0.5, // Back Bottom Right   - 6
         -0.5,-0.5,-0.5, // Back Bottom Left    - 7
};

GLuint elements[]{
    0,1,2,
    2,3,0
};

int main()
{
    CheckForGLErrors(__func__, __LINE__);

    /*Initialization*/
    GLFWwindow* window = InitializeContext();
    CheckForGLErrors(__func__, __LINE__);

    //Create our element buffer object using the elements array
    ElementBuffer = InitializeElementBuffer(elements, sizeof(elements)/sizeof(GLuint));
    CheckForGLErrors(__func__, __LINE__);

    //Load and compile the shaders
    ShaderProgram = LoadShaders("vertexshader.cpp", "fragmentshader.cpp");
    CheckForGLErrors(__func__, __LINE__);

    //Create our vertex attribute array
    VertexArray = CreateVertexArray();
    CheckForGLErrors(__func__, __LINE__);

    //Create our vertex  buffer object using the vertices array
    VertexBuffer = InitializeVertexBuffer(vertices, sizeof(vertices)/sizeof(GLfloat));
    CheckForGLErrors(__func__, __LINE__);

    //Assign our array the appropriate structure
    InitializeVertexArray(ShaderProgram);
    CheckForGLErrors(__func__, __LINE__);




    while(!glfwWindowShouldClose(window))
    {
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
            glfwSetWindowShouldClose(window, GL_TRUE);
        }

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        CheckForGLErrors(__func__, __LINE__);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    CheckForGLErrors(__func__, __LINE__);

    glfwTerminate();
}

I would be glad to provide more information as requested.

Note: I have been able to draw triangles n stuff before, but my old code offers no insight to me as to why this is failing.

Upvotes: 0

Views: 173

Answers (1)

user3256930
user3256930

Reputation: 1143

When you pass an array as a function parameter it treats it like a pointer and you cannot use sizeof within the function to get the full size of the array. In this case you will need to pass the size of the array as a function parameter as well. When you call glBufferData the size parameter expects the size in bytes of the array rather than the number of elements in the array.

Upvotes: 2

Related Questions