Você está na página 1de 10

Ring Documentation, Release 1.

deltaMove = 0

func changeSize
w = glutEventWidth()
h = glutEventHeight()

// Prevent a divide by zero, when window is too short


// (you cant make a window of zero width).
if h = 0
h = 1
ok

ratio = w * 1.0 / h

// Use the Projection Matrix


glMatrixMode(GL_PROJECTION)

// Reset Matrix
glLoadIdentity()

// Set the viewport to be the entire window


glViewport(0, 0, w, h)

// Set the correct perspective.


gluPerspective(45.0, ratio, 0.1, 100.0)

// Get Back to the Modelview


glMatrixMode(GL_MODELVIEW)

func drawSnowMan

glColor3f(1.0, 1.0, 1.0)

// Draw Body

glTranslatef(0.0 ,0.75, 0.0)


glutSolidSphere(0.75,20,20)

// Draw Head
glTranslatef(0.0, 1.0, 0.0)
glutSolidSphere(0.25,20,20)

// Draw Eyes
glPushMatrix()
glColor3f(0.0,0.0,0.0)
glTranslatef(0.05, 0.10, 0.18)
glutSolidSphere(0.05,10,10)
glTranslatef(-0.1, 0.0, 0.0)
glutSolidSphere(0.05,10,10)
glPopMatrix()

// Draw Nose
glColor3f(1.0, 0.5 , 0.5)
glRotatef(0.0,1.0, 0.0, 0.0)
glutSolidCone(0.08,0.5,10,2)

55.10. The Camera 495


Ring Documentation, Release 1.5

func computePos deltaMove

x += deltaMove * lx * 0.1
z += deltaMove * lz * 0.1

func computeDir deltaAngle

angle += deltaAngle
lx = sin(angle)
lz = -cos(angle)

func renderScene

if deltaMove
computePos(deltaMove)
ok

if deltaAngle
computeDir(deltaAngle)
ok

// Clear Color and Depth Buffers


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

// Reset transformations
glLoadIdentity()
// Set the camera
gluLookAt( x, 1.0, z,
x+lx, 1.0, z+lz,
0.0, 1.0, 0.0)

// Draw ground

glColor3f(0.9, 0.9, 0.9)


glBegin(GL_QUADS)
glVertex3f(-100.0, 0.0, -100.0)
glVertex3f(-100.0, 0.0, 100.0)
glVertex3f( 100.0, 0.0, 100.0)
glVertex3f( 100.0, 0.0, -100.0)
glEnd()

// Draw 36 SnowMen

for i = -3 to 2
for j=-3 to 2
glPushMatrix()
glTranslatef(i*10.0,0,j * 10.0)
drawSnowMan()
glPopMatrix()
next
next
glutSwapBuffers()

func pressKey
key = glutEventKey()
xx = glutEventX()

55.10. The Camera 496


Ring Documentation, Release 1.5

yy = glutEventY()

switch key
on GLUT_KEY_LEFT
deltaAngle = -0.01
on GLUT_KEY_RIGHT
deltaAngle = 0.01
on GLUT_KEY_UP
deltaMove = 0.5
on GLUT_KEY_DOWN
deltaMove = -0.5
off

func releaseKey

key = glutEventKey()

switch key
on GLUT_KEY_LEFT
deltaAngle = 0.0
on GLUT_KEY_RIGHT
deltaAngle = 0.0
on GLUT_KEY_UP
deltaMove = 0
on GLUT_KEY_DOWN
deltaMove = 0
off

func main

// init GLUT and create window


glutInit()
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowPosition(100,100)
glutInitWindowSize(320,320)
glutCreateWindow("RingFreeGLUT - Test 8")

// register callbacks
glutDisplayFunc(:renderScene)
glutReshapeFunc(:changeSize)
glutIdleFunc(:renderScene)

glutSpecialFunc(:pressKey)

// here are the new entries


glutIgnoreKeyRepeat(1)
glutSpecialUpFunc(:releaseKey)

// OpenGL init
glEnable(GL_DEPTH_TEST)

// enter GLUT event processing cycle


glutMainLoop()

55.10. The Camera 497


Ring Documentation, Release 1.5

55.11 Mouse Events

Example:
load "freeglut.ring"
load "opengl21lib.ring"

// angle of rotation for the camera direction


angle = 0.0

// actual vector representing the camera's direction


lx=0.0 lz=-1.0

// XZ position of the camera


x=0.0 z=5.0

// the key states. These variables will be zero


