在opengl中纹理化之后隐藏了立方体的两面

时间:2014-07-11 10:50:29

标签: c++ opengl textures cube

我尝试使用opengl在每一侧绘制一个具有不同纹理的立方体 但现在 ... 有什么问题?! 在为立方体的每个面添加不同的纹理之后:

See Picture

这是我的代码:

> #include "stdafx.h"
> #include <stdio.h>
> #include <stdlib.h>
> #include <gl/glut.h>
> #include <iostream>
> 
> static GLuint tex[6]; static GLdouble angle = 1;   
> 
> void init() {     glClearColor(0,0,0,1);  glEnable(GL_TEXTURE_2D); }
> GLuint loadtexture(const char *filename,int width,int height) {
>   GLuint texture;     unsigned char *data;    FILE *f;    f =
> fopen(filename,"rb");     if(!f)          return 0;   data = (unsigned
> char*)malloc(width*height*3);     fread(data,width*height*3,1,f);
>   fclose(f);  glGenTextures(1,&texture);
>   glBindTexture(GL_TEXTURE_2D,texture);
>   gluBuild2DMipmaps(GL_TEXTURE_2D,3,width,height,GL_RGB,GL_UNSIGNED_BYTE,data);
>   free(data);     return (texture); } void display() {    glPushMatrix();
>   glClear(GL_COLOR_BUFFER_BIT);   glClear(GL_DEPTH_BUFFER_BIT);
>   glGenTextures(1,&tex[0]);   glGenTextures(1,&tex[1]);
>   glGenTextures(1,&tex[2]);   glGenTextures(1,&tex[3]);
>   glGenTextures(1,&tex[4]);   glGenTextures(1,&tex[5]);   tex[0] =
> loadtexture("E:/1.raw",8,8);  tex[1] = loadtexture("E:/2.raw",16,16);
>   tex[2] = loadtexture("E:/3.raw",32,32);     tex[3] =
> loadtexture("E:/4.raw",64,64);    tex[4] =
> loadtexture("E:/5.raw",256,256);  tex[5] =
> loadtexture("E:/6.raw",512,512);  glRotatef(angle,1.0,0.0,0.0);
>   glRotatef(angle,0.0,1.0,0.0);   glRotatef(angle,0.0,0.0,1.0);
>   glPushMatrix();     glColor3f(1.0, 1.0, 1.0); 
>   glBindTexture(GL_TEXTURE_2D,tex[0]);    glBegin(GL_QUADS);
>   glTexCoord2d(0.0f,0.0f);glVertex3f(-0.5f, -0.5f, 0.5f); 
>   glTexCoord2d(0.0f,0.5f);glVertex3f( 0.5f, -0.5f, 0.5f); 
>   glTexCoord2d(0.5f,0.0f);glVertex3f( 0.5f, 0.5f, 0.5f); 
>   glTexCoord2d(0.5f,0.5f);glVertex3f( -0.5f, 0.5f, 0.5f);     glEnd();
>   glPopMatrix();  glPushMatrix();     glColor3f(1.0, 1.0, 1.0);
>   glBindTexture(GL_TEXTURE_2D,tex[1]);    glBegin(GL_QUADS);
>   glTexCoord2d(0.0f,0.0f);glVertex3f( -0.5f, -0.5f, -0.5f); 
>   glTexCoord2d(0.5f,0.0f);glVertex3f( -0.5f, 0.5f, -0.5f); 
>   glTexCoord2d(0.0f,0.5f);glVertex3f( 0.5f, 0.5f, -0.5f); 
>   glTexCoord2d(0.5f,0.5f);glVertex3f( 0.5f, -0.5f, -0.5f);    glEnd();
>   glPopMatrix();  glPushMatrix();     glColor3f(1.0, 1.0, 1.0); 
>   glBindTexture(GL_TEXTURE_2D,tex[2]);    glBegin(GL_QUADS);
>   glTexCoord2d(0.0f,0.0f);glVertex3f( -0.5f, -0.5f, 0.5f); 
>   glTexCoord2d(0.5f,0.5f);glVertex3f( -0.5f, 0.5f, 0.5f); 
>   glTexCoord2d(0.0f,0.0f);glVertex3f( -0.5f, 0.5f, -0.5f); 
>   glTexCoord2d(0.5f,0.0f);glVertex3f( -0.5f, -0.5f, -0.5f);   glEnd();
>   glPopMatrix();  glPushMatrix();     glColor3f(1.0, 1.0, 1.0);
>   glBindTexture(GL_TEXTURE_2D,tex[3]);    glBegin(GL_QUADS);
>   glTexCoord2d(0.0f,0.0f);glVertex3f( 0.5f, -0.5f, -0.5f); 
>   glTexCoord2d(0.5f,0.5f);glVertex3f( 0.5f, 0.5f, -0.5f); 
>   glTexCoord2d(0.0f,0.5f);glVertex3f( 0.5f, 0.5f, 0.5f); 
>   glTexCoord2d(0.5f,0.0f);glVertex3f( 0.5f, -0.5f, 0.5f);     glEnd();
>   glPopMatrix();  glPushMatrix();     glColor3f(1.0,1.0,1.0); 
>   glBindTexture(GL_TEXTURE_2D,tex[4]);    glBegin(GL_QUADS);
>   glTexCoord2d(0.0f,0.0f);glVertex3f( -0.5f, 0.5f, 0.5f); 
>   glTexCoord2d(0.0f,0.5f);glVertex3f( 0.5f, 0.5f, 0.5f); 
>   glTexCoord2d(0.5f,0.5f);glVertex3f( 0.5f, 0.5f, -0.5f); 
>   glTexCoord2d(0.5f,0.0f);glVertex3f( -0.5f, 0.5f, -0.5f);    glEnd();
>   glPopMatrix();  glPushMatrix();     glColor3f(1.0,1.0,1.0);
>   glBindTexture(GL_TEXTURE_2D,tex[5]);    glBegin(GL_QUADS);
>   glTexCoord2d(0.5f,0.5f);glVertex3f( -0.5f, -0.5f, 0.5f); 
>   glTexCoord2d(0.5f,0.0f);glVertex3f( -0.5f, -0.5f, -0.5f); 
>   glTexCoord2d(0.0f,0.5f);glVertex3f( 0.5f, -0.5f, -0.5f); 
>   glTexCoord2d(0.0f,0.0f);glVertex3f( 0.5f, -0.5f, 0.5f);     glEnd();
>   glPopMatrix();  glPopMatrix();  glutSwapBuffers();  angle+=0.09;
>   glutPostRedisplay(); } void main(int argc, char ** argv) {
>   glutInit(& argc,argv);  glutInitDisplayMode(GLUT_DOUBLE);
>   glutInitWindowSize(500,500);    glutInitWindowPosition(100,100);
>   glutCreateWindow("moka'ab");    init();     glutDisplayFunc(display);
>   glutMainLoop(); }

有什么不对? 请帮帮我

1 个答案:

答案 0 :(得分:0)

在绘图之前

glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL);应该修复它!