2012-11-21 103 views
4

我正在爲OpenGL中的天空盒設計環境地圖,並遇到了紋理問題。我的代碼生成我想要映射的紋理的貼圖,而不是一個大紋理。瓷磚也失去了大部分的分辨率,而且非常小。OpenGL中的天空盒

這裏是我的代碼:

#include <windows.h> 
#include <stdio.h> 
#include <glew.h> 
#include <glut.h> 
#include "Camera.h" 

Camera cam; 
GLuint texture [6]; //the array for our texture 

GLfloat angle = 0.0; 

GLuint LoadTexture(const char * filename, int width, int height) { 
    GLuint texture; 
    unsigned char * data; 
    FILE* file; 

    file = fopen(filename, "rb"); 
    if (file == NULL) return 0; 
    data = (unsigned char *)malloc(width * height * 3); 
    fread(data, width * height * 3, 1, file); 
    fclose(file); 

    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_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_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 skybox (void) { 
float x = 0; 
float y = 0; 
float z = 0; 
float width = 100; 
float height = 100; 
float length = 100; 
// Bind the BACK texture of the sky map to the BACK side of the cube 
glBindTexture(GL_TEXTURE_2D, texture[0]); 
// Center the skybox 
x = x - width/2; 
y = y - height/2; 
z = z - length/2; 
glBegin(GL_QUADS);  
    glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z); 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z); 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z); 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z); 
glEnd(); 
glBindTexture(GL_TEXTURE_2D, texture[1]); 
glBegin(GL_QUADS); 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z + length); 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length); 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z + length); 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z + length); 
glEnd(); 

glBindTexture(GL_TEXTURE_2D, texture[4]); 
glBegin(GL_QUADS);  

    glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z); 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z + length); 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y, z + length); 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z); 
glEnd(); 
glBindTexture(GL_TEXTURE_2D, texture[5]); 
glBegin(GL_QUADS);  
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z); 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y + height, z + length); 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length); 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z); 
glEnd(); 
glBindTexture(GL_TEXTURE_2D, texture[2]); 
glBegin(GL_QUADS);  
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z); 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z + length); 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z + length); 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);  

glEnd(); 
glBindTexture(GL_TEXTURE_2D, texture[3]); 
glBegin(GL_QUADS); 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z); 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z + length); 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z + length); 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z); 
glEnd(); 
//glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]); //bind the texture 
//glRotatef(angle, 1.0f, 1.0f, 1.0f); 
//glutSolidSphere(2, 40, 40); 
} 

void display (void) { 
glClearColor (0.0,0.0,0.0,1.0); 
glClear (GL_COLOR_BUFFER_BIT); 
glLoadIdentity(); 
cam.camera(); 
//gluLookAt (20.0, 20.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 
texture[0] = LoadTexture("Back.bmp", 256, 256); //load the texture 
texture[1] = LoadTexture("Front.bmp", 256, 256); //load the texture 
texture[2] = LoadTexture("Left.bmp", 256, 256); //load the texture 
texture[3] = LoadTexture("Right.bmp", 256, 256); //load the texture 
texture[4] = LoadTexture("Bottom.bmp", 256, 256); //load the texture 
texture[5] = LoadTexture("Top.bmp", 256, 256); //load the texture 
glEnable(GL_TEXTURE_2D); //enable 2D texturing 
glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation 
glEnable(GL_TEXTURE_GEN_T); 
skybox(); 
for (int i = 0; i < 6; i++) { 
    FreeTexture(texture[i]); 
} 
glutSwapBuffers(); 
angle = angle + 0.5; 
cam.incAngle(); 
} 

void keyboard (unsigned char key, int x, int y) { 
switch (key) { 
case 'q': cam.lookUpwards(); break; 
case 'z': cam.lookDownwards(); break; 
case 'w': cam.slideForward(); break; 
case 's': cam.slideBackward(); break; 
case 'a': cam.strafeLeft(); break; 
case 'd': cam.strafeRight(); break; 
case 'e': exit(0); break; 
default: break; 
} 
} 

void reshape(int x, int y) { 
cam.reshape(x, y); 
} 

void mouseMovement(int x, int y) { 
cam.mouseMovement(x, y); 
} 

int main (int argc, char **argv) { 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_DOUBLE); 
glutInitWindowSize(500, 500); 
glutInitWindowPosition(100, 100); 
glutCreateWindow("A basic OpenGL Window"); 
glutDisplayFunc(display); 
glutIdleFunc(display); 
glutReshapeFunc(reshape); 
glutKeyboardFunc(keyboard); 
glutPassiveMotionFunc(mouseMovement); 
glutMainLoop(); 
} 
+0

這是固定功能的OpenGL,這是相當過時。請考慮使用帶着色器的可編程管線。 –

+10

@StephanvandenHeuvel:請考慮回答實際問題,而不是建議OP不需要的東西。日期與否,固定功能即使在今天也有很多用途。製作GL4着色器只是爲了繪製天空盒而已。 – SigTerm

+0

這個問題爲什麼downvoted? –

回答

2

既然要指定紋理手動座標,您應該刪除/禁用以下自動生成:

glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation 
glEnable(GL_TEXTURE_GEN_T); 

此外,它看起來可疑,你是將.bmp文件加載爲原始RGB數據。

1

顏色就會變回正常,如果我們更換GL_BGR GL_RGB包含在「glew.h」