PDA

View Full Version : Cylinder from Quads



matthew
04-04-2007, 17:46
This Programme will display a Cylinder on your Screen and Rotate it. :)




' Cylinder in thinBASIC using TBGL...
'

uses "TBGL" ' thinBASIC OpenGL Library
uses "MATH" ' Used for Trigonometry

' Create, Show Our Window and Reset Status of all Keys...
'

Dim hWnd As dword = tbgl_createwindow("Cylinder - Press 'Esc' to Quit")
tbgl_showwindow

tbgl_getasynckeystate(-1)

' Dimension and Initialize any Variables...
'

dim Xrot, Yrot, Zrot as single ' X,Y and Z Rotation Variables
dim CylinderRadius, CylinderHeight as integer ' Radius and Height of Cylinder
dim CylinderShade as single ' Level of Brightness
dim i as integer ' Polygon Drawing Loop Variable

' Main Programme Loop
'

while tbgl_iswindow(hWnd)

tbgl_clearframe ' Clear Screen
tbgl_resetmatrix ' Clear the Current Matrix
tbgl_camera 0, 0, 1, 0, 0, 0 ' Set Camera Position
tbgl_translate 0.0, 0.0, -5.0 ' Move Object into Screen by 5.0

tbgl_Rotate Xrot, 1.0, 0.0, 0.0 ' Rotation around the X-Axis
tbgl_Rotate Yrot, 0.0, 1.0, 0.0 ' Rotation around the Y-Axis
tbgl_Rotate Zrot, 0.0, 0.0, 1.0 ' Rotation around the Z-Axis

CylinderRadius = 1
CylinderHeight = 4

tbgl_color 128, 128, 128 ' Cylinder Top, Grey.

tbgl_beginpoly %gl_polygon

for i = 0 to 36

tbgl_vertex CylinderRadius * cos(degtorad(i*10)), (CylinderHeight / 2) * -1, CylinderRadius * Sin(degtorad(i*10))

next

tbgl_endpoly

tbgl_color 255, 255, 255 ' Set Colour to White

tbgl_beginpoly %gl_quad_strip

' Draw Actual Cylinder in 10 Degree Steps...
'

for i = 0 to 36

' Shade Surface of Cylinder...
'

CylinderShade = abs(13.25 * (18-i))
tbgl_color CylinderShade, CylinderShade, CylinderShade

' Draw Top Vertex...
'

tbgl_vertex CylinderRadius * Cos(degtorad(i*10)), (CylinderHeight / 2) * -1, CylinderRadius * Sin(degtorad(i*10))

' Draw Bottom Vertex...
'

tbgl_vertex CylinderRadius * cos(degtorad(i*10)), (CylinderHeight / 2), CylinderRadius * Sin(degtorad(i*10))

next

tbgl_endpoly

' Draw Bottom of Cylinder...
'

tbgl_color 128, 128, 128
tbgl_beginpoly %gl_polygon

for i = 0 to 36

tbgl_vertex CylinderRadius * cos(degtorad((36-i))*10), CylinderHeight / 2, CylinderRadius * Sin(degtorad((36-i)*10))

next

tbgl_endpoly

tbgl_drawframe ' Swap the Drawing Buffers

Xrot = Xrot + 0.1 ' X axis rotation
Yrot = Yrot + 0.2 ' Y axis rotation
Zrot = Zrot + 0.05 ' Z axis rotation

if tbgl_getwindowkeystate(hWnd, %vk_escape) then exit while

wend

tbgl_destroywindow ' Closes OpenGL Window

Petr Schreiber
04-04-2007, 20:34
Hi Matthew,

Good to see you after time :)
I was nervous brother-in-OpenGL left the forums ;D

Nice demo!

Bye,
Petr

Michael Hartlef
04-04-2007, 20:38
Thanks for sharing Matthew. :)

matthew
04-04-2007, 23:18
@Mike - Thank's Mike. :)



Good to see you after time :)
I was nervous brother-in-OpenGL left the forums ;D


I visit the thinBASIC Forum everyday but I don't bother posting if I haven't written a Programme. :D

Michael Clease
02-06-2007, 10:43
tbgl_camera 0, 0, 1, 0, 0, 0 ' Set Camera Position
tbgl_resetmatrix ' Clear the Current Matrix


Shouldnt that be.



tbgl_resetmatrix ' Clear the Current Matrix
tbgl_camera 0, 0, 1, 0, 0, 0 ' Set Camera Position


You reset the matrix then do your translations, rotation and camera stuff.

matthew
02-06-2007, 16:55
^^
Yeah, when I first started using thinBASIC I used to make that mistake. :P

Source Updated. ;)

Petr Schreiber
02-06-2007, 20:04
And to make it even more complicated :D,

just:


tbgl_ClearFrame
tbgl_Camera 0,0,1,0,0,0


is enough! TBGL_ClearFrame handles the matrix reset automatically.
But this is just mini problem, I still like the sample :)


Thanks,
Petr