OpenGL Basics CS334 Assignment 2 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

OpenGL Basics CS334 Assignment 2

Description:

... commands to control the lights and the light model, and move the viewpoint. ... Collection of functions to interface with your graphics hardware (GPU) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 24
Provided by: cvan5
Category:

less

Transcript and Presenter's Notes

Title: OpenGL Basics CS334 Assignment 2


1
OpenGL BasicsCS334 Assignment 2
2
About Assignment 2
  • You will write a program that creates a basic
    scene model and for each frame issues OpenGL
    commands to control the lights and the light
    model, and move the viewpoint.

3
OpenGL
  • Collection of functions to interface with your
    graphics hardware (GPU)
  • Supports drawing of primitives, texturing,
    lighting, etc.
  • Graphics card receives this data and uses
    specialized hardware to compute result image
  • Alternative is DirectX
  • More common in gaming
  • Programmers Documentation http//www.opengl.org/d
    ocumentation/red_book/
  • Tutorials http//nehe.gamedev.net/

4
GLUT
  • Graphics Library Utility Toolkit
  • Adds functionality such as windowing operations
    to OpenGL
  • Event-based callback interface
  • Display callback
  • Resize callback
  • Idle callback
  • Keyboard callback
  • Mouse movement callback
  • Mouse button callback

5
GLUI
  • OpenGL User Interface Library
  • Built on top of GLUT
  • Better GUI
  • Spinners
  • Text Boxes
  • Checkboxes
  • Radio Buttons
  • File Browser
  • Download and documentation http//www.cs.unc.edu/
    rademach/glui/

6
Reading Material for this Assignment
  • OpenGL Red Book
  • Chapter 3 Viewing
  • Chapter 6 Lighting
  • Appendix G Homogeneous Coordinates and
    Transformation Matrices
  • Note
  • Use function Glut Solid to create 3D objects

7
Matrix Transformations
8
Chapter 3 Viewing
9
Analogy
10
Matrix Transformations
  • Each of modelview and projection matrix is a 4x4
    matrix
  • OpenGL functions
  • glMatrixMode()
  • glLoadIdentity()
  • glLoadMatrixf()
  • glMultMatrix()
  • glTranslate()
  • glScale()
  • glRotate()

11
Matrix Transformations
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glMultMatrixf(N) / apply transformation /
  • glMultMatrixf(M) / apply transformation M /
  • glMultMatrixf(L) / apply transformation L /
  • glBegin(GL_POINTS)
  • glVertex3f(v) / draw transformed vertex v /
  • glEnd()
  • draw transformed point N(M(Lv)

12
Modeling Transformations
glRotatef(45,0,0,1)
glScalef(2,-0.5,1.0)
glTranslate3f(tx,ty,tz)
13
Modeling Transformations
glRotatef(d,rx,ry,rz) glTranslate3f(tx,ty,tz)
glTranslate3f(tx,ty,tz) glRotatef(d,rx,ry,rz)
14
Viewing Transformations
15
Projection Transformations
void glFrustum(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble near,
GLdouble far)
16
Projection Transformations
void gluPerspective(GLdouble fovy, GLdouble
aspect, GLdouble near, GLdouble far)
17
Projection Transformations
void glOrtho(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble near,
GLdouble far) void gluOrtho2D(GLdouble left,
GLdouble right, GLdouble bottom, GLdouble top)

18
Viewport Transformations
19
Simple Program
  • include ltGL/gl.hgt
  • main()
  • InitializeAWindowPlease()
  • glMatrixMode(GL_PROJECTION)
  • glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
  • glClearColor (0.0, 0.0, 0.0, 0.0)
  • glClear (GL_COLOR_BUFFER_BIT)
  • glColor3f (1.0, 1.0, 1.0)
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glTranslate3f(1.0, 1.0, 1.0)
  • glBegin(GL_POLYGON)
  • glVertex3f (0.25, 0.25, 0.0)
  • glVertex3f (0.75, 0.25, 0.0)
  • glVertex3f (0.75, 0.75, 0.0)
  • glVertex3f (0.25, 0.75, 0.0)
  • glEnd()
  • glFlush()

20
Simple OpenGL GLUT Program
  • include ltgt
  • DisplayCallback()
  • ltClear windowgt
  • ltLoad Projection matrixgt
  • ltLoad Modelview matrixgt
  • ltDraw primitivesgt
  • (ltSwap buffersgt)
  • IdleCallback()
  • ltDo some computationsgt
  • ltMaybe force a window refreshgt
  • KeyCallback()
  • MouseMovementCallback
  • ltHandle mouse movementgt
  • MouseButtonsCallback
  • ltHandle mouse buttonsgt
  • Main()
  • ltInitialize GLUT and callbacksgt
  • ltCreate a windowgt
  • ltInitialize OpenGL stategt
  • ltEnter main event loopgt

21
Simple OpenGL GLUT Program
  • include ltGL/gl.hgt
  • include ltGL/glu.hgt
  • include ltGL/glut.hgt
  • void init(void)
  • glClearColor (0.0, 0.0, 0.0, 0.0)
  • glShadeModel (GL_FLAT)
  • void display(void)
  • glClear (GL_COLOR_BUFFER_BIT)
  • glColor3f (1.0, 1.0, 1.0)
  • glLoadIdentity ()
  • gluLookAt (0, 0, 5, 0, 0, 0, 0, 1, 0)
  • glScalef (1.0, 2.0, 1.0)
  • glutWireCube (1.0)
  • glFlush ()
  • void reshape (int w, int h)
  • glViewport (0, 0, (GLsizei) w, (GLsizei) h)
  • glMatrixMode (GL_PROJECTION)
  • glLoadIdentity ()
  • glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
  • glMatrixMode (GL_MODELVIEW)
  • int main(int argc, char argv)
  • glutInit(argc, argv)
  • glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
  • glutInitWindowSize (500, 500)
  • glutInitWindowPosition (100, 100)
  • glutCreateWindow (argv0)
  • init ()
  • glutDisplayFunc(display)
  • glutReshapeFunc(reshape)

22
Example Program with Lighting
  • include ltGL/gl.hgt
  • include ltGL/glu.hgt
  • include ltGL/glut.hgt
  • void init(void)
  • GLfloat mat_specular 1.0, 1.0, 1.0, 1.0
  • GLfloat mat_shininess 50.0
  • GLfloat light_position 1.0, 1.0, 1.0,
    0.0
  • glClearColor (0.0, 0.0, 0.0, 0.0)
  • glShadeModel (GL_SMOOTH)
  • glMaterialfv(GL_FRONT, GL_SPECULAR,
    mat_specular)
  • glMaterialfv(GL_FRONT, GL_SHININESS,
    mat_shininess)
  • glLightfv(GL_LIGHT0, GL_POSITION,
    light_position)
  • glEnable(GL_LIGHTING)
  • glEnable(GL_LIGHT0)
  • glEnable(GL_DEPTH_TEST)
  • void reshape (int w, int h)
  • glViewport (0, 0, (GLsizei) w, (GLsizei) h)
  • glMatrixMode (GL_PROJECTION)
  • glLoadIdentity()
  • if (w lt h)
  • glOrtho (-1.5, 1.5, -1.5(GLfloat)h/(GLfloat
    )w,
  • 1.5(GLfloat)h/(GLfloat)w, -10.0, 10.0)
  • else
  • glOrtho (-1.5(GLfloat)w/(GLfloat)h,
  • 1.5(GLfloat)w/(GLfloat)h, -1.5, 1.5,
    -10.0, 10.0)
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • int main(int argc, char argv)
  • glutInit(argc, argv)
  • glutInitDisplayMode (GLUT_SINGLE GLUT_RGB
    GLUT_DEPTH)

23
GLUI
  • Graphics Library User Interface
Write a Comment
User Comments (0)
About PowerShow.com