Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 1 more developer from Canada and 12 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!


nickwinters

Member Since 23 Nov 2002
Offline Last Active Jan 01 2015 11:58 AM

Posts I've Made

In Topic: Performance problems at ~1,000,000 triangles. Am I doing something wrong?

02 January 2014 - 10:39 PM

I just moved each chunk into it's own vertex buffer.  It's running at 60fps in debug mode.  I haven't even begun to remove blocks that cannot be seen.  This means I'll have to add textures earlier than I thought.

 

Thank you for your help.


In Topic: Performance problems at ~1,000,000 triangles. Am I doing something wrong?

02 January 2014 - 06:57 PM

@Laztrezort:  Thanks for the page, that's an interesting find.  I hadn't considered giving each chunk its own vbo.  I'll go through the tutorials to see how it works.  I believe I can only set one texture per vbo.  That shouldn't be a problem, since I can merge the possible textures into one png.  However, a single vbo means each cube in a chunk would share its own material.  I'll have to think of the ramifications of that.  

 

I planned to not render cubes that cannot be seen, but I thought I should ask since a couple million triangles shouldn't be a problem for today's graphics cards.

 

@dpadam450: is there a particular batching technique you recommend I look into?


In Topic: Performance problems at ~1,000,000 triangles. Am I doing something wrong?

02 January 2014 - 01:44 PM

I've followed a couple, but haven't written one myself. I'll try a stripped down version of my project tonight and if it doesn't work, post it here. Thank you!

In Topic: Performance problems at ~1,000,000 triangles. Am I doing something wrong?

01 January 2014 - 09:14 PM

I'm now attempting instancing, but am doing something wrong.  Nothing is being rendered.
 
I changed modelMatrix and color from uniform to regular inputs.

#version 420 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec4 pointLightPosition;

in vec4 in_Position;
in vec4 in_Normal;
in vec2 in_uv;
in mat4 modelMatrix;
in vec4 color;

out vec4 pass_Color;
smooth out vec3 normal;
smooth out vec3 lightVector;

void main(void)
{
     gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;
	 vec4 normalWorld = transpose(modelMatrix) * in_Normal;
	 normal = normalize(vec3(normalWorld));
	 lightVector = normalize(vec3(pointLightPosition) - vec3(modelMatrix * in_Position));
     pass_Color = color;
}

Initialized my vbos.  The two new ones are at the bottom.

    glGenVertexArrays(1, &_vaoId); // Create our Vertex Array Object  
    glBindVertexArray(_vaoId); // Bind our Vertex Array Object so we can use it  

    glGenBuffers(1, &_vboId); // Generate our Vertex Buffer Object  

    glBindBuffer(GL_ARRAY_BUFFER, _vboId); // Bind our Vertex Buffer Object  
    int vertexSize = sizeof(Vertex);
    glBufferData(GL_ARRAY_BUFFER, 36 * vertexSize, vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW  
    int positionId = glGetAttribLocation(_shader->id(), "in_Position");
    int normalId = glGetAttribLocation(_shader->id(), "in_Normal");
    int texId = glGetAttribLocation(_shader->id(), "in_uv");
    glVertexAttribPointer(positionId, 4, GL_FLOAT, GL_FALSE, vertexSize, 0); // Set up our vertex attributes pointer  
    glVertexAttribPointer(normalId, 4, GL_FLOAT, GL_FALSE, vertexSize, (void*)16); // Set up our vertex attributes pointer  
    glEnableVertexAttribArray(positionId); // Enable the first vertex attribute array  
    glEnableVertexAttribArray(normalId); // Enable the first vertex attribute array  

    glBindVertexArray(0); // Disable our Vertex Array Object  

    delete[] vertices;
    int colorId = glGetAttribLocation(_shader->id(), "color");
    glGenBuffers(1, &_colorVbo);
    glBindBuffer(GL_ARRAY_BUFFER, _colorVbo);
    glEnableVertexAttribArray(colorId);
    glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, sizeof(vec4), 0);
    glVertexAttribDivisor(colorId, 1); //is it instanced?

    int modelMatrixId = glGetAttribLocation(_shader->id(), "modelMatrix");
    glGenBuffers(1, &_modelVbo);
    glBindBuffer(GL_ARRAY_BUFFER, _modelVbo);
    for (int c = 0; c < 4; ++c)
    {
        glEnableVertexAttribArray(modelMatrixId + c);
        glVertexAttribPointer(modelMatrixId + c, 4, GL_FLOAT, GL_FALSE, sizeof(mat4), (void*)(c * sizeof(vec4)));
        glVertexAttribDivisor(modelMatrixId + c, 1); //is it instanced?
    }

