Ever heard of Buffer Streaming

This commit is contained in:
__
2022-04-11 18:59:47 +02:00
parent 2cf5d90727
commit 3c8ba05952

View File

@@ -17,13 +17,23 @@ int main(int argc, char *argv[]) {
glfwSetKeyCallback(window, &key_pressed);
float vertices[] = {
const float triangle[] = {
//pos // ColorIndecies //Texture coordnites
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, 1.0f
};
const float square[] = {
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
};
const unsigned int indices[] = {
0, 1 ,2,
0, 2, 3
};
// CreateShader
char* vertex1 = "assets/shaders/vertex4.glsl\0";
@@ -32,15 +42,15 @@ int main(int argc, char *argv[]) {
shader1 = createShader(vertex1, fragment1);
// Initialise VBO and VAO
unsigned int VAO, VBO;
unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//unsigned int EBO;
//glGenBuffers(1, &EBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(square), NULL, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0 );
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3,GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
@@ -48,28 +58,62 @@ int main(int argc, char *argv[]) {
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_DYNAMIC_DRAW);
float *buffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
memcpy(buffer, triangle, sizeof(triangle));
glUnmapBuffer(GL_ARRAY_BUFFER);
// Create Texture
unsigned int texture;
texture = createTexture("assets/textures/wall.jpg");
unsigned int transformLoc = glGetUniformLocation(shader1, "transform");
mat4x4 transform;
mat4x4_identity(transform);
bool toogle=false;
float prev_time=0, dt=0, timer=0, time=0;
while(!glfwWindowShouldClose(window)) {
time = glfwGetTime();
dt = time - prev_time;
prev_time = time;
glClearColor(0.6f, 0.0f, 0.8f, 0.0f);
// printf("dt: %f \n",dt*1000);
timer += dt;
if(timer>= 1.5f){
timer=0;
buffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
if (toogle){
memcpy(buffer, triangle,sizeof(triangle));
buffer[16]=0.5f;
} else {
memcpy(buffer, square, sizeof(square));
buffer[16]=0.0f;
}
printf("%f ", buffer[16]);
glUnmapBuffer(GL_ARRAY_BUFFER);
toogle = !toogle;
}
glClearColor(0.6f, 0.0f, 0.7f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
mat4x4_translate(transform, pos[0], pos[1], pos[2]);
unsigned int transformLoc = glGetUniformLocation(shader1, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, *transform);
glDrawArrays(GL_TRIANGLES, 0, 3);
if(toogle){
glDrawArrays(GL_TRIANGLES, 0, 3);
} else {
glDrawElements(GL_TRIANGLES, 6,GL_UNSIGNED_INT, 0);
}
glfwSwapBuffers(window);
glfwPollEvents();