Adding cube mapping code to Texture.h

So now, what we actually want to do is essentially the similar process as we did with the texture file, but for cube mapping. The code will be very similar, so to begin with what we are going to do is duplicate the texture loading code and paste below it. Then, we'll make the following highlighted changes to the code:

static GLuint LoadCubemap( vector<const GLchar * > faces) 
    { 
        GLuint textureID; 
        glGenTextures( 1, &textureID );      
        int imageWidth, imageHeight; 
        unsigned char *image;
        glBindTexture( GL_TEXTURE_CUBE_MAP, textureID ); 
        for ( GLuint i = 0; i < faces.size( ); i++ ) 
        { 
            image = SOIL_load_image( faces[i], &imageWidth, &imageHeight,
0, SOIL_LOAD_RGB ); glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB,
imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image ); SOIL_free_image_data( image ); }
glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,
GL_LINEAR ); glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,
GL_LINEAR ); glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R,
GL_CLAMP_TO_EDGE ); glBindTexture( GL_TEXTURE_CUBE_MAP, 0); return textureID; }

In the preceding code, we added GLchars because we don't have one path; remember, we're going to have six different paths. Then, we created the for loop because we wanted to go over our six different images with ease, and also we didn't want to repeat the code, which was the whole point of doing what we're doing.

So, if we go back to our main file that is in our main.cpp, we can actually finish off what we were doing. Go to the section where we are loading our texture file, and after that code, add the following highlighted code:

// Cubemap (Skybox) 

    vector<const GLchar*> faces; 
    faces.push_back( "res/images/skybox/right.tga" ); 
    faces.push_back( "res/images/skybox/left.tga" ); 
    faces.push_back( "res/images/skybox/top.tga" ); 
    faces.push_back( "res/images/skybox/bottom.tga" ); 
    faces.push_back( "res/images/skybox/back.tga" ); 
    faces.push_back( "res/images/skybox/front.tga" ); 
    GLuint cubemapTexture = TextureLoading::LoadCubemap( faces ) 

In the preceding code, we added the cubemap texture. The order here does matter, so you can't just willy-nilly put it in. If you downloaded other images from a website, you might need to rearrange it properly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset