Open the window for drawing:
Here is the sample code for opening window for drawing using OpenGL Toolkit.
code for intializiation window for drawing :
int main(int argc, char **argv)
{
glutInit(&argc, argv); // initializes the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set the display mode
glutInitWindowSize(640,480); //set the window size
glutInitWindowPosition(100,100); //set the window position on screen
glutCreateWindow(“MyProgram”); //open the screen window
/* register callback functions */
myInit(); // additional initializations, if necessary
glutMainLoop(); //go into a perpetual loop
}
Note:
the argument of glutinit() and main() must be same.
Define myInit(); function:
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
OpenGL Events:
There are four main types of event functions that we work with in OpenGL.
int main(int argc, char **argv)
{
/* initialize basic things */
/* register callback functions */
glutDisplayFunc(myDisplay); //register the redraw function
glutReshapeFunc(myReshape); //register the reshape function
glutMouseFunc(myMouse); //register the mouse action function
glutKeyboardFunc(myKeyboard); //register the keyboard action function
/* end register callback functions */
/* may be more initializations */
glutMainLoop(); //enter the unending main loop
}
glutDisplayFunc(myDisplay):
Windows issues a “redraw” event when it determines that a window should be redrawn on the screen. This happens when a window is first opened. Here myDisplay() is a registered callback function for the redraw event.glutReshapeFunc(myReshape):
Screen windows can be reshaped by a user, by resizing the windows. So myReshape() is registered with the reshape event.
glutMouseFunc(myMouse):
This command registers the myMouse() function. It is called whenever a mouse event occurs like click. myMouse() is automatically passed arguments telling the exact position of mouse and the nature of action initiated.
glutKeyboardFunc(myKeyboard):
This command registers the myKeyboard() function with the event of pressing or releasing any key on the keyboard.
Color in OpenGL:
Initialize color buffer before you use colors. You need to set the color value. You can
use the function glColor3f(red, green, blue) for this purpose. For example
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glBegin(GL_TRIANGLES); // Begin Drawing Triangles
glColor3f(1.0,1.0,1.0) //set white color
….
.....
....
...
...
0 comments:
Post a Comment