Demo 61 of 68 in category RedBook
 |
# texsub.tcl
#
# An example of the OpenGL red book modified to work with Tcl3D.
# The original C sources are Copyright (c) 1993-2003, Silicon Graphics, Inc.
# The Tcl3D sources are Copyright (c) 2005-2022, Paul Obermeier.
# See file LICENSE for complete license information.
#
# This program texture maps a checkerboard image onto
# two rectangles. This program clamps the texture, if
# the texture coordinates fall outside 0.0 and 1.0.
# If the s key is pressed, a texture subimage is used to
# alter the original texture. If the r key is pressed,
# the original texture is restored.
package require tcl3d
# Create checkerboard texture
set checkImageWidth 64
set checkImageHeight 64
set subImageWidth 16
set subImageHeight 16
set checkImage [tcl3dVector GLubyte [expr $checkImageHeight*$checkImageWidth*4]]
set subImage [tcl3dVector GLubyte [expr $subImageHeight*$subImageWidth*4]]
# Font to be used in the Tk listbox.
set listFont {-family {Courier} -size 10}
# Show errors occuring in the Togl callbacks.
proc bgerror { msg } {
tk_messageBox -icon error -type ok -message "Error: $msg\n\n$::errorInfo"
exit
}
# Print info message into widget a the bottom of the window.
proc PrintInfo { msg } {
if { [winfo exists .fr.info] } {
.fr.info configure -text $msg
}
}
# Note: A faster method to calculate and specify textures in Tcl has been
# introduced with Tcl3D version 0.3. See Tcl3D demo bytearray.tcl.
proc makeCheckImage {} {
for { set i 0 } { $i < $::checkImageHeight } { incr i } {
for { set j 0 } { $j < $::checkImageWidth } { incr j } {
set c [expr {(((($i&0x8)==0)^(($j&0x8))==0))*255}]
$::checkImage set [expr {($i*$::checkImageWidth + $j)*4 + 0}] $c
$::checkImage set [expr {($i*$::checkImageWidth + $j)*4 + 1}] $c
$::checkImage set [expr {($i*$::checkImageWidth + $j)*4 + 2}] $c
$::checkImage set [expr {($i*$::checkImageWidth + $j)*4 + 3}] 255
}
}
for { set i 0 } { $i < $::subImageHeight } { incr i } {
for { set j 0 } { $j < $::subImageWidth } { incr j } {
set c [expr {(((($i&0x4)==0)^(($j&0x4))==0))*255}]
$::subImage set [expr {($i*$::subImageWidth + $j)*4 + 0}] $c
$::subImage set [expr {($i*$::subImageWidth + $j)*4 + 1}] 0
$::subImage set [expr {($i*$::subImageWidth + $j)*4 + 2}] 0
$::subImage set [expr {($i*$::subImageWidth + $j)*4 + 3}] 255
}
}
}
proc CreateCallback { toglwin } {
glClearColor 0.0 0.0 0.0 0.0
glShadeModel GL_FLAT
glEnable GL_DEPTH_TEST
makeCheckImage
glPixelStorei GL_UNPACK_ALIGNMENT 1
set ::texName [tcl3dVector GLuint 1]
glGenTextures 1 $::texName
glBindTexture GL_TEXTURE_2D [$::texName get 0]
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S $::GL_REPEAT
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T $::GL_REPEAT
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER $::GL_NEAREST
glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER $::GL_NEAREST
glTexImage2D GL_TEXTURE_2D 0 $::GL_RGBA \
$::checkImageWidth $::checkImageHeight \
0 GL_RGBA GL_UNSIGNED_BYTE $::checkImage
}
proc DisplayCallback { toglwin } {
glClear [expr $::GL_COLOR_BUFFER_BIT | $::GL_DEPTH_BUFFER_BIT]
# Viewport command is not really needed, but has been inserted for
# Mac OSX. Presentation framework (Tk) does not send a reshape event,
# when switching from one demo to another.
glViewport 0 0 [$toglwin width] [$toglwin height]
glEnable GL_TEXTURE_2D
glTexEnvf GL_TEXTURE_ENV GL_TEXTURE_ENV_MODE $::GL_DECAL
glBindTexture GL_TEXTURE_2D [$::texName get 0]
glBegin GL_QUADS
glTexCoord2f 0.0 0.0 ; glVertex3f -2.0 -1.0 0.0
glTexCoord2f 0.0 1.0 ; glVertex3f -2.0 1.0 0.0
glTexCoord2f 1.0 1.0 ; glVertex3f 0.0 1.0 0.0
glTexCoord2f 1.0 0.0 ; glVertex3f 0.0 -1.0 0.0
glTexCoord2f 0.0 0.0 ; glVertex3f 1.0 -1.0 0.0
glTexCoord2f 0.0 1.0 ; glVertex3f 1.0 1.0 0.0
glTexCoord2f 1.0 1.0 ; glVertex3f 2.41421 1.0 -1.41421
glTexCoord2f 1.0 0.0 ; glVertex3f 2.41421 -1.0 -1.41421
glEnd
glFlush
glDisable GL_TEXTURE_2D
$toglwin swapbuffers
}
proc ReshapeCallback { toglwin { w -1 } { h -1 } } {
set w [$toglwin width]
set h [$toglwin height]
glViewport 0 0 $w $h
glMatrixMode GL_PROJECTION
glLoadIdentity
gluPerspective 60.0 [expr double($w)/double($h)] 1.0 30.0
glMatrixMode GL_MODELVIEW
glLoadIdentity
glTranslatef 0.0 0.0 -3.6
}
proc SetSubTexture {} {
glBindTexture GL_TEXTURE_2D [$::texName get 0]
glTexSubImage2D GL_TEXTURE_2D 0 12 44 $::subImageWidth \
$::subImageHeight GL_RGBA GL_UNSIGNED_BYTE $::subImage
.fr.toglwin postredisplay
}
proc Reset {} {
glBindTexture GL_TEXTURE_2D [$::texName get 0]
glTexImage2D GL_TEXTURE_2D 0 $::GL_RGBA \
$::checkImageWidth $::checkImageHeight \
0 GL_RGBA GL_UNSIGNED_BYTE $::checkImage
.fr.toglwin postredisplay
}
frame .fr
pack .fr -expand 1 -fill both
togl .fr.toglwin -width 400 -height 400 -double true -depth true \
-createcommand CreateCallback \
-reshapecommand ReshapeCallback \
-displaycommand DisplayCallback
listbox .fr.usage -font $::listFont -height 3
label .fr.info
grid .fr.toglwin -row 0 -column 0 -sticky news
grid .fr.usage -row 1 -column 0 -sticky news
grid .fr.info -row 2 -column 0 -sticky news
grid rowconfigure .fr 0 -weight 1
grid columnconfigure .fr 0 -weight 1
wm title . "Tcl3D demo: OpenGL Red Book example texsub"
bind . <Key-s> "SetSubTexture"
bind . <Key-r> "Reset"
bind . <Key-Escape> "exit"
.fr.usage insert end "Key-s Set Subtexture"
.fr.usage insert end "Key-r Reset"
.fr.usage insert end "Key-Escape Exit"
.fr.usage configure -state disabled
PrintInfo [tcl3dOglGetInfoString]
|
