If you are new to the GLFW(OpenGL framework) you can follow beginning tutorial here.
On Contrary with GLUT texture mapping is very easy in GLFW . We want to use pre-generated 2D images for surface textures, light maps, transparency maps etc.These images are stored with a standard image format in a file, which requires the program to decode and load the image(s) from file(s), which can require much work from the programmer.To make programming easier for OpenGL developers, GLFW has built-in support for loading images from files.
In this tutorial I give example for surface textures only. Surface textures are important because it helps to make object realistic.
To load a texture from a file, you can use the function glfwLoadTexture2D:
int glfwLoadTexture2D( const char *name, int flags )
This function reads a 2D image from a Truevision Targa format file (.TGA) with the name given by name, and uploads it to texture memory. It is similar to the OpenGL function glTexImage2D, except that the image data is read from a file instead of from main memory, and all the pixel format and data storage flags are handled automatically. The flags argument can be used to control how the texture is loaded. If flags is GLFW_ORIGIN_UL_BIT, the origin of the texture will be the upper left corner (otherwise it is the lower left corner). If flags is GLFW_BUILD_MIPMAPS_BIT, all mipmap levels will be generated and uploaded to texture memory (otherwise only one mipmap level is loaded). If flags is GLFW_ALPHA_MAP_BIT, then any gray scale images will be loaded as alpha maps rather than luminance maps. To make combinations of the flags, or them together (e.g. like this: GLFW_ORIGIN_UL_BIT |GLFW_BUILD_MIPMAPS_BIT).
Here is the sample program to illustrate the texture mapping with complete source code written in c++ with gcc compile and code::blocks IDE. You can also download the project file.
main.cpp
You can download project file with image form here downloadOn Contrary with GLUT texture mapping is very easy in GLFW . We want to use pre-generated 2D images for surface textures, light maps, transparency maps etc.These images are stored with a standard image format in a file, which requires the program to decode and load the image(s) from file(s), which can require much work from the programmer.To make programming easier for OpenGL developers, GLFW has built-in support for loading images from files.
In this tutorial I give example for surface textures only. Surface textures are important because it helps to make object realistic.
To load a texture from a file, you can use the function glfwLoadTexture2D:
int glfwLoadTexture2D( const char *name, int flags )
This function reads a 2D image from a Truevision Targa format file (.TGA) with the name given by name, and uploads it to texture memory. It is similar to the OpenGL function glTexImage2D, except that the image data is read from a file instead of from main memory, and all the pixel format and data storage flags are handled automatically. The flags argument can be used to control how the texture is loaded. If flags is GLFW_ORIGIN_UL_BIT, the origin of the texture will be the upper left corner (otherwise it is the lower left corner). If flags is GLFW_BUILD_MIPMAPS_BIT, all mipmap levels will be generated and uploaded to texture memory (otherwise only one mipmap level is loaded). If flags is GLFW_ALPHA_MAP_BIT, then any gray scale images will be loaded as alpha maps rather than luminance maps. To make combinations of the flags, or them together (e.g. like this: GLFW_ORIGIN_UL_BIT |GLFW_BUILD_MIPMAPS_BIT).
Here is the sample program to illustrate the texture mapping with complete source code written in c++ with gcc compile and code::blocks IDE. You can also download the project file.
main.cpp
//include header file for glfw library so that we can use OpenGL#include <GL/glfw.h>#include <stdlib.h> //needed for exit function#include <iostream>using namespace std;//Initializes 3D renderingvoid initializeRendering(){glfwInit();//Makes 3D drawing work when something is in front of something elseglEnable(GL_DEPTH_TEST);//glEnable( GL_TEXTURE_2D );}//Called when a key is pressedvoid GLFWCALL handleKeypress(int key,int press) //The key that was pressed{switch (key) {case GLFW_KEY_ESC: //Escape keyexit(0); //Exit the program}}//Called when the window is resizedvoid GLFWCALL handleResize(int width,int height){//Tell OpenGL how to convert from coordinates to pixel valuesglViewport( 0, 0, width, height );glMatrixMode( GL_PROJECTION ); //Switch to setting the camera perspective//Set the camera perspectiveglLoadIdentity(); //reset the cameragluPerspective( 45.0f, //camera angle(GLfloat)width/(GLfloat)height, //The width to height ratio1.0f, //The near z clipping coordinate100.0f ); //The far z clipping coordinate}GLuint LoadTexture(const char* TextureName){GLuint Texture; //variable for textureglGenTextures(1,&Texture); //allocate the memory for textureglBindTexture(GL_TEXTURE_2D,Texture); //Binding the textureif(glfwLoadTexture2D(TextureName, GLFW_BUILD_MIPMAPS_BIT)){glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);return Texture;}else return -1;}void display(){glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); //clear background screen to black//Clear information from last drawglClear( GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspectiveglLoadIdentity(); //Reset the drawing perspectiveglTranslatef(0.0f,0.0f,-35.0f); //Translate whole scene to -ve z-axis by -35 unitGLuint text2D;text2D = LoadTexture("cicb.tga"); //loading image for textureglEnable(GL_TEXTURE_2D); //Enable textureglBindTexture(GL_TEXTURE_2D,text2D);//Binding textureglPushMatrix();glBegin(GL_POLYGON); //Begin quadrilateral coordinatesglNormal3f(0.0f, 0.0f, 1.0f);//normal vectorglTexCoord2f(0.0f, 0.0f); //Texture co-ordinate origin or lower left cornerglVertex3f(-10.0f,-11.0f,5.0f);glTexCoord2f(1.0f, 0.0f); //Texture co-ordinate lower right cornerglVertex3f(10.0f,-11.0f,5.0f);glTexCoord2f(1.0f, 1.0f);//Texture co-ordinate top right cornerglVertex3f(10.0f,-1.0f,-15.0f);glTexCoord2f(0.0f, 1.0f);//Texture co-ordinate top left cornerglVertex3f(-10.0f,-1.0f,-15.0f);glEnd(); //End quadrilateral coordinatesglPopMatrix();glDisable(GL_TEXTURE_2D);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D,text2D);glPushMatrix();glBegin(GL_POLYGON);glNormal3f(0.0f, 0.0f, 1.0f);glTexCoord2f(0.0f, 0.0f);//Texture co-ordinate origin or lower left cornerglVertex3f(-10.0f,-1.0f,-15.0f);glTexCoord2f(10.0f, 0.0f); //Texture co-ordinate for repeating image ten times form//origin to lower right cornerglVertex3f(10.0f,-1.0f,-15.0f);glTexCoord2f(10.0f, 10.0f);//repeat texture ten times form lower to top right corner.glVertex3f(10.0f,15.0f,-15.0f);glTexCoord2f(0.0f, 10.0f);//repeat texture ten time form top right to top left corner.glVertex3f(-10.0f,15.0f,-15.0f);glEnd();glPopMatrix();glDisable(GL_TEXTURE_2D); //Disable the textureglfwSwapBuffers();}int main(){// int width, height;bool running = true;initializeRendering();if( !glfwOpenWindow( 800, // width of window800, //height of window0, //redbits0, //greenbits0, //bluebits0, //alphabits0, //depthbits0, //stencilbitsGLFW_WINDOW ) //mode) //return false if window is not created{glfwTerminate(); //terminating glfw windowreturn 0;}glfwSetWindowTitle("codeincodeblock.blogspot.com - Texture");glfwSetWindowSizeCallback(handleResize); //callback function of GLFW to handle window resizeglfwSetKeyCallback(handleKeypress); //callback function to handle keypresswhile(running) // infinite loop to draw object again and again{ // because once object is draw then window is terminateddisplay();running = glfwGetWindowParam( GLFW_OPENED ); //when glfw window is opened then it return true//if closed then return false}return 0;}
Download Texture.rar
Hi,
ReplyDeleteI tried your code about texturing,
but I noticed a problem.
The memory ressource increase non stop.
How to fix that?
Have a nice day,
I have asked about problem here http://stackoverflow.com/questions/11923885/memory-resource-increase-non-stop it might help you
DeleteHi,
ReplyDeleteThank you, I'll follow the answer.
Actually, I solved my problem
with an inelegant way:
glfwLoadTexture2D( "image.tga", GLFW_BUILD_MIPMAPS_BIT );
// Use trilinear interpolation for minification
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );
// Use bilinear interpolation for magnification
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
// Enable texturing
glEnable( GL_TEXTURE_2D );
//draw plane...
glBegin( GL_QUADS );
glTexCoord2f(0.0f, 0.0f); glVertex3f(-volume.X, 0, -volume.Z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-volume.X, 0, volume.Z);
glTexCoord2f(1.0f, 1.0f); glVertex3f( volume.X, 0, volume.Z);
glTexCoord2f(0.0f, 1.0f); glVertex3f( volume.X, 0, -volume.Z);
glEnd();
glDisable(GL_TEXTURE_2D);
Have a nice day,
Thanks @HeartyGFX you solved my mistake
DeleteWith the original code, the memory usage continually increases because memory is allocated to load the texture on each call of the "display" function. A better method would be to load the texture once in an initialization step and then use that same loaded texture repeatedly for the display.
ReplyDelete