Title: OpenGL Tutorial CISC 640440 Computer Graphics
1OpenGL Tutorial CISC 640/440 Computer Graphics
- TA Qi Li/Mani Thomas
- qili_at_cis.udel.edu/manivt_at_cis.udel.edu
2Outline
- Submission policy
- OpenGL Basics and setup
- Draw geometry primitives
- Transformations in OpenGL
- Sample codes
- Office hour
3Submission Policy
- How to submit
- What to submit
- On late submission
4How to Submit
- Tar/zip file
- Unix
- gt tar -cf lastname_projectNum.tar projectDir
- gt gzip lastname_projectNum.tar
- Windows
- get a winzip application (if you don't have)
- go the directory storing your assignments
- right click your mouse to zip the directory
- Submit the tar/zip file through the course web
(More details will be announced later)
5What to Submit
- Must contain
- readme
- Makefile
- Source codes
- Output figures (if any)
- Must NOT contain
- obj intermediate files
- obj data files
6What to Submit Readme
- My name
- My email myemail_at_udel.edu
- Project Num
-
- Part 1 description of this project
- This project is to apply xxx algorithm to
plot xxx, - Part 2 what I did and what I didn't do
- I completed all/most/some functionalities
required in this project. - The system is robust and the rendering is
fairly efficient, - I didn't do . The reason is .
- Part 3 What files contained
- Part 4 How to compile and how to run
- The project is developed in windows system
and tested in stimpy (strauss) unix system,
7What to Submit Makefile for EECIS
- Makefile for EECIS
- uncomment the following line for the C
compiler(cc) - CC cc CC CC
- TOP /usr/local/mesa
- list the name of the executable you wish to
produce - exec hello
- list all your object files separated by spaces
- objects hello.o
- list all your header files separated by spaces
- includes lines5.h lines5_2.h
- LINKDIR -L(TOP)/lib -L/usr/openwin/lib
- INCLUDEDIR (TOP)/include
- LLDLIBS -lGL -lglut -lMesaGLU -lMesaGL -lm
-lX11 -lXext -lXmu \ -lXt -lXi
- (exec) (objects)
- (CC) -o _at_ (LINKDIR) (objects) (LLDLIBS)
- (objects) (includes) (CC) -c -I(INCLUDEDIR)
lt - .PHONY clean
- clean -rm -f (objects) (exec)
8What to Submit Makefile for Composer
- Makefile for Composer/Strauss
- uncomment the following line for the C
compiler(cc) - CC cc CC CC
- TOP /home/base/usrb/chandrak/640/OpenGL/Mesa-2.6
- list the name of the executable you wish to
produce - exec hello
- list all your object files separated by spaces
- objects hello.o
- list all your header files separated by spaces
- includes lines5.h lines5_2.h
- LINKDIR -L(TOP)/lib -L/usr/openwin/lib
- INCLUDEDIR (TOP)/include
- LLDLIBS -lGL -lglut -lMesaGLU -lMesaGL -lm
-lX11 -lXext -lXmu \ -lXt -lXi
- (exec) (objects)
- (CC) -o _at_ (LINKDIR) (objects) (LLDLIBS)
- (objects) (includes) (CC) -c -I(INCLUDEDIR)
lt - .PHONY clean
- clean -rm -f (objects) (exec)
9Late Submission
- N 10 percent of the points you got deducted if
there are N (lt5) late days (not counting
weekends). No acceptance for the submission more
than 5-day late - Each student has three free (i.e. without any
penalty) late days for entire semester. You
should notify the TA the use of free late days
ahead
10Outline
- Submission policy
- OpenGL Basics and setup
- Draw geometry primitives
- Transformations in OpenGL
- Sample codes
- Office hour
11OpenGL What is It?
- GL (Graphics Library) Library of 2-D, 3-D
drawing primitives and operations - API for 3-D hardware acceleration
- GLU (GL Utilities) Miscellaneous functions
dealing with camera set-up and higher-level shape
descriptions - GLUT (GL Utility Toolkit) Window-system
independent toolkit with numerous utility
functions, mostly dealing with user interface
12OpenGL Setup in Unix
- Unix (strongly recommended)
- add the following command in your .cshrc
- EECIS setenv
LD_LIBRARY_PATH /usr/local/mesa/lib/usr/openwin/l
ib - Composor setenv LD_LIBRARY_PATH
/home/usra/usrb/chandrak/640/OpenGL/Mesa-2.6/usr/
openwin/lib - Download Makefile (for hello.c)
- Makefile_eecis, or
- Makefile_composor
- remote display (If your local machine is not
stimpy or strauss, you need to do the following) - localmachine gt ssh stimpy.cis.udel.edu
- Compile stimpygt make
- Run stimpy gt hello
13OpenGL Setup in Windows
- Windows/VC 6.0
- Get glut http//www.xmission.com/nate/glut.html
- An excellent source for the setup of openGL on
Windows/VC 6.0 is available at - http//www.lighthouse3d.com/opengl/glut/index.php
14OpenGL Setup in Windows
- Windows (step 1 create new project)
15OpenGL Setup in Windows
- Windows (step 2 create new project)
16OpenGL Setup in Windows
- Windows (step 3 insert file)
17OpenGL Setup in Windows
- Windows (step 4 project setting)
18OpenGL Setup in Windows
- Windows (step 5 project setting)
19Outline
- Submission policy
- OpenGL Basics and setup
- Draw geometry primitives
- Transformations in OpenGL
- Sample codes
- Office hour
20OpenGL Geometric Primitives
21Specifying Geometric Primitives
- Primitives are specified using
- glBegin(primType)
- ...
- glEnd()
- primType determines how vertices are combined
GLfloat red, green, blue GLfloat x,
y glBegin(primType) for (i 0 i lt nVerts
i) glColor3f(red, green, blue)
glVertex2f(x, y) ... // change coord.
values glEnd()
22OpenGL Vertex/Color Command Formats
glVertex3fv( v )
glColor3fv( v )
Number of components
Data Type
Vector
b - byte ub - unsigned byte s - short us -
unsigned short i - int ui - unsigned int f -
float d - double
omit v for scalar form e.g., glVertex2f(x,
y) glColor3f(r, g, b)
2 - (x,y) 3 - (x,y,z), (r,g,b) 4 -
(x,y,z,w), (r,g,b,a)
23Outline
- Policy
- OpenGL Basics and setup
- Draw geometry primitives
- Transformations in OpenGL
- Sample codes
- Office hour
24OpenGL 3-D coordinates
- Right-handed system
- From point of view of camera looking out
into scene - X right, -X left
- Y up, -Y down
- Z behind camera, -Z in front
- Positive rotations are counterclockwise around
axis of rotation
25Transformations in OpenGl
- Modeling transformation
- Viewing transformation
- Projection transformation
26Modeling Transformation
- Refer to the transformation of models (i.e., the
scenes, or objects) - Generally,
- glMultMatrixf(M_i)
- Some simple transformations
- Translation glTranslate(x,y,z)
- Scale glScale(sx,sy,sz)
- Rotation glRotate(theta, x,y,z)
- x,y,z are components of vector defining axis of
rotation - Angle in degrees direction is counterclockwise
27Viewing Transformation
- Refer to the transformation on the camera
- Using glTranslate() and glRotate()
- Using gluLookAt()
- gluLookAt (eyeX, eyeY, eyeZ, centerX, centerY,
centerZ, upX, upY, upZ) - eye (eyeX, eyeY, eyeZ)T Desired camera
position - center (centerX, centerY, centerZ)T Where
camera is looking - up (upX, upY, upZ)T Cameras up vector
28Viewing Transformation
from Woo et al
29Projection Transformation
- Refer to the transformation from scene to image
- Orthographic projection
- glOrtho (left, right, bottom, top, near, far)
from Hill
30Projection Transformation
- Refer to the transformation from scene to image
- Orthographic projection
- glOrtho (left, right, bottom, top, near, far)
- Perspective projection
- glFrustum (left, right, bottom, top, near, far)
31Projection Transformation
- Refer to the transformation from scene to image
- Orthographic projection
- glOrtho (left, right, bottom, top, near, far)
- Perspective projection
- glFrustum (left, right, bottom, top, near, far)
32Notes on openGl transformations
- Before applying modeling or viewing
transformations, need to set glMatrixMode(GL_MODE
LVIEW) - Before applying projection transformations, need
to set glMatrixMode(GL_Projection) - Replacement by either following
commands glLoadIdentity() glLoadMatrix(M) - Multiple transformations (either in modeling or
viewing) are applied in reverse order
33Notes on openGl transformations
- Before applying modeling or viewing
transformations, need to set glMatrixMode(GL_MODE
LVIEW) - Before applying projection transformations, need
to set glMatrixMode(GL_Projection) - Replacement by either following
commands glLoadIdentity() glLoadMatrix(M) - Multiple transformations (either in modeling or
viewing) are applied in reverse order
34Outline
- Policy
- OpenGL Basics and setup
- Draw 2-D primitives
- Transformation in OpenGL
- Sample codes
- Office hour
35Sample codes
- void main(int, char)
- void init (void)
- void reshape (int, int)
- void display (void)
- void menu (void)
- void dda_line (int, int, int, int)
- void midpoint_line (int, int, int, int)
- void gl_line (int, int, int, int)
- void mouse (int, int, int, int)
- void keyboard (unsigned char, int, int)
36Sample code main function
int main (int argc, char argv) glutInit
(argc, argv) glutInitDisplayMode (GLUT_SINGLE
GLUT_RGB) glutInitWindowSize (400, 400)
glutInitWindowPosition (400, 350)
glutCreateWindow ("Scan Conversion
Algorithms") init () glutDisplayFunc
(display) glutReshapeFunc (reshape)
glutMouseFunc (mouse) glutKeyboardFunc
(keyboard) menu () glutMainLoop ()
return (0) / ANSI C requires main to return
int. /
37Sample code init
void init (void) / select clearing color
/ / this color is white --- full red,
green, and blue components / / the last
component is alpha(transparency) --- use zero /
glClearColor (1.0, 1.0, 1.0, 0.0)
38Sample code reshape
void reshape (int w, int h) / This routine
is called when the initial GL window is created
and when the window is resized. It is
followed in execution by the 'display'
routine. / / initialize viewing values /
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)
39Sample code display
void display (void) / clear all pixels /
glClear (GL_COLOR_BUFFER_BIT) / don't wait!
start processing buffered OpenGL routines
/ glFlush ()
40Sample code gl_line
void gl_line (int x0, int y0, int x1, int y1)
glColor3ub (0, 255, 0) / green / glBegin
(GL_LINES) glVertex2i (x0, y0)
glVertex2i (x1, y1) glEnd ()
41Sample code mouse
void mouse (int button, int state, int x, int
y) switch (button) case
GLUT_LEFT_BUTTON if (state GLUT_DOWN)
printf ("x d, y d\n", x, y)
break case GLUT_RIGHT_BUTTON if
(state GLUT_DOWN) exit (0)
break default break
42Outline
- Policy
- OpenGL Basics and setup
- Draw 2-D primitives
- Transformation in OpenGL
- Sample codes
- Office hour
43Office hour of CISC640
- Monday 300-500pm Pearson 115B
- Email qili_at_cis.udel.edu
- Webpage http//www.cis.udel.edu/qili/
44Office hour of CISC440
- Wed 500-700pm Pearson 115B
- Email manivt_at_cis.udel.edu
- Webpage http//www.cis.udel.edu/manivt/