0

main.c

#define APIENTRY __stdcall
#include<glad/glad.h>
#include <GL/glut.h>
#include <stdio.h>
#include "VertexBuffer.h"

//GLuint bufferID;
//GLuint vao;
struct VertexBuffer buffer1;
struct VertexBuffer *buffer1Ptr;

void display(void)
{
    glClearColor(0,1.0,1.0,0);
    glClear(GL_COLOR_BUFFER_BIT); //Löscht Bild aus Speicher

    bindVertexBuffer(&buffer1);
    //glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLES,0, 3); //Modus, start, anzahl
    unbindVertexBuffer();

    glFlush(); //Gibt dem Bildschirm das Bild
}

void keyboard(unsigned char key, int x, int y)
{
    if(key == 27) //Wenn Escape schließen
    {
        exit(0);
    }
}

void reshape(int width, int height)
{

}

int main (int argc, char **argv)
{

    glutInit(&argc, argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(183,100);
    glutCreateWindow("TestApp");
    glutDisplayFunc(display); //display func initialisieren
    glutKeyboardFunc(keyboard); //keyboard func initialisieren
    glutReshapeFunc(reshape);


    gladLoadGL();



    //Initialize the triangle-------------------------
    struct Vertex verticies[3];
    verticies[0].x = -0.5;
    verticies[0].y = -0.5;
    verticies[0].z = 0;

    verticies[1].x = 0;
    verticies[1].y = 0.5;
    verticies[1].z = 0;

    verticies[2].x = 0.5;
    verticies[2].y = -0.5;
    verticies[2].z = 0;

     uint32_t numVerticies = 3;
    //-------------------------------------------------


    buffer1.data = verticies;
    buffer1.numVerticies = numVerticies;

    initVertexBuffer(&buffer1);




    //glGenVertexArrays(1, &vao);
    //glBindVertexArray(vao);

   // glGenBuffers(1, &bufferID);
   // glBindBuffer(GL_ARRAY_BUFFER, bufferID);
   // glBufferData(GL_ARRAY_BUFFER, numVerticies * sizeof(struct Vertex), verticies, GL_STATIC_DRAW);

   // glEnableVertexAttribArray(0);
    //glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), 0);
    //glBindVertexArray(0);

    //LOOP starts
    glutMainLoop();
    return 0;
}

VertexBuffer.h

#include<glad/glad.h>

struct Vertex
{
    float x;
    float y;
    float z;
};


struct VertexBuffer
{

    //Parameter
    void* data;
    uint32_t numVerticies;

    GLuint bufferID;
    GLuint vao;



};

void initVertexBuffer(struct VertexBuffer *bufferPtr)
{

    struct VertexBuffer buffer = *bufferPtr;

    glGenVertexArrays(1, &buffer.vao);
    glBindVertexArray(buffer.vao);



    glGenBuffers(1, &buffer.bufferID);
    glBindBuffer(GL_ARRAY_BUFFER, buffer.bufferID);
    glBufferData(GL_ARRAY_BUFFER, buffer.numVerticies * sizeof(struct Vertex), buffer.data, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), 0);
    glBindVertexArray(0);

    printf("%p\n", &(buffer.vao));
    printf("%p\n", buffer.vao);
}

void bindVertexBuffer(struct VertexBuffer *bufferPtr)
{

    struct VertexBuffer buffer = *bufferPtr;

    glBindVertexArray(buffer.vao);

    printf("%p\n", &buffer.vao);
    printf("%p\n", buffer.vao);


}

void unbindVertexBuffer()
{
    glBindVertexArray(0);
}

I want to render a rectangle, using vao´s. When I run the code which is commented in the main.c, OpenGL renders the rectangle. But if I use instead my headerfile with the struct it isn´t working. I figured out that the Struct changes his memory address. After the printf() in the initVertexBuffer() it outputs the memory address and the value ´1´. But in bindVertexBuffer() it outputs every time the value ´0´ instead of ´1´ and the memory address is postponed. Could you please help me?

  • 1
    *"Struct moves his memory address [...]"* - No, it does not, please learn the basics. That's a very basic issue. `struct VertexBuffer buffer = *bufferPtr;` creates a local copy of the data referenced by `bufferPtr`. Actually you set the elements of the local variable `buffer`, the structure referenced by `bufferPtr` is never changed. Delete the line `struct VertexBuffer buffer = *bufferPtr;` and use `bufferPtr->` instead of `buffer.`, in the functions `initVertexBuffer` and `bindVertexBuffer`. – Rabbid76 Dec 22 '19 at 21:29

1 Answers1

0

The problem is with initVertexBuffer.

initVertexBuffer takes the address of buffer1 from main.c. Then it reads the data from the VertexBuffer from that address, and stores it into a local variable. Then it sets the vao member in that local variable. buffer1 does not get modified at any time.

Instead of this:

// this line creates a new VertexBuffer and copies the data from the variable that bufferPtr points to.
struct VertexBuffer buffer = *bufferPtr;

// these lines access the new copy of the data, not the original.
glGenVertexArrays(1, &buffer.vao);
glBindVertexArray(buffer.vao);

use this:

// these lines access the variable that bufferPtr points to.
glGenVertexArrays(1, &bufferPtr->vao);
glBindVertexArray(bufferPtr->vao);

and so on.

user253751
  • 57,427
  • 7
  • 48
  • 90