FAST Circle draw routine
Below a fast routine to draw a complete circle in OpenGL
Since other programmers here can benefit from it, i drop the code here
.
My demo code is mainly a routine which does draw a lot of circles about 166 from small to large.
It does draw a lot of circles which seams to 'scrol by' but in fact they're all redrawn so it shows the speed of this routine..
Usually drawing large circles is a slow operation....
Its fast because i minimize complex math like sin and cos (which in this language is called sind and cosd).
It should require little conversion to rewrite this to C++ as i used Basic4GL which nativly supports openGL v1.0
main routine splits the drawing and start drawing from top bottem and both sides.
lines will be drawn till 45 degrees since i start from 4 sides, at 45 degrees the lines connect to a circle.
Delta defines how often splits are made (and so a one time recalculate with cos and sin is needed).
the code can even be more optimized for real small circles (using a few sprites)
I found an equivalent for line(x1,y2,x2,y2) in the OpenGL command glBegin(GL_lines) (see pic below code)
This code runs in Basic4GL (handy to test openGL routines), it not a full blown language like vb6, vb.net, or c++
But typing is easy and since it uses openGL its amazing Fast.. (but not complete enough to make DB clone in it, lacks mod function and others).
[div class=\'codetop\']CODE[div class=\'codemain\' style=\'height:200px;white-space:pre;overflow:auto\']
const Xsize = 1000, ysize = 600
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho (0, Xsize,Ysize, 0,0,1)
glMatrixmode(GL_MODELVIEW)
'turn of depth
glDisable(GL_DEPTH_TEST)
'clear screen
glClear(GL_COLOR_BUFFER_BIT)
'
http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL dim radius as integer
dim xpos,ypos
dim w1,h1,w2,h2,corner,delta,i
for i =1 to 800
xpos = -400+i
ypos = -400 +i
For radius = 0 to 500 step 3
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_LINES)
corner = 0
w1 = 0
h1 = radius
if radius < 300 then delta = 6 endif
if radius < 280 then delta = 10 endif
if radius < 100 then delta = 15 endif
if radius < 60 then delta = 22 endif
if radius < 15 then delta = 45 endif
while corner <46
corner = corner + delta
w2 = radius * sind(corner)
h2 = radius * cosd(corner)
glVertex2f(xpos+w1, ypos+h1): glVertex2f(xpos+w2, ypos+h2)
glVertex2f(xpos-w1, ypos+h1): glVertex2f(xpos-w2, ypos+h2)
glVertex2f(xpos+w1, ypos-h1): glVertex2f(xpos+w2, ypos-h2)
glVertex2f(xpos-w1, ypos-h1): glVertex2f(xpos-w2, ypos-h2)
glVertex2f(xpos+h1, ypos+w1): glVertex2f(xpos+h2, ypos+w2)
glVertex2f(xpos-h1, ypos+w1): glVertex2f(xpos-h2, ypos+w2)
glVertex2f(xpos+h1, ypos-w1): glVertex2f(xpos+h2, ypos-w2)
glVertex2f(xpos-h1, ypos-w1): glVertex2f(xpos-h2, ypos-w2)
w1 = w2
h1 = h2
wend
next
glend()
line types