1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| bool SKYBOX::Initialize()
{
// Test de l'extension GL_ARB_texture_cube_map
char* extensions = (char*) glGetString(GL_EXTENSIONS);
if(strstr(extensions, "GL_ARB_texture_cube_map") != NULL)
{
// Liste des faces successives pour la création des textures de CubeMap
GLenum cube_map_target[6] = {
GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
};
// Chargement des six textures
int texture_image[6];
//AUX_RGBImageRec * texture_image[6];
texture_image[0] = LoadBMP( "Skybox/XN.bmp" );
texture_image[1] = LoadBMP( "Skybox/XP.bmp" );
texture_image[2] = LoadBMP( "Skybox/YN.bmp" );
texture_image[3] = LoadBMP( "Skybox/YP.bmp" );
texture_image[4] = LoadBMP( "Skybox/ZN.bmp" );
texture_image[5] = LoadBMP( "Skybox/ZP.bmp" );
// Génération d'une texture CubeMap
glGenTextures(1, &cube_map_texture_ID);
// Configuration de la texture
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cube_map_texture_ID);
int i;
for (i = 0; i < 6; i++)
{
if (texture_image[i])
{
//glTexImage2D(cube_map_target[i], 0, 3, texture_image[i]->sizeX, texture_image[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_image[i]->data);
glTexImage2D(cube_map_target[i], 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, im);
/*
if (texture_image[i]->data)
{
free(texture_image[i]->data);
}
free(texture_image[i]);
*/
}
}
// Configuration des parametres de la texture
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
return true;
}
return false;
}
void SKYBOX::DrawSkyBox( float camera_yaw, float camera_pitch )
{
// Taille du cube
float t = 1.0f;
// Utilisation de la texture CubeMap
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cube_map_texture_ID);
// Réglage de l'orientation
glPushMatrix();
glLoadIdentity();
glRotatef( camera_pitch, 1.0f, 0.0f, 0.0f );
glRotatef( camera_yaw, 0.0f, 1.0f, 0.0f );
// Rendu de la géométrie
glBegin(GL_TRIANGLE_STRIP); // X Négatif
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
glEnd();
glBegin(GL_TRIANGLE_STRIP); // X Positif
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
glEnd();
glBegin(GL_TRIANGLE_STRIP); // Y Négatif
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
glEnd();
glBegin(GL_TRIANGLE_STRIP); // Y Positif
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
glEnd();
glBegin(GL_TRIANGLE_STRIP); // Z Négatif
glTexCoord3f(-t,-t,-t); glVertex3f(-t,-t,-t);
glTexCoord3f(t, -t,-t); glVertex3f(t,-t,-t);
glTexCoord3f(-t,t,-t); glVertex3f(-t,t,-t);
glTexCoord3f(t,t,-t); glVertex3f(t,t,-t);
glEnd();
glBegin(GL_TRIANGLE_STRIP); // Z Positif
glTexCoord3f(-t,-t,t); glVertex3f(-t,-t,t);
glTexCoord3f(-t,t,t); glVertex3f(-t,t,t);
glTexCoord3f(t,-t,t); glVertex3f(t,-t,t);
glTexCoord3f(t,t,t); glVertex3f(t,t,t);
glEnd();
// Réinitialisation de la matrice ModelView
glPopMatrix();
}
int SKYBOX::LoadBMP(char *filename)
{
int i, j=0; //Index variables
FILE *l_file; //File pointer
unsigned char *l_texture; //The pointer to the memory zone in which we will load the texture
// windows.h gives us these types to work with the Bitmap files
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBTRIPLE rgb;
num_texture++; // The counter of the current texture is increased
if( (l_file = fopen(filename, "rb"))==NULL) return (-1); // Open the file for reading
fread(&fileheader, sizeof(fileheader), 1, l_file); // Read the fileheader
fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader
fread(&infoheader, sizeof(infoheader), 1, l_file); // and read the infoheader
// Now we need to allocate the memory for our image (width * height * color deep)
l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
// And fill it with zeros
memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);
// At this point we can read every pixel of the image
for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
{
// We load an RGB value from the file
fread(&rgb, sizeof(rgb), 1, l_file);
// And store it
l_texture[j+0] = rgb.rgbtRed; // Red component
l_texture[j+1] = rgb.rgbtGreen; // Green component
l_texture[j+2] = rgb.rgbtBlue; // Blue component
l_texture[j+3] = 255; // Alpha value
j += 4; // Go to the next position
}
fclose(l_file); // Closes the file stream
glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter
// The next commands sets the texture parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //The minifying function
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // We don't combine the color with the original surface color, use only the texture map.
// Finally we define the 2d texture
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
// And create 2d mipmaps for the minifying function
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
free(l_texture); // Free the memory we used to load the texture
return (num_texture); // Returns the current texture OpenGL ID
} |
Partager