0
我遇到了紋理顯示通過它前面的紋理的問題。我正在建造一個迷宮,牆壁後面牆壁上的紋理正在顯示出來。我該如何解決這個問題?OpenGL紋理深度(裁剪紋理)
繼承人我的代碼。
#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
GLuint texture; //the array for our texture
GLuint sky;
float cameraz = -1.0;
GLuint LoadTexture(const char * filename)
{
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;
int i;
file = fopen(filename, "rb");
if (file == NULL) return 0;
width = 512;
height = 512;
data = (unsigned char *)malloc(width * height * 3);
//int size = fseek(file,);
fread(data, width * height * 3, 1, file);
fclose(file);
for(i = 0; i < width * height ; ++i)
{
int index = i*3;
unsigned char B,R;
B = data[index];
R = data[index+2];
data[index] = R;
data[index+2] = B;
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data);
free(data);
return texture;
}
void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}
/*
void drawBackground()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glOrtho(0, 1, 0, 1, 0, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// No depth buffer writes for background.
//glDepthMask(false);
glBindTexture(GL_TEXTURE_2D,sky);
glBegin(GL_QUADS); {
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
} glEnd();
//glDepthMask(true);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}*/
void drawMaze (void) {
glBegin(GL_QUADS);
glVertex3d(-500.0, 0, -500.0);
glVertex3d(500.0, 0, -500.0);
glVertex3d(500.0, 0, 500.0);
glVertex3d(-500.0, 0, 500.0);
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(0.0, 0.0, 0.0);
glTexCoord2d(7.0,0.0); glVertex3d(0.0, 0.0, 7.0);
glTexCoord2d(7.0,1.0); glVertex3d(0.0, 1.0, 7.0);
glTexCoord2d(0.0,1.0); glVertex3d(0.0, 1.0, 0.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(0.0, 0.0, 7.0);
glTexCoord2d(4.0,0.0); glVertex3d(4.0, 0.0, 7.0);
glTexCoord2d(4.0,1.0); glVertex3d(4.0, 1.0, 7.0);
glTexCoord2d(0.0,1.0); glVertex3d(0.0, 1.0, 7.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(5.0, 0.0, 7.0);
glTexCoord2d(1.0,0.0); glVertex3d(6.0, 0.0, 7.0);
glTexCoord2d(1.0,1.0); glVertex3d(6.0, 1.0, 7.0);
glTexCoord2d(0.0,1.0); glVertex3d(5.0, 1.0, 7.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(6.0, 0.0, 0.0);
glTexCoord2d(7.0,0.0); glVertex3d(6.0, 0.0, 7.0);
glTexCoord2d(7.0,1.0); glVertex3d(6.0, 1.0, 7.0);
glTexCoord2d(0.0,1.0); glVertex3d(6.0, 1.0, 0.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(1.0, 0.0, 0.0);
glTexCoord2d(5.0,0.0); glVertex3d(6.0, 0.0, 0.0);
glTexCoord2d(5.0,1.0); glVertex3d(6.0, 1.0, 0.0);
glTexCoord2d(0.0,1.0); glVertex3d(1.0, 1.0, 0.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(1.0, 0.0, 0.0);
glTexCoord2d(2.0,0.0); glVertex3d(1.0, 0.0, 2.0);
glTexCoord2d(2.0,1.0); glVertex3d(1.0, 1.0, 2.0);
glTexCoord2d(0.0,1.0); glVertex3d(1.0, 1.0, 0.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(1.0, 0.0, 2.0);
glTexCoord2d(2.0,0.0); glVertex3d(2.0, 0.0, 2.0);
glTexCoord2d(2.0,1.0); glVertex3d(2.0, 1.0, 2.0);
glTexCoord2d(0.0,1.0); glVertex3d(1.0, 1.0, 2.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(3.0, 0.0, 0.0);
glTexCoord2d(1.0,0.0); glVertex3d(3.0, 0.0, 1.0);
glTexCoord2d(1.0,1.0); glVertex3d(3.0, 1.0, 1.0);
glTexCoord2d(0.0,1.0); glVertex3d(3.0, 1.0, 0.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(2.0, 0.0, 1.0);
glTexCoord2d(1.0,0.0); glVertex3d(3.0, 0.0, 1.0);
glTexCoord2d(1.0,1.0); glVertex3d(3.0, 1.0, 1.0);
glTexCoord2d(0.0,1.0); glVertex3d(2.0, 1.0, 1.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(4.0, 0.0, 1.0);
glTexCoord2d(2.0,0.0); glVertex3d(6.0, 0.0, 1.0);
glTexCoord2d(2.0,1.0); glVertex3d(6.0, 1.0, 1.0);
glTexCoord2d(0.0,1.0); glVertex3d(4.0, 1.0, 1.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(3.0, 0.0, 2.0);
glTexCoord2d(2.0,0.0); glVertex3d(5.0, 0.0, 2.0);
glTexCoord2d(2.0,1.0); glVertex3d(5.0, 1.0, 2.0);
glTexCoord2d(0.0,1.0); glVertex3d(3.0, 1.0, 2.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(3.0, 0.0, 2.0);
glTexCoord2d(1.0,0.0); glVertex3d(3.0, 0.0, 3.0);
glTexCoord2d(1.0,1.0); glVertex3d(3.0, 1.0, 3.0);
glTexCoord2d(0.0,1.0); glVertex3d(3.0, 1.0, 2.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(2.0, 0.0, 3.0);
glTexCoord2d(1.0,0.0); glVertex3d(3.0, 0.0, 3.0);
glTexCoord2d(1.0,1.0); glVertex3d(3.0, 1.0, 3.0);
glTexCoord2d(0.0,1.0); glVertex3d(2.0, 1.0, 3.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(2.0, 0.0, 3.0);
glTexCoord2d(1.0,0.0); glVertex3d(2.0, 0.0, 4.0);
glTexCoord2d(1.0,1.0); glVertex3d(2.0, 1.0, 4.0);
glTexCoord2d(0.0,1.0); glVertex3d(2.0, 1.0, 3.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(2.0, 0.0, 4.0);
glTexCoord2d(1.0,0.0); glVertex3d(3.0, 0.0, 4.0);
glTexCoord2d(1.0,1.0); glVertex3d(3.0, 1.0, 4.0);
glTexCoord2d(0.0,1.0); glVertex3d(2.0, 1.0, 4.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(3.0, 0.0, 4.0);
glTexCoord2d(1.0,0.0); glVertex3d(3.0, 0.0, 5.0);
glTexCoord2d(1.0,1.0); glVertex3d(3.0, 1.0, 5.0);
glTexCoord2d(0.0,1.0); glVertex3d(3.0, 1.0, 4.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(1.0, 0.0, 5.0);
glTexCoord2d(2.0,0.0); glVertex3d(3.0, 0.0, 5.0);
glTexCoord2d(2.0,1.0); glVertex3d(3.0, 1.0, 5.0);
glTexCoord2d(0.0,1.0); glVertex3d(1.0, 1.0, 5.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(1.0, 0.0, 3.0);
glTexCoord2d(3.0,0.0); glVertex3d(1.0, 0.0, 6.0);
glTexCoord2d(3.0,1.0); glVertex3d(1.0, 1.0, 6.0);
glTexCoord2d(0.0,1.0); glVertex3d(1.0, 1.0, 3.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(0.0, 0.0, 3.0);
glTexCoord2d(1.0,0.0); glVertex3d(1.0, 0.0, 3.0);
glTexCoord2d(1.0,1.0); glVertex3d(1.0, 1.0, 3.0);
glTexCoord2d(0.0,1.0); glVertex3d(0.0, 1.0, 3.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(4.0, 0.0, 3.0);
glTexCoord2d(2.0,0.0); glVertex3d(6.0, 0.0, 3.0);
glTexCoord2d(2.0,1.0); glVertex3d(6.0, 1.0, 3.0);
glTexCoord2d(0.0,1.0); glVertex3d(4.0, 1.0, 3.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(4.0, 0.0, 3.0);
glTexCoord2d(2.0,0.0); glVertex3d(4.0, 0.0, 5.0);
glTexCoord2d(2.0,1.0); glVertex3d(4.0, 1.0, 5.0);
glTexCoord2d(0.0,1.0); glVertex3d(4.0, 1.0, 3.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(4.0, 0.0, 5.0);
glTexCoord2d(1.0,0.0); glVertex3d(5.0, 0.0, 5.0);
glTexCoord2d(1.0,1.0); glVertex3d(5.0, 1.0, 5.0);
glTexCoord2d(0.0,1.0); glVertex3d(4.0, 1.0, 5.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(5.0, 0.0, 4.0);
glTexCoord2d(1.0,0.0); glVertex3d(6.0, 0.0, 4.0);
glTexCoord2d(1.0,1.0); glVertex3d(6.0, 1.0, 4.0);
glTexCoord2d(0.0,1.0); glVertex3d(5.0, 1.0, 4.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(2.0, 0.0, 6.0);
glTexCoord2d(3.0,0.0); glVertex3d(5.0, 0.0, 6.0);
glTexCoord2d(3.0,1.0); glVertex3d(5.0, 1.0, 6.0);
glTexCoord2d(0.0,1.0); glVertex3d(2.0, 1.0, 6.0);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex3d(4.0, 0.0, 6.0);
glTexCoord2d(1.0,0.0); glVertex3d(4.0, 0.0, 7.0);
glTexCoord2d(1.0,1.0); glVertex3d(4.0, 1.0, 7.0);
glTexCoord2d(0.0,1.0); glVertex3d(4.0, 1.0, 6.0);
glEnd();
glPopMatrix();
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_TEXTURE_2D); //enable texturing
//glDepthMask(GL_TRUE);
gluLookAt (0.5, 0.5, cameraz, 0.5, 1.0, cameraz + 1.0, 0.0, 1.0, 0.0);
//drawBackground();
drawMaze();
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective (60, (GLfloat)w/(GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
void arrow_keys (int a_keys, int x, int y) // Create Special Function (required for arrow keys)
{
switch (a_keys) {
case GLUT_KEY_UP:
cameraz += 0.1;
break;
case GLUT_KEY_DOWN:
cameraz -= 0.1;
break;
case GLUT_KEY_LEFT:
cameraz += 0.1;
break;
case GLUT_KEY_RIGHT:
cameraz += 0.1;
break;
default:
break;
}
}
void init(){
texture = LoadTexture("wall.bmp");
//sky = LoadTexture("sky.bmp");
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Mipmaps example");
init();
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutSpecialFunc (arrow_keys);
texture = LoadTexture("texture.raw", 256, 256);
glutMainLoop();
FreeTexture(texture);
return 0;
}
感謝
我認爲深度測試默認情況下是啓用的,所以你對這兩個問題都是正確的:)非常感謝你 – user1637466