100% found this document useful (1 vote)
172 views

Introduction To OpenGL Programming

This document provides an introduction and overview of OpenGL including: - The hierarchy of abstraction levels in OpenGL from lowest level GL to higher level GLU and GLUT. - That OpenGL is an API and how it is implemented on different platforms. - The basic steps a programmer needs to take including specifying geometry, camera, and lights. - An overview of geometric primitives, specifying vertices, and color commands in OpenGL.

Uploaded by

Ayush Maheshwari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
172 views

Introduction To OpenGL Programming

This document provides an introduction and overview of OpenGL including: - The hierarchy of abstraction levels in OpenGL from lowest level GL to higher level GLU and GLUT. - That OpenGL is an API and how it is implemented on different platforms. - The basic steps a programmer needs to take including specifying geometry, camera, and lights. - An overview of geometric primitives, specifying vertices, and color commands in OpenGL.

Uploaded by

Ayush Maheshwari
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

IntroductiontoOpenGL Programming

OpenGLHierarchy
Severallevelsofabstractionareprovided GL
Lowestlevel:vertex,matrixmanipulation glVertex3f(point.x,point.y,point.z)

GLU
Helperfunctionsforshapes,transformations gluPerspective(fovy,aspect,near,far)

GLUT
Highestlevel:Windowandinterfacemanagement glutSwapBuffers()

OpenGLImplementations
OpenGLISanAPI
#include<GL/gl.h> #include<GL/glu.h> #include<GL/glut.h>

Windows,Linux,UNIX,etc.allprovideaplatform specific implementation. Windows:opengl32.libglu32.libglut32.lib Linux:lGLlGLUlGLUT

OpenGLAPI
Asaprogrammer,youneedtodothe followingthings:
Specifythelocation/parametersofcamera. Specifythegeometry(andappearance). Specifythelights(optional).

OpenGLGeometricPrimitives
GL_LINES GL_POLYGON GL_POINTS GL_LINE_STRIP GL_LINE_LOOP

GL_TRIANGLES GL_QUADS GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

SpecifyingGeometricPrimitives
Primitivesarespecifiedusing
glBegin(primType); ... glEnd();

primType determineshowverticesarecombined
GLfloat red, green, blue; GLfloat x, y; glBegin(primType); for (i = 0; i < nVerts; i++) { glColor3f(red, green, blue); glVertex2f(x, y); ... // change coord. values } glEnd();

OpenGLVertex/Color CommandFormats
glVertex3fv( v ) glColor3fv( v )

Number of components
2 - (x,y) 3 - (x,y,z), (r,g,b) 4 - (x,y,z,w), (r,g,b,a)

Data Type
b ub s us i ui f d byte unsigned byte short unsigned short int unsigned int float double

Vector
omit v for scalar form e.g., glVertex2f(x, y) glColor3f(r, g, b) Example

OpenGL:Camera
Twothingstospecify:
Physicallocationofcamerainthescene(MODELVIEW matrixinOpenGL).
Whereisthecamera? Whichdirectionisitpointing? Whatistheorientationofthecamera?

Projectionpropertiesofthecamera(PROJECTIONmatrixin OpenGL):
Depthoffield? Fieldofviewinthexandydirections?

OpenGL:MODELVIEW
glMatrixMode(GL_MODELVIEW);//Specifymatrixmode glLoadIdentity();//Clearmodelviewmatrix //UtilityfunctionprovidedbytheGLUAPI(includedwithOpenGL) gluLookAt(eyex,eyey,eyez,centerx,centery,centerz,upx,upy,upz);
y Camera z eye x World center=(0,0,0) x up=y Pointp inworldis: (MODELVIEW*p) incameracoordsys!!!

OpenGL3Dcoordinates
Righthandedsystem Frompointofviewofcameralooking outintoscene: +X right,X left +Y up,Y down +Z behind camera,Z infront Positiverotationsarecounterclockwise aroundaxisofrotation

Sampleprogram
void main( int argc, char** argv ) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutCreateWindow( argv[0] ); init(); glutDisplayFunc( display ); glutReshapeFunc( resize ); glutKeyboardFunc( key ); glutIdleFunc( idle ); glutMainLoop(); }

OpenGLInitialization
Setupwhateverstateyouregoingtouse
void init( void ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glClearDepth( 1.0 ); glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST ); }

Reshape
voidreshape(int w,int h) { /*ThisroutineiscalledwhentheinitialGLwindowiscreated andwhenthewindowisresized.Itisfollowedinexecution bythe'display'routine.*/ /*initializeviewingvalues*/ glViewport (0,0,(GLint)w 1,(GLint)h 1); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho (0.0,(GLdouble)w,0.0,(GLdouble)h,1.0,1.0); }

Display
voiddisplay(void) { /*clearallpixels*/ glClear (GL_COLOR_BUFFER_BIT); /*don'twait! *startprocessingbufferedOpenGLroutines */ glFlush (); }

Keyboard
Processuserinput

glutKeyboardFunc( keyboard );
void keyboard( unsigned char key, int x, int y ) { switch( key ) { case q : case Q : exit( EXIT_SUCCESS ); break; case r : case R : rotate = GL_TRUE; glutPostRedisplay(); break; } }

Mouse
{ voidmouse(int button,int state,int x,int y) switch(button) { caseGLUT_LEFT_BUTTON: if(state==GLUT_DOWN) printf ("x=%d,y=%d\n",x,y); break; caseGLUT_RIGHT_BUTTON: if(state==GLUT_DOWN) exit(0); break; default: break; } }

SpecifyingTransformations
Programmerhastwostylesofspecifying transformations
specifymatrices(glLoadMatrix, glMultMatrix) specifyoperation(glRotate, glOrtho)

MatrixOperations
SpecifyCurrentMatrixStack
glMatrixMode( GL_MODELVIEW or GL_PROJECTION )

OtherMatrixorStackOperations
glLoadIdentity() glPushMatrix() glPopMatrix()

Viewport
usuallysameaswindowsize viewportaspectratioshouldbesameasprojectiontransformationor resultingimagemaybedistorted

glViewport( x, y, width, height )

ProjectionTransformation
Shapeofviewingfrustum Perspectiveprojection
gluPerspective( fovy, aspect, zNear, zFar ) glFrustum( left, right, bottom, top, zNear, zFar )

Orthographicparallelprojection
glOrtho( left, right, bottom, top, zNear, zFar )

gluOrtho2D( left, right, bottom, top )


callsglOrtho withzvaluesnearzero

Example

ModelingTransformations
Moveobject glTranslate{fd}( x, y, z ) Rotateobjectaroundarbitraryaxis glRotate{fd}( angle, x, y, z )
angleisindegrees

Dilate(stretchorshrink)ormirrorobject glScale{fd}( x, y, z )

Example

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy