Baljeet
Member
- Jan 31, 2011
- 76
- 0
Hi all, its me again
Important Notes:
Ill continue from where we stopped in the first tutorial, in part one we learned how to setup an opengl application window and draw a little shape, in this tutorial we will learn how to add input from the keyboard and adding textures to our shapes.
After that we will do a little game with the new stuff we have learned
A.Adding input:
Making the application read what the user do on keyboard is really easy and the same as any c++ winapi application, all you have to do is add a function called "keyboard__input()" (or any other name) to this part of your winapi function:
The function "keyboard_Input" will only run if the program is in active state and not busy, the function will read the key pressed by the user and you can add functions or statements to each key state, just like this:
And don't forget to declare the function in the beginning of the code
B.adding textures :
Textures are images (usually bmp) that cover our objects, this images are stored outside the .exe in a .bmp file, its most effective when you want to put the same textures to more than one object.
Adding textures simply means reading image from a file and putting the image on an object, before that we will need to enable 2d textures in our opengl initialization function by doing this
After that we will create the function that willread the image and store in in a variable called "texture[]" which is an "GLuint" array to hold our textures, lets take a look on that function:
In the beginning we declared an "AUX_RGBImageRec" array that will hold the attributes and details of the image or .bmp that we will read, then we use the function "LoadBMP()" which takes the path of the image file and returns the image into the variable we have just declared, if the file is not found or empty the code wont run.
After that we will run some opengl function:
After reading te texture , you can use the texture on any object you want in the DrawGLScene() function
That's all, now lets try these stuff:
I used a bmp image with codecall logo and aplied the texture on a box, when the user clicks the arrows the box will rotate, heres the code fo alying the texture on an object
First we bind the texture using the "glBindTexture()" which takes two arguments; the type of the texture and the texture which is already stored on the array, then we use "glTexCoord2f()" to adjust the coordinates of the image
Notice the "glRotatef()" function which is used to rotate an opengl function, it takes 4 arguments: the rotate angle, and the direction of the rotation, of course it wont rotate because the Rangle is zero, we can manipulate this variable from the keyboard input function, just like this:
The final output will look like this:
I hope that you liked this tutorial, in the next tutorial we will wrap things up and make a small brick game, which in the user will play against the pc.
All Credits goes to one who really made this...
Important Notes:
- you will need to read the first part to keep up with this tutorial
- I'm using the same code in the first part
- for simplicity I've used easier approaches, though it doesn't mean that it's the best way or the most professional approach. But I'm doing this so people who have basic knowledge of c++ can keep up with the tutorial and not give up
Ill continue from where we stopped in the first tutorial, in part one we learned how to setup an opengl application window and draw a little shape, in this tutorial we will learn how to add input from the keyboard and adding textures to our shapes.
After that we will do a little game with the new stuff we have learned
A.Adding input:
Making the application read what the user do on keyboard is really easy and the same as any c++ winapi application, all you have to do is add a function called "keyboard__input()" (or any other name) to this part of your winapi function:
Code:
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG msg;
BOOL done=FALSE;
startglwindow("Tutorial no.2 - OpenGl",640,480,32);
while(!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
done=TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
Keyboard_Input(); //here we are calling our new funtion
if (ready)
{
DrawGLScene();
SwapBuffers(dc);
}
}
}
KillGLWindow();
return (msg.wParam);
}
Code:
void Keyboard_Input()
{
if((GetKeyState(VK_LEFT) & 0x80))
{
//do this when user clicks left
}
if(GetKeyState('A')& 0x80)
{
//do this when user clicks a or A
}
}
B.adding textures :
Textures are images (usually bmp) that cover our objects, this images are stored outside the .exe in a .bmp file, its most effective when you want to put the same textures to more than one object.
Adding textures simply means reading image from a file and putting the image on an object, before that we will need to enable 2d textures in our opengl initialization function by doing this
Code:
glEnable(GL_TEXTURE_2D);
Code:
void readtexture()
{
AUX_RGBImageRec *TextureImage[1];
if (TextureImage[0]=LoadBMP("1.bmp"))
{
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D,
0,
3,
TextureImage[0]->sizeX,
TextureImage[0]->sizeY,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
}
}
After that we will run some opengl function:
- glGenTextures(1, &texture[0]): will generate a texture in that exact array field
- glBindTexture(GL_TEXTURE_2D, texture[0]): sets up the type of the texture in the array
- glTexImage2D(): simply put the texture from the "TextureImage" array and puts it in the global variable "texture[0]", it takes arguments about the read texture file like the type of the texture, it's horizontal size.
Code:
int InitGL(GLvoid)
{
readtexture();
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.5f, 1.0f, 1.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
That's all, now lets try these stuff:
I used a bmp image with codecall logo and aplied the texture on a box, when the user clicks the arrows the box will rotate, heres the code fo alying the texture on an object
Code:
GLfloat Rangle=0;
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(Rangle,0.0,0.0,5.0);
glTranslatef(0,0,-5);
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 0.0f); //up right corner
glTexCoord2f(0.0f, 1.0f);
glVertex3f( -1.0f, 1.0f, 0.0f); //up left corner
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f); //down left
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.f,-1.0f, 0.0f);//down right
glEnd();
return TRUE;
}
Notice the "glRotatef()" function which is used to rotate an opengl function, it takes 4 arguments: the rotate angle, and the direction of the rotation, of course it wont rotate because the Rangle is zero, we can manipulate this variable from the keyboard input function, just like this:
Code:
void Keyboard_Input()
{
if((GetKeyState(VK_LEFT) & 0x80))
{
Rangle+=.1;
}
if((GetKeyState(VK_RIGHT) & 0x80))
{
Rangle-=.1;
}
if(GetKeyState('A')& 0x80)
{MessageBox(NULL,"you clicked a ","hi",NULL);
}
}
I hope that you liked this tutorial, in the next tutorial we will wrap things up and make a small brick game, which in the user will play against the pc.
All Credits goes to one who really made this...