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);
}
我想使用vao渲染矩形。当我运行main.c中注释的代码时,OpenGL渲染矩形。但是,如果我改为将我的头文件与该结构一起使用,它将无法正常工作。我发现Struct更改了他的内存地址。在initVertexBuffer()中的printf()之后,它将输出内存地址和值“ 1”。但是在bindVertexBuffer()中,每当值“ 0”而不是“ 1”时输出,并且内存地址被推迟。您能帮我吗?
更新(但是如何获取结构成员的地址)
#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 *buffer)
{
//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 *buffer)
{
// struct VertexBuffer buffer = *bufferPtr;
glBindVertexArray((*buffer).vao);
//printf("%p\n", &buffer.vao);
//printf("%p\n", buffer.vao);
}
void unbindVertexBuffer()
{
glBindVertexArray(0);
}