First public contribution.
1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 * Portions of this code, in particular the fghCircleTable function and the code to
18 * calculate the vertices for the solid sphere, is ported from freeglut_geometry.cpp
19 * which is distributed under the following terms:
21 ******************************************************************
22 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
23 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
24 * Creation date: Fri Dec 3 1999
26 * Permission is hereby granted, free of charge, to any person obtaining a
27 * copy of this software and associated documentation files (the "Software"),
28 * to deal in the Software without restriction, including without limitation
29 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
30 * and/or sell copies of the Software, and to permit persons to whom the
31 * Software is furnished to do so, subject to the following conditions:
33 * The above copyright notice and this permission notice shall be included
34 * in all copies or substantial portions of the Software.
35 *********************************************************************
36 * Apart from porting to use Open GL ES instead of Open GL the other major change is
37 * to separate out the code that calculates vertices from the code that draws the shape.
41 #include "geometrystructs.h"
51 TInt error = Math::Sin(r,t);
52 ASSERT(error==KErrNone);
60 TInt error = Math::Cos(r,t);
61 ASSERT(error==KErrNone);
70 Vertex3F::Vertex3F(float x, float y, float z)
80 int fghCircleTable(float **sint,float **cost,const int n)
84 /* Table size, the sign of n flips the circle direction */
85 const int size = abs(n);
87 /* Determine the angle between samples */
88 const float angle = 2*KPi/(float)( ( n == 0 ) ? 1 : n );
90 /* Allocate memory for n samples, plus duplicate of first entry at the end */
91 *sint = new float[sizeof(float) * size+1];
98 *cost = new float[sizeof(float) * size+1];
106 /* Compute cos and sin around the circle */
110 for (i=1; i<size; i++)
112 (*sint)[i] = sin(angle*i);
113 (*cost)[i] = cos(angle*i);
116 /* Last sample is duplicate of the first */
118 (*sint)[size] = (*sint)[0];
119 (*cost)[size] = (*cost)[0];
123 CSolidSphere* CSolidSphere::NewLC(TReal aRadius, TInt aSlices, TInt aStacks)
125 CSolidSphere* self = new(ELeave) CSolidSphere(aSlices, aStacks);
126 CleanupStack::PushL(self);
127 self->ConstructL(aRadius);
131 CSolidSphere::~CSolidSphere()
133 delete[] iTopVertices;
134 delete[] iTopNormals;
135 delete[] iBottomVertices;
136 delete[] iBottomNormals;
137 delete[] iStackVertices;
138 delete[] iStackNormals;
141 void CSolidSphere::Draw() const
144 glVertexPointer(3, GL_FLOAT, 0, iTopVertices);
145 glNormalPointer(GL_FLOAT, 0, iTopNormals);
146 glDrawArrays(GL_TRIANGLE_FAN, 0, iSlices+2);
147 //stacks, one at a time
148 glVertexPointer(3, GL_FLOAT, 0, iStackVertices);
149 glNormalPointer(GL_FLOAT, 0, iStackNormals);
151 TInt verticesPerStack = (iSlices+1)*2;
152 for(TInt i=1; i<iStacks-1; i++ )
154 glDrawArrays(GL_TRIANGLE_STRIP, offset, verticesPerStack);
155 offset+=verticesPerStack;
158 glVertexPointer(3, GL_FLOAT, 0, iBottomVertices);
159 glNormalPointer(GL_FLOAT, 0, iBottomNormals);
160 glDrawArrays(GL_TRIANGLE_FAN, 0, iSlices+2);
163 CSolidSphere::CSolidSphere(TInt aSlices, TInt aStacks)
164 :iSlices(aSlices), iStacks(aStacks)
168 void CSolidSphere::ConstructL(TReal aRadius)
170 //* Pre-computed circle
177 fghCircleTable(&sint1,&cost1,-iSlices);
178 CleanupArrayDeletePushL(sint1);
179 CleanupArrayDeletePushL(cost1);
181 fghCircleTable(&sint2,&cost2,iStacks*2);
182 CleanupArrayDeletePushL(sint2);
183 CleanupArrayDeletePushL(cost2);
185 //* The top stack is covered with a triangle fan
187 //* Adjust z and radius as stacks are drawn.
192 z1 = cost2[(iStacks>0)?1:0];
194 r1 = sint2[(iStacks>0)?1:0];
196 iTopVertices = new (ELeave) Vertex3F[iSlices+2];
197 iTopNormals = new (ELeave) Vertex3F[iSlices+2];
198 TInt topVerticesIndex = 0;
199 iTopVertices[topVerticesIndex] = Vertex3F(0,0,aRadius);
200 iTopNormals[topVerticesIndex] = Vertex3F(0,0,1);
201 for(j=iSlices; j>=0; j--)
204 Vertex3F vertex(cost1[j]*r1*aRadius, sint1[j]*r1*aRadius, z1*aRadius);
205 iTopVertices[topVerticesIndex] = vertex;
206 Vertex3F normal(cost1[j]*r1, sint1[j]*r1, z1 );
207 iTopNormals[topVerticesIndex] = normal;
210 //*calculate the vertices for each stack
211 TInt stackVertexCount = ((iSlices+1)*2) *iStacks;
212 iStackVertices = new (ELeave) Vertex3F[stackVertexCount];
213 iStackNormals = new (ELeave) Vertex3F[stackVertexCount];
215 for( i=1; i<iStacks-1; i++ )
217 z0 = z1; z1 = cost2[i+1];
218 r0 = r1; r1 = sint2[i+1];
219 for(j=0; j<=iSlices; j++)
221 Vertex3F v1(cost1[j]*r1*aRadius, sint1[j]*r1*aRadius, z1*aRadius);
222 Vertex3F v2(cost1[j]*r0*aRadius, sint1[j]*r0*aRadius, z0*aRadius);
223 iStackVertices[stackIndex*2] = v1;
224 iStackVertices[(stackIndex*2)+1] = v2;
225 Vertex3F n1(cost1[j]*r1, sint1[j]*r1, z1);
226 Vertex3F n2(cost1[j]*r0, sint1[j]*r0, z0);
227 iStackNormals[stackIndex*2] = n1;
228 iStackNormals[(stackIndex*2)+1] = n2;
232 //* The bottom stack is covered with a triangle fan
236 iBottomVertices = new (ELeave) Vertex3F[iSlices+2];
237 iBottomNormals = new (ELeave) Vertex3F[iSlices+2];
239 iBottomVertices[0] = Vertex3F(0,0,-aRadius);
240 iBottomNormals[0] = Vertex3F(0,0,-1);
241 for(j=0; j<=iSlices; j++)
243 Vertex3F vertex(cost1[j]*r0*aRadius, sint1[j]*r0*aRadius, z0*aRadius);
244 iBottomVertices[j+1] = vertex;
245 Vertex3F normal(cost1[j]*r0, sint1[j]*r0, z0);
246 iBottomNormals[j+1] = normal;
248 //* Release sin and cos tables
249 CleanupStack::PopAndDestroy(4);