0
\$\begingroup\$

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.

enter image description here

enter image description here

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?

\$\endgroup\$
  • \$\begingroup\$ Without seeing your code it's impossible to help and find the issue. \$\endgroup\$ – cozmic Jan 7 '17 at 9:13
  • \$\begingroup\$ If you want to take pictures of your screen, use print screen \$\endgroup\$ – Bálint Jan 7 '17 at 15:01
  • \$\begingroup\$ Show us the code, where you render stuff \$\endgroup\$ – Bálint Jan 7 '17 at 15:03
  • \$\begingroup\$ @Bálint like that? \$\endgroup\$ – Ben Beazley Jan 7 '17 at 15:54
  • \$\begingroup\$ Just having a quick look without diving too much into it, but your 3rd GL call from the first snippet seems suspicious. You are calling bufferData with a sizeof on cubeVertices, which is likely a pointer of some sort. I'd assume you instead would mean to do something like sizeof(CubeVertex) * amount of vertices. \$\endgroup\$ – Athos vk Jan 7 '17 at 17:42
0
\$\begingroup\$

Here was the problem. I stored all of the textures on a texture atlas and referred to them by including an integer in the instance data. The problem was that I used glVertexAttribPointer with the parameter that says the data is normalized. When using integers as vertex attributes you use glVerteAttribIPointer (extra I). Then the coastline looked fine.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.