Hello,
in short: I am trying to draw a tile map with instanced drawing and the different tiles are all within one texture atlas.
My original idea was to create a tile object, instance-draw it and then move each tile by manipulating the position with a vector array.
The different tiles would then result from manipulating the texture coordniates.
As each tile I want to draw right now is right from the original, I just manipulate the x-coordinate of the texture.
Here is the source code. I first create the translation vectors & then create the texture offset vectors (to manipulate the texture coordinates):
for(signed int x = -64; x < 64; x++)
{
for(signed int y = -64; y <64; y++)
{
glm::vec3 translation;
translation.x = (float)x / 64.0f;
translation.y = (float)y / 64.0f;
translation.z = 0.0f;
//translation = glm::normalize(translation);
translations[transIndex] = translation;
transIndex += 1;
}
}
glm::vec2 TexOffset[16384];
int texIndex = 0;
for(unsigned int x = 0; x < giv_GroundTileData.size()+1; x++)
{
for(unsigned int y; y < giv_GroundTileData.size()+1; y++)
{
float xCoord = giv_vStartingPointOnTileMap.x + giv_GroundTileData[x][y] / 10.0f;
glm::vec2 TexCoordinates = glm::vec2(xCoord, giv_vStartingPointOnTileMap.y);
TexOffset[texIndex++]=TexCoordinates;
}
}
My vertex shader:
#version 330 core
layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 tex;
layout (location = 2) in vec3 pOffset;
layout (location = 3) in vec2 tOffset;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
void main()
{
TexCoords = tex + tOffset;
gl_Position = projection * view * model * vec4(Position + pOffset, 1.0);
}
My “Render-Data function”:
//Create the instance Buffers
glGenBuffers(1, &this->m_posVBO);
glGenBuffers(1, &this->m_texVBO);
//Bind the position Buffer (Offset)
glGenBuffers(1, &this->m_posVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_posVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * 16384, &translations[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Bind the texture offset buffer
glGenBuffers(1, &this->m_texVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_texVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * 16384, &TexOffset[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &this->m_VAO_std);
glGenBuffers(1, &this->VBO_std);
glBindVertexArray(this->m_VAO_std);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO_std);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexArray), VertexArray, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1,2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, this->m_posVBO);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribDivisor(2, 1);
glEnableVertexAttribArray(3);
glBindBuffer(GL_ARRAY_BUFFER, this->m_texVBO);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribDivisor(3, 1);
glBindVertexArray(0);
In my thought this would lead to an addition of my translation-vectors (m_posVBO) to the position vector & of the texture-offset (m_texVBO) to the texture coordinates which are passed to the fragment shader.
However this leads to this result:
[ATTACH=CONFIG]1612[/ATTACH]
I experimented a little bit with the translations vectors, but they have to be normalized vectors don’t they?
Each of the tiles is 64x64px and I try to render a 128*128 TileMap.
In my brain that should lead to the vector-creating-loop I coded, but obviously there seems to something wrong in my understanding.
The Offset btw. the tiles should be 64px - the length/height of a tile. with 1 being equal to 128*64 (128 Tiles, each 64px?) the offset would be 0.015625.
I also tried to increase the offset which lead to the result of 3 “stuffed blocks” of tiles, each being roughly the value of the added offset away from each other.
So my question:
Where am I wrong here? Which part of the instancing concept do I get wrong?