Older Version Newer Version

bluatigro bluatigro Oct 16, 2013

this is what i made of it

my whitespaces always disappeer when posting here

[[code format="lb"]]
nomainwin

dim pntx(256),pnty(256),pntz(256)

'========================================

' color objects

'========================================

global black , red , green , yellow

global blue , magenta , cyan , white

global gray , pink , purple , orange

black = rgba( 000 , 000 , 000 , 255 )

red = rgba( 255 , 000 , 000 , 255 )

green = rgba( 000 , 255 , 000 , 255 )

yellow = rgba( 255 , 255 , 000 , 255 )

blue = rgba( 000 , 000 , 255 , 255 )

magenta = rgba( 255 , 000 , 255 , 255 )

cyan = rgba( 000 , 255 , 255 , 255 )

white = rgba( 255 , 255 , 255 , 255 )

gray = rgba( 127 , 127 , 127 , 255 )

pink = rgba( 255 , 127 , 127 , 255 )

purple = rgba( 127 , 000 , 127 , 255 )

orange = rgba( 255 , 127 , 000 , 255 )

'===========================================

' opengl consts

'===========================================

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384

global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256

'===========================================

' primatifs

' for starting drawing objects

'===========================================

global GL.TRIANGLES : GL.TRIANGLES = 4

global GL.QUADS : GL.QUADS = 7

'===========================================

' light

'===========================================

Global GL.LIGHTING: GL.LIGHTING = 2896

Global GL.LIGHT0: GL.LIGHT0 = 16384

Global GL.AMBIENT: GL.AMBIENT = 4608

Global GL.DIFFUSE: GL.DIFFUSE = 4609

Global GL.SPECULAR: GL.SPECULAR = 4610

Global GL.SHININESS: GL.SHININESS = 5633

Global GL.EMISSION: GL.EMISSION = 5632

Global GL.POSITION: GL.POSITION = 4611

Global GL.FRONT.AND.BACK: GL.FRONT.AND.BACK=1032

'===========================================

' Fog

'===========================================

Global GL.FOG: GL.FOG = 291

Global GL.FOG.MODE: GL.FOG.MODE = 2917

Global GL.FOG.DENSITY: GL.FOG.DENSITY = 2914

Global GL.FOG.COLOR: GL.FOG.COLOR = 2918

Global GL.FOG.INDEX: GL.FOG.INDEX = 2913

Global GL.FOG.START: GL.FOG.START = 2915

Global GL.FOG.END: GL.FOG.END = 2916

Global GL.LINEAR: GL.LINEAR = 9729

Global GL.EXP: GL.EXP = 2048

Global GL.EXP2: GL.EXP2 = 2049

''create a fullscreen window

WindowWidth = DisplayWidth

WindowHeight = DisplayHeight

global MainH , MainDC ''wil be filled by code

global angle

''load the opengl libery

open "opengl32.dll" for dll as #gl

open "Triangle 1.3" for window as #m

#m , "trapclose [quit]"

MainH = hwnd( #m )

''start opengl

call openglInit

timer 40 , [tmr]

wait

[tmr]

''clear opengl screen

calldll #gl,"glClear" _

, GL.COLOR.BUFFER.BIT as long _

, ret as long

''reset drawingmatrix

call glLoadIdentity

''use a rotated drawingmatrix

call glRotate angle , 0 , 0 , 1

''drawingobject to screen

call colortriangle

''drawing ocurs at backbuffer

''swaping backbuffer and screenbuffer

''so drawed object gets on screen

calldll #gdi32,"SwapBuffers" _ , MainDC as ulong _ , ret as long

angle = angle + 1

wait

function rgba( r , g , b , a )

''create a color object

r = r and 255

g = g and 255

b = b and 255

a = a and 255

rgba = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3

end function

function colorR( clr )

''read red part of color object

colorR = int( clr and 255 ) / 256

end function

function colorG( clr )

''read green part of color object

colorG = ( int( clr / 256 ) and 255 ) / 256

end function

function colorB( clr )

''read blue part of color object

colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256

end function

function colorA( clr )

''read alpha / tranparent part of color object

colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256

end function

function lenght( x1 , y1 , z1 , x2 , y2 , z2 )

''calculate distanse between 2 points

lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ) + 1e-10

end function

function nx( x1 , y1 , z1 , x2 , y2 , z2 )

''calculate x part of crossproduct of 2 points

x = y1 * z2 - y2 * z1

nx = x / lenght(x1,y1,z1,x2,y2,z2)

end function

function ny( x1 , y1 , z1 , x2 , y2 , z2 )

''calculate y part of crossproduct of 2 points

y = z1 * x2 - z2 * x1

ny = y / lenght(x1,y1,z1,x2,y2,z2)

end function

function nz( x1 , y1 , z1 , x2 , y2 , z2 )

''calculate z part of crossproduct of 2 points

z = x1 * y2 - x2 * y1

nz = z / lenght(x1,y1,z1,x2,y2,z2)

end function

sub colortriangle

''draw the triangle from the example

''start drawing object as triangles

call glBegin GL.TRIANGLES

''add a red point to the dawing object

call setcolor red

call glVertex 1 , 1 , 0

''add a green point to the drawing object

call setcolor blue

call glVertex 0 , -1 , 0

''add a blue point to the drawing object

call setcolor green

call glVertex -1 , 0 , 0

call glEnd

end sub

sub point no , x , y , z

''create a point in the swarm

if no < 0 or no > 256 then exit sub

pntx( no ) = x

pnty( no ) = y

pntz( no ) = z

end sub

sub triangle p1 , p2 , p3

''create a triangle from 3 point outof the swarm

if p1 < 0 or p1 > 256 then exit sub

if p2 < 0 or p2 > 256 then exit sub

if p3 < 0 or p3 > 256 then exit sub

call tri pntx( p1 ) , pnty( p1 ) , pntz( p1 ) _

, pntx( p2 ) , pnty( p2 ) , pntz( p2 ) _

, pntx( p3 ) , pnty( p3 ) , pntz( p3 )

end sub

sub tri x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3

''create a triangle from coordinates

''first calculate the normal

x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )

y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )

z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )

''put a triangle in the screen

call glBegin GL.TRIANGLES

calldll #gl , "glNormal3f" _

, x as double _

, y as double _

, z as double _

, ret as long

call glVertex x1 , y1 , z1

call glVertex x2 , y2 , z2

call glVertex x3 , y3 , z3

call glEndend sub

sub quad p1 , p2 , p3 , p4

''create a quad from points in the swarm

x1 = pntx( p1 )

y1 = pnty( p1 )

z1 = pntz( p1 )

x2 = pntx( p2 )

y2 = pnty( p2 )

z2 = pntz( p2 )

x3 = pntx( p3 )

y3 = pnty( p3 )

z3 = pntz( p3 )

x4 = pntx( p4 )

y4 = pnty( p4 )

z4 = pntz( p4 )

''calculate the normal of the quad

x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )

y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )

z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )

''put a quadangle in the screen

call glBegin GL.QUADS

calldll #gl , "glNormal3f" _

, x as double _

, y as double _

, z as double _

, ret as long

call glVertex x1 , y1 , z1

call glVertex x2 , y2 , z2

call glVertex x3 , y3 , z3

call glVertex x4 , y4 , z4

call glEnd

end sub

sub glLoadIdentity

''reset the drawmatrix

calldll #gl , "glLoadIdentity" _ , ret as long

end sub

sub glEnd

''end drawing object

calldll #gl , "glEnd" _ , ret as void

end sub

sub glEnable i

calldll #gl , "glEnable" _ , i as long _ , ret as long

end sub

sub glBegin i

''begin drawing object

calldll #gl , "glBegin" _ , i as long _ , ret as long

end sub

sub glScale x , y , z

''change drawingmatrix to shrink or enlarge drawing

calldll #gl , "glScaled" _

, x as double _

, y as double _

, z as double _

, ret as long

end sub

sub glTranslate x , y , z

''change drawingmatrix to move drawing

calldll #gl , "glTranslated" _

, x as double _

, y as double _

, z as double _

, ret as long

end sub

sub glRotate a , x , y , z

''change drawingmatrix to rotate drawing

''a is angle in degrees

calldll #gl , "glRotated" _

, a as double _

, x as double _

, y as double _

, z as double _

, ret as long

end sub

sub glVertex x , y , z

''add a point to the drawing object

calldll #gl , "glVertex3d" _

, x as double _

, y as double _

, z as double _

, ret as long

end sub

sub setcolor clr

''set the drawing color of folowing draw object part[s]

r = colorR( clr )

g = colorG( clr )

b = colorB( clr )

calldll #gl , "glColor3d" _

, r as double _

, g as double _

, b as double _

, ret as long

end sub

sub openglInit

''start openGL and screen

struct PFD _

, Size as word _

, Version as word _

, Flags as long _

, PixelType as char[1] _

, ColorBits as char[1] _

, RedBits as char[1] _

, RedShift as char[1] _

, GreenBits as char[1] _

, GreenShift as char[1] _

, BlueBits as char[1] _

, BlueShift as char[1] _

, AlphaBits as char[1] _

, AlphaShift as char[1] _

, AccumBits as char[1] _

, AccumRedBits as char[1] _

, AccumGreenBits as char[1] _

, AccumBlueBits as char[1] _

, AccumAlphaBits as char[1] _

, DepthBits as char[1] _

, StencilBits as char[1] _

, AuxBuffers as char[1] _

, LayerType as char[1] _

, Reserved as char[1] _

, LayerMask as long _

, VisibleMask as long _

, DamageMask as long

''i have no idea on what wil happen if these are changed

PFD.Version.struct = 1

PFD.ColorBits.struct = 24

PFD.DepthBits.struct = 16

PFD.Size.struct=len( PFD.struct )

PFD.Flags.struct = 37

calldll #user32,"GetDC", MainH as ulong, MainDC as ulong

calldll #gdi32,"ChoosePixelFormat", MainDC as ulong, PFD as struct, ret as long

calldll #gdi32, "SetPixelFormat", MainDC as ulong, ret as long, PFD as struct, t as long

calldll #gl,"wglCreateContext", MainDC as ulong, GLContext as ulong

calldll #gl,"wglMakeCurrent", MainDC as ulong, GLContext as ulong, ret as long

end sub

[quit]

calldll #gl,"wglMakeCurrent", 0 as ulong, 0 as ulong, ret as long

calldll #gl,"wglDeleteContext", GLContext as ulong, ret as long

calldll #user32, "ReleaseDC", MainH as ulong, MainDC as ulong,ret as long

close #m

close #gl

end
[[code]]