In my draw call, I bind the two vectors to the vbo pointers, and then attempt to draw my triangles.

    glViewport(0, 0, _windowWidth, _windowHeight); // Set the viewport size to fill the window  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clear required buffers
    _modelMatrix = glm::mat4();  // Create our model matrix which will halve the size of our model  
    _lightPosition = glm::vec4(-10.0, 30.0, 16.0, 1.0);
    _shader->bind(); // Bind our shader
    int projectionMatrixLocation = glGetUniformLocation(_shader->id(), "projectionMatrix"); // Get the location of our projection matrix in the shader  
    int viewMatrixLocation = glGetUniformLocation(_shader->id(), "viewMatrix"); // Get the location of our view matrix in the shader  
    int modelMatrixLocation = glGetUniformLocation(_shader->id(), "modelMatrix"); // Get the location of our model matrix in the shader  
    int pointLightPosition = glGetUniformLocation(_shader->id(), "pointLightPosition"); // Get the location of our model matrix in the shader  
    int material = glGetUniformLocation(_shader->id(), "color"); // Get the location of our model matrix in the shader  
    ChunkList chunks = _chunkManager.GetVisibleChunks();
    _viewMatrix = _camera->GetViewMatrix();

    glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &_projectionMatrix[0][0]); // Send our projection matrix to the shader  
    glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &_viewMatrix[0][0]); // Send our view matrix to the shader  
    glUniform4fv(pointLightPosition, 1, &_lightPosition[0]);
    vector<mat4> modelMatrices;
    vector<vec4> colors;
    for (ChunkList::iterator it = chunks.begin(); it != chunks.end(); ++it)
    {
        Chunk *chunk = *it;
        glm::mat4 world = glm::mat4();
        world = glm::translate(world, glm::vec3(chunk->GetX() * Chunk::CHUNK_SIZE, chunk->GetY() * Chunk::CHUNK_SIZE, chunk->GetZ() * Chunk::CHUNK_SIZE));
        for (float i = 0; i < Chunk::CHUNK_SIZE; ++i)
        {

            for (float j = 0; j < Chunk::CHUNK_SIZE; ++j)
            {
                for (float k = 0; k < Chunk::CHUNK_SIZE; ++k)
                {
                    Block &block = *(chunk->GetBlock(i, j, k));
                    if (!block.IsActive())
                    {
                        continue;
                    }


                    glm::vec4 colorArray;
                    switch (block.GetBlockType())
                    {
                        case BlockType::Dirt:
                            colorArray.r = 0.55;
                            colorArray.g = 0.27;
                            colorArray.b = 0.074;
                            colorArray.a = 1.0;
                            break;
                        case BlockType::Grass:
                            colorArray.r = 0.0;
                            colorArray.g = 1.0;
                            colorArray.b = 0.0;
                            colorArray.a = 1.0;
                            break;
                        case BlockType::Bedrock:
                            colorArray.r = 0.5;
                            colorArray.g = 0.5;
                            colorArray.b = 0.5;
                            colorArray.a = 1.0;
                            break;
                    }
                   
                    _modelMatrix = glm::translate(world, glm::vec3(i, j, k));
                    modelMatrices.push_back(_modelMatrix);
                    colors.push_back(colorArray);
                }
            }
        }
    }
    glBindVertexArray(_vaoId); // Bind our Vertex Array Object  
    
    glBindBuffer(GL_ARRAY_BUFFER, _modelVbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(mat4)* modelMatrices.size(), &modelMatrices[0], GL_DYNAMIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, _colorVbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vec4)* colors.size(), &colors[0], GL_DYNAMIC_DRAW);
    glDrawArraysInstanced(GL_TRIANGLES, 0, 36, colors.size());

    glBindVertexArray(0);

    _shader->unbind(); // Unbind our shader

    SwapBuffers(_hdc); // Swap buffers so we can see our rendering

Did I miss any steps?  Is the order of something incorrect?
 
Thank you.
 
Edit: Changed my initialization of the matrix vertex buffer to use 4 vectors.


In Topic: Performance problems at ~1,000,000 triangles. Am I doing something wrong?

01 January 2014 - 12:50 PM

Moving the glGetUniformLocation and constant glUniforms out of the drawing loop gave me a boost from 10 fps to 13 15fps at 5MM triangles, so that's a start.  I'll look into Instancing.


PARTNERS