0

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();
}
  • engine (pastebin link because SO doesn't seem to want me having that much text in my post) UPDATED to fix flaws pointed out by user3256930
  • engine_shaders.hpp (pastebin again for same reasons)

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.

Community
  • 1
  • 1
Andy Rama
  • 43
  • 1
  • 6
  • I see a few things that are potential problems. First in your initializeVertexBuffer function you have an array parameter and you do sizeof within the function. That will actually only give you the size of a single GLfloat because of how array parameters are handled in C++ so that function is likely not loading all of the data. I would recommend adding a size parameter to that function. The second potential problem is the order of initializing your VAOs and VBOs. You need to do the VAO first because it keeps track of which VBOs get bound when you do the glVertexAttribPointer calls. – user3256930 Mar 17 '14 at 00:00
  • @user3256930 [This tutorial](http://open.gl/drawing) uses `sizeof(vertices)` so I don't think thats the problem. I'll have to take a look at whether the order that the VBO and VAO are created matters. – Andy Rama Mar 17 '14 at 02:19
  • 1
    That tutorial defines the array in the same function in which they are doing sizeof. You are passing the array as a function parameter. See this post about why that does not work: http://www.cplusplus.com/forum/articles/20881/. Also in that same tutorial in the section about vertex arrays it says you need to create the VAO first. – user3256930 Mar 17 '14 at 03:28
  • @user3256930 I made the changes you suggested by: 1. Making the element and vertex buffer objects take in a parameter `size_t size` and having it print the values too make sure they are correct 2. Splitting InitializeVertexArray into CreatVertexArray and InitializeVertexArray (see post) but it still does not render anything – Andy Rama Mar 19 '14 at 05:06
  • I think you also need to move the initialization of the index buffer after you create the vertex array. Just a sanity check, but where are you setting a color? Is that hard coded in your shader? – user3256930 Mar 19 '14 at 05:26
  • @user3256930 the fragment shader has the color hard coded (`OutputColor = vec4(1.0f, 0.0f, 1.0f, 1.0f);`) and it's nothing fancy so hopefully thats not the problem. I have tried moving `InitializeElementBuffer` to before and after `CreateVertexArray`, `InitializeVertexBuffer`, and `InitializeVertexArray`, to no avail :( Also thanks for all the help so far :) – Andy Rama Mar 19 '14 at 05:37
  • When you call glBufferData it expects the size in bytes but your size parameter is not the total number of bytes since you are dividing by sizeof(GLuint) and sizeof(GLfloat) when you pass the size as a parameter to your initialization functions. – user3256930 Mar 19 '14 at 05:48
  • Oh interesting for some reason I thought it should be the # of elements in the array. It works now! :) If you type up your answer as an answer I will mark it as being the answer (if that makes sense) – Andy Rama Mar 19 '14 at 05:55

1 Answers1

2

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.

user3256930
  • 1,123
  • 7
  • 12