A simple OpenGL like software renderer written in BlitzBasic.
Highlights:
- Perspective projection
- Z-Buffering
- Supported Primitives:
SR_POINTS
,SR_LINES
,SR_TRIANGLES
- Transformation:
srRotate
,srTranslate
,srScale
- Lightning:
SR_LIGHT1
(pointlight) is alway turned on. - Shading: Flat shading (vertex normals currently ignored)
Copy renderer.bb
and math.bb
into you project's include folder and type include "renderer.bb"
. Switch to the graphics mode by call the Graphics
command. Every call to the software renderer must made between srInit
and srDestroy
. Don't forget to call Flip
every frame.
Example taken from triangle-example.bb. Renders a filled triangle.
Include "renderer.bb"
Graphics(800, 600, 0, 2)
SetBuffer BackBuffer()
srInit()
srMatrixMode(SR_PROJECTION)
srLoadIdentity()
srPerspective(1.0, Float(GraphicsWidth())/Float(GraphicsHeight()), 0.1, 100.0)
srDepthRange(0.0, 100.0)
srMatrixMode(SR_MODELVIEW)
While Not KeyDown(1)
Cls()
srLoadIdentity()
srTranslate(0.0, 0.0, -2)
srBegin(SR_TRIANGLES)
srVertex( 0, 1, 0) ; Top Middle
srVertex(-1, -1, 0) ; Bottom Left
srVertex( 1, -1, 0) ; Bottom Right
srEnd()
Flip(0)
Wend
srDestroy()
End
Rendering is allmost done in OpenGL style:
- Eye coordinates = ModelView x Object coordinates
- Eye normal = Inverse ModelView x Object normal coordinates
- Clip coordinates = Projection matrix x Eye coordinates
- Normalized device coordinates = Clip coordinates x, y, z / Clip w
- View transformation
- TempOriginX = Viewport width/2 + Viewport x
- TempOriginY = Viewport height/2 + Viewport y
- TempRangeHalf = (Depth range far - Depth range near)/2
- Window X = Viewport width/2 * Device x + TempOriginX
- Window Y = Viewport height/2 * Device y + TempOriginY
- Window Z = TempRangeHalf*Device z + TempRangeHalf
- Clear Z Buffer
- Draw primitives in window coordinates. (Depth function is always set to GL_GREATER)