0

I am making a little voxel engine using a chunk system (like in Minecraft). I decided to make 1 VBO per chunk, so the VBO contain multiple cubes that will use different textures.

I actually have the UV of a cube and i would like to use it on all cubes in a VBO so the texture will wrap all cubes the same way if the cubes were in separated VBOs.

Here is what I'm actually getting:

actual result

How to tell OpenGL to do the same thing as the first cube on all cubes?

EDIT: here are my shaders: vertex shader #version 400

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;

out vec2 UV;

uniform mat4 MVP;

void main() {
    gl_Position = MVP * vec4(vertexPosition_modelspace, 1);
    UV = vertexUV;
}

fragment shader

#version 400

in vec2 UV;

out vec3 color;

uniform sampler2D textureSampler;

void main(){
  color = texture(textureSampler, UV).rgb;
}

my glfw loop:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &chunkHandler.player.mvp[0][0]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, grass);
glUniform1i(textureID, 0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, chunkHandler.loaded_chunks[0]->vboID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, tboID);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);

glDrawArrays(GL_TRIANGLES, 0, chunkHandler.loaded_chunks[0]->nbVertices);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glfwSwapBuffers(window);
glfwPollEvents();

tboID: tbo is for texture buffer object

how i create the TBO:

glGenBuffers(1, &tboID);
    glBindBuffer(GL_ARRAY_BUFFER, tboID);
    glBufferData(GL_ARRAY_BUFFER, 36 * 2 * sizeof(float), uvcube, GL_STATIC_DRAW);
  • 2
    It's not really clear. You want to have 1 unique texture for your VBO (applied to all cubes in the VBO) or 1 texture per cube in the VBO ? – xryl669 Feb 08 '19 at 17:36
  • 1 unique texture for the VBO applied to all cubes in the VBO. (Multiple textures is another problem i don't need help for it) – Shirakawa42 Feb 08 '19 at 17:41
  • Not part of the question, but I would consider instancing if you are displaying a lot of cubes. – Guillaume Racicot Feb 08 '19 at 17:49
  • Also, would `glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);` work? – Guillaume Racicot Feb 08 '19 at 17:51
  • I don't understand your problem then. When you store your vertex in the VBO, you are storing the UV coordinate for the texture, for each vertex, right ? You have also set the texture unit correctly (from your screenshot), so it's just a matter of calling the texture2D call in your shader. – xryl669 Feb 08 '19 at 17:53
  • You think the problem is only in shaders ? i'll edit my question with my current shaders and my loop – Shirakawa42 Feb 08 '19 at 18:00
  • @GuillaumeRacicot no it's not working, i already tried – Shirakawa42 Feb 08 '19 at 18:05
  • I don't get it. As far as i understand, there are 4 cubes in the image. There is 1 shader program and 1 texture, isn't it. So if the grass texture is bound it should be wrapped on each cube. What about `tboID`? Is the array buffer `tboID` (for the texture coordinates) valid for all 4 cubes? – Rabbid76 Feb 08 '19 at 18:32
  • @Rabbid76 yes there are 4 cubes on this image, all cubes are in 1 VBO, there is 1 shader and 1 texture, but the texture only wrap 1 cube (on all it's sides, also masked sides) but i don't know how to make it wrap all cubes 1 by 1, staying with 1 VBO – Shirakawa42 Feb 08 '19 at 18:41
  • @Shirakawa42 You don't need to do anything special, do the same for all 4 cubes. I guess that `tboID` is only valide for the 1st cube. Do you delete it somewhere? Is the variable `tboID` not set for the 2nd, 3rd and 4th cube? – Rabbid76 Feb 08 '19 at 18:42
  • @Rabbid76 that's what i'm looking for, i create the tbo just before entering the loop and i never delete it, but i don't know why it's not repeating the tbo on all cubes – Shirakawa42 Feb 08 '19 at 19:07
  • @Rabbid76 i added how i create the TBO but i don't know what to add to code :/ – Shirakawa42 Feb 08 '19 at 19:17
  • @Shirakawa42 Try the following; add `glBindBuffer(GL_ARRAY_BUFFER, 0);` right **after** `glBindBuffer(GL_ARRAY_BUFFER, tboID);` `glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);` – Rabbid76 Feb 08 '19 at 19:20
  • @Rabbid76 not working :/ – Shirakawa42 Feb 08 '19 at 19:42
  • @Shirakawa42 I'm out of ideas. But read [OpenGL object in C++ RAII class no longer works](https://stackoverflow.com/questions/46839586/opengl-object-in-c-raii-class-no-longer-works) – Rabbid76 Feb 08 '19 at 19:44

1 Answers1

0

While not a complete answer, I can help in debugging. It seems to me that the texture coordinate are wrong (you don't provide the code for filling them).

In your fragment shader, I would output the U and V coordinate as colors:

#version 400

in vec2 UV;

out vec3 color;

uniform sampler2D textureSampler;

void main(){
  color = vec3(UV.u, UV.v, 0);
}

If the coordinates are correct, you should have a gradient on each cube face (each cube vertex will be colored based on its UV, so a (0,0) vertex is black and (0,1) is green and so on). If it's not the case, try to fix the texture value until you can see them correctly.

This looks suspicious to me: glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);

xryl669
  • 3,376
  • 24
  • 47
  • texture coordinates are good, actually all the sides of the textured cube are textured, also the masked sides. And when i am displaying only one cube it looks great – Shirakawa42 Feb 08 '19 at 18:39
  • Did you get the gradient cubes ? Because if your texture coordinates are all 0 (or 1) (except for the bottom left cube), you'd get a uniform color for all other cubes. Please provide a dump of uvcube array. The size of the array is strange, you should have 18 vertex for 4 adjacent cubes (if sharing vertices) or 32 if independant and not 36. Make sure it's matching the number of vertices in the VBO's – xryl669 Feb 08 '19 at 23:02
  • Also, it'd be a better idea to associate the position and texture coordinate in a single object (store `x,y,z,u,v` per vertex, and use the 5th parameter to give the stride to the u,v pair). That way you can't mix the number of vertices, you've a single buffer to upload, and you don't need many fancy enabling disabling sequence. – xryl669 Feb 08 '19 at 23:05