spectral

11-18-2017, 04:35 PM

Hello guys,

I am trying to render a few thousand/million cubes using glDrawArraysInstanced(). This is the result I'm currently getting vs what happens when I draw each cube individually:25542555

Here is the code used to draw:

Terrain::Terrain(GLuint id, GLuint uniformHandle) {

programID = id;

mvpHandle = uniformHandle;

Block a;

ModelMatrixHandle = glGetUniformLocation(programID, "ModelMatrix");

for (int i = 0; i < 100; i++) {

for (int j = 0; j < 2; j++) {

for (int k = 0; k < 25; k++) {

cubes.push_back(Block(programID, i, j, k, DIRT));

cubepositions.push_back(glm::vec3(i, j, k));

}

}

}

glGenBuffers(1, &cubepositionbuffer);

glBindBuffer(GL_ARRAY_BUFFER, cubepositionbuffer);

glBufferData(GL_ARRAY_BUFFER, cubepositions.size() * sizeof(glm::vec3), &cubepositions[0], GL_STREAM_DRAW);

}

void Terrain::render(glm::mat4 View, glm::mat4 Projection) {

glUseProgram(programID);

for (int i = 0; i < cubes.size(); i++) {

glUniformMatrix4fv(ModelMatrixHandle, 1, GL_FALSE, &cubes[i].getModelMatrix()[0][0]);

cubes[i].render();

}

Block::prepareBatch();

glEnableVertexAttribArray(3);

glBindBuffer(GL_ARRAY_BUFFER, cubepositionbuffer);

glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glVertexAttribDivisor(3, 1);

//glDrawArraysInstanced(GL_TRIANGLES, 3, Block::getVerticesSize(), cubepositions.size());

}

Where prepareBatch() loads the vertex+uv buffers for the a cube and calls glVertexAttribPointer/glEnableVertexAttribArray

I am trying to create the translation/model matrix inside the GLSL vertex shader using a vec3 of coordinates rather than send the shader a full mat4 modelmatrix.

#version 330 core

// Input vertex data, different for all executions of this shader.

layout(location = 0) in vec3 vertexPosition_modelspace;

layout(location = 1) in vec2 vertexUV;

layout(location = 2) in vec3 vertexNormals;

layout(location = 3) in vec3 vertexPosition_worldSpace;

// Output data ; will be interpolated for each fragment.

out vec2 UV;

// Values that stay constant for the whole mesh.

uniform mat4 MVP;

uniform mat4 ViewMatrix;

uniform mat4 ProjectionMatrix;

uniform mat4 ModelMatrix;

void main(){

//mat4 model = mat4 (1.0, 0, 0, vertexPosition_worldSpace.x,

// 0, 1.0, 0, vertexPosition_worldSpace.y,

// 0, 0, 1.0, vertexPosition_worldSpace.z,

// 0, 0, 0, 1.0);

gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(vertexPosition_modelspace, 1);

//gl_Position = ProjectionMatrix * ViewMatrix * model * vec4(vertexPosition_modelspace, 1);

// UV of the vertex. No special space for this one.

UV = vertexUV;

}

My fragment shader is simply sampling a texture:

#version 330 core

// Interpolated values from the vertex shaders

in vec2 UV;

// Ouput data

out vec3 color;

// Values that stay constant for the whole mesh.

uniform sampler2D myTextureSampler;

void main(){

// Output color = color of the texture at the specified UV

color = texture( myTextureSampler, UV ).rgb;

}

If anyone could help me get a foothold with this instance rendering I'd greatly appreciate it!

I am trying to render a few thousand/million cubes using glDrawArraysInstanced(). This is the result I'm currently getting vs what happens when I draw each cube individually:25542555

Here is the code used to draw:

Terrain::Terrain(GLuint id, GLuint uniformHandle) {

programID = id;

mvpHandle = uniformHandle;

Block a;

ModelMatrixHandle = glGetUniformLocation(programID, "ModelMatrix");

for (int i = 0; i < 100; i++) {

for (int j = 0; j < 2; j++) {

for (int k = 0; k < 25; k++) {

cubes.push_back(Block(programID, i, j, k, DIRT));

cubepositions.push_back(glm::vec3(i, j, k));

}

}

}

glGenBuffers(1, &cubepositionbuffer);

glBindBuffer(GL_ARRAY_BUFFER, cubepositionbuffer);

glBufferData(GL_ARRAY_BUFFER, cubepositions.size() * sizeof(glm::vec3), &cubepositions[0], GL_STREAM_DRAW);

}

void Terrain::render(glm::mat4 View, glm::mat4 Projection) {

glUseProgram(programID);

for (int i = 0; i < cubes.size(); i++) {

glUniformMatrix4fv(ModelMatrixHandle, 1, GL_FALSE, &cubes[i].getModelMatrix()[0][0]);

cubes[i].render();

}

Block::prepareBatch();

glEnableVertexAttribArray(3);

glBindBuffer(GL_ARRAY_BUFFER, cubepositionbuffer);

glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glVertexAttribDivisor(3, 1);

//glDrawArraysInstanced(GL_TRIANGLES, 3, Block::getVerticesSize(), cubepositions.size());

}

Where prepareBatch() loads the vertex+uv buffers for the a cube and calls glVertexAttribPointer/glEnableVertexAttribArray

I am trying to create the translation/model matrix inside the GLSL vertex shader using a vec3 of coordinates rather than send the shader a full mat4 modelmatrix.

#version 330 core

// Input vertex data, different for all executions of this shader.

layout(location = 0) in vec3 vertexPosition_modelspace;

layout(location = 1) in vec2 vertexUV;

layout(location = 2) in vec3 vertexNormals;

layout(location = 3) in vec3 vertexPosition_worldSpace;

// Output data ; will be interpolated for each fragment.

out vec2 UV;

// Values that stay constant for the whole mesh.

uniform mat4 MVP;

uniform mat4 ViewMatrix;

uniform mat4 ProjectionMatrix;

uniform mat4 ModelMatrix;

void main(){

//mat4 model = mat4 (1.0, 0, 0, vertexPosition_worldSpace.x,

// 0, 1.0, 0, vertexPosition_worldSpace.y,

// 0, 0, 1.0, vertexPosition_worldSpace.z,

// 0, 0, 0, 1.0);

gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vec4(vertexPosition_modelspace, 1);

//gl_Position = ProjectionMatrix * ViewMatrix * model * vec4(vertexPosition_modelspace, 1);

// UV of the vertex. No special space for this one.

UV = vertexUV;

}

My fragment shader is simply sampling a texture:

#version 330 core

// Interpolated values from the vertex shaders

in vec2 UV;

// Ouput data

out vec3 color;

// Values that stay constant for the whole mesh.

uniform sampler2D myTextureSampler;

void main(){

// Output color = color of the texture at the specified UV

color = texture( myTextureSampler, UV ).rgb;

}

If anyone could help me get a foothold with this instance rendering I'd greatly appreciate it!