//when no key is being presses
deltaAngle = 0.0
deltaMove = 0.0
xOrigin = -1

func changeSize
w = glutEventWidth()
h = glutEventHeight()

// Prevent a divide by zero, when window is too short


// (you cant make a window of zero width).
if h = 0
h = 1
ok

ratio = w * 1.0 / h

// Use the Projection Matrix


glMatrixMode(GL_PROJECTION)

// Reset Matrix
glLoadIdentity()

// Set the viewport to be the entire window


glViewport(0, 0, w, h)

// Set the correct perspective.


gluPerspective(45.0, ratio, 0.1, 100.0)

// Get Back to the Modelview


glMatrixMode(GL_MODELVIEW)

func drawSnowMan

glColor3f(1.0, 1.0, 1.0)

// Draw Body
glTranslatef(0.0 ,0.75, 0.0)
glutSolidSphere(0.75,20,20)

55.11. Mouse Events 498


Ring Documentation, Release 1.5

// Draw Head
glTranslatef(0.0, 1.0, 0.0)
glutSolidSphere(0.25,20,20)

// Draw Eyes
glPushMatrix()
glColor3f(0.0,0.0,0.0)
glTranslatef(0.05, 0.10, 0.18)
glutSolidSphere(0.05,10,10)
glTranslatef(-0.1, 0.0, 0.0)
glutSolidSphere(0.05,10,10)
glPopMatrix()

// Draw Nose
glColor3f(1.0, 0.5 , 0.5)
glRotatef(0.0,1.0, 0.0, 0.0)
glutSolidCone(0.08,0.5,10,2)

func computePos deltaMove

x += deltaMove * lx * 0.1
z += deltaMove * lz * 0.1

func renderScene

if deltaMove
computePos(deltaMove)
ok

// Clear Color and Depth Buffers


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

// Reset transformations
glLoadIdentity()
// Set the camera
gluLookAt( x, 1.0, z,
x+lx, 1.0, z+lz,
0.0, 1.0, 0.0)

// Draw ground

glColor3f(0.9, 0.9, 0.9)


glBegin(GL_QUADS)
glVertex3f(-100.0, 0.0, -100.0)
glVertex3f(-100.0, 0.0, 100.0)
glVertex3f( 100.0, 0.0, 100.0)
glVertex3f( 100.0, 0.0, -100.0)
glEnd()

// Draw 36 SnowMen

for i = -3 to 2
for j=-3 to 2
glPushMatrix()
glTranslatef(i*10.0,0,j * 10.0)
drawSnowMan()
glPopMatrix()

55.11. Mouse Events 499


Ring Documentation, Release 1.5

next
next
glutSwapBuffers()

func processNormalKeys

key = glutEventKey()

if key = 27
shutdown()
ok

func pressKey
key = glutEventKey()

switch key
on GLUT_KEY_UP
deltaMove = 0.5
on GLUT_KEY_DOWN
deltaMove = -0.5
off

func releaseKey
key = glutEventKey()
switch key
on GLUT_KEY_UP
deltaMove = 0
on GLUT_KEY_DOWN
deltaMove = 0
off

func mouseMove
xx = glutEventX()
yy = glutEventY()
// this will only be true when the left button is down
if xOrigin >= 0

// update deltaAngle
deltaAngle = (xx - xOrigin) * 0.001

// update camera's direction


lx = sin(angle + deltaAngle)
lz = -cos(angle + deltaAngle)
ok

func mouseButton

button = glutEventButton()
state = glutEventState()
xx = glutEventX()
yy = glutEventY()

// only start motion if the left button is pressed

55.11. Mouse Events 500


Ring Documentation, Release 1.5

if button = GLUT_LEFT_BUTTON
// when the button is released
if state = GLUT_UP
angle += deltaAngle
xOrigin = -1
else
// state = GLUT_DOWN
xOrigin = xx
ok
fflush(stdout)
ok

func main

// init GLUT and create window


glutInit()
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowPosition(100,100)
glutInitWindowSize(320,320)
glutCreateWindow("RingFreeGLUT - Test 9")

// register callbacks
glutDisplayFunc(:renderScene)
glutReshapeFunc(:changeSize)
glutIdleFunc(:renderScene)

glutIgnoreKeyRepeat(1)
glutKeyboardFunc(:processNormalKeys)
glutSpecialFunc(:pressKey)
glutSpecialUpFunc(:releaseKey)

// here are the two new functions


glutMouseFunc(:mouseButton)
glutMotionFunc(:mouseMove)

// OpenGL init
glEnable(GL_DEPTH_TEST)

