Razielruss
Razielruss

Reputation: 159

c++ with openGl can't render

I try to make the code cleaner by extracting the whole main Proramm in some classes. So I have a vertex buffer, index buffer and a shader class. Only the array buffer is in the main and it worked.

Now I created a layout class and a vertex array class to put different layouts in the vertex array and the renderfunction in my main stopped to work.

Here is my code:

vertexBuffer.cpp

#include "../headerData/VertexBuffer.h"

VertexBuffer::VertexBuffer(const void* data, unsigned int size) {

    glGenBuffers(1, &bufferId);
    glBindBuffer(GL_ARRAY_BUFFER, bufferId);
    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);


}
VertexBuffer::~VertexBuffer() {

    glDeleteBuffers(1, &bufferId);
}

void VertexBuffer::bind() const {
    glBindBuffer(GL_ARRAY_BUFFER, bufferId);
}

void VertexBuffer::unbind() const {
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

vertex Index cpp:

#include "../headerData/IndexBuffer.h"

IndexBuffer::IndexBuffer(const void* data, unsigned int count) {

    glGenBuffers(1, &bufferId);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, count, data, GL_STATIC_DRAW);


}
IndexBuffer::~IndexBuffer() {

    glDeleteBuffers(1, &bufferId);
}

void IndexBuffer::bind() const {
    glBindBuffer(GL_ARRAY_BUFFER, bufferId);
}

void IndexBuffer::unbind() const {
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

VertexLayout.h

#pragma once
#include <vector>


//Stubides Layout da vieles vorgegeben wird
struct ElementLayout {
    unsigned int size;          //anzahl an koordinaten die zählen
    unsigned int stride;        //Die größe der 
};

class VertexLayout {

private:
    std::vector<ElementLayout> elements;
public:
    VertexLayout();
    ~VertexLayout();

    void layoutPush(unsigned int size);
    std::vector<ElementLayout> getElements() const;
};

vertexLayout.cpp:

#include"../headerData/VertexLayout.h"


VertexLayout::VertexLayout() {}
VertexLayout::~VertexLayout() {}

void VertexLayout::layoutPush(unsigned int size)
{
    elements.push_back({ size, size * sizeof(float) });
}

std::vector<ElementLayout> VertexLayout::getElements() const {
    return elements;
}

VertexArray.cpp

#include "../headerData/VertexArray.h"
#include <iostream>

VertexArray::VertexArray() {
    glGenVertexArrays(1, &vertexArrayID);

}
VertexArray::~VertexArray() {
    glDeleteVertexArrays(1, &vertexArrayID);
}

void VertexArray::bind() const {
    glBindVertexArray(vertexArrayID);
}
void VertexArray::unbind() const {
    glBindVertexArray(0);
}

//Vom Buffer soll auch das zugehörige Layout übergeben werden
void VertexArray::addBuffer(VertexBuffer& vb, VertexLayout& layout) {
    bind();
    vb.bind();
    std::vector<ElementLayout> element = layout.getElements();
    unsigned int offset = 0;
    for (int i = 0; i < element.size(); i++) {

        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, element[i].size, GL_FLOAT, GL_FALSE, element[i].stride, (void*)0);
        offset += element[i].size * sizeof(float);
        std::cout << GL_NO_ERROR;
    }

    std::cout << glGetError();

}

main.cpp:

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include "headerData/VertexBuffer.h"
#include "headerData/IndexBuffer.h"
#include "headerData/Shader.h"
#include "headerData/VertexLayout.h"
#include "headerData/VertexArray.h"


void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    Shader shader;
    shader.bind();
    // set up vertex data (and buffer(s)) and configure vertex attributes
    // ------------------------------------------------------------------
    float vertices[] = {
         0.5f,  0.5f, 0.0f,  // top right
         0.5f, -0.5f, 0.0f,  // bottom right
        -0.5f, -0.5f, 0.0f,  // bottom left
        -0.5f,  0.5f, 0.0f,   // top left
        -1.0f, -1.0f, 0.0f  //zum Testen
        //dreieck

    };
    unsigned int indices[] = {  // note that we start from 0!
        0, 1, 3,   // first triangle
        1, 2, 3    // second triangle
    };

    float vertices2[] = {
    -1.0f, -1.0f, 0.0f,
     1.0f, -1.0f, 0.0f,
     0.0f,  0.0f, 0.0f
    };
    unsigned int test;
    glGenVertexArrays(1, &test);

    // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
    glBindVertexArray(test);

    //glBindBuffer(GL_ARRAY_BUFFER, VBO);
    //glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    VertexBuffer vb(vertices, 5 * 3 * sizeof(float));
    IndexBuffer ib(indices, 6 * sizeof(unsigned int));
    VertexLayout layout;
    layout.layoutPush(3);
    VertexArray va;
    //va.addBuffer(vb, layout);


    //va.addBuffer(vb, layout); when this is uncomment the render function doesn't work

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


    // uncomment this call to draw in wireframe polygons.
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        shader.bind();
        //glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
        //glDrawArrays(GL_TRIANGLES, 0, 3);
        //glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        // glBindVertexArray(0); // no need to unbind it every time 

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // optional: de-allocate all resources once they've outlived their purpose:
    // ------------------------------------------------------------------------
    glDeleteVertexArrays(1, &test);


    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

With glGetError() I get the number 1281 =>invalid value but where.

Upvotes: 1

Views: 262

Answers (1)

Rabbid76
Rabbid76

Reputation: 211230

The index buffer (ELEMENT_ARRAY_BUFFER) binding is stored within the Vertex Array Object. When glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId) is called a reference to the element buffer object is stored in the currently bound Vertex Array Object. Therefore the VAO must be bound before the element buffer with glBindVertexArray(vertexArrayID):

VertexArray va;
va.bind();
IndexBuffer ib(indices, 6 * sizeof(unsigned int));

Upvotes: 4

Related Questions