OpenGL demo: Starfield
Oct 23, 2010 19:50:55 GMT 1
Post by Pjot on Oct 23, 2010 19:50:55 GMT 1
Looking into the GL/GLUT API a little bit more, I wondered if it was possible to do some simple 2D drawing. It is not that difficult. Below a demo which needs the GL and the GLUT include files:
'
' Starfield demonstration using GL and GLUT - learning 2D GL/GLUT programming.
'
' Inspired by Cybermonkey's STARS demo:
' http://forum.basicprogramming.org/index.php/topic,1509.msg9584.html#msg9584
'
' October 2010, PvE - GPL
'
'--------------------------------------------------------------------------------------------
INCLUDE "glut.bac"
INCLUDE "gl.bac"
CONST XSize = 640
CONST YSize = 480
CONST message$ = "S T A R S"
DECLARE x[501], y[501], z[501] TYPE int
SUB draw
LOCAL i, length
glMatrixMode (GL_PROJECTION)
glLoadIdentity
glOrtho(0, XSize, 0, YSize, 0, 1)
glDisable(GL_DEPTH_TEST)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity
glClearColor(0.1, 0.1, 0.1, 0)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1, 1, 1)
FOR i = 0 TO 500
glBegin(GL_POINTS)
glVertex2i(x[i], y[i])
glEnd
NEXT
FOR i = 1 TO LEN(message$)
INCR length, glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, ASC(MID$(message$, i)))
NEXT
glColor3f(1, 1, 0)
glRasterPos2f(XSize/2-length/2, YSize/2)
FOR i = 1 TO LEN(message$)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ASC(MID$(message$, i)))
NEXT
glutSwapBuffers
END SUB
SUB idle
FOR i = 0 TO 500
x[i] = x[i] - z[i]
IF x[i] < 0 THEN x[i] = x[i] + XSize
NEXT
SLEEP 50
glutPostRedisplay
END SUB
SUB key (NUMBER k, NUMBER x, NUMBER y)
END
END SUB
SUB mainprog (NUMBER argc, STRING argv)
glutInit(ADDRESS(argc), ADDRESS(argv))
glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE)
glutInitWindowSize(XSize, YSize)
glutCreateWindow ("S T A R S")
glutDisplayFunc (ADDRESS (draw))
glutKeyboardFunc (ADDRESS(key))
glutIdleFunc (ADDRESS(idle))
glutMainLoop
END SUB
FOR i = 0 TO 500
x[i] = RANDOM(XSize)
y[i] = RANDOM(YSize)
z[i] = RANDOM(8)+1
NEXT
mainprog(0, "")