Repeat UV for multiple cubes in one vertex buffer object (VBO)?

柔情痞子 提交于 2019-12-11 23:24:33

问题


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:


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);

回答1:


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);



来源:https://stackoverflow.com/questions/54597564/repeat-uv-for-multiple-cubes-in-one-vertex-buffer-object-vbo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!