OpenGL (I).


85 views
Uploaded on:
Description
OpenGL (I) What is OpenGL (OGL)? OGL is a 3D representation and displaying library Can likewise utilize it to draw 2D objects. What is OpenGL (OGL)? Intuitive PC design framework that permits us to get to illustrations equipment Simple to utilize Projects run proficiently Equipment autonomous
Transcripts
Slide 1

OpenGL (I)

Slide 2

What is OpenGL (OGL)? OGL is a 3D representation & displaying library Can likewise utilize it to draw 2D objects.

Slide 3

What is OpenGL (OGL)? Intelligent PC representation framework that permits us to get to design equipment Easy to utilize Programs run effectively Hardware-free Graphics API (Application Programming Interface) A library of capacities Others: DirectX (Microsoft), Java3D

Slide 4

What is OpenGL (OGL)? OGL is autonomous of any particular window framework  fundamental window operations are excluded Graphics client interface programming: GLUT = OGL Utility Toolkit Simple yet simple to utilize

Slide 5

Programming Environment OGL is typically introduced on a MS Windows machine. Programming environment MS Visual Studio .Net 2003 Libraries we will utilize OGL (essential API instrument) GLU (OGL Utility Library) GLUT (OGL Utility Toolkit, a windowing toolbox that handles window framework operations)

Slide 6

OGL Data Types To make more versatile, OGL gives interior information sorts

Slide 7

Basic OGL Syntax Functions are prefixed with gl , glBegin , glClear , glClearColor Constants in capital letters, and the underscore is utilized as a separator GL_2D , GL_LINES , GL_TRIANGLES Built-in information sort names start with GL GLbyte , GLshort , GLint , GLboolean

Slide 8

First OGL Program: DrawDots

Slide 9

First OGL Program: DrawDots Our first program is not intelligent. It comprises of three capacities: primary , myDisplay , myInit . (see going with demo program) int principle ( int argc , singe ** argv ) { glutInit (& argc , argv ); /introduce the toolbox glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );/set the showcase mode glutInitWindowSize (640, 480);/set window size glutInitWindowPosition (100, 150);/set window position glutCreateWindow ("my first attemp");/open the screen window glutDisplayFunc ( myDisplay );/register redraw capacity myInit();/extra introductions as vital glutMainLoop (); /go into a ceaseless circle }

Slide 10

Creating Window for Drawing The initial five capacity calls utilization GLUT to open a window for drawing sx (150, 100) Coordinate framework in OGL sy

Slide 11

First OGL Program: DrawDots Draw primitives Display callback capacity myDisplay void myDisplay () { glClear ( GL_COLOR_BUFFER_BIT );/clear the screen glBegin ( GL_POINTS ); glVertex2i (100, 50); /draw three dabs glVertex2i (100, 130); glVertex2i (150, 130); glEnd (); glFlush (); /send all yield to show }

Slide 12

First OGL Program: DrawDots Draw primitives glBegin(GLenum mode) mode can be GL_POINTS , GL_LINES , GL_POLYGON , and so forth

Slide 13

First OGL Program: DrawDots Initialization void myInit () { glClearColor (1.0, 0.0, 0.0, 0.0);/set red foundation shading glColor3f (0.0, 1.0, 0.0);/set the drawing shading glPointSize (10.0);/a "dab" is 10 by 10 pixels/The accompanying lines set up the direction framework. /Details will be secured later. glMatrixMode ( GL_PROJECTION ); glLoadIdentity (); gluOrtho2D (0, 640, 0, 480); }

Slide 14

First OGL Program: DrawDots OpenGL is a state machine It stays informed regarding numerous state variables Current size of a point, current shade of drawing, current foundation shading, and so on. Shade of drawing is determined utilizing glColor3f(red, green, blue) ; (range: [0, 1]) Background shading is set with glClearColor(red, green, blue, alpha) . glClear clears the whole window to the foundation shading (cf. myDisplay ). The estimation of a state variable stays dynamic until another worth is given.

Slide 15

Line Drawing glLineWidth (2.0); /set line thickness glBegin ( GL_LINES ); glVertex2i (10, 20); /first level line glVertex2i (40, 20); glVertex2i (20, 10); /first vertical line glVertex2i (20, 40);/four more calls to glVertex here for the other two lines glEnd ();

Slide 16

Drawing Modes glBegin(GLenum mode)

Slide 17

Skeleton of an intuitive OGL project utilizing GLUT int principle ( int argc , singe ** argv ) { glutInit (& argc , argv ); /instate the toolbox glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );/set the presentation mode glutInitWindowSize (640, 480); /set window size glutInitWindowPosition (150, 100); /set window position glutCreateWindow ("my first attempt"); /open the screen window/register the callback capacities glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit (); /extra introductions as important glutMainLoop (); /go into a ceaseless circle }

Slide 18

Event-driven Programming & Callbacks Event-driven programming A property of most window-based projects The projects react to different occasions Click of a mouse, resizing of a window, and so forth. The framework consequently deals with an occasion line Deal with occasions first-come, initially served Programmers compose capacities (called callback capacities ) that will be called when occasions happen.

Slide 19

Event-driven Programming & Callbacks Callback capacities in GLUT For instance, glutMouseFunc(myMouse) Register capacity myMouse() as the capacity to be executed when a mouse occasion happens. Prefix excess demonstrates that this capacity is a piece of GLUT If a specific system does not make utilization of a mouse, the relating callback capacity require not be enlisted.

Slide 20

Event-driven Programming & Callbacks glutDisplayFunc(myDisplay) Whenever the framework establishes that a window ought to be redrawn, it issues a “redraw” occasion. Redraw occasion happens When the window is initially opened When the window is uncovered by moving another window off it When your system unequivocally asks for that it be redrawn (through the call glutPostRedisplay ) (helpful for activity) myDisplay() is enlisted as the callback capacity for a redraw occasion. Coordinate framework in OpenGL

Slide 21

Event-driven Programming & Callbacks glutReshapeFunc(myReshape) Screen windows can be resized by the client, more often than not by dragging a window\'s edge to another position with the mouse. myReshape(int width, int stature) is naturally passed contentions reporting the new width and tallness of the reshaped window. Coordinate framework in OpenGL

Slide 22

Event-driven Programming & Callbacks glutMouseFunc(myMouse) When one of the mouse catch is squeezed or discharged, a mouse occasion is issued. myMouse(int catch, int state, int x, int y) is consequently passed contentions depicting the mouse\'s area and the activity\'s way started by squeezing the catch. Coordinate framework in OpenGL

Slide 23

Event-driven Programming & Callbacks glutKeyboardFunc(myKeyboard) Keyboard occasion is issued when some key is squeezed or discharged. myKeyboard(unsigned singe key, int x, int y) is consequently passed contentions that tell which key was squeezed. It is likewise passed the information demonstrating the mouse\'s area at the time the key was squeezed. Coordinate framework in OpenGL

Slide 24

Summary of Common Callbacks

Slide 25

Mouse Interaction Let’s see a demo for mouse & console callbacks glutMouseFunc(myMouse) Mouse catch is squeezed or discharged. glutMotionFunc(myMouse) Mouse is moved while one of the catches is squeezed. void myMouse ( int catch , int state , int x , int y ) { if ( catch == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) { glBegin ( GL_POINTS ); glVertex2i ( x , screenHeight - y ); glEnd (); glFlush (); } } Note the y worth is the quantity of pixels down from the highest point of the window!

Slide 26

Mouse Interaction Mouse movement illustration “freehand” drawing with a fat brush See second demo system void myMovedMouse ( int mouseX , int mouseY ) { GLint x = mouseX ; GLint y = screenHeight - mouseY ; /flip it as common GLint brushSize = 20; glRecti ( x , y , x + brushSize , y + brushSize ); glFlush (); }

Slide 27

Keyboard Interaction void myKeyboard ( unsigned scorch key , int mouseX , int mouseY ) { GLint x = mouseX ; GLint y = screenHeight - mouseY ; /flip it as normal switch ( key )/estimation of key is the ASCII estimation of the key squeezed { case \'p\': /draw a spot at the mouse position glBegin ( GL_POINTS ); glVertex2i ( x , y ); glEnd (); glFlush (); break ; case \'E\': exit (- 1);/end the project } }

Slide 28

Other GLUT Techniques Recommended GLUT instructional exercise http://www.lighthouse3d.com/opengl/excess/Topics we didn’t cover however helpful Animation: void glutIdleFunc(void (*func)(void)) Popup menus: int glutCreateMenu(void (*func)(int esteem)); Bitmap text styles: void glutBitmapCharacter(void *font, int character) Sub windows: int glutCreateSubWindow(int parentWindow, int x, int y, int width, int stature); Recommended perusing material Chapter 1 & 2 of OpenGL red book

Slide 29

Recommended Resources OpenGL official Website OpenGL Utility Toolkit (GLUT) (download GLUT) Nice GLUT Tutorial NEHE : OpenGL instructional exercise OpenGL red book .:tsli

Recommended
View more...