简体   繁体   中英

Texturing in Tao.OpenGL when drawing via VBO

I am making an application which uses VBO buffers to draw models, instead of gl.Vertex . Parallelepiped which I want to draw, is drawn correctly, but texture doesn't. After an hour of searching I found, that I also need to add normals and vertices. But how I can get them?

My variables:

        static float x = 0;
    static float y = 0;
    static float z = 0;
    static float width = 10;
    static float height = 5;
    static float depth = 3;
    float[] points = new float[72] {x, y, z ,  x + width, y, z ,  x + width, y - height, z ,  x, y - height, z ,  x, y, z + depth ,  x + width, y, z + depth,
             x + width, y - height, z + depth ,  x, y - height, z + depth ,  x, y, z ,  x, y, z + depth ,  x, y - height, z + depth ,  x, y - height, z,  x + width, y, z,
             x + width, y, z + depth ,  x + width, y - height, z + depth  ,  x + width, y - height, z ,  x, y, z ,  x + width, y, z ,  x + width, y, z + depth ,  x, y, z + depth,
             x, y - height, z ,  x + width, y - height, z ,  x + width, y - height, z + depth ,  x, y - height, z + depth};

    float[] texcoords = new float[48] { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 };
    int vSize;
    int tSize;
    uint[] vboName = new uint[1] { 0 };
    uint[] texName = new uint[1] { 0 };

My method to load buffers:

            Gl.glEnable(Gl.GL_ARRAY_BUFFER_ARB);

        vSize = points.Length / 3;
        int s = points.Length * sizeof(float);
        object ss = s;
        IntPtr size = new IntPtr(ss.GetHashCode());

        tSize = texcoords.Length / 2;
        int t = texcoords.Length * sizeof(float);
        object tt = t;
        IntPtr tsize = new IntPtr(tt.GetHashCode());

        Gl.glGenBuffersARB(1, vboName);
        Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);
        Gl.glBufferDataARB(Gl.GL_ARRAY_BUFFER_ARB, size, points, Gl.GL_STATIC_DRAW);

        Gl.glGenBuffersARB(1, texName);
        Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
        Gl.glBufferDataARB(Gl.GL_ARRAY_BUFFER_ARB, tsize, texcoords, Gl.GL_STATIC_DRAW);

        Gl.glDisable(Gl.GL_ARRAY_BUFFER_ARB);

My method to draw models:

            Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
        Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);

        Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
        Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);

        Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, null);
        Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, null);

        Gl.glDrawArrays(Gl.GL_QUADS, 0, 24);

        Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
        Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);

And, finally, the result:

So dumb mistake... I have just forgotten to bind buffer before applying an texture:

        Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
        Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);


        Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);

        Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, null);
        Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
        Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, null);

        Gl.glDrawArrays(Gl.GL_QUADS, 0, 24);

        Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
        Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM