Programming In Glut (lesson 6) - Texture filters and lighting

free web hosting
Open Discussion > CONTRIBUTE > Tutorials

Programming In Glut (lesson 6) - Texture filters and lighting

t3jem
This is the last of the six tutorials I have moved from astahost. I hope you enjoy these and the rest soon to come.

QUOTE
This tutorial demonstrates how to use texture filters and will let you see the differences of each filter. I will also be introducing lighting into this tutorial.

We first have to create our "bitmap.h" header file with the functions we use to load our bitmaps, this is changed from the last tutorial to fit the needs of this one.

The first thing to do is to link all of our OpenGL libraries and include our needed headers for the functions we will call.
CODE
#pragma comment(lib, "opengl32.lib")//Link to the OpenGL libraries
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")


#include <windows.h>        // Header File For Windows
#include <gl\gl.h>            // Header File For The OpenGL32 Library
#include <gl\glu.h>            // Header File For The GLu32 Library
#include <gl\glaux.h>        // Header File For The Glaux Library


Next we define our first function with the same three arguments as the last tutorial (The texture array, file location, and place in array). This functon is going to load a linear filtered texture into our program. I will talk more about linear texturing in a little bit.
CODE
bool linearbmp(UINT textureArray[], LPSTR strFileName, int ID)//define our arguments
{


First, we want to make sure a file name was given, if it wasnt we return a false value to let the program know it failed, if a filename was given then we load our file into a new variable which we will use to manipulate the data into a readable format by our program.
CODE
if(!strFileName)   return false;//check for a filename
    
    AUX_RGBImageRec *pBitMap = auxDIBImageLoad(strFileName); //Load our file

If there was no data loaded (usually in case of a false filename) then we exit the program
CODE
if(pBitMap == NULL)    exit(0);// If no data was loaded then exit the program.

Now that we are done with all of our error checking we generate our texture and place it into our texture array; however, we are not done yet.
CODE
glGenTextures(1, &textureArray[ID]);//Generate our texture

Next we have to bind our texture to texture targets. Texture targets are just polygons that use the glVertex command.
CODE
glBindTexture(GL_TEXTURE_2D, textureArray[ID]);//bind our texture to texture targets


now for our filtering. We want to set our filtering in this function to use Linear filtering. Linear filtering will resize the image to the size needed throughout runtime, this uses the most CPU, but generates the best effect. We set linear filtering twice because the second argument in the function defines when we want to use the filter, "GL_TEXTURE_MIN_FILTER" is telling the program to use the set filter when the texture is near "...MAG_FILTER" tells the program to use the set filter when the texture is far.
CODE
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//set linear filtering
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//set linear filtering


now we finally finish the creation of our texture. The first argument tells the function we are using a 2D image, the second is the detail of the image (usually set to 0), the third is how many components are in use (red, green, blue), the fourth and fifth tell the application to find the size of the image itself (you can substitute for the actual size if you want, but it reduces compatibility with other images), the sixth argument is the size of the border, then we tell the application the type of image we are using (red, green, blue), then we let it know the data is in unsigned bytes, then we tell it where to get the data.
CODE
glTexImage2D(GL_TEXTURE_2D, 0, 3, pBitMap->sizeX, pBitMap->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pBitMap->data);//create our texture


then we need to clear our variable to preven memory leaks
CODE
if (pBitMap)    //If pBitMap still has a value then we want to clear it.                                    
    {
        if (pBitMap->data)                                
        {
            free(pBitMap->data);                        
        }
        free(pBitMap);                                    
    }
    return true;
}


now we need to define our new function that will load images with the nearest filter.
CODE
bool nearestbmp(UINT textureArray[], LPSTR strFileName, int ID)//define our new function
{


First, we want to make sure a file name was given, if it wasnt we return a false value to let the program know it failed, if a filename was given then we load our file into a new variable which we will use to manipulate the data into a readable format by our program.
CODE
if(!strFileName)   return false;//check for a filename
    
    AUX_RGBImageRec *pBitMap = auxDIBImageLoad(strFileName); //Load our file

If there was no data loaded (usually in case of a false filename) then we exit the program
CODE
if(pBitMap == NULL)    exit(0);// If no data was loaded then exit the program.

Now that we are done with all of our error checking we generate our texture and place it into our texture array; however, we are not done yet.
CODE
glGenTextures(1, &textureArray[ID]);//Generate our texture

Next we have to bind our texture to texture targets. Texture targets are just polygons that use the glVertex command.
CODE
glBindTexture(GL_TEXTURE_2D, textureArray[ID]);//bind our texture to texture targets


now for our filtering. We want to set our filtering in this function to use nearest filtering. the nearest filtering setting uses the least amount of CPU, but begins to become pixelized when brought closer to the screen.
CODE
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);//set linear filtering
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);//set linear filtering


now we finally finish the creation of our texture. The first argument tells the function we are using a 2D image, the second is the detail of the image (usually set to 0), the third is how many components are in use (red, green, blue), the fourth and fifth tell the application to find the size of the image itself (you can substitute for the actual size if you want, but it reduces compatibility with other images), the sixth argument is the size of the border, then we tell the application the type of image we are using (red, green, blue), then we let it know the data is in unsigned bytes, then we tell it where to get the data.
CODE
glTexImage2D(GL_TEXTURE_2D, 0, 3, pBitMap->sizeX, pBitMap->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pBitMap->data);//create our texture


then we need to clear our variable to preven memory leaks
CODE
if (pBitMap)    //If pBitMap still has a value then we want to clear it.                                    
    {
        if (pBitMap->data)                                
        {
            free(pBitMap->data);                        
        }
        free(pBitMap);                                    
    }
    return true;
}


We now define our function that will load mipmap images, it has the same arguments as the previous two functions.
CODE
bool mipmapbmp(UINT textureArray[], LPSTR strFileName, int ID)//define our function
{

First, we want to make sure a file name was given, if it wasnt we return a false value to let the program know it failed, if a filename was given then we load our file into a new variable which we will use to manipulate the data into a readable format by our program.
CODE
if(!strFileName)   return false;//check for a filename
    
    AUX_RGBImageRec *pBitMap = auxDIBImageLoad(strFileName); //Load our file

If there was no data loaded (usually in case of a false filename) then we exit the program
CODE
if(pBitMap == NULL)    exit(0);// If no data was loaded then exit the program.

Now that we are done with all of our error checking we generate our texture and place it into our texture array; however, we are not done yet.
CODE
glGenTextures(1, &textureArray[ID]);//Generate our texture

Next we have to bind our texture to texture targets. Texture targets are just polygons that use the glVertex command.
CODE
glBindTexture(GL_TEXTURE_2D, textureArray[ID]);//bind our texture to texture targets


our filter for textures far from the camera will still be linear; however, when they are closer to the camera we will use mipmapping. Mipmapping loads the image then resizes it into lots of different sizes and during runtime decides which texture to use. This is the most efficient filter for the quality. Mipmapping also eliminates the need for the width and height of the image to be a power of two.

CODE
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//set linear filter
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);    //(NEW) set mipmapping filter


and next we have to create our texture, the next function is much like glTexImage2d except that it creates the different textures needed for the mipmapping, it has all of the same arguments except for the detail argument which is ommited.
CODE
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitMap->sizeX, pBitMap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitMap->data); //(NEW)create our mipmap texture


then we need to clear our variable to preven memory leaks
CODE
if (pBitMap)    //If pBitMap still has a value then we want to clear it.                                    
    {
        if (pBitMap->data)                                
        {
            free(pBitMap->data);                        
        }
        free(pBitMap);                                    
    }
    return true;
}



The next section of code is the main body of the program.

we start by loading our header files, we need to load our bitmap.h file to so we can use the functions defined in it
CODE
#include<glut.h>
#include"bmpload.h"//Load the header file we created above


next we create an array to hold 3 textures, 1 for each filter. We then create a rotation variable and speed variable to keep track of the cube's spin and speed, lastly we initialize a variable to keep track of which filter we are using.
CODE
GLuint    textures[3];//create our texture array
float rotation = 0;//initialize rotation variable
float rotspeed = 0;//initialize speed variable
int filter=0;//(NEW) initialize filter variable


now we create our init function that will initialize our opengl settings, it will also include the lighting settings.

CODE
void init()
{


we need to create three arrays. Each array will hold a different value for our light. The first array will define the ambient color, this is the color the object will look, we have to use this setting instead of glcolor3f(), but to define the colors for all light values it is the same. The second array will define the diffuse color, this is the color of the bright areas of the object or the color of the light. The last array is the position of the light, this is the same as glVertex3f, but it has an added variable in it which should just be set to 1.
CODE
GLfloat ambient[]= {0.5f,0.5f,0.5f,1.0f};                 // (NEW) define the object color
    GLfloat diffuse[]= {1.0f,1.0f,1.0f,1.0f};                    // (NEW) define the light color
    GLfloat position[]= {10,5,-5,1};                    // (NEW) set the light position


next we have to put our settings into effect. We have to initialize our settings with the glLightfv() function. The first parameter is which light the setting will effect (there are 8 lights), the second argument is the setting to affect and the last is your variable holding the setting. There are five different settings, but we will only go over three in this tutorial.
CODE
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);                //(NEW) set the ambient value
    glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);                //(NEW) set the diffuse value
    glLightfv(GL_LIGHT1, GL_POSITION,position);            //(NEW) set the position


next we have to enable two things, lighting and light1. Enable light1 enables the light we have just set up and enabling lighting enables all lighting.
CODE
glEnable(GL_LIGHT1);//(NEW) enable light1
    glEnable(GL_LIGHTING);//(NEW) enable lighting


next we setup our clear color and our screen size.
CODE
glClearColor(0,0,0,0);
    glOrtho(-10,10,-10,10,-10,10);//setup screen size.


now we load our textures using the functions we created in our "bitmap.h" file. We load our texture with the linear filter, nearest filter, and mipmap filter into our texture array in that order. The we finally enable depth testing and finish with our init function.
CODE
linearbmp(textures, "image.bmp",0);// (NEW) load a linear texture
    nearestbmp(textures, "image.bmp",1);// (NEW) load a nearest texture
    mipmapbmp(textures, "image.bmp",2);// (NEW) load a mipmap texture

    glEnable(GL_DEPTH_TEST);//3D objects require depth testing to be on
}


now we start writing our display function which will draw all our objects to the screen. As always we will start by clearing the screen.

CODE
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//clear the screen


next we enable texturing and then bind the texture we want based on the filter selected.
CODE
glEnable(GL_TEXTURE_2D);//Enable 2D texturing
    glBindTexture(GL_TEXTURE_2D, textures[filter]);//bind our texture


Now we create a matrix for our transformation, we will rotate the cube based on the amount of the rotation variable, with this you can watch the light changing on the faces of the cube.
CODE
glPushMatrix();

    glRotatef(rotation, 0.5,-.6,.2);//rotate our cube

Next we create our cube. The only difference in creating our cube this time is the glNormal3f() function. This function defines a ray that points outward at a 90 degree angle from the middle of the polygon you are defining it for. glNormal3f() tells the program how to light the polygon depending on its angle to the light, you can use it point by point or you can use one glnormal3f() call per face. For this tutorial we will call it once per face.
CODE
glBegin(GL_QUADS);// begin drawing our cube
    glNormal3f(0,0,-1);//(NEW) set our normal
    glTexCoord2f(0,1);
    glVertex3f(-4,4,-4);
    glTexCoord2f(1,1);
    glVertex3f(4,4,-4);
    glTexCoord2f(1,0);
    glVertex3f(4,-4,-4);
    glTexCoord2f(0,0);
    glVertex3f(-4,-4,-4);

    //This is the back face
    glNormal3f(0,0,1);//(NEW)
    glTexCoord2f(0,1);
    glVertex3f(-4,4,4);
    glTexCoord2f(1,1);
    glVertex3f(4,4,4);
    glTexCoord2f(1,0);
    glVertex3f(4,-4,4);
    glTexCoord2f(0,0);
    glVertex3f(-4,-4,4);

    //This is the left face
    glNormal3f(-1,0,0);//(NEW)
    glTexCoord2f(0,1);
    glVertex3f(-4,4,4);
    glTexCoord2f(1,1);
    glVertex3f(-4,4,-4);
    glTexCoord2f(1,0);
    glVertex3f(-4,-4,-4);
    glTexCoord2f(0,0);
    glVertex3f(-4,-4,4);

    //This is the right face
    glNormal3f(1,0,0);//(NEW)
    glTexCoord2f(0,1);
    glVertex3f(4,4,4);
    glTexCoord2f(1,1);
    glVertex3f(4,4,-4);
    glTexCoord2f(1,0);
    glVertex3f(4,-4,-4);
    glTexCoord2f(0,0);
    glVertex3f(4,-4,4);

    //This is the bottom face
    glNormal3f(0,-1,0);//(NEW)
    glTexCoord2f(0,1);
    glVertex3f(-4,-4,4);
    glTexCoord2f(1,1);
    glVertex3f(-4,-4,-4);
    glTexCoord2f(1,0);
    glVertex3f(4,-4,-4);
    glTexCoord2f(0,0);
    glVertex3f(4,-4,4);

    //This is the top face
    glNormal3f(0,1,0);//(NEW)
    glTexCoord2f(0,1);
    glVertex3f(-4,4,4);
    glTexCoord2f(1,1);
    glVertex3f(-4,4,-4);
    glTexCoord2f(1,0);
    glVertex3f(4,4,-4);
    glTexCoord2f(0,0);
    glVertex3f(4,4,4);
    glEnd();


