OpenGL纹理级别

Posted

技术标签:

【中文标题】OpenGL纹理级别【英文标题】:OpenGL texturing level 【发布时间】:2011-08-17 21:25:54 【问题描述】:

我正在尝试学习 OpenGL,我想开始纹理,但我卡住了。我已经创建了一个包含两个房间和一个走廊的关卡,所以我想要不同部分的不同纹理(即地板的纹理、墙壁的纹理等等),但是在查看了NeHe 教程后,我我还是卡住了,我需要把它放在我的代码哪里?

#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <gl\glu.h>

void init(void);
void display(void);
void keyboard(unsigned char, int, int);
void resize(int, int);

int is_depth; /* depth testing flag */

int main (int argc, char **argv)

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(40, 40);
    glutCreateWindow("The Cube World");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    /* This time we're going to keep the aspect ratio
    constant by trapping the window resizes. */
    glutReshapeFunc(resize);

    glutMainLoop();
    return 0;


void init(void)

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    is_depth = 1;
    glMatrixMode(GL_MODELVIEW);


void display(void)

    if (is_depth)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    else
        glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);

    //floor
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-50.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-50.0, 0.0, 25.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 0.0, 25.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 0.0, -25.0);

    //left wall
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-50.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-50.0, 00.0, 25.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(-50.0, 25.0, 25.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(-50.0, 25.0, -25.0);

    //right wall
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(50.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(50.0, 00.0, 25.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 25.0, 25.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 25.0, -25.0);

    //roof
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-50.0, 25.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-50.0, 25.0, 25.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 25.0, 25.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 25.0, -25.0);

    //right panel
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(10.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(10.0, 15.0, -25.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 15.0, -25.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 0.0, -25.0);

    //left panel
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-10.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-10.0, 15.0, -25.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(-50.0, 15.0, -25.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(-50.0, 0.0, -25.0);

    //top panel
    glColor3f(0.2f, 0.2f, 0.2f);
    //bottom right
    glVertex3f(50.0, 15.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    //top right
    glVertex3f(50.0, 25.0, -25.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    //top left
    glVertex3f(-50.0, 25.0, -25.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    //bottom left
    glVertex3f(-50.0, 15.0, -25.0);

    // corridor floor
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-10.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-10.0, 0.0, -275.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(10.0, 0.0, -275.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(10.0, 0.0, -25.0);

    // corridor left wall
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-10.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-10.0, 00.0, -275.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(-10.0, 15.0, -275.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(-10.0, 15.0, -25.0);

    // corridor right wall
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(10.0, 0.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(10.0, 00.0, -275.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(10.0, 15.0, -275.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(10.0, 15.0, -25.0);

    //corridor roof
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-10.0, 15.0, -25.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-10.0, 15.0, -275.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(10.0, 15.0, -275.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(10.0, 15.0, -25.0);

    //right panel room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(10.0, 0.0, -275.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(10.0, 15.0, -275.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 15.0, -275.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 0.0, -275.0);

    //left panel room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-10.0, 0.0, -275.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-10.0, 15.0, -275.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(-50.0, 15.0, -275.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(-50.0, 0.0, -275.0);

    //top panel room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(50.0, 15.0, -275.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(50.0, 25.0, -275.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(-50.0, 25.0, -275.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(-50.0, 15.0, -275.0);

    //right wall room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(50.0, 0.0, -275.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(50.0, 00.0, -325.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 25.0, -325.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 25.0, -275.0);

    //left wall room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-50.0, 0.0, -275.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-50.0, 00.0, -325.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(-50.0, 25.0, -325.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(-50.0, 25.0, -275.0);

    //roof room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-50.0, 25.0, -275.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-50.0, 25.0, -325.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 25.0, -325.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 25.0, -275.0);

    //back wall room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(50.0, 0.0, -325.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(50.0, 25.0, -325.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(-50.0, 25.0, -325.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(-50.0, 0.0, -325.0);

    //floor room 2
    glColor3f(0.2f, 0.2f, 0.2f);
    glVertex3f(-50.0, 0.0, -275.0);
    glColor3f(0.4f, 0.4f, 0.4f);
    glVertex3f(-50.0, 0.0, -325.0);
    glColor3f(0.6f, 0.6f, 0.6f);
    glVertex3f(50.0, 0.0, -325.0);
    glColor3f(0.8f, 0.8f, 0.8f);
    glVertex3f(50.0, 0.0, -275.0);

    glEnd();
    glutSwapBuffers();


void keyboard(unsigned char key, int x, int y)

    switch (key)
    
        case 'a':
        case 'A':
            glTranslatef(5.0, 0.0, 0.0);
            break;
        case 'd':
        case 'D':
            glTranslatef(-5.0, 0.0, 0.0);
            break;
        case 'w':
        case 'W':
            glTranslatef(0.0, 0.0, 5.0);
            break;
        case 's':
        case 'S':
            glTranslatef(0.0, 0.0, -5.0);
            break;
        case 't':
        case 'T':
            if (is_depth)
            
                is_depth = 0;
                glDisable(GL_DEPTH_TEST);
            
            else
            
                is_depth = 1;
                glEnable(GL_DEPTH_TEST);
            
    
    display();


void resize(int width, int height)

    if (height == 0)
        height = 1;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    /* Note we divide our width by our height to get the aspect ratio. */
    gluPerspective(45.0, width / height, 1.0, 400.0);

    /* Set initial position */
    glTranslatef(0.0, -5.0, -150.0);

    glMatrixMode(GL_MODELVIEW);

【问题讨论】:

我建议你去实际阅读教程。这个问题太可怕了。 【参考方案1】:

您似乎没有任何纹理加载代码。我建议你回到 NeHe 并阅读本教程 http://nehe.gamedev.net/tutorial/texture_mapping/12038/

感觉好像你走得太远了,你应该按顺序阅读大部分(如果不是全部)教程。如果您正在尝试制作第一人称射击游戏,我还建议您从模型文件中加载关卡(NeHe 上也有关于此的文章)

【讨论】:

以上是关于OpenGL纹理级别的主要内容,如果未能解决你的问题,请参考以下文章

手把手教会OpenGL之纹理贴图包含纹理载入纹理过滤边界处理纹理参数设置(入门级别案例,棋盘)

手把手教会OpenGL之纹理贴图包含纹理载入纹理过滤边界处理纹理参数设置(入门级别案例,棋盘)

OpenGL中的纹理坐标

OpenGL RGB DXT1压缩纹理mipmap上传

OpenGL ES纹理

OpenGL-纹理(下)