What are the texture coordinates for a cube in OpenGL?

I have a cube that is defined as:

float vertices[] = { -width, -height, -depth, // 0
                              width, -height, -depth, // 1
                              width,  height, -depth, // 2
                             -width,  height, -depth, // 3
                             -width, -height,  depth, // 4
                              width, -height,  depth, // 5
                              width,  height,  depth, // 6
                             -width,  height,  depth // 7
        };  

and I have a 128x128 image that I just want to draw on each of the 6 faces of the cube and nothing else. So what is the texture of cooridinates? I need actual values.

This is the drawing code:

// Counter-clockwise winding.
        gl.glFrontFace(GL10.GL_CCW);
        // Enable face culling.
        gl.glEnable(GL10.GL_CULL_FACE);
        // What faces to remove with the face culling.
        gl.glCullFace(GL10.GL_BACK);
        // Enabled the vertices buffer for writing and to be used during
        // rendering.
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        // Specifies the location and data format of an array of vertex
        // coordinates to use when rendering.
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer);


            // Bind the texture according to the set texture filter
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[filter]);
            gl.glEnable(GL10.GL_TEXTURE_2D);



            // Enable the texture state
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            // Point to our buffers
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);



        // Set flat color
        gl.glColor4f(red, green, blue, alpha);


        gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices,
                GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);

        // ALL the DRAWING IS DONE NOW

        // Disable the vertices buffer.
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);


            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);


        // Disable face culling.
        gl.glDisable(GL10.GL_CULL_FACE);

This is an array of indices:

short indices[] = { 0, 2, 1,
                0, 3, 2,

                1,2,6,
                6,5,1,

                4,5,6,
                6,7,4,

                2,3,6,
                6,3,7,

                0,7,3,
                0,4,7,

                0,1,5,
                0,5,4


               };  

I'm not sure if an index array is needed to find the tex coordinates. Note that the vertex array of the cube I gave is the most efficient representation of the cube using an array of indices. The cube draws fine, but not texture. Only one side shows the correct picture, but the other sides are corrupted. I used the methods described in various online textbooks on textures, but it does not work.

+3
source share
5 answers

, , . OpenGL ( ) 2D . , . ( , !) , ( ).

GLuint mHandle;
glGenTextures(1, &mHandle); // create your texture normally

// Note the target being used instead of GL_TEXTURE_2D!
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glBindTexture(GL_TEXTURE_CUBE_MAP, mHandle);

// Now, load in your six distinct images. They need to be the same dimensions!
// Notice the targets being specified: the six sides of the cube map.
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, width, height, 0,
    format, GL_UNSIGNED_BYTE, data1);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, width, height, 0,
    format, GL_UNSIGNED_BYTE, data2);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, width, height, 0,
    format, GL_UNSIGNED_BYTE, data3);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, width, height, 0,
    format, GL_UNSIGNED_BYTE, data4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, width, height, 0,
    format, GL_UNSIGNED_BYTE, data5);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, width, height, 0,
    format, GL_UNSIGNED_BYTE, data6);

glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

// And of course, after you are all done using the textures...
glDeleteTextures(1, &mHandle);

3 2. 8 , . N = 1.0/sqrt (3.0), N, N, N; - N, N, -N; .

+7
  • , ( , )
  • , , ,
  • , (0, 0) (0, 1) (1, 1) (1, 0). (24 , 4 ) .
+3

width = x, height = y depth = z. 6 .

float vertices[] = { -x, -y, -z, // 0
                              x, -y, -z, // 1
                              x,  y, -z, // 2
                             -x,  y, -z, // 3
                             -x, -y,  z, // 4
                              x, -y,  z, // 5
                              x,  y,  z, // 6
                             -x,  y,  z// 7
        };  

, ( 0,0,0 , ), , 8 4 , 4,5,6,7, -x, -y -x, y.

, -z, 0,1,2,3.

? -x, 0,3,4,7, x, 1,2,5,6.

.

+2

2 , , , [0] - [3] , :

float texCoords[] = { 0.0, 0.0,  //bottom left of texture
                      1.0, 0.0,  //bottom right "    "
                      1.0, 1.0,  //top right    "    "
                      0.0, 1.0   //top left     "    "
                    };

You can use these coordinates to texture each subsequent side of the entire texture.

+2
source

To display skybox (cubemap), the following shader works for me:

Cubemap vertexshader::
        attribute vec4 a_position;             
        varying vec3 v_cubemapTexture;          
        vec3 texture_pos;                           
        uniform vec3 u_cubeCenterPt;            
        uniform mat4 mvp;                       
        void main(void)                     
        {                                       
         gl_Position = mvp * a_position;        
         texture_pos = vec3(a_position.x - u_cubeCenterPt.x, a_position.y - u_cubeCenterPt.y, a_position.z - u_cubeCenterPt.z);
         v_cubemapTexture = normalize(texture_pos.xyz); 
        }                                       

Cubemap fragmentshader::
        precision highp float;                                              
        varying vec3 v_cubemapTexture;                                      
        uniform samplerCube cubeMapTextureSample;                           
        void main(void)                                                 
        {                                                                   
          gl_FragColor = textureCube(cubeMapTextureSample, v_cubemapTexture);  
        }

Hope this is helpful ...

0
source

Source: https://habr.com/ru/post/1791814/


All Articles