now we close our matrix, disable texturing (in good habit to prevent odd effects in future applications), tell the program to draw everything to the screen, tell the program to start drawing again, and finally we add our rotation speed variable to our rotation variable.
CODE
glPopMatrix();

    glDisable(GL_TEXTURE_2D);//disable texturing

    glFlush();
    glutPostRedisplay();

    rotation += rotspeed;
}


next we define our keyboard function which will use keyboard commands to tell the program what to do.
CODE
void keyboard(unsigned char key, int x, int y)
{

we start our switch statements to tell the program what to do according to the key presses.

when we press 'L' or 'R' then it will adjust the speed of the spinning cube. pressing 1-3 will change the filter we use on the cube
CODE
switch(key)//start are switch statements
    {
    case 'L':case'l'://Increase the speed of the spinning cube.
        rotspeed += 0.05;
        break;
    case 'R':case'r'://Increase the speed of the spinning cube
        rotspeed -= 0.05;
        break;
    case '1'://choose the filter using our # keys
        filter = 0;
        break;
    case '2':
        filter = 1;
        break;
    case '3':
        filter = 2;
        break;
    default:
        break;//Don't do anything if any other keys are pressed
    }
}

we now finally finish our program with our main function that has been unchanged.
CODE
void main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(10,50);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);//Depth buffer required for 3D objects
    glutCreateWindow("Lesson 6");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);//This tells our program which function to send keyboard events to.
    glutMainLoop();
}



There are 7 different lights you can use at once GL_LIGHT1, GL_LIGHT2... You can enable all of them at once.

There are a few more settings for lighting, but we will get into that later on in the tutorials. I hope this tutorial was helpful, and if you have any questions feel free to leave a reply.

 

 

 


Reply

vvspk
QUOTE(t3jem @ Apr 29 2007, 09:00 PM) *
This is the last of the six tutorials I have moved from astahost. I hope you enjoy these and the rest soon to come.

Hello dear friend,
I started learning OpenGL / GLUT with your Tutorials.
I could see only six (6) lessons. Are there any more lessons else where?
Cloud you please give me any links to more OpenGL tutorials by you or some one else.

Thanks your very much for your lessons.
smile.gif

Reply

Josti
QUOTE(vvspk @ Jun 8 2008, 11:38 PM) *
Hello dear friend,
I started learning OpenGL / GLUT with your Tutorials.
I could see only six (6) lessons. Are there any more lessons else where?
Cloud you please give me any links to more OpenGL tutorials by you or some one else.

Thanks your very much for your lessons.
smile.gif


You've already qouted the part which says end of the 6 lessons so there are not more lessons than six wink.gif

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. bmpload opengl - 68.70 hr back. (2)
  2. glut draw cube with points - 118.78 hr back. (1)
  3. basic glut program - 124.47 hr back. (1)
  4. glut image filters - 155.77 hr back. (1)
  5. enabling lighting in glut - 158.63 hr back. (1)
  6. glut lighting - 174.93 hr back. (1)
  7. glut lighting color 3d - 224.44 hr back. (1)
  8. glut textures - 159.25 hr back. (4)
  9. auxdibimageload - 271.43 hr back. (1)
  10. opengl glut cube - 318.98 hr back. (1)
  11. basic glut texture - 321.78 hr back. (1)
  12. glut cube problems - 327.77 hr back. (1)
  13. glut object border - 343.56 hr back. (1)
  14. glut 3d for lesson - 353.30 hr back. (1)
Similar Topics

Keywords : programming, glut, lesson, 6, texture, filters, lighting

  1. Programming Using Basic Language!
    This topic will be big - PM me for more! (0)
  2. Programming In Glut (lesson 5)
    Texture mapping and keyboard controls (0)
    Lesson 5 of 6. These are beginning to be more advanced tutorials. If you are not familiar with my
    previous tutorials you may need to go back and read them. QUOTE In this tutorial I am going to
    teach you how to texture map your polygons and implement basic keyboard control. There is alot of
    new material in this tutorial and I hope I have explained it enough. Before you get started you
    will want to name a bmp file "image.bmp" or "image" depending on your computer settings (if one
    doesn't work then use the other) and place it in the same folder as your project.....
  3. Programming In Glut (lesson 4)
    Creating 3D objects (0)
    Lesson 4 of 6. I hope you are enjoying them /laugh.gif" style="vertical-align:middle"
    emoid=":lol:" border="0" alt="laugh.gif" /> . QUOTE Hello, in this tutorial we will be creating
    a 3D pyramid. We are building this tutorial from Lesson 3, but I took out the 2D objects and placed
    a 3D pyramid in there instead. The 3rd axis for drawing can be a litle confusing, but after you get
    the hand of it you'll do fine. Now when you are setting a 3D vertex just remember that the
    camera is on the positive end of the z axis. So things that have a more positive z axis va....
  4. Programming In Glut (lesson 3)
    Animating your objects (0)
    Lesson 3 of 6. QUOTE In this tutorial I am going to talk about how to get animation in your
    program. I will be working directly off of Lesson 2 and will now take out the notes left behind
    from Lesson 1, but I will leave the notes from Lesson 2. CODE #include<glut.h>//include
    our glut library First we are going to initialize a variable called "time" which will record how
    much time has passed so we can use it to determine how to animate the objects. Then we have our
    init function that initializes some stuff in GLUT CODE float time = 0;//(NEW....
  5. Programming Glut (lesson 2)
    Drawing 2D objects (0)
    This is the second of the six from astahost, again, I hope you enjoy. /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> QUOTE This is the second
    lesson in my series of tutorials on how to use GLUT to create graphics. In this tutorial I am going
    to be teaching you how to create different types of polygons. I am going to be adding on to last
    tutorial's code and will leave the notes in to help you remember what all the function are. I
    will also be noting the new functions that we will be using and how to use them. CODE #inc....
  6. Programming In Glut (lesson 1)
    Creating a windwo (0)
    This is the first of six lessons I am transferring from Astahost for programming in GLUT, and after
    the six I hope to make more, I hope you enjoy. QUOTE Hello, I'm starting a series on how to
    program in OpenGL using the OpenGL Utility Toolkit, a.k.a. GLUT. I chose GLUT because it is quick
    and easy to write, and very easy to learn. In this tutorial I am going to teach you how to create a
    basic window which we will build off of in later tutorials. Throughout the tutorial I will leave
    notes to let you know what each command does, and how you can modify it to fit....
  7. Installing Glut To Dev
    (3)
    Hello, before I came to Trap17 I had a series of GLUT programming tutorials at astahost. Since then
    I have moved here and wanted to continue the series. As I am told I cannot just repost the
    tutorials here so I will put them into quotes until I start making new tutorials. I hope everyone
    will enjoy the tutorials I bring to this community. Ok to get started. Below are some instructions
    on installing GLUT, the tool we will be using to program in my tutorials. This was actually given
    to me back when I had a programming site and James deserves full credit for this tuto....
  8. Tutorial: Help About C & C++ Programming Langauge
    (2)
    Help About C & C++ Programming Langauge Prologue I remember the time I
    started learning C. It was not too long ago. I liked programming, I always did, but I didn't
    know a lot of languages. The only languages I knew were useless, such as Visual Basic, PHP, ASP, and
    the basics of Javascript and VBScript. So I decided to learn a real language: C. So I downloaded
    some tutorials and started reading. The well known "Hello, world!" program didn't cause any
    difficulties, nor did the few programs after it. But then some new word showed up ....
  9. Centered Website With Fixed Width
    ...begginers lesson... (2)
    This will be tutorial on how to make a simple website design with fixed width and center position.
    This is actually rather simple tutorial, but hopefully it will help others that are new in world of
    creating websites. Website will also contain header, content area and footer. First we must do
    Wrapper. Wrapper is actually an area of fixed width and will be position into center, so that our
    page doesn't fall apart. CODE body{ width:100%;  margin:0px;  padding:
    0px;  text-align:center; background:#FFFFFF; } This is actually an IE....
  10. Web Programming Tuts For The Beginner
    HTML for the beginner (8)
    Hello and welcome to our lil' web programming session. Here we assume that our students are
    complete noovices of web programming. If you havent ever done any web prograqming, then you've
    come to the right place. HERE WE GO
    -------------------------------------------------------------------------------- HTML TAGS HTML
    works in a very simple, very logical, format. It reads like you do, top to bottom, left to right.
    That's important to remember. HTML is written with TEXT. What you use to set certain sections
    apart as bigger text, smaller text, bold text,....

    1. Looking for programming, glut, lesson, 6, texture, filters, lighting

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for programming, glut, lesson, 6, texture, filters, lighting

*MORE FROM TRAP17.COM*
advertisement



Programming In Glut (lesson 6) - Texture filters and lighting



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE