GL_POINTS Sphere

Started by
2 comments, last by Acer 22 years, 6 months ago
I have some code that if I dont touch it, it makes a Cube made up of GL_POINTS. Here is some of the code I remember. Const N = 10000 Dim G(C) as Double Dim X(C) as Double Dim Y(C) as Double .... The it has glVertex3D TmpX, TmpY, TmpZ I can alter that buy adding a * TmpZ * TmpX etc. I want to make a Sphere with the GL_POINTS. And maybe make it morph into a Cube and then maybe into a Pyramid. I really just care for the Sphere.
Tight
Advertisement
Here is the code to modify:
Const N = 1000

Dim D(N) As Double
Dim x(N) As Double
Dim Y(N) As Double
Dim Z(N) As Double
Dim G(N) As Byte
Dim TmpX As Double
Dim TmpY As Double
Dim TmpZ As Double
Dim C As Double

Private Sub Form_Click()
wglDeleteContext hRC
wglMakeCurrent 0, 0
End
End Sub

Private Sub Form_Load()
Me.Move 0, 0, Screen.Width, Screen.Height
On Error Resume Next
Dim RotX As Double
Dim RotY As Double

InitOpenGL
Me.Show: DoEvents


For C = 0 To N
x(C) = Rnd * 360
Y(C) = Rnd * 360
Z(C) = Rnd * 360 '' C
D(C) = 1
G(C) = Rnd * 250
Next C
glEnable GL_DEPTH_TEST
Do
RotX = RotX + 2 ''4
RotY = RotY + 2.5 ''5
glClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT
glPushMatrix
glRotated RotX, 1, 0, 0
glRotated RotY, 0, 1, 1
glScalef 0.6, 0.6, 0.6 ''Size
glBegin GL_POINTS ''Object to make shape



For C = 0 To N

TmpX = Cos(x(C) * (100.14 / 280)) * D(C)
TmpY = -Sin(Y(C) * (100.14 / 280)) * D(C)
TmpZ = Cos(Z(C) * (100.14 / 280)) * D(C)

''X(C) = X(C) + 1
''Y(C) = Y(C) + 1
''Z(C) = Z(C) + 1
glColor3ub 200, 75, 100
glVertex3d TmpX, TmpY, TmpZ

''D(C) = D(C) + ((Rnd / 10) - (0.5 / 10))
''X(C) = X(C) + 1
Next C
glEnd
glPopMatrix
SwapBuffers Me.hDC
DoEvents
Loop




End Sub

Private Function InitOpenGL()
Dim PFD As PIXELFORMATDESCRIPTOR
PFD.nVersion = 1
PFD.nSize = Len(PFD)
PFD.dwFlags = PFD_DRAW_TO_WINDOW Or _
PFD_SUPPORT_OPENGL Or PFD_DOUBLEBUFFER
PFD.iLayerType = PFD_MAIN_PLANE
PFD.iPixelType = PFD_TYPE_RGBA
PFD.cColorBits = 16
PFD.cDepthBits = 16

Dim pixelformat As Long

pixelformat = ChoosePixelFormat(Me.hDC, PFD)
SetPixelFormat Me.hDC, pixelformat, PFD

hRC = wglCreateContext(Me.hDC)
wglMakeCurrent Me.hDC, hRC
End Function


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
''Render Y / 30, X / 30
End Sub

Private Sub Form_Unload(Cancel As Integer)
wglDeleteContext hRC
wglMakeCurrent 0, 0
End
End Sub

Private Sub Render(RotX As Double, RotY As Double)
glClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT
glPushMatrix
glRotated RotX, 1, 2, 3
glRotated RotY, 2, 1, 1
glScalef 0.5, 0.6, 0.7
glBegin GL_LINES
For C = 0 To N
TmpX = Cos(x(C) * (3.14 / 180)) * D(C)
TmpY = -Sin(Y(C) * (3.14 / 180)) * D(C)
TmpZ = Cos(Z(C) * (3.14 / 180)) * D(C)
''X(C) = X(C) + 1
''Y(C) = Y(C) + 1
''Z(C) = Z(C) + 1
glColor3f 0, 1, 0

glColor3ub 0, G(C), G(C)
glVertex3d TmpX, TmpY, TmpX * TmpX * TmpZ * TmpY * TmpX * TmpZ
''D(C) = D(C) + ((Rnd / 100) - (0.5 / 100))
x(C) = x(C) + 1
Next C
glEnd
glPopMatrix
SwapBuffers Me.hDC
DoEvents
End Sub
Tight
This will generate a whole bunch of random points on the surface of a sphere:

Const radius = 10Const numPoints = 1000Dim x As DoubleDim y As DoubleDim z As DoubleDim dist As DoubleFor i = 1 To numPoints      x = rand      y = rand      z = rand      dist = sqr(x ^ 2 + y ^ 2 + z ^ 2)      x = (x / dist) * radius      y = (y / dist) * radius      z = (z / dist) * radius      glVertex3f(x, y, z)Next i  


If you want to morph from a cube to a sphere, just put a sixth of your sphere verteces at each cube vertex, then generate sphere positions for each, and linearly interpolate between the two. This only works with points.

Edited by - TerranFury on October 5, 2001 7:16:07 PM
You''ll have to convert it to BASIC, but...

// 0.0174532925199432 is just pi/180
// draws a sphere with tex coords
// radius - radius
// lat_step - number of horizontal sections
// long_step - number of vertical sections
// draw_type - how the sphere is to be rendered
void DrawSphere(double radius, int lat_step, int long_step, bool lighting, int draw_type)
{
double DEGTORAD = 0.0174532925199432;
glPushMatrix();
glBegin(draw_type);

for (double i = 0; i < 180; i += 180/lat_step)
{
for (double j = 0; j < 360; j += 360/long_step)
{
if (lighting)
glNormal3d(cos(DEGTORAD * (j + 360/(long_step * 2))) * sin((i + 180/(lat_step * 2)) * DEGTORAD),
cos((i + 180/(lat_step * 2)) * DEGTORAD),
-sin(DEGTORAD * (j + 360/(long_step * 2))) * sin((i + 180/(lat_step * 2)) * DEGTORAD));

glTexCoord2d(j/360, i/180);
glVertex3d(cos(DEGTORAD * j) * radius * sin(i * DEGTORAD),
cos(i * DEGTORAD) * radius,
-sin(DEGTORAD * j) * radius * sin(i * DEGTORAD));

glTexCoord2d(j/360, (i + 180/lat_step)/180);
glVertex3d(cos(DEGTORAD * j) * radius * sin((i + 180/lat_step) * DEGTORAD),
cos((i + 180/lat_step) * DEGTORAD) * radius,
-sin(DEGTORAD * j) * radius * sin((i + 180/lat_step) * DEGTORAD));

glTexCoord2d((j + 360/long_step)/360, (i + 180/lat_step)/180);
glVertex3d(cos(DEGTORAD * (j + 360/long_step)) * radius * sin((i + 180/lat_step) * DEGTORAD),
cos((i + 180/lat_step) * DEGTORAD) * radius,
-sin(DEGTORAD * (j + 360/long_step)) * radius * sin((i + 180/lat_step) * DEGTORAD));

glTexCoord2d((j + 360/long_step)/360, i/180);
glVertex3d(cos(DEGTORAD * (j + 360/long_step)) * radius * sin(i * DEGTORAD),
cos(i * DEGTORAD) * radius,
-sin(DEGTORAD * (j + 360/long_step)) * radius * sin(i * DEGTORAD));
}
}

glEnd();
glPopMatrix();
}

This topic is closed to new replies.

Advertisement