3D IMAGE RENDERING
Simple Geometric Object.
The following are two geometric objects: a pyramid and a cube. The
pyramid is generated with primitives defined by GL_TRIANGLES.
The cube is generated with primitives defined by GL_QUAD_STRIP and
GL_QUADS.
The attached source code highlights a architecture of a molular GUI implementation
for scriptive OpenGL rendering.
#
# Copyright(c) 1999-2001, Chengye Mao, email: chengye.geo@yahoo.com
#
# simple - To render two simple 3D objects, pyramid and cube, with
# a GLW widget. It demonstrates the architecture
of a modular scriptive
# GUI implemention and shows how to use RC associated
variables and
# Tcl procedures sandwiched by "glw current"
commands for OpenGL
# rendering in multiple rendering contexts.
#
# Binding of mouse motion with left-button pressed
# object rotation
#
# Procedures:
# simple.init - initialize
# simple.pyramid - create a pyramid object
# simple.cube - create a cube
object
# simple.motion - handle mouse motion event
# simple.button - handle mouse button press
event
# simple.reshape - handle widget configuration
# simple.display - display object
# simple.create - entry to create a simple
GLW widget
#
proc simple.init w {
set old [glw current $w]
GLint listId
GLfloat xrot
GLfloat yrot
GLfloat xc
GLfloat yc
glset listId [glGenLists 1]
glLoadIdentity
glShadeModel GL_SMOOTH
glClearColor 0 0 0 0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST
glw current $old
}
proc simple.pyramid w {
set old [glw current $w]
glNewList listId GL_COMPILE
glClear GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT
glTranslate 0.0 0.0
0.0
glBegin GL_TRIANGLES
glColor 1 0 0; glVertex
0.0 0.5 0.5
glColor 0 1 0; glVertex
-0.5 -0.5 0.5
glColor 1 1 1; glVertex
0.0 0.0 -0.5
glColor 1 0 0; glVertex
0.0 0.5 0.5
glColor 1 1 1; glVertex
0.0 0.0 -0.5
glColor 0 0 1; glVertex
0.5 -0.5 0.5
glColor 0 0 1; glVertex
0.5 -0.5 0.5
glColor 0 1 0; glVertex
-0.5 -0.5 0.5
glColor 1 1 1; glVertex
0.0 0.0 -0.5
glColor 1 0 0; glVertex
0.0 0.5 0.5
glColor 0 0 1; glVertex
0.5 -0.5 0.5
glColor 0 1 0; glVertex
-0.5 -0.5 0.5
glEnd
glEndList
glw current $old
simple.display $w
}
proc simple.cube w {
set old [glw current $w]
glNewList listId GL_COMPILE
glClear GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT
glTranslate 0.0 0.0 0.0
glBegin GL_QUAD_STRIP
glColor 1 0 1; glVertex
-0.5 0.5 0.5
glColor 1 0 0; glVertex
-0.5 -0.5 0.5
glColor 1 1 1; glVertex
0.5 0.5 0.5
glColor 1 1 0; glVertex
0.5 -0.5 0.5
glColor 0 1 1; glVertex
0.5 0.5 -0.5
glColor 0 1 0; glVertex
0.5 -0.5 -0.5
glColor 0 0 1; glVertex
-0.5 0.5 -0.5
glColor 0 0 0; glVertex
-0.5 -0.5 -0.5
glColor 1 0 1; glVertex
-0.5 0.5 0.5
glColor 1 0 0; glVertex
-0.5 -0.5 0.5
glEnd
glBegin GL_QUADS
glColor 1 0 1; glVertex
-0.5 0.5 0.5
glColor 1 1 1; glVertex
0.5 0.5 0.5
glColor 0 1 1; glVertex
0.5 0.5 -0.5
glColor 0 0 1; glVertex
-0.5 0.5 -0.5
glColor 1 0 0; glVertex
-0.5 -0.5 0.5
glColor 1 1 0; glVertex
0.5 -0.5 0.5
glColor 0 1 0; glVertex
0.5 -0.5 -0.5
glColor 0 0 0; glVertex
-0.5 -0.5 -0.5
glEnd
glEndList
glw current $old
simple.display $w
}
proc simple.motion {w x y} {
set old [glw current $w]
glset xrot [expr [glv xrot] + $x - [glv xc]]
glset yrot [expr [glv yrot] + $y - [glv yc]]
glset xc $x
glset yc $y
glw current $old
simple.display $w
}
proc simple.button {w x y} {
set old [glw current $w]
glset xc $x
glset yc $y
glw current $old
}
proc simple.reshape {w width height} {
set old [glw current $w]
glViewport 0 0 $width $height
glMatrixMode GL_PROJECTION
glLoadIdentity
gluOrtho2D 0 $width 0 $height
glw current $old
}
proc simple.display w {
set old [glw current $w]
glLoadIdentity
glRotate xrot 0 1 0
glRotate yrot 1 0 0
glCallList listId
glFlush
glw draw
glw current $old
}
proc simple.create {w {option {}}} {
if [winfo exist $w] {
error "Window $w already
exists!"
}
if {$option == "toplevel"} {
toplevel $w
} else {
frame $w
}
pack [frame $w.f] -side top -fill x
pack [button $w.f.b1 -bd 0 -text Pyramid \
-command "simple.pyramid
$w.simple"] \
[button $w.f.b2
-bd 0 -text Cube \
-command "simple.cube
$w.simple"] -side left
set s $w.simple
glw $s
simple.init $s
bind $s <Button-1> "simple.button $s
%x %y"
bind $s <B1-Motion> "simple.motion $s %x
%y"
bind $s <Expose> "simple.display
$s"
bind $s <Configure> "simple.reshape $s %w
%h"
pack $s -expand 1 -fill both
simple.pyramid $s
return $w
}
|