I'm making a voxel game like Minecraft which was working fine until I added water. Then I realized that whatever the texture was for the first visible block was applied to the rest of the blocks in the chunk. I am drawing a total of 36 vertices per cube and it seems that although the instanced array for the cube positions is working, only the first 36 texture coordinates are being used.
I raised all visible ocean. Locks in the second picture which is how I determined the actual coastline.
creating the buffers
glGenVertexArrays(1,&chunk->chunkVAO); //glGenBuffers(1,&chunk->chunkEBO); glGenBuffers(1,&chunk->cubeVBO); glGenBuffers(1,&chunk->posOffsetVBO); glGenBuffers(1,&chunk->texCoordVBO);
glBindVertexArray(chunk->chunkVAO);
glBindBuffer(GL_ARRAY_BUFFER,chunk->cubeVBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(cubeVertices),cubeVertices,GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(GLfloat),(GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,chunk->texCoordVBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(glm::vec2)*36*65536,NULL,GL_DYNAMIC_DRAW);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,2*sizeof(GLfloat),(GLvoid*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER,chunk->posOffsetVBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(glm::vec3)*65536,NULL,GL_DYNAMIC_DRAW);
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,3*sizeof(GLfloat),(GLvoid*)0);
glEnableVertexAttribArray(2);
glVertexAttribDivisor(2,1);
filling the texture and position VBO after occlusion culling
glBindBuffer(GL_ARRAY_BUFFER,chunk->texCoordVBO);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(texCoords),&texCoords[0]);
glBindBuffer(GL_ARRAY_BUFFER,chunk->posOffsetVBO);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(positions),&positions[0]);
glBindBuffer(GL_ARRAY_BUFFER,0);
rendering
glBindVertexArray(temp->chunkVAO);
glDrawArraysInstanced(GL_TRIANGLES,0,36,temp->visibleBlocks);
How come only one of the blocks worth of texture coordinates is being used?