0
\$\begingroup\$

Good Morning,

I've been trying to learn some OpenGL by following the tutorials on learnopengl.com and I've hit a bit of a snag. I'm trying to implement the same functionality as the "Hello Triangle" tutorial (learnopengl.com->Getting Started->Hello Triangle) except I'm trying to put it in its own class. Essentially it is the same code just moved around, The issue is coming from glBufferData(...). from my understanding this function is to essentially bind the data with the buffer object. When glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); is called in my main.cpp file the program operates how it should and it draws the square. But if i comment that out on Main.cpp and move it to BindBuffers on Window.cpp the program still runs but it does not render the square. Any input on why this is happening/ what im doing wrong?

Main.cpp

#include "Engine\Window.h"
#include <glad/glad.h>

#include <GLFW/glfw3.h>

#include <iostream>

int main()

{
    Window window;

// 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 
};
unsigned int indices[] = {  // note that we start from 0!
    0, 1, 3,  // first Triangle
    1, 2, 3   // second Triangle
};

window.GenBuffers();

window.BindBuffers(vertices,indices);
//glBindVertexArray(window.GetVAO());
//glBindBuffer(GL_ARRAY_BUFFER, window.GetVBO());
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, window.GetEBO());
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

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

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (window.CheckClose())
{
    window.processInput();
    window.Begin();
    window.Render(window.GetVAO());
    window.End();
    window.Update();
}
window.~Window();
return 0;
}

Window.cpp

#include "Window.h"

Window::~Window() {
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);
    glfwTerminate();
}
Window::Window() {
/*** Creates window and intializes shader, if this is needed I can edit but I chopped it to save some room*/
}

void Window::Update() {
    glfwPollEvents();
}
void Window::Begin() {
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
}
void Window::End() {
    glfwSwapBuffers(window);
}
bool Window::CheckClose() {
/*Checks if window was closed*/
}
int Window::GetShader() {
    return shaderProgram;
}
void Window::Render(unsigned int VAO) {
    // draw our first triangle
    glUseProgram(GetShader());
    glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
                            //glDrawArrays(GL_TRIANGLES, 0, 6);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    // glBindVertexArray(0); // no need to unbind it every time 
}
void Window::GenBuffers() {
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);
}
void Window::BindBuffers(float vertices[],unsigned int indices[]) {
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    //glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
unsigned int Window::GetVBO() {
    return VBO;
}
unsigned int Window::GetVAO() {
    return VAO;
}
unsigned int Window::GetEBO() {
    return EBO;
}
\$\endgroup\$
0
\$\begingroup\$

the issue is related to how sizeof behaves. It is a fully compile time construct that derives it's result only from the type information.

When an array is passed as a parameter it loses information about how many elements there are in it. So you need to pass that explicitly.

void Window::BindBuffers(float vertices[],unsigned int vertexCount, unsigned int indices[], unsigned int indexCount) {
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0])*vertexCount, vertices, GL_STATIC_DRAW);
}
\$\endgroup\$
  • \$\begingroup\$ That is pretty interesting, I never knew that arrays could lose information but i suppose it does make sense. Learn something new everyday. \$\endgroup\$ – Jacob Hughes Apr 12 '18 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.