// enter GLUT event processing cycle


glutMainLoop()

55.12 Menu Events

Example:
load "freeglut.ring"
load "opengl21lib.ring"

// angle of rotation for the camera direction


angle = 0.0

// actual vector representing the camera's direction


lx=0.0 lz=-1.0

// XZ position of the camera

55.12. Menu Events 501


Ring Documentation, Release 1.5

x=0.0 z=5.0

// the key states. These variables will be zero


//when no key is being presses
deltaAngle = 0.0
deltaMove = 0
xOrigin = -1

// Constant definitions for Menus

// for RingFreeGLUT - We must have different ID for each menu item


C_RED = 1
C_GREEN = 2
C_BLUE = 3
C_ORANGE = 4

C_FILL = 5
C_LINE = 6

C_SHRINK = 7
C_NORMAL = 8

// Pop up menu identifiers


fillMenu= 0
shrinkMenu= 0
mainMenu=0
colorMenu=0

// color for the nose


red = 1.0 blue=0.5 green=0.5

// scale of snowman
scale = 1.0

// menu status
menuFlag = 0

func changeSize
w = glutEventWidth()
h = glutEventHeight()

// Prevent a divide by zero, when window is too short


// (you cant make a window of zero width).
if h = 0
h = 1
ok

ratio = w * 1.0 / h

// Use the Projection Matrix


glMatrixMode(GL_PROJECTION)

// Reset Matrix
glLoadIdentity()

// Set the viewport to be the entire window


glViewport(0, 0, w, h)

55.12. Menu Events 502


Ring Documentation, Release 1.5

// Set the correct perspective.


gluPerspective(45.0, ratio, 0.1, 100.0)

// Get Back to the Modelview


glMatrixMode(GL_MODELVIEW)

func drawSnowMan

glScalef(scale, scale, scale)


glColor3f(1.0, 1.0, 1.0)

// Draw Body
glTranslatef(0.0 ,0.75, 0.0)
glutSolidSphere(0.75,20,20)

// Draw Head
glTranslatef(0.0, 1.0, 0.0)
glutSolidSphere(0.25,20,20)

// Draw Eyes
glPushMatrix()
glColor3f(0.0,0.0,0.0)
glTranslatef(0.05, 0.10, 0.18)
glutSolidSphere(0.05,10,10)
glTranslatef(-0.1, 0.0, 0.0)
glutSolidSphere(0.05,10,10)
glPopMatrix()

// Draw Nose
glColor3f(red, green, blue)
glRotatef(0.0,1.0, 0.0, 0.0)
glutSolidCone(0.08,0.5,10,2)

glColor3f(1.0, 1.0, 1.0)

func computePos deltaMove

x += deltaMove * lx * 0.1
z += deltaMove * lz * 0.1

func renderScene

if deltaMove
computePos(deltaMove)
ok

// Clear Color and Depth Buffers


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

// Reset transformations
glLoadIdentity()
// Set the camera
gluLookAt( x, 1.0, z,
x+lx, 1.0, z+lz,
0.0, 1.0, 0.0)

55.12. Menu Events 503


Ring Documentation, Release 1.5

// Draw ground

glColor3f(0.9, 0.9, 0.9)


glBegin(GL_QUADS)
glVertex3f(-100.0, 0.0, -100.0)
glVertex3f(-100.0, 0.0, 100.0)
glVertex3f( 100.0, 0.0, 100.0)
glVertex3f( 100.0, 0.0, -100.0)
glEnd()

// Draw 36 SnowMen

for i = -3 to 2
for j = -3 to 2
glPushMatrix()
glTranslatef(i*10.0, 0.0, j * 10.0)
drawSnowMan()
glPopMatrix()
next
next
glutSwapBuffers()

// -----------------------------------
// KEYBOARD
// -----------------------------------

func processNormalKeys
key = glutEventKey()
xx = glutEventX()
yy = glutEventY()

glutSetMenu(mainMenu)
switch key
on 27
glutDestroyMenu(mainMenu)
glutDestroyMenu(fillMenu)
glutDestroyMenu(colorMenu)
glutDestroyMenu(shrinkMenu)
shutdown()

on 's'
if not menuFlag
glutChangeToSubMenu(2,"Shrink",shrinkMenu)
ok
on 'c'
if not menuFlag
glutChangeToSubMenu(2,"Color",colorMenu)
ok
off
if key = 27
shutdown()
ok

func pressKey

key = glutEventKey()

55.12. Menu Events 504

Você também pode gostar