Advertisement Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

104 Neutral

About ZekeBraiden

  • Rank

Personal Information

  • Interests
  1. Hi, I'm starting to learn OpenGL and it's been really fun because of the amount of things I can do. I would like to implement a tilemap in my game engine, but the way I'm trying to do this is consuming a lot of CPU and GPU, just to draw parts of the texture, without entities or animations. The approach I'm using is to load a texture with all the tiles (tileset) and traverse the image by drawing only parts of it in specific (side-by-side) positions in the visible region of the screen. The OpenGL version is 3.3. And the code can be seen below. I think there must be a right way to do it, because old games like Diablo 2 draw a big map with several things happening during the game and consume practically nothing, even playing for hours. void GameMap::draw() { // Use shader m_shader->use(); // Use texture m_texture->bind(); glBindVertexArray(m_quadVAO); // Draw map for (size_t r = 0; r < 10; r++) { for (size_t c = 0; c < 10; c++) { // Tile position m_tileCoord->setX(c*m_tileHeight); m_tileCoord->setY(r*m_tileHeight); // Isometric perspective m_tileCoord->convert2DToIso(); // Draw tile by index drawTile(0); } } glBindVertexArray(0); } void GameMap::drawTile(GLint index) { // Identity matrix glm::mat4 position_coord = glm::mat4(1.0f); glm::mat4 texture_coord = glm::mat4(1.0f); // Part of the texture m_srcX = index * m_tileWidth; GLfloat clipX = m_srcX / m_texture->m_width; GLfloat clipY = m_srcY / m_texture->m_height; // Coordinates of the texture texture_coord = glm::translate(texture_coord, glm::vec3(clipX, clipY, 0.0f)); // Coordinates of the vertices position_coord = glm::translate(position_coord, glm::vec3(m_tileCoord->getX(), m_tileCoord->getY(), 0.0f)); position_coord = glm::scale(position_coord, glm::vec3(m_tileWidth, m_tileHeight, 1.0f)); // Change the shader m_shader->setMatrix4("texture_coord", texture_coord); m_shader->setMatrix4("position_coord", position_coord); // Draws part of the texture glDrawArrays(GL_TRIANGLES, 0, 6); } void GameMap::initRenderData() { // Coordinates GLfloat posX = (GLfloat)m_tileWidth / m_texture->m_width; GLfloat posY = (GLfloat)m_tileHeight / m_texture->m_height; // Configure VAO/VBO GLuint VBO; GLfloat vertices[] = { // Left triangle // Pos // Tex 0.0f, 1.0f, 0.0f, posY, // Bottom left corner 1.0f, 0.0f, posX, 0.0f, // Top right corner 0.0f, 0.0f, 0.0f, 0.0f, // Upper left corner // Right triangle 0.0f, 1.0f, 0.0f, posY, // Lower left corner 1.0f, 1.0f, posX, posY, // Bottom right corner 1.0f, 0.0f, posX, 0.0f // Top right corner }; glGenVertexArrays(1, &m_quadVAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindVertexArray(m_quadVAO); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } // ------------------------------------------------------------------------------------------ // Vertex Shader #version 330 core layout (location = 0) in vec4 vertex; // <vec2 position, vec2 texCoords> out vec4 TexCoords; uniform mat4 texture_coord; uniform mat4 position_coord; uniform mat4 projection; void main() { TexCoords = texture_coord * vec4(vertex.z, vertex.w, 1.0, 1.0); gl_Position = projection * position_coord * vec4(vertex.xy, 0.0, 1.0); } // Fragment Shader #version 330 core out vec4 FragColor; in vec4 TexCoords; uniform sampler2D image; void main() { FragColor = texture(image, vec2(TexCoords.x, TexCoords.y)); }
  2. ZekeBraiden

    Tile map bad performance

    I'm developing a 2D tile based game in JavaFx in college, I already have the sprites, animations, audio, menu ..., but I found a huge problem in rendering the map. On a 4GB notebook, Intel graphics card, Core i5, where any 2D Steam game works well at 60 FPS, my game runs from 27 to 31 FPS. I noticed that the precarious part is rendering the map, but I do not know why it's so slow. Here is the map rendering code: // Clear Layers this.getLayer1GC().clearRect(0, 0, this.getLayer1GC().getCanvas().getWidth(), this.getLayer1GC().getCanvas().getHeight()); this.getLayer2GC().clearRect(0, 0, this.getLayer2GC().getCanvas().getWidth(), this.getLayer2GC().getCanvas().getHeight()); int index = 0; int tileSize = 64 * Engine.SCALE; // 128x128 int startCol = Math.max(0, (int) ( / tileSize)); int endCol = Math.min(this.width, (int) (( + / tileSize) + 1); int startRow = Math.max(0, (int) ( / tileSize)); int endRow = Math.min(this.height, (int) (( + / tileSize) + 1); int tileX, tileY, indexLayer1, indexLayer2; for (int c = startCol; c < endCol; c++) { for (int r = startRow; r < endRow; r++) { // Tile Index index = this.tileManager.getTileIndex(c, r, this.width); // Tile Coordinates tileX = (int) (c * tileSize -; tileY = (int) (r * tileSize -; // Cache Tileset Images indexLayer1 = this.layer1.tileArray[index]; indexLayer2 = this.layer2.tileArray[index]; // Layer 1 if (indexLayer1 > 0) { this.getLayer1GC().drawImage(this.mapTilset.tileImages[indexLayer1], 0, 0, tileSize, tileSize, tileX, tileY, tileSize, tileSize); } // Layer 2 if (indexLayer2 > 0) { this.getLayer2GC().drawImage(this.mapTilset.tileImages[indexLayer2], 0, 0, tileSize, tileSize, tileX, tileY, tileSize, tileSize); } } }
  3. This means that I tried , but not understand the logic behind how this works, was the same as not trying. Well, I'll be more clear, I already have the code to detect the collision between two convex polygons using SAT, I would just adapt it to work with circle too . I could spend hours trying to get the right way to do it, but I'm counting on some veteran game developer area to put me on track and explain it objectively without having to be Harvard-educated to understand   No need to draw, nor give ready code only explain the steps   These are the best tutorials I found were
  4. Hello guys , can someone explain me the collision algorithm and response (move away) between the circle and a polygon any, I heard That You can use SAT, but it did not work...
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!