Browse Source

real instanced rendering

master
alistair 3 years ago
parent
commit
41589d3250
  1. 54
      src/main.cpp
  2. 7
      src/shaders/instanced.vert

54
src/main.cpp

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <random>
#include <GL/glew.h>
#include <SDL2/SDL_opengl.h>
@ -121,10 +122,11 @@ void printProgramLog( GLuint program ) @@ -121,10 +122,11 @@ void printProgramLog( GLuint program )
class SDLGLGLWindow {
/* array buffers */
GLuint gVBO = 0;
GLuint quadVBO = 0;
GLuint gIBO = 0;
GLuint gVAO = 0;
unsigned int gVBO = 0;
unsigned int quadVBO = 0;
// unsigned int gIBO = 0;
unsigned int gVAO = 0;
unsigned int instance_VBO;
unsigned int lightVAO;
unsigned int quadVAO;
unsigned int framebuffer;
@ -133,6 +135,7 @@ class SDLGLGLWindow { @@ -133,6 +135,7 @@ class SDLGLGLWindow {
unsigned int texture[2];
unsigned int cubemap;
glm::vec3 *translations = new glm::vec3[100000];
glm::vec3 cubePositions[10] = {
glm::vec3( 0.0f, 0.0f, 0.0f),
@ -291,30 +294,29 @@ class SDLGLGLWindow { @@ -291,30 +294,29 @@ class SDLGLGLWindow {
glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
glCheckError();
glm::vec3 *translations = new glm::vec3[100];
int index = 0;
float offset = 0.1f;
for (int y = -10; y < 10; y += 2) {
for (int x = -10; x < 10; x += 4) {
for (int z = -10; z < 10; z += 4) {
if (index >= 100) {
goto yeetbreak;
}
translations[index++] = glm::vec3(x,y,z);
}
}
std::default_random_engine generator;
std::uniform_real_distribution<float> distribution(-50,50);
for (int i = 0; i < 100000; i ++) {
float x = distribution(generator) ;
float y = distribution(generator) ;
float z = distribution(generator) ;
translations[i] = glm::vec3(i,y,z);
}
yeetbreak:
shader->use();
for (unsigned int i = 0; i < 100; i++) {
shader->setVec3("offsets[" + std::to_string(i) + "]", translations[i]);
}
glGenBuffers(1, &instance_VBO);
glBindBuffer(GL_ARRAY_BUFFER, instance_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * 100, &translations[0], GL_STATIC_DRAW);
glCheckError();
glCheckError();
glCheckError();
delete[] translations;
/* textures */
@ -465,6 +467,12 @@ yeetbreak: @@ -465,6 +467,12 @@ yeetbreak:
glEnableVertexAttribArray(2);
glCheckError();
glBindBuffer(GL_ARRAY_BUFFER, instance_VBO);
glVertexAttribPointer(3,3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), NULL);
glVertexAttribDivisor(3,1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(3);
/*********** ENDCUBES **************/
@ -652,7 +660,7 @@ yeetbreak: @@ -652,7 +660,7 @@ yeetbreak:
glBindVertexArray(gVAO);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glCheckError();
glDrawArraysInstanced(GL_TRIANGLES, 0,36,100);
glDrawArraysInstanced(GL_TRIANGLES, 0,36,100000);
glCheckError();
/*

7
src/shaders/instanced.vert

@ -3,23 +3,20 @@ @@ -3,23 +3,20 @@
layout (location = 0) in vec3 LVertexPos2D;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 offset;
out vec3 theTexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform bool skyboxON;
out vec3 Normal;
out vec3 FragPos;
uniform vec3 offsets[100];
void main()
{
vec3 offset = offsets[gl_InstanceID];
vec4 pos = (vec4( LVertexPos2D, 1.0) - vec4(offset, 0));
vec4 pos = (vec4( LVertexPos2D, 1.0) + vec4(offset, 0));
gl_Position = projection * view * (model) * pos;

Loading…
Cancel
Save