; PureBasic IRC: http://rafb.net/paste/results/XZqRFq65.html ; Author: traumatic ; Date: 17. April 2005 ; OS: Windows ; Demo: Yes ; sloppy code, direct3d implentation but it may give you the idea: http://rafb.net/paste/results/XZqRFq65.html #D3DFVF_XYZ = 2 #D3DFVF_NORMAL = 16 #D3DFVF_DIFFUSE = 64 #D3DFVF_TEX1 = 256 Procedure DrawSphereTriangleList(nRings.l, nSegments.l) #D3DFVF_CUSTOMVERTEX = #D3DFVF_XYZ | #D3DFVF_NORMAL | #D3DFVF_DIFFUSE | #D3DFVF_TEX1 Structure CUSTOMVERTEX x.f : y.f : z.f nx.f : ny .f : nz.f colour.l tu.f : tv.f EndStructure ; nRings.l = 8 ; nSegments.l = 24 dwNumOfVertices.l = (nRings + 1) * (nSegments + 1) dwNumOfPolygons.l = dwNumOfVertices * 3 pVertexBuffer.IDirect3DVertexBuffer8 Dim pVertex.CUSTOMVERTEX(dwNumOfVertices*3) ; Dim pVertex.CUSTOMVERTEX(25*3) vNormal.D3DpbVECTOR3 nCurrentRing.l = 0 ;Setup some angles rDeltaRingAngle.f = (#PI / nRings) rDeltaSegAngle.f = (2.0 * #PI / nSegments) ;PART-1- Read in all the vertices that make up the shape ;Generate the group of rings for the sphere For nCurrentRing=0 To nRings r0.f = Sin(nCurrentRing * rDeltaRingAngle) y0.f = Cos(nCurrentRing * rDeltaRingAngle) ;OOooo Generate the group of segments for the current ring For nCurrentSegment=0 To nSegments x0.f = r0 * Sin(nCurrentSegment * rDeltaSegAngle) z0.f = r0 * Cos(nCurrentSegment * rDeltaSegAngle) vNormal\x = x0 vNormal\y = y0 vNormal\z = z0 ;D3DXVec3Normalize(@vNormal, @vNormal) D3DpbNormalize(@vNormal) ;Add one vector to the strip which makes up the sphere i.l = nCurrentRing * nSegments + nCurrentSegment pVertex(i)\x = x0 pVertex(i)\y = y0 pVertex(i)\z = z0 pVertex(i)\nx = vNormal\x pVertex(i)\ny = vNormal\y pVertex(i)\nz = vNormal\z pVertex(i)\tu = 1.0 - nCurrentSegment / nRings pVertex(i)\tv = nCurrentRing / nSegments pVertex(i)\colour = $FFFFFFFF Next Next ;PART-2- Arrange them in list order ;This part puts all of our lovely vertices into a nice tidy order of triangles. Dim pVertexList.CUSTOMVERTEX(dwNumOfVertices*2*3) ; Dim pVertexList.CUSTOMVERTEX(50*3) index.l = 0 For nCurrentRing = 0 To nRings For nCurrentSegment = 0 To nSegments If(nCurrentRing <> nRings) ;These few lines specify the triangles into the pVertexList, as ;above we have just generated the vertices for the sphere, but they ;aren't aranged in an order of triangles, e.g. the first three points ;make up a triangle, the next three points make up a second triangle ;etc. These lines within the loop take the points and generate ;a list of triangles which we can draw using D3DPT_TRIANGLELIST i.l = nCurrentRing * nSegments pVertexList(index)\x = pVertex(i+nCurrentSegment)\x pVertexList(index)\y = pVertex(i+nCurrentSegment)\y pVertexList(index)\z = pVertex(i+nCurrentSegment)\z pVertexList(index)\nx = pVertex(i+nCurrentSegment)\nx pVertexList(index)\ny = pVertex(i+nCurrentSegment)\ny pVertexList(index)\nz = pVertex(i+nCurrentSegment)\nz pVertexList(index)\tu = pVertex(i+nCurrentSegment)\tu pVertexList(index)\tv = pVertex(i+nCurrentSegment)\tv pVertexList(index)\colour = pVertex(i+nCurrentSegment)\colour index+1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;/ pVertexList(index)\x = pVertex(i+nCurrentSegment + nSegments)\x pVertexList(index)\y = pVertex(i+nCurrentSegment + nSegments)\y pVertexList(index)\z = pVertex(i+nCurrentSegment + nSegments)\z pVertexList(index)\nx = pVertex(i+nCurrentSegment + nSegments)\nx pVertexList(index)\ny = pVertex(i+nCurrentSegment + nSegments)\ny pVertexList(index)\nz = pVertex(i+nCurrentSegment + nSegments)\nz pVertexList(index)\tu = pVertex(i+nCurrentSegment + nSegments)\tu pVertexList(index)\tv = pVertex(i+nCurrentSegment + nSegments)\tv pVertexList(index)\colour = pVertex(i+nCurrentSegment + nSegments)\colour index+1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; pVertexList(index)\x = pVertex(i+nCurrentSegment +1)\x pVertexList(index)\y = pVertex(i+nCurrentSegment +1)\y pVertexList(index)\z = pVertex(i+nCurrentSegment +1)\z pVertexList(index)\nx = pVertex(i+nCurrentSegment +1)\nx pVertexList(index)\ny = pVertex(i+nCurrentSegment +1)\ny pVertexList(index)\nz = pVertex(i+nCurrentSegment +1)\nz pVertexList(index)\tu = pVertex(i+nCurrentSegment +1)\tu pVertexList(index)\tv = pVertex(i+nCurrentSegment +1)\tv pVertexList(index)\colour = pVertex(i+nCurrentSegment+1)\colour index+1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;/ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;/ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;/ pVertexList(index)\x = pVertex(i+nCurrentSegment+ nSegments)\x pVertexList(index)\y = pVertex(i+nCurrentSegment+ nSegments)\y pVertexList(index)\z = pVertex(i+nCurrentSegment+ nSegments)\z pVertexList(index)\nx = pVertex(i+nCurrentSegment+ nSegments)\nx pVertexList(index)\ny = pVertex(i+nCurrentSegment+ nSegments)\ny pVertexList(index)\nz = pVertex(i+nCurrentSegment+ nSegments)\nz pVertexList(index)\tu = pVertex(i+nCurrentSegment+ nSegments)\tu pVertexList(index)\tv = pVertex(i+nCurrentSegment+ nSegments)\tv pVertexList(index)\colour = pVertex(i+nCurrentSegment+ nSegments)\colour index+1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;/ pVertexList(index)\x = pVertex(i+nCurrentSegment + nSegments + 1)\x pVertexList(index)\y = pVertex(i+nCurrentSegment + nSegments + 1)\y pVertexList(index)\z = pVertex(i+nCurrentSegment + nSegments + 1)\z pVertexList(index)\nx = pVertex(i+nCurrentSegment + nSegments + 1)\nx pVertexList(index)\ny = pVertex(i+nCurrentSegment + nSegments + 1)\ny pVertexList(index)\nz = pVertex(i+nCurrentSegment + nSegments + 1)\nz pVertexList(index)\tu = pVertex(i+nCurrentSegment + nSegments + 1)\tu pVertexList(index)\tv = pVertex(i+nCurrentSegment + nSegments + 1)\tv pVertexList(index)\colour = pVertex(i+nCurrentSegment + nSegments)\colour index+1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; pVertexList(index)\x = pVertex(i+nCurrentSegment +1)\x pVertexList(index)\y = pVertex(i+nCurrentSegment +1)\y pVertexList(index)\z = pVertex(i+nCurrentSegment +1)\z pVertexList(index)\nx = pVertex(i+nCurrentSegment +1)\nx pVertexList(index)\ny = pVertex(i+nCurrentSegment +1)\ny pVertexList(index)\nz = pVertex(i+nCurrentSegment +1)\nz pVertexList(index)\tu = pVertex(i+nCurrentSegment +1)\tu pVertexList(index)\tv = pVertex(i+nCurrentSegment +1)\tv pVertexList(index)\colour = pVertex(i+nCurrentSegment+1)\colour index+1 EndIf Next Next d3dDevice\CreateVertexBuffer(index * SizeOf(CUSTOMVERTEX), 0, #D3DFVF_CUSTOMVERTEX, #D3DPOOL_DEFAULT, @pVertexBuffer) *pV.b pVertexBuffer\Lock(0,0, @*pV, 0) CopyMemory(@pVertexList(), *pV, SizeOf(CUSTOMVERTEX)*index) pVertexBuffer\Unlock() d3dDevice\SetStreamSource(0, pVertexBuffer, SizeOf(CUSTOMVERTEX)) d3dDevice\SetVertexShader(#D3DFVF_CUSTOMVERTEX) d3dDevice\DrawPrimitive(#D3DPT_TRIANGLELIST, 0, index/3);dwNumOfPolygons pVertexBuffer\Release() EndProcedure ; IDE Options = PureBasic v4.02 (Windows - x86) ; Folding = -