Jump to content
  • Advertisement
Sign in to follow this  
InusualZ

Problems with BatchRenderer

This topic is 529 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I'm trying to render an level, by an BatchRenderer of my own. But for some reason the tile only render in the center of the screen: 
v42m0T4.png
 
I really don't know what I'm doing wrong. I bet that it have something to do with the model matrix but I don't seems to get it working. My code:
#include "LevelRenderer.h"
 
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
 
#include "../graphic/VertexData.h"
 
LevelRenderer::LevelRenderer() : vbo(0), vao(0), ebo(0) {
    initGraphic();
}
 
LevelRenderer::~LevelRenderer() {
    glDeleteBuffers(1, &vbo);
    glDeleteBuffers(1, &ebo);
    glDeleteVertexArrays(1, &vao);
}
 
 
void LevelRenderer::initGraphic() {
    glGenBuffers(1, &vbo);
    glGenBuffers(1, &ebo);
    glGenVertexArrays(1, &vao);
 
    glBindVertexArray(vao);
 
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, MAX_VERTICES_SIZE, nullptr, GL_DYNAMIC_DRAW); // TODO: Calculate `MAX_VERTICES_SIZE` based on level width and height
 
    GLuint* indices = new GLuint[INDICES_BUFFER_SIZE];
 
    int offset = 0;
    for (int i = 0; i < INDICES_BUFFER_SIZE; i += 6) {
        indices[i] = offset + 0;
        indices[i + 1] = offset + 1;
        indices[i + 2] = offset + 2;
 
       indices[i + 3] = offset + 2;
       indices[i + 4] = offset + 3;
       indices[i + 5] = offset + 0;
 
       offset += 4;
    }
 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDICES_BUFFER_SIZE, indices, GL_STATIC_DRAW);
 
    glVertexAttribPointer(VERTICES_ID, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), nullptr);
    glVertexAttribPointer(COLOR_ID, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const GLvoid*)(3 * sizeof(GL_FLOAT)));
 
    glEnableVertexAttribArray(VERTICES_ID);
    glEnableVertexAttribArray(COLOR_ID);
 
    glBindVertexArray(0);
 
    delete[] indices;
}
 
void LevelRenderer::render() const {
 
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    VertexData* data = (VertexData*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
    if (data == nullptr) {
        throw "FATAL ERROR";
    }
 
    static const glm::vec4 topRight(0.5f, 0.5f, 0.0f, 0.0f); // Top Right
    static const glm::vec4 bottomRight(0.5, -0.5f, 0.0f, 0.0f); // Bottom Right
    static const glm::vec4 bottomLeft(-0.5f, -0.5f, 0.0f, 0.0f); // Bottom Left
    static const glm::vec4 topLeft(-0.5f, 0.5f, 0.0f, 0.0f); // Top Left
 
    GLuint indexCount = 0;
 
    glm::mat4 projection = glm::ortho(0.0f, (f32)WIDTH, (f32)HEIGHT, 0.0f, -1.0f, 1.0f);
 
    glm::mat4 model;
    model = glm::scale(model, glm::vec3(TILE_WIDTH, TILE_HEIGHT, 1.0f));
 
    for (uint32 x = 0; x < MAX_TILE; x++) {
        for (uint32 y = 0; y < MAX_TILE; y++) {
            model = glm::translate(model, glm::vec3((f32)x + TILE_WIDTH, (f32)y + TILE_HEIGHT, 1.0f));
 
            glm::vec3 position = projection * model * topRight;
            data->x = position.x;
            data->y = position.y;
            data->z = 0.0f;
 
            data->r = 0.2f;
            data->g = 0.3f;
            data->b = 0.6f;
            data++;
 
            position = projection * model * bottomRight;
            data->x = position.x;
            data->y = position.y;
            data->z = 0.0;
 
            data->r = 0.2f;
            data->g = 0.3f;
            data->b = 0.6f;
            data++;
 
            position = projection * model * bottomLeft;
            data->x = position.x;
            data->y = position.y;
            data->z = 0.0f;
  
            data->r = 0.2f;
            data->g = 0.3f;
            data->b = 0.6f;
            data++;
 
            position = projection * model * topLeft;
            data->x = position.x;
            data->y = position.y;
            data->z = 0.0f;
 
            data->r = 0.2f;
            data->g = 0.3f;
            data->b = 0.6f;
            data++;
 
            indexCount += 6;
        }
    }
 
    glUnmapBuffer(GL_ARRAY_BUFFER);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
 
    glBindVertexArray(vao);
  
    glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr);
 
    glBindVertexArray(0);
}
Thanks in advance :) Edited by InusualZ

Share this post


Link to post
Share on other sites
Advertisement

Serious advice: Read some tutorials / buy a book.

 

The fact that you are performing this operation "projection * model * topRight" in your render loop means you are ill advised.

 

Your variables projection,model and topRight  do not change in your tile for loop, so I'm not sure if you are expecting to see tiles in a grid or not.

 

So your tiles are obviously all at the same position, so why are they small? Probably because perspective projection is on by default in openGL? Have you called glOrtho()?

Edited by dpadam450

Share this post


Link to post
Share on other sites

Serious advice: Read some tutorials / buy a book.
 
The fact that you are performing this operation "projection * model * topRight" in your render loop means you are ill advised.
 
Your variables projection,model and topRight  do not change in your tile for loop, so I'm not sure if you are expecting to see tiles in a grid or not.
 
So your tiles are obviously all at the same position, so why are they small? Probably because perspective projection is on by default in openGL? Have you called glOrtho()?


Look that bad ah! Well, I'm struggling with the matrix thing. Do you have an book or something related to transformation. Would be very appreciated. Edited by InusualZ

Share this post


Link to post
Share on other sites

 

glm::vec3 position = projection * model * topRight;

This happens by the GPU every frame. You should always have a static 3D model. The GPU will calculate those new actual translated and projected points every frame. So learn to use glMatrixMode() and how glTranslatef() work.

 

If you are using a newer version of openGL that doesn't support that, then you will need to send down a model and projection matrix to the shader and compute a final position there as " position = projection * model * in_Vertex;"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!