开发者

OpenGL texturing level

I'm trying to learn OpenGL, and I want to start texturing but I got stuck. I've created a level with two rooms and a corridor, so I want diferent textures for different parts (that is, a texture for the floor, texture for the walls and so on) but after looking at the NeHe tutorial I'm still stuck, where do I need to put it in my code?

#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,开发者_Python百科 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);
}


You don't seem to have any code for texture loading. I'd suggest you head back to NeHe and read this tutorial http://nehe.gamedev.net/tutorial/texture_mapping/12038/

It feels like you are getting too far ahead of yourself, and you should be going through most (if not all) of the tutorials in order. If you're trying to make a first person shooter, I'd also suggest loading the level from a model file (there are articles about this on NeHe too)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