OpenGL, GLUT, GLUI - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

OpenGL, GLUT, GLUI

Description:

OpenGL is a C/C library. It is an interface to the graphics hardware. ... Look-at point. Camera Position. Car 1. Car 2. What you see on the screen ... – PowerPoint PPT presentation

Number of Views:180
Avg rating:3.0/5.0
Slides: 15
Provided by: csS74
Category:
Tags: glui | glut | opengl | car1

less

Transcript and Presenter's Notes

Title: OpenGL, GLUT, GLUI


1
OpenGL, GLUT, GLUI
  • Soon Tee Teoh
  • CS 116B

2
Introduction
  • OpenGL
  • OpenGL is a C/C library. It is an interface to
    the graphics hardware. It is included in C
    packages.
  • GLUT
  • GLUT manages window in the OS, also manages mouse
    clicks.
  • GLUI
  • User interface buttons, check-boxes.
  • Download GLUT and GLUI yourself. Good training in
    downloading and installing packages

3
int main(int argc, char argv) //
GLUT stuff
glutInitDisplayMode( GLUT_RGB
GLUT_DOUBLE GLUT_DEPTH ) side_window
glutCreateWindow( "BGPViz" )
glutPositionWindow( 10 , 10 ) // starting x, y
of window glutReshapeWindow( 512, 512 ) // x,
y size of window glutDisplayFunc(
sideGlutDisplay ) glutReshapeFunc(
sideGlutReshape ) glutKeyboardFunc(
sideGlutKeyboard ) glutMotionFunc(
sideGlutMotion ) glutMouseFunc( sideGlutMouse
) // GLUI stuff
glui
GLUI_Master.create_glui( "Control Panel", 0,
520,20 ) /
name, flags, x, and y / / Add invisible
panel to hold rest of controls / GLUI_Panel
panel1 glui-gtadd_panel( "", GLUI_PANEL_NONE
) / Start a new column in this panel /
glui-gtadd_column_to_panel(panel1, false) /
'false' means don't draw bar /
glui-gtadd_button_to_panel(panel1, "Clear", 0,
clear_cb) mode_chkbx0 glui-gtadd_checkbox_to
_panel(panel1, "OpenGL Line", (DL.mode), 0,
mode_cb) mode_chkbx1 glui-gtadd_checkbox_to_
panel(panel1, "DDL", (DL.mode), 1, mode_cb)
mode_chkbx2 glui-gtadd_checkbox_to_panel(panel1
, "Bresenham", (DL.mode), 2, mode_cb)
mode_cb(0) GLUI_Master.set_glutIdleFunc(
myGlutIdle ) // GLUT stuff

glutMainLoop() return 0
Set up the display window
Set up functions (these functions are
automatically called)
Give control to GLUT
4
2D Display Example
include "glut.h" ? gl.h is automatically
included in glut.h include "glui.h // this
function is called to display graphics in the
window // the window has been created by GLUT //
the graphics will be drawn by OpenGL void
sideGlutDisplay( void ) glClearColor(0.0,0.0,0
.0,0.0) glClear( GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT ) glMatrixMode(GL_PROJECTIO
N) glLoadIdentity() gluOrtho2D(0.0,1.0,0.0,1
.0) // left, right, bottom, top
glViewport(0,0,256,256) // startx, starty,
xsize, ysize // coordinates begin from lower
left corner of window glMatrixMode(GL_MODELVIEW)
glLoadIdentity() glDisable(GL_TEXTURE_2D)
glDisable(GL_LIGHTING) glBegin(GL_QUADS)
glVertex3f(0.2,0.2,0.0) glVertex3f(0.2,0.8,0.
0) glVertex3f(0.8,0.8,0.0)
glVertex3f(0.8,0.2,0.0) glEnd()
glutSwapBuffers()
These are all OpenGL code
Set up viewing
Draw a rectangle
Flush to screen
5
Creating a windowThis is the job of GLUT
side_window glutCreateWindow( "BGPViz"
) glutPositionWindow( 10 , 10 ) // starting x,
y of window glutReshapeWindow( 512, 512 ) // x,
y size of window
screen
10 pixels
10
512 pixels
Window created by GLUT
512 pixels
6
Setting up viewing in the windowThis is the job
of OpenGL
glMatrixMode(GL_PROJECTION) glLoadIdentity() glu
Ortho2D(0.0,10.0,0.0,10.0) // left, right,
bottom, top glViewport(0,0,256,256) // startx,
starty, xsize, ysize // coordinates begin from
lower left corner of window
Window created by GLUT
(5.0,2.0)
256 pixels
256 pixels
7
Mouse Click handled by GLUT
int main(int argc, char argv) //
GLUT stuff
glutInitDisplayMode( GLUT_RGB
GLUT_DOUBLE GLUT_DEPTH ) side_window
glutCreateWindow( "BGPViz" )
glutPositionWindow( 10 , 10 ) // starting x, y
of window glutReshapeWindow( 512, 512 ) // x,
y size of window glutDisplayFunc(
sideGlutDisplay ) glutReshapeFunc(
sideGlutReshape ) glutKeyboardFunc(
sideGlutKeyboard ) glutMotionFunc(
sideGlutMotion ) glutMouseFunc( sideGlutMouse
) // GLUI stuff
glui
GLUI_Master.create_glui( "Control Panel", 0,
520,20 ) /
name, flags, x, and y / / Add invisible
panel to hold rest of controls / GLUI_Panel
panel1 glui-gtadd_panel( "", GLUI_PANEL_NONE
) / Start a new column in this panel /
glui-gtadd_column_to_panel(panel1, false) /
'false' means don't draw bar /
glui-gtadd_button_to_panel(panel1, "Clear", 0,
clear_cb) mode_chkbx0 glui-gtadd_checkbox_to
_panel(panel1, "OpenGL Line", (DL.mode), 0,
mode_cb) mode_chkbx1 glui-gtadd_checkbox_to_
panel(panel1, "DDL", (DL.mode), 1, mode_cb)
mode_chkbx2 glui-gtadd_checkbox_to_panel(panel1
, "Bresenham", (DL.mode), 2, mode_cb)
mode_cb(0) GLUI_Master.set_glutIdleFunc(
myGlutIdle ) // GLUT stuff

glutMainLoop() return 0
The function that you specify here is
automatically called when mouse click detected.
8
Mouse function
void sideGlutDisplay( void )
glClearColor(0.0,0.0,0.0,0.0) glClear(
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT )
glMatrixMode(GL_PROJECTION) glLoadIdentity()
gluOrtho2D(0.0,1.0,0.0,1.0) // left, right,
bottom, top glViewport(0,0,512,512) //
startx, starty, xsize, ysize // coordinates
begin from lower left corner of window
glMatrixMode(GL_MODELVIEW) glLoadIdentity()
glDisable(GL_TEXTURE_2D) glDisable(GL_LIGHTING
) glPointSize(2) glColor3f(1.0,0.0,0.0)
glBegin(GL_POINTS) glVertex3f(gx,gy,0.0)
glEnd() glutSwapBuffers()
512
512
float gx, gy void sideGlutMouse(int button, int
state, int x, int y ) if ( button
GLUT_LEFT_BUTTON state GLUT_DOWN )
gx (float)x/512.0 gy
(float)(512-y)/512.0
sideGlutDisplay() glutPostRedisplay()
9
Mouse function
void sideGlutDisplay( void )
glClearColor(0.0,0.0,0.0,0.0) glClear(
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT )
glMatrixMode(GL_PROJECTION) glLoadIdentity()
gluOrtho2D(0.0,1.0,0.0,1.0) // left, right,
bottom, top glViewport(0,0,512,512) //
startx, starty, xsize, ysize // coordinates
begin from lower left corner of window
glMatrixMode(GL_MODELVIEW) glLoadIdentity()
glDisable(GL_TEXTURE_2D) glDisable(GL_LIGHTING
) glPointSize(2) glColor3f(1.0,0.0,0.0)
glBegin(GL_POINTS) glVertex3f(gx,gy,0.0)
glEnd() glutSwapBuffers()
45
x
512
503
512
503 45
float gx, gy void sideGlutMouse(int button, int
state, int x, int y ) if ( button
GLUT_LEFT_BUTTON state GLUT_DOWN )
gx (float)x/512.0 gy
(float)(512-y)/512.0
sideGlutDisplay() glutPostRedisplay()
0.982 0.912
10
GLUI creates a GUI control panel
include lttime.hgt clock_t old_time float delay
1.0 void playbutton_cb(int id) playing
id glutSetWindow(win) glutPostRedisplay()
void myGlutIdle( void ) clock_t
new_time new_time clock() if
(((double)(new_time-old_time)/(double)CLOCKS_PER_S
EC)gtdelay) old_time new_time if
(playing) // if playing flag is set, then
update graphics here ...
glutSetWindow(win) glutPostRedisplay()
int main() win
glutCreateWindow(Game) ... dynamics_glui
GLUI_Master.create_glui( "Animation", 0,
controlx2, controly2 ) GLUI_Panel
dynamics_panel1 dynamics_glui-gtadd_panel( "",
GLUI_PANEL_NONE ) GLUI_Panel
dynamics_animation_panel dynamics_glui-gtadd_pan
el_to_panel ( dynamics_panel2, "Animation" )
dynamics_glui-gtadd_button_to_panel(dynamics_animat
ion_panel, "Play", 1, playbutton_cb)
dynamics_glui-gtadd_button_to_panel(dynamics_animat
ion_panel, Stop", 0, playbutton_cb)
GLUI_Master.set_glutIdleFunc( myGlutIdle )
Set current window to the display window
Called every time button is pressed
Argument to the callback function
11
3D Graphics with OpenGL
Cars Modeling Coordinates
y
// draw a car centered at x0 and z0, and with
bottom y0 // with y as the up direction // with
z as the front direction // with length 2 and
width 1 void DrawCar() // draw the driver
glPushMatrix() glTranslatef(0.25,1.25,0.5)
glutSolidSphere(0.25,10,10) // r, nLatitudes,
nLongitudes glPopMatrix() // draw the car
glPushMatrix() glTranslatef(0.0,0.5,0.0)
glScalef(1.0,1.0,2.0) glutSolidCube(1) //
side length glPopMatrix()
1
0.5
1
x
z
Note glutSolidSphere draws a sphere with radius
R, centered on (0,0,0)
Note glutSolidCube draws a cube with side
lengths L, centered on (0,0,0)
12
void sideGlutDisplay( void ) float
light_ambient4 0.2, 0.2, 0.2, 1.0 // r,
g, b, a float light_diffuse4 0.8, 0.3,
0.1, 1.0 // r, g, b, a float
light_specular4 0.8, 0.3, 0.1, 1.0 // r,
g, b, a float light_position4 -1.0, 0.0,
0.0 , 0.0 // x, y, z, w float ad_col4
1.0, 0.5, 0.5, 1.0 // r, g, b, a float
ad_col24 1.0, 1.0, 1.0, 1.0 // r, g, b,
a float spec_col4 1.0, 1.0, 1.0, 1.0
// r, g, b, a glClearColor(0.0,0.0,0.0,0.0)
glClear( GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT ) glMatrixMode(GL_PROJECTIO
N) glLoadIdentity() gluPerspective(45.0,(flo
at)sidewidth/(float)sideheight,5.0,5000.0)
// theta, aspect, dnear,
dfar glViewport(0,0,sidewidth,sideheight) //
startx, starty, xsize, ysize
glMatrixMode(GL_MODELVIEW) glLoadIdentity()
glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST)
glEnable(GL_NORMALIZE) glLightfv(GL_LIGHT0
, GL_AMBIENT, light_ambient)
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
glLightfv(GL_LIGHT0, GL_SPECULAR,
light_specular) glLightfv(GL_LIGHT0,
GL_POSITION, light_position)
glEnable(GL_LIGHT0)
Set up the projection matrix
Start the modelview matrix
13
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
ad_col) glMaterialfv(GL_FRONT, GL_SPECULAR,
spec_col) // viewer is at (-10,10,-10) looking
towards the center of the terrain gluLookAt(-10,1
0,-10,128,0,128,0,1,0) // draw the terrain //
a 256x256 square with lower left corner
(0,0,0) // up direction is y glBegin(GL_QUADS)
glNormal3f(0.0,1.0,0.0) glVertex3f(0.0,0.0,0.0)
glVertex3f(256.0,0.0,0.0) glVertex3f(256.0,0.
0,256.0) glVertex3f(0.0,0.0,256.0) glEnd()
glMaterialfv(GL_FRONT_AND_BACK,
GL_AMBIENT_AND_DIFFUSE, ad_col2) // draw the
first car at the origin pointing in the z
direction glPushMatrix() DrawCar() glPopMatri
x() // draw the second car at (0,0,10)
pointing 45 degrees inwards glPushMatrix() glT
ranslatef(0.0,0.0,10.0) glRotatef(45.0,0.0,1.0,0
.0) DrawCar() glPopMatrix() glutSwapBuffers
()
Transform from world coordinates to viewing
coordinates
World Coordinates
y
(-10,10,-10)
256
256
x
(128,0,128)
z
14
In world coordinate space
y
Car 1
Camera Position
Car 2
(-10,10,-10)
256
256
x
(128,0,128)
z
Look-at point
What you see on the screen
Write a Comment
User Comments (0)
About PowerShow.com