Draw a sphere: Difference between revisions

m (Uploaded Rust output image)
Line 2,630:
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_window = 1
include "Tlbx agl.incl"
begin enum output 1
include "Tlbx glut.incl"
_sphereImageView
end enum
 
void local fn BuildWindow
output file "Rotating Sphere"
CGRect r = fn CGRectMake( 0, 0, 400, 400 )
window _window, @"Rosetta Code Sphere", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
r = fn CGRectMake( 20, 20, 360, 360 )
imageview _sphereImageView, YES, , r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFrameNone, _window
end fn
 
local fn SphereImageWithCIFilter( imageDimension as NSUInteger, sphereColor as ColorRef, backgroundColor as ColorRef, radiusBlur as float, radiusSphere as float ) as ImageRef
local fn SphereDraw
CIVectorRef civ = fn CIVectorWithXY( imageDimension/2, imageDimension/2 )
'~'1
CIFilterRef filter = fn CIFilterWithName( @"CIRadialGradient" )
begin globals
CIFilterSetDefaults( filter )
dim as double  sRotation // 'static' var
ObjectSetValueForKey( filter, civ, @"inputCenter" )
end globals
ObjectSetValueForKey( filter, fn NumberWithFloat( radiusBlur ), @"inputRadius0" )
 
ObjectSetValueForKey( filter, fn NumberWithFloat( radiusSphere ), @"inputRadius1" )
// Speed of rotation
ObjectSetValueForKey( filter, fn CIColorWithCGColor( fn ColorCGColor( sphereColor ) ), @"inputColor0" )
sRotation += 2.9
ObjectSetValueForKey( filter, fn CIColorWithCGColor( fn ColorCGColor( backgroundColor ) ), @"inputColor1" )
glMatrixMode( _GLMODELVIEW )
 
ImageRef resultImage = fn ImageWithSize( fn CGSizeMake( imageDimension, imageDimension ) )
glLoadIdentity()
ImageLockFocus( resultImage )
 
CIImageDrawAtPoint( fn CIFilterOutputImage( filter ), CGPointZero, fn CGRectMake( 0, 0, imageDimension, imageDimension ), NSCompositeDestinationAtop, 1.0 )
// Position parameters: x axis, y axis, z axis
ImageUnlockFocus( resultImage )
// Set to center of screen:
end fn = resultImage
glTranslated( 0.0, 0.0, 0.0 )
 
// Rotation (wobble) parameters: angle, x, y
glRotated( sRotation, -0.45, -0.8, -0.6 )
 
// Set color of sphere's wireframe
glColor3d( 1.0, 0.0, 0.3 )
 
// Set width of sphere's wireframe lines
glLineWidth( 1.5 )
 
// Apply above to GLUT's built-in sphere wireframe
// Size & frame parameters: radius, slices, stack
fn glutWireSphere( 0.8, 25, 25 )
 
local fn SphereDrawBuildSphere
ImageRef sphereImage = fn SphereImageWithCIFilter( 340, fn ColorWithRGB( 0.988, 0.335, 0.176, 1.0 ), fn ColorBlack, 0.0, 125.0 )
ImageViewSetImage( _sphereImageView, sphereImage )
end fn
 
fn BuildWindow
// main program
fn BuildSphere
dim as GLint           attrib(2)
dim as CGrafPtr        port
dim as AGLPixelFormat  fmt
dim as AGLContext      glContext
dim as EventRecord     ev
dim as GLboolean       yesOK
 
// Make a window
window 1, @"Rotating Sphere", (0,0) - (500,500)
 
// Minimal setup of OpenGL
attrib(0) = _AGLRGBA
attrib(1) = _AGLDOUBLEBUFFER
attrib(2) = _AGLNONE
 
fmt = fn aglChoosePixelFormat( 0, 0, attrib(0) )
glContext = fn aglCreateContext( fmt, 0 )
aglDestroyPixelFormat( fmt )
 
// Set the FB window as port for drawing
port = window( _wndPort )
yesOK = fn aglSetDrawable( glContext, port )
yesOK = fn aglSetCurrentContext( glContext )
 
// Background color: red, green, blue, alpha
glClearColor( 0.0, 0.0, 0.0, 0.0 )
 
// 60/s HandleEvents Trigger
poke long event - 8, 1
do
// Clear window
glClear( _GLCOLORBUFFERBIT )
// Run animation
fn SphereDraw
aglSwapBuffers( glContext )
HandleEvents
until gFBquit
</syntaxhighlight>
{{output}}
[[File:Rosetta Code Sphere.png]]
 
=={{header|Go}}==
715

edits