OpenGL纹理级别

时间:2011-08-17 21:25:54

标签: c++ opengl

我正在尝试学习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 个答案:

答案 0 :(得分:0)

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

感觉你已经远远超过了自己,你应该按顺序完成大部分(如果不是全部)教程。如果您正在尝试制作第一人称射击游戏,我还建议您从模型文件中加载关卡(在NeHe上也会有相关文章)