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);
}
}
discuss this topic to forum
