+ Reply to Thread
Results 1 to 7 of 7

Thread: C++/opengl --part2 : using textures and keyboard input

  1. #1
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    C++/opengl --part2 : using textures and keyboard input

    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:
    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);							
    }
    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:
    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
    	}
    }
    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
    Code:
    glEnable(GL_TEXTURE_2D);
    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:
    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);
    }
    }
    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:
    • 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.
    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:
    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;
    }
    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
    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; 
    }
    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:
    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);
    }
    }
    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.
    Attached Thumbnails Attached Thumbnails C++/opengl --part2 : using textures and keyboard input-sample.jpg  
    Attached Files Attached Files
    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: C++/opengl --part2 : using textures and keyboard input

    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  4. #3
    Jordan Guest

    Re: C++/opengl --part2 : using textures and keyboard input

    Another neat one! +rep

  5. #4
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: C++/opengl --part2 : using textures and keyboard input

    nice one .. amr .. +rep

  6. #5
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: C++/opengl --part2 : using textures and keyboard input

    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"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  7. #6
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: C++/opengl --part2 : using textures and keyboard input

    Quote Originally Posted by amrosama View Post
    thnx egzon.
    you are the man this rep will make me codewarrior
    congrats .. you deserve it

  8. #7
    michaelvd12's Avatar
    michaelvd12 is offline Learning Programmer
    Join Date
    Apr 2009
    Posts
    99
    Rep Power
    11

    Re: C++/opengl --part2 : using textures and keyboard input

    i get an warning

    Code:
    In function 'voidKeyboard_Input();
    
    [WARNING]passing NULL used for non-pointer converting 4 of 'int MessageBoxHWND__*,const CHAR*, const CHAR*,UINT)'
    
    line 292
    i am using dev c++ 4.9.9.2

    but without that it is a great tutorial !

    +rep
    Last edited by michaelvd12; 04-19-2009 at 04:58 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how to scan keyboard input
    By pindo in forum Java Help
    Replies: 2
    Last Post: 04-12-2011, 09:32 AM
  2. Keyboard input to double
    By Lavaderman in forum Java Help
    Replies: 4
    Last Post: 02-07-2011, 09:55 AM
  3. Visual C# keyboard input?
    By MagicIdiot in forum C# Programming
    Replies: 4
    Last Post: 02-01-2011, 04:04 PM
  4. keyboard input
    By NicholasIT in forum C and C++
    Replies: 2
    Last Post: 09-05-2010, 07:10 AM
  5. Replies: 6
    Last Post: 05-23-2010, 05:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts