OpenGL rectangle doesn't render when method is called indirectly but renders properly when I call it directly [closed]

2 weeks ago 8
ARTICLE AD BOX

THE PROBLEM

I'm trying to make my codebase modular but like the title says, whenever I use a method that has all my vertex points inside another method that itself is being called from main.cpp(or in the future, it'll be my gameloop.cpp file), it doesn't render

Main.cpp

Vert2D cornerA = Vert2D(-0.5f, -0.5f); Vert2D cornerD = Vert2D(0.5f, 0.5f); //[DATA] -> Points basicMesh.CreateRectangle(new Vert2D(-0.5f, -0.5f), new Vert2D(0.5f, 0.5f));

Wrapper Method

void BasicMesh::CreateRectangle(Vert2D *CornerA, Vert2D *CornerD) { MeshIndexing MeshIndex; std::cout << "CornerA is: " << CornerA->x << "." << CornerA->y << std::endl; std::cout << "CornerD is: " << CornerD->x << "." << CornerD->y << std::endl; MeshIndex.GetVerts(CornerA, CornerD);

MeshIndex

void MeshIndexing::GetVerts(Vert2D *CornerA, Vert2D *CornerD) { //(-0.5f, -0.5f) (0.5f, 0.5f) std::array<float, 12> Verts = { CornerA->x, CornerA->y, 0.0f, //Bottom Left Corner -> Corner A -CornerA->x, CornerA->y, 0.0f, //Bottom Right Corner -> Corner B -CornerD->x, CornerD->y, 0.0f, //Top Left Corner -> Corner C CornerD->x, CornerD->y, 0.0f //Top Right Corner -> Corner D }; localVerts = Verts;

BufferData Line

glBufferData(GL_ARRAY_BUFFER, sizeof(localVerts), &localVerts, GL_STATIC_DRAW); //Upload vertex data to gpu

WHAT I TRIED

To start inside my main.cpp file, I tried calling Vert2D directly (both using 'new' and not using 'new') and calling them by their variable names (both using the reference symbol and not using the reference symbol). For the wrapper method and the Vert method, I've tried setting the parameters as pointers and references. As you can see, I used std::cout in both methods to help me debug what's going on and both of the resulted in the correct values.

Furthermore, for the bufferdata line of code, I tried '(void*) &localVerts' as well as 'localVerts.Data()' for the 3rd argument (as well as what I have currently). I have to use 'localVerts' instead of just 'Verts' because the vert data method and the bufferdata line of code are in 2 separate methods so 'localVerts' is a class member. I would've said the problem was 'localVerts', but whenever I call the vertdata directly from main.cpp, my rectangle renders. My suspicion is there's something wrong with the parameters of the wrapper method. Ohhh, and I also tried just equaling localVerts directly to the array, but that didn't do anything.

I also tried changing my Indices to go counterclockwise like this posts answer recommended, but no such luck

WHAT I EXPECTED

I just expected it to render a rectangle, even when I'm calling the wrapper method and for it not to be this hard. Is what I'm asking for even possible?

REPROUCE PROJECT

Heres the project you can reproduce (Project link). Line 38, you can keep it the way it is to show it not rendering the rectangle or change it to 'Shapes.GetVert(<arguments>) to show it rendering a triangle

Read Entire Article