0

我已经修改了我的原始代码(上一篇文章)以使用两个纹理。他们来了:

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "support.h"

using namespace std;

GLuint *images;
GLubyte *image;
GLuint texture1=0;
GLuint texture2=0;
GLuint LoadTexture(int width, int height, char *fName);

int n=200;
int m=200;

void showPictureW();
void showPictureR();

float rX=0;
float rY=0;
float rZ=0;
float tX=0;
float tY=0;
float tZ=-3.5;
float nr = -5.0;
float fr = -4.0;
float tpx = 1.0;
float tpy = 1.0;
float btx = -1.0;
float bty = -1.0;
float dZ =-2.0;

void showPictureW()
{
    /*  clear all pixels  */
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3d(1,1,1);

    //face 1
    glBegin( GL_POLYGON );
        glTexCoord2d(1.0,1.0); glVertex3d(-1.0,1.0,-2.0);
        glTexCoord2d(1.0,0.0); glVertex3d(-1,-1.0,-2.0);
        glTexCoord2d(0.0,0.0); glVertex3d(1.0,-1.0,-2.0);
    glEnd();

    //face 2
    glBegin( GL_POLYGON );
        glTexCoord2d(0.0,0.0); glVertex3d(1.0,-1.0,-2.0);
        glTexCoord2d(0.0,1.0); glVertex3d(1.0,1.0,-2.0);
        glTexCoord2d(1.0,1.0); glVertex3d(-1.0,1.0,-2.0);
    glEnd();
}

void showPictureR()
{
    //clear all pixels
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,1,1);

    //face 1
    glBegin( GL_POLYGON );
        glTexCoord2d(1.0,1.0); glVertex3d(-1.2,1.0,-2.0);
        glTexCoord2d(1.0,0.0); glVertex3d(0.0,1.0,-2.0);
        glTexCoord2d(0.0,0.0); glVertex3d(0.0,2.0,-2.0);
    glEnd();

    //face 2
    glBegin( GL_POLYGON );
        glTexCoord2d(0.0,0.0); glVertex3d(0.0,2.0,-2.0);
        glTexCoord2d(0.0,1.0); glVertex3d(0.0,1.0,-2.0);
        glTexCoord2d(1.0,1.0); glVertex3d(1.2,1.0,-2.0);
    glEnd();
}

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, texture1 );
    glPushMatrix();
        glTranslated(0,0,0);
        glRotated(rX,0,0,1);
        glRotated(rY,0,1,0);
        glRotated(rZ,1,0,0);
        showPictureW();
    glPopMatrix();

    glBindTexture( GL_TEXTURE_2D, texture2 );
    glPushMatrix();
        glTranslated(0,0,0);
        glRotated(rX,0,0,1);
        glRotated(rY,0,1,0);
        glRotated(rZ,1,0,0);
        showPictureR();
    glPopMatrix();

    glutSwapBuffers();
}

static void key(unsigned char key, int x, int y)
{
    switch (key) 
    {
        case 27 : 
        case 'q':
            exit(0);
            break;
        case '+':
            break;
        ...
    }
    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("FreeGLUT Shapes");
    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    texture1 = LoadTexture( 200, 200, "wall.ppm" );
    texture2 = LoadTexture( 200, 200, "roof.ppm" );

    glutMainLoop();

    return EXIT_SUCCESS;
}

GLuint LoadTexture(int width, int height, char *fName)
{
    FILE *fd;
    int k, nm;

    int i;
    char bc[71];
    float s;
    unsigned int red, green, blue;
    char c;

    fd = fopen(fName,"r");
    fscanf(fd,"%[^\n]",bc); // reads data from stream
    printf("Nilai %s \n\n",bc);
    if(bc[0]!='P'|| bc[1]!='3')
    {
        printf("%s Not a PPM file\n",bc);
        exit(0);
    }
    printf("%s is a PPM file\n",bc);

    fscanf(fd,"%s",bc);
    printf("line 1 %s \n",bc);

    fscanf(fd,"%s",bc);
    printf("line 2 %s \n",bc);

    fscanf(fd,"%s",bc);
    printf("line 3 %s \n",bc);

    fscanf(fd,"%s",bc);
    printf("line 4 %s \n",bc);

    fscanf(fd,"%c",&c);
    ungetc(c,fd);
    fscanf(fd,"%d %d %d", &n,&m,&k);

    nm = n*m;
    images = new GLuint[3*sizeof(GLuint)*nm];

    for (i=0;i<nm;i++)
    {
        fscanf(fd,"%u %u %u", &red, &green, &blue);
        images[3*nm-3*i-3] = red;
        images[3*nm-3*i-2] = green;
        images[3*nm-3*i-1] = blue;    
    }
    int totSize = 3*nm;
    int tot=0;
    for(i=0;i<nm;i++)
    {
        tot++;
    }

    image = new BYTE[width*height*3*sizeof(BYTE)];
    int nmOne = width*height;
    for(int i=0;i<nmOne*3;i++)//copy each value 3 times
    {
        image[i] = (GLubyte)images[i];
        image[i] = (GLubyte)images[i];
        image[i] = (GLubyte)images[i];
    }  

    bool wrap = true;
    GLuint texture = 0;
    glGenTextures( 1, &texture );
    // select our current texture
    glBindTexture( GL_TEXTURE_2D, texture );
    // select modulate to mix texture with color for shading
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    // if wrap is true, the texture wraps over at the edges (repeat)
    //       ... false, the texture ends at the edges (clamp)
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,wrap ? GL_REPEAT : GL_CLAMP );
    // build our texture mipmaps
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
    GL_RGB, GL_UNSIGNED_BYTE, image );

    return texture;
}

我的问题是它不会同时加载两个纹理。我试过一次用一个,没问题。但是使用上面的代码只会出现第二个纹理(“roof.ppm”)。我在另一篇文章中找到了下面的代码,似乎与我的问题非常相似,但我不确定如何使用它。

void DrawFrame()
{
    ...

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D(0, this.Width, 0, this.Height);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    // "camera" transform(s)

    foreach object
    {
        glPushMatrix();
        // per-object matrix transform(s)

        // draw object
        glPopMatrix();
    }
}
4

1 回答 1

4

你确实打电话

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

在你的showPictureWshowPictureR函数中,所以最后只能看到这些调用中的最后一个效果应该不会让你感到惊讶。

于 2013-06-01T23:36:29.877 回答