You are on page 1of 10

Ring Documentation, Release 1.5.

glVertex3f(0.5, 0.0, 0.0)


glVertex3f(0.5, 0.5, 0.0)
glVertex3f(0.0, 0.5, 0.0)
glEnd()
glColor3f(255,0,0)
glBegin(GL_POLYGON)
glVertex3f(0.0, 0.0, 0.0)
glVertex3f(0.5, 0.0, 0.0)
glVertex3f(-0.5,- 1, 0.0)
glVertex3f(0.0, -1, 0.0)
glEnd()
glColor3f(0,0,255)
glBegin(GL_POLYGON)
glVertex3f(0.0, 0.0, 0.0)
glVertex3f(-0.5, 0.0, 0.0)
glVertex3f(-0.5,- 0.5, 0.0)
glVertex3f(0.0, -0.5, 0.0)
glEnd()

glFlush()

Screen Shot

54.5. Drawing using RingOpenGL 505


Ring Documentation, Release 1.5.4

54.6 The First Triangle

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

func main
glutInit()
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowSize(320,320)
glutInitWindowPosition(100, 10)
glutCreateWindow("RingFreeGLUT - Test 3")
glutDisplayFunc(:renderScene)
glutMainLoop()

func renderScene

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glBegin(GL_TRIANGLES)
glVertex3f(-0.5,-0.5,0.0)
glVertex3f(0.5,0.0,0.0)
glVertex3f(0.0,0.5,0.0)
glEnd()

glutSwapBuffers()

Screen Shot

54.6. The First Triangle 506


Ring Documentation, Release 1.5.4

54.7 Window Resize Event

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

func main

// init GLUT and create window


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

glutDisplayFunc(:renderScene)
glutReshapeFunc(:changeSize)

glutMainLoop()

func renderScene

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

glBegin(GL_TRIANGLES)
glVertex3f(-2,-2,-5.0)
glVertex3f(2,0.0,-5.0)
glVertex3f(0.0,2,-5.0)
glEnd()

glutSwapBuffers()

func changesize

h = glutEventHeight()
w = glutEventWidth()

// 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,ratio,1,100)

54.7. Window Resize Event 507


Ring Documentation, Release 1.5.4

// Get Back to the Modelview


glMatrixMode(GL_MODELVIEW)

54.8 Triangle Rotation

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

angle = 0

func main

// init GLUT and create window


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

glutDisplayFunc(:renderScene)
glutReshapeFunc(:changeSize)
glutIdleFunc(:renderScene)

glutMainLoop()

func renderScene

// Clear Color and Depth Buffers


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

// Reset transformations
glLoadIdentity()
// Set the camera
gluLookAt( 0.0, 0.0, 10.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0)

glRotatef(angle, 0.0, 1.0, 0.0)

glBegin(GL_TRIANGLES)
glVertex3f(-2.0,-2.0, 0.0)
glVertex3f( 2.0, 0.0, 0.0)
glVertex3f( 0.0, 2.0, 0.0)
glEnd()

angle+=0.1

glutSwapBuffers();

func changesize

h = glutEventHeight()
w = glutEventWidth()

54.8. Triangle Rotation 508


Ring Documentation, Release 1.5.4

// 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,ratio,1,100)

// Get Back to the Modelview


glMatrixMode(GL_MODELVIEW)

Screen Shot

54.9 Keyboard Events and Colors

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

54.9. Keyboard Events and Colors 509


Ring Documentation, Release 1.5.4

angle = 0

red=1.0
blue=1.0
green=1.0

func main

// init GLUT and create window


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

glutDisplayFunc(:renderScene)
glutReshapeFunc(:changeSize)
glutIdleFunc(:renderScene)

// here are the new entries


glutKeyboardFunc(:processNormalKeys)
glutSpecialFunc(:processSpecialKeys)

glutMainLoop()

func renderScene

// Clear Color and Depth Buffers


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

// Reset transformations
glLoadIdentity()
// Set the camera
gluLookAt( 0.0, 0.0, 10.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0)

glRotatef(angle, 0.0, 1.0, 0.0)

glColor3f(red,green,blue);

glBegin(GL_TRIANGLES)
glVertex3f(-2.0,-2.0, 0.0)
glVertex3f( 2.0, 0.0, 0.0)
glVertex3f( 0.0, 2.0, 0.0)
glEnd()

angle+=0.1

glutSwapBuffers();

func changesize

h = glutEventHeight()
w = glutEventWidth()

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

54.9. Keyboard Events and Colors 510


Ring Documentation, Release 1.5.4

// (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,ratio,1,100)

// Get Back to the Modelview


glMatrixMode(GL_MODELVIEW)

func processNormalKeys
key = GLUTEventKey()
if key = 27
shutdown()
ok

func processSpecialKeys

key = GLUTEventKey()

switch key
on GLUT_KEY_F1
red = 1.0
green = 0.0
blue = 0.0
on GLUT_KEY_F2
red = 0.0
green = 1.0
blue = 0.0
on GLUT_KEY_F3
red = 0.0
green = 0.0
blue = 1.0
off

Screen Shot

54.9. Keyboard Events and Colors 511


Ring Documentation, Release 1.5.4

54.10 The Camera

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

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)

54.10. The Camera 512


Ring Documentation, Release 1.5.4

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)
glutSolidCone(0.08,0.5,10,2)

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 processNormalKeys
key = glutEventKey()

if key = 27
shutdown()
ok

func renderScene

// Clear Color and Depth Buffers

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

// Reset transformations
glLoadIdentity()
// Set the camera

54.10. The Camera 513


Ring Documentation, Release 1.5.4

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 processSpecialKeys

key = glutEventKey()

fraction = 0.1

switch key
on GLUT_KEY_LEFT
angle -= 0.01
lx = sin(angle)
lz = -cos(angle)
on GLUT_KEY_RIGHT
angle += 0.01
lx = sin(angle)
lz = -cos(angle)
on GLUT_KEY_UP
x += lx * fraction
z += lz * fraction
on GLUT_KEY_DOWN
x -= lx * fraction
z -= lz * fraction
off

func main

// init GLUT and create window

glutInit()
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)

54.10. The Camera 514

You might also like