dimanche 30 avril 2017

Setting opengl vertex array object in seperate class breaks drawing

I'm new to c++ and OpenGL and my code now that I changed it to be more object oriented doas not draw anymore. I don't get an error message.

I tried to bugfix this for a while now and I have still no Idea what the problem could be. Hopefully you can help me :D

Here is the main Program I left out all the includes and some basic stuff:

// Game variables
TestEntity *testEntity;

int main()
{
glfwInit(); 
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "TopDown-Shooter", nullptr, nullptr); // Create a GLFWwindow object that we can use for GLFW's functions
glfwMakeContextCurrent(window);

// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
glewExperimental = GL_TRUE; // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewInit(); // Initialize GLEW to setup the OpenGL Function pointers

int width, height; // Define the viewport dimensions
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);

Shader myShader("shader.vs", "shader.frag");

testEntity = new TestEntity; // creating world object. Always create game objectrs after seeting up OpenGl context

// Game loop
while (!glfwWindowShouldClose(window))
{
    // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
    glfwPollEvents();

    // Render
    glClearColor(0.1796875f, 0.5078125f, 0.796875f, 1.0f); // Clear the colorbuffer
    glClear(GL_COLOR_BUFFER_BIT);

    // Drawing
    myShader.Use(); // Activates Shaders

    // Draw first triangle
    glBindTexture(GL_TEXTURE_2D, testEntity->getTex());
    GLuint drawSize = testEntity->getIndices().size();
    glBindVertexArray(testEntity->getVAO());
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    // Swap the screen buffers
    glfwSwapBuffers(window);
}

// Properly de-allocate all resources once they've outlived their purpose
    //testEntity->disallocateRecources();

// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;

}

and my TestEntity object:

class TestEntity {
public:
TestEntity() {
    vertices = {
        // Positions          // Colors           // Texture Coords
        0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // Top Right
        0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // Bottom Right
        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // Bottom Left
        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // Top Left 
    };
    indices = {
        0, 1, 3, // First Triangle
        1, 2, 3 // Second Triangle
    };

    // VertexArray;
    glGenVertexArrays(1, &VAO); 
    glGenBuffers(1, &EBO); // generates element buffer object
    glGenBuffers(1, &VBO); // generates vertex buffer object
    glBindVertexArray(VAO); // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_DYNAMIC_DRAW); // Writes vertices to the VBO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_DYNAMIC_DRAW); // writes indices to the EBO
    // Specifying vertexAttribPointers (connection to the vertex shader)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0); // Vertices
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); // Colors
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat))); // Texture Coordinates
    glEnableVertexAttribArray(2);
    glBindVertexArray(0);

    glGenTextures(1, &tex); // generating textures
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Specifies wrapping settings for T and S axis
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Specifies Pixleinterpolation
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    int imgWidth, imgHeight;
    unsigned char* myImage;
    myImage = SOIL_load_image("Textures\\Test_IMG_container.jpg", &imgWidth, &imgHeight, 0, SOIL_LOAD_RGB); // loads texture image
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, myImage); // converts image to texture
    glGenerateMipmap(GL_TEXTURE_2D); // generates Mipmap
    SOIL_free_image_data(myImage);
    glBindTexture(GL_TEXTURE_2D, 0);
}

Aucun commentaire:

Enregistrer un commentaire