0
votes

I'm trying to texture a cube by using only one texture, but only rendering one part of it on each face.

This is the texture I wanted to use for now:

enter image description here

My goal is to get this done via the shaders, I'm not experienced with them yet but I'm trying my best to learn..

Here's my current fragment shader:

uniform sampler2D tex0;

void main()
{
    vec2 pos = vec2(gl_TexCoord[0] / 4.0);
    vec3 test = texture2D(tex0,pos).rgb;
    gl_FragColor.rgb = test;
}

Obviously it's just the beginning and all of the sides are using the same part of the texture, I'm just new to shaders and I'm not even sure if this really can be done this way..

I just have no clue if it's really possible to determine which face I'm currently on and such, I thought I might be able to pass information about the vertex to the fragment shader and then act according to it?

P.S: I went for this in the first place because I want a functionality where if the cube will be resized, the texture on every side will repeat itself rather than stretch

1

1 Answers

0
votes

gl_TexCoord is deprecated. You should create another general-purpose vertex attribute and store texture coordinates in it.

While applying texture is done in fragment shader, texture coordinates are usually supplied to vertex shader first and then interpolated in it linearly across all generated fragments:

#version 430
//...
layout(location = 1) in vec2 attr_texcoord;
out vec2 out_texcoord;

void main () {
    out_texcoord = attr_texcoord;
    //...
}

Now you can use out_texcoord in fragment shader.