Hey i managed to make my application do instanced rendering and im rendering 250K rectangles at once with one draw call.
i’v implemented a functionality so that when my mouse position is greater/lesser than the window dimensions the window moves in that direction.
However when i hover over the reactangle cluster containg 250K rectangle in different sizes it lags a bit.
So im just wondering if some lag should be expected when rendering 250K rectangles at once.
My graphics card is Nvidia GeForce GTX 1060 6gb
I’ll append my cpp file and my shader:
VertexShader
#version 330 core
layout (location = 0) in vec3 inPos;
layout (location = 3) in vec4 inCol;
layout (location = 7) in mat4 modelMatrix;
out vec4 colorFromVS;
uniform mat4 view;
uniform mat4 projection;
void main()
{
vec3 position = inPos;
gl_Position = projection * view * modelMatrix * vec4(position, 1.0f);
colorFromVS = inCol;
}
.cpp file
int main()
{
GLFWwindow *window = {};
window = setupGLFWandGLAD(SCREEN_WIDTH, SCREEN_HEIGHT, "cj_GL_GLFW");
Shader shaderObject = {};
shaderObject.createVS("vertexShader.glsl");
shaderObject.createFS("fragmentShader.glsl");
shaderObject.linkShaderProgram();
//Camera camera = {};
//camera.init();
float verts[] =
{
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int indx[]=
{
0, 1, 2,
2, 0, 3
};
srand(time(NULL));
glm::mat4 modelMatrix[NUMOBJECTS];
for (int i = 0; i < NUMOBJECTS; i++)
{
float rPosX = (float)(rand() % 700);
float rPosY = (float)(rand() % 500);
float rScaleX = (float)(rand() % 100);
float rScaleY = (float)(rand() % 100);
modelMatrix[i] = glm::mat4(1.0f);
modelMatrix[i] = glm::translate(modelMatrix[i], glm::vec3(rPosX, rPosY, 0.0f));
modelMatrix[i] = glm::scale(modelMatrix[i], glm::vec3(rScaleX, rScaleY, 0.0f));
}
unsigned int instanceVBO, VBO, IBO, VAO;
glGenBuffers(1, &instanceVBO);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(modelMatrix), &modelMatrix, GL_STATIC_DRAW);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indx), indx, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glEnableVertexAttribArray(7);
glVertexAttribPointer(7, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0);
glVertexAttribDivisor(7, 1);
glEnableVertexAttribArray(8);
glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(1 * sizeof(glm::vec4)));
glVertexAttribDivisor(8, 1);
glEnableVertexAttribArray(9);
glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(2 * sizeof(glm::vec4)));
glVertexAttribDivisor(9, 1);
glEnableVertexAttribArray(10);
glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(3 * sizeof(glm::vec4)));
glVertexAttribDivisor(10, 1);
unsigned int projectionLocation = glGetUniformLocation(shaderObject.shaderProgram, "view");
unsigned int viewLocation = glGetUniformLocation(shaderObject.shaderProgram, "projection");
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
shaderObject.useProgram();
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::ortho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::vec3 eye = {};
glm::vec3 center = {};
glm::vec3 up = {};
eye = glm::vec3(0.0f, 0.0f, 0.0f);
center = glm::vec3(0.0f, 0.0f, -1.0f);
up = glm::vec3(0.0f, 1.0f, 0.0f);
while(!(glfwWindowShouldClose(window)) && running)
{
setViewportToWindowSize(window);
checkInput(window);
MouseCoords mouseCoords = getMousePosNDC(window);
processInput(mouseCoords);
glClearColor(0.1f, 0.1f, 0.1f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT);
if(mouseCoords.x >= (1.0f -0.01f))
{
//camera.move(5.0f, 0.0f);
eye.x += 0.021f;
center.x += 0.021f;
}
else if(mouseCoords.x <= -1.0f)
{
eye.x -= 0.021f;
center.x -= 0.021f;
//camera.move(-5.0f, 0.0f);
}
if(mouseCoords.y >= 1.0f)
{
//camera.move(0.0f, -5.0f);
eye.y += 0.021f;
center.y += 0.021f;
}
else if(mouseCoords.y <= -1.0f)
{
//camera.move(0.0f, 5.0f);
eye.y -= 0.021f;
center.y -= 0.021f;
}
if(keyPressed['W'])
{
//camera.zoomIn(10.0f, 10.0f);
}
if(keyPressed['S'])
{
//camera.zoomOut(10.0f, 10.0f);
}
view = glm::mat4(1.0f);
view = glm::lookAt(eye, center, up);
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, glm::value_ptr(projection));
glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, NUMOBJECTS);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteProgram(shaderObject.shaderProgram);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &IBO);
glDeleteBuffers(1, &VAO);
glDeleteBuffers(1, &instanceVBO);
glfwTerminate();
return 0;
}