Loading Texture to Cube

Let's cover the cube we created in the previous article with texture. But, I will change the generating cube method. Because it is not possible to apply texture coordinates using an index buffer. (It may be possible)

If you don't know how to use texture, you can check it out Texture in OpenTK | let's develop games (letsdevelopgames.com)
    private void GenerateCube()
    {
        float len = 1.0f;

        float[] vertices = 
        {
            -len, -len, -len,   // 0
             len, -len, -len,   // 1 
             len, -len,  len,   // 2 
            -len, -len,  len,   // 3 

            -len,  len, -len,   // 4
             len,  len, -len,   // 5
             len,  len,  len,   // 6
            -len,  len,  len,   // 7
        };

        float[] cubeVertices = new float[180];


        int[] indices = 
        {
            7, 6, 2,  7, 2, 3,      // front face
            4, 5, 1,  4, 1, 0,      // back face
            4, 5, 6,  4, 6, 7,      // top face
            0, 1, 2,  0, 2, 3,      // bottom face
            6, 5, 1,  6, 1, 2,      // right face
            7, 4, 0,  7, 0, 3       // left face
        };

        float[] texCoords = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f }; 

        for (int i = 0; i < indices.Length; i++)
        {
            int index = indices[i];
            cubeVertices[i * 5 + 0] = vertices[(index * 3) + 0];
            cubeVertices[i * 5 + 1] = vertices[(index * 3) + 1];
            cubeVertices[i * 5 + 2] = vertices[(index * 3) + 2];
            cubeVertices[i * 5 + 3] = texCoords[(i % 6) * 2];
            cubeVertices[i * 5 + 4] = texCoords[(i % 6) * 2 + 1];
        }

        indexCount = 36;

        GenerateVAOforCube(len, cubeVertices, indices);
    }
I removed the ebo in the GenerateVAOforCube:
    private void GenerateVAOforCube(float len, float[] vertices, int[] indices)
    {
        GL.GenVertexArrays(1, out vao);
        GL.BindVertexArray(vao);
        // create vbo to store the data in opengl and copy data to vbo
        GL.GenBuffers(1, out vbo);
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
        GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * vertices.Length, vertices, BufferUsageHint.StaticDraw);
        // tell opengl how to use the data via attributes
        // position attribute
        // int attrPosition_Position = GL.GetAttribLocation(shader.ShaderProgram, "aPos");
        int attrPosition_Position = 0;
        GL.EnableVertexAttribArray(attrPosition_Position);
        GL.VertexAttribPointer(attrPosition_Position, 3, VertexAttribPointerType.Float, false, sizeof(float) * 5, 0);
        int attrPosition_TextureCoords = 1;
        GL.EnableVertexAttribArray(attrPosition_TextureCoords);
        GL.VertexAttribPointer(attrPosition_TextureCoords, 2, VertexAttribPointerType.Float, false, sizeof(float) * 5, sizeof(float) * 3);
        
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        GL.BindVertexArray(0);
    }
I'm going to use this image as texture:
cube texture
I changed the draw method of GameObject class:
        public void Draw()
        {
            GL.UseProgram(Shader!.ShaderProgram);

            ...

            GL.BindVertexArray(VAO);
            if(objectType == ObjectType.Cube)
                GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
            else
                GL.DrawElements(PrimitiveType.Triangles, IndexCount, DrawElementsType.UnsignedInt, 0);
            GL.BindVertexArray(0);
        }
I added one cube to the scene:
            objectManager.AddObject(ObjectType.Cube);
            objectManager.gameObjects[objectManager.gameObjects.Count-1].Texture = new Texture("resources/images/cubetexture.png");
This is the output:
Textured Cube
Let's add more cube:
Cubes

No comments:

Post a Comment