Hi all, its me again
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:
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: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); }
And don’t forget to declare the function in the beginning of the codeCode: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
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:Code:glEnable(GL_TEXTURE_2D);
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.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:Till this point we have created the function that reads the texture , all we have to do now is to put it in the “InitGL()” function, which will look like this:
- 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.
After reading te texture , you can use the texture on any object you want in the DrawGLScene() functionCode: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
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 imageCode: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:
The final output will look 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.
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
Another neat one! +rep
nice one .. amr .. +rep![]()
thnx egzon.
you are the man this rep will make me codewarrior
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
i get an warning
i am using dev c++ 4.9.9.2Code:In function 'voidKeyboard_Input(); [WARNING]passing NULL used for non-pointer converting 4 of 'int MessageBoxHWND__*,const CHAR*, const CHAR*,UINT)' line 292
but without that it is a great tutorial !
+rep
Last edited by michaelvd12; 04-19-2009 at 04:58 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks