坐标纹理数组无法与glDrawElements一起正常使用 [英] Coord texture array does not works correctly with glDrawElements

查看:108
本文介绍了坐标纹理数组无法与glDrawElements一起正常使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序的目的是加载并在每个面上显示具有相同纹理的简单多维数据集.但是问题是输出不是很好(只有4个面正确地纹理化).我尝试了纹理数组中许多坐标的组合,但是大多数时候情况更糟.是否可以使用glDrawElements函数正确设置纹理?

the purpose of my program is to load and display a simple cube with the same texture on each face. But the problem is that the output is not very good (just 4 faces are textured correctly).I tried lots of combinations of coordinates in the textures array but most of the time it's worse. Is it possible to set textures correctly with the function glDrawElements or not?

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

GLfloat vertices[] =
{
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    1.0, 1.0, -1.0,
    -1.0, 1.0, -1.0,
    -1.0, -1.0, 1.0,
    1.0, -1.0, 1.0,
    1.0, 1.0, 1.0,
    -1.0, 1.0, 1.0,
};

GLubyte indices[] =
{
    0, 1, 2, 3,
    4, 7, 6, 5,
    0, 4, 5, 1,
    3, 2, 6, 7,
    0, 3, 7, 4,
    1, 5, 6, 2
};

GLfloat textures[] =
{
    0,0, 1,0, 1,1, 0,1,
    0,1, 1,1, 1,0, 0,0,
    /*//0,0, 1,0, 1,1, 0,1,
    //0,0, 1,0, 1,1, 0,1,
    0,1, 1,1, 1,0, 0,0,
    0,1, 1,1, 1,0, 0,0,
    //0,0, 1,0, 1,1, 0,1*/
};

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Texture Mapping",NULL);
    SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

    bool continuer = true;
    SDL_Event event;
    GLuint texCube;

    glClearDepth(1.0f);
    glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
    glewInit();

    texCube = loadTexture("caisse.jpg");
    glBindTexture(GL_TEXTURE_2D, texCube);

    glEnable(GL_TEXTURE_2D);

    while (continuer)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = false;
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

        //Draw Cube ---------------------------------------

        glPushMatrix();
        glRotatef(45.0f, 1.0f, 1.0f, 0.0f);

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, 0, textures);

        glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        //-------------------------------------------------

        glPopMatrix();

        glFlush();
        SDL_GL_SwapBuffers();
    }

    glDisable(GL_TEXTURE_2D);
    SDL_Quit();

    return 0;
}

推荐答案

您正尝试仅使用8个顶点.为了获得正确的纹理,您必须使用12个单独的三角形,每个三角形都带有正确的"纹理坐标.问题是,当您尝试将平面贴图映射到球体的同胚点(例如,您的情况下为立方体)时,您会遇到这样的麻烦".

You are trying to use only 8 vertices. To get correct texturing you must use 12 separate triangles, each of them with the "correct" texture coordinates. The thing is when you're trying to do the planar map onto something homeomorphic to the sphere (e.g., a cube in your case), you will get "troubles" like this.

因此,将多维数据集拆分为12个三角形(是的,它们将共享坐标),并使用一种映射方法来计算纹理坐标.

So, split the cube into 12 triangles (yes, they will share coordinates) and calculate the texture coordinates with one of the mapping methods.

有关映射的详细信息,请参见此处:

See here for mapping details:

http://local.wasp.uwa.edu.au /〜pbourke/texture_colour/texturemap/

您的问题的答案是否可以使用glDrawElements函数正确设置纹理?"是是,但是您必须更改数组的布局".

The answer to your question "Is it possible to set textures correctly with the function glDrawElements or not?" is "Yes, but you have to change the arrays' layout".

更多的是,此站点上已经记录了一个解决方案: 对立方体进行纹理处理的问题

Even more, there's a solution already written down on this site: Problems texturing a cube

此处的顶点和纹理坐标的详细布局如下:

Detailed layout of the vertices and texture coordinates goes here: http://blogs.msdn.com/b/dawate/archive/2011/01/20/constructing-drawing-and-texturing-a-cube-with-vertices-in-xna-on-windows-phone-7.aspx

这篇关于坐标纹理数组无法与glDrawElements一起正常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