1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/graphicstest/uibench/s60/src/geometrystructs.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,251 @@
1.4 +// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +/*
1.20 + * Portions of this code, in particular the fghCircleTable function and the code to
1.21 + * calculate the vertices for the solid sphere, is ported from freeglut_geometry.cpp
1.22 + * which is distributed under the following terms:
1.23 + *
1.24 + ******************************************************************
1.25 + * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
1.26 + * Written by Pawel W. Olszta, <olszta@sourceforge.net>
1.27 + * Creation date: Fri Dec 3 1999
1.28 + *
1.29 + * Permission is hereby granted, free of charge, to any person obtaining a
1.30 + * copy of this software and associated documentation files (the "Software"),
1.31 + * to deal in the Software without restriction, including without limitation
1.32 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1.33 + * and/or sell copies of the Software, and to permit persons to whom the
1.34 + * Software is furnished to do so, subject to the following conditions:
1.35 + *
1.36 + * The above copyright notice and this permission notice shall be included
1.37 + * in all copies or substantial portions of the Software.
1.38 + *********************************************************************
1.39 + * Apart from porting to use Open GL ES instead of Open GL the other major change is
1.40 + * to separate out the code that calculates vertices from the code that draws the shape.
1.41 + */
1.42 +
1.43 +
1.44 +#include "geometrystructs.h"
1.45 +
1.46 +#include <GLES/gl.h>
1.47 +#include <e32math.h>
1.48 +
1.49 +
1.50 +float sin(float x)
1.51 + {
1.52 + double t = x;
1.53 + double r;
1.54 + TInt error = Math::Sin(r,t);
1.55 + ASSERT(error==KErrNone);
1.56 + return r;
1.57 + }
1.58 +
1.59 +float cos(float x)
1.60 + {
1.61 + double t = x;
1.62 + double r;
1.63 + TInt error = Math::Cos(r,t);
1.64 + ASSERT(error==KErrNone);
1.65 + return r;
1.66 + }
1.67 +
1.68 +int abs(int x)
1.69 + {
1.70 + return Abs(x);
1.71 + }
1.72 +
1.73 +Vertex3F::Vertex3F(float x, float y, float z)
1.74 + :iX(x), iY(y), iZ(z)
1.75 + {
1.76 + }
1.77 +
1.78 +Vertex3F::Vertex3F()
1.79 +:iX(0), iY(0), iZ(0)
1.80 + {
1.81 + }
1.82 +
1.83 +int fghCircleTable(float **sint,float **cost,const int n)
1.84 +{
1.85 + int i;
1.86 +
1.87 + /* Table size, the sign of n flips the circle direction */
1.88 + const int size = abs(n);
1.89 +
1.90 + /* Determine the angle between samples */
1.91 + const float angle = 2*KPi/(float)( ( n == 0 ) ? 1 : n );
1.92 +
1.93 + /* Allocate memory for n samples, plus duplicate of first entry at the end */
1.94 + *sint = new float[sizeof(float) * size+1];
1.95 +
1.96 + if(*sint==NULL)
1.97 + {
1.98 + return KErrNoMemory;
1.99 + }
1.100 +
1.101 + *cost = new float[sizeof(float) * size+1];
1.102 +
1.103 + if(*cost==NULL)
1.104 + {
1.105 + delete[] sint;
1.106 + return KErrNoMemory;
1.107 + }
1.108 +
1.109 + /* Compute cos and sin around the circle */
1.110 + (*sint)[0] = 0.0;
1.111 + (*cost)[0] = 1.0;
1.112 +
1.113 + for (i=1; i<size; i++)
1.114 + {
1.115 + (*sint)[i] = sin(angle*i);
1.116 + (*cost)[i] = cos(angle*i);
1.117 + }
1.118 +
1.119 + /* Last sample is duplicate of the first */
1.120 +
1.121 + (*sint)[size] = (*sint)[0];
1.122 + (*cost)[size] = (*cost)[0];
1.123 + return KErrNone;
1.124 +}
1.125 +
1.126 +CSolidSphere* CSolidSphere::NewLC(TReal aRadius, TInt aSlices, TInt aStacks)
1.127 + {
1.128 + CSolidSphere* self = new(ELeave) CSolidSphere(aSlices, aStacks);
1.129 + CleanupStack::PushL(self);
1.130 + self->ConstructL(aRadius);
1.131 + return self;
1.132 + }
1.133 +
1.134 +CSolidSphere::~CSolidSphere()
1.135 + {
1.136 + delete[] iTopVertices;
1.137 + delete[] iTopNormals;
1.138 + delete[] iBottomVertices;
1.139 + delete[] iBottomNormals;
1.140 + delete[] iStackVertices;
1.141 + delete[] iStackNormals;
1.142 + }
1.143 +
1.144 +void CSolidSphere::Draw() const
1.145 + {
1.146 +//top fan
1.147 + glVertexPointer(3, GL_FLOAT, 0, iTopVertices);
1.148 + glNormalPointer(GL_FLOAT, 0, iTopNormals);
1.149 + glDrawArrays(GL_TRIANGLE_FAN, 0, iSlices+2);
1.150 +//stacks, one at a time
1.151 + glVertexPointer(3, GL_FLOAT, 0, iStackVertices);
1.152 + glNormalPointer(GL_FLOAT, 0, iStackNormals);
1.153 + TInt offset = 0;
1.154 + TInt verticesPerStack = (iSlices+1)*2;
1.155 + for(TInt i=1; i<iStacks-1; i++ )
1.156 + {
1.157 + glDrawArrays(GL_TRIANGLE_STRIP, offset, verticesPerStack);
1.158 + offset+=verticesPerStack;
1.159 + }
1.160 +//bottom fan
1.161 + glVertexPointer(3, GL_FLOAT, 0, iBottomVertices);
1.162 + glNormalPointer(GL_FLOAT, 0, iBottomNormals);
1.163 + glDrawArrays(GL_TRIANGLE_FAN, 0, iSlices+2);
1.164 + }
1.165 +
1.166 +CSolidSphere::CSolidSphere(TInt aSlices, TInt aStacks)
1.167 + :iSlices(aSlices), iStacks(aStacks)
1.168 + {
1.169 + }
1.170 +
1.171 +void CSolidSphere::ConstructL(TReal aRadius)
1.172 + {
1.173 + //* Pre-computed circle
1.174 + float* sint1 = NULL;
1.175 + float* cost1 = NULL;
1.176 + float* sint2 = NULL;
1.177 + float* cost2 = NULL;
1.178 +
1.179 +
1.180 + fghCircleTable(&sint1,&cost1,-iSlices);
1.181 + CleanupArrayDeletePushL(sint1);
1.182 + CleanupArrayDeletePushL(cost1);
1.183 +
1.184 + fghCircleTable(&sint2,&cost2,iStacks*2);
1.185 + CleanupArrayDeletePushL(sint2);
1.186 + CleanupArrayDeletePushL(cost2);
1.187 +
1.188 + //* The top stack is covered with a triangle fan
1.189 + int i,j;
1.190 + //* Adjust z and radius as stacks are drawn.
1.191 + float z0,z1;
1.192 + float r0,r1;
1.193 +
1.194 + z0 = 1.0;
1.195 + z1 = cost2[(iStacks>0)?1:0];
1.196 + r0 = 0.0;
1.197 + r1 = sint2[(iStacks>0)?1:0];
1.198 +
1.199 + iTopVertices = new (ELeave) Vertex3F[iSlices+2];
1.200 + iTopNormals = new (ELeave) Vertex3F[iSlices+2];
1.201 + TInt topVerticesIndex = 0;
1.202 + iTopVertices[topVerticesIndex] = Vertex3F(0,0,aRadius);
1.203 + iTopNormals[topVerticesIndex] = Vertex3F(0,0,1);
1.204 + for(j=iSlices; j>=0; j--)
1.205 + {
1.206 + topVerticesIndex++;
1.207 + Vertex3F vertex(cost1[j]*r1*aRadius, sint1[j]*r1*aRadius, z1*aRadius);
1.208 + iTopVertices[topVerticesIndex] = vertex;
1.209 + Vertex3F normal(cost1[j]*r1, sint1[j]*r1, z1 );
1.210 + iTopNormals[topVerticesIndex] = normal;
1.211 + }
1.212 +
1.213 + //*calculate the vertices for each stack
1.214 + TInt stackVertexCount = ((iSlices+1)*2) *iStacks;
1.215 + iStackVertices = new (ELeave) Vertex3F[stackVertexCount];
1.216 + iStackNormals = new (ELeave) Vertex3F[stackVertexCount];
1.217 + TInt stackIndex = 0;
1.218 + for( i=1; i<iStacks-1; i++ )
1.219 + {
1.220 + z0 = z1; z1 = cost2[i+1];
1.221 + r0 = r1; r1 = sint2[i+1];
1.222 + for(j=0; j<=iSlices; j++)
1.223 + {
1.224 + Vertex3F v1(cost1[j]*r1*aRadius, sint1[j]*r1*aRadius, z1*aRadius);
1.225 + Vertex3F v2(cost1[j]*r0*aRadius, sint1[j]*r0*aRadius, z0*aRadius);
1.226 + iStackVertices[stackIndex*2] = v1;
1.227 + iStackVertices[(stackIndex*2)+1] = v2;
1.228 + Vertex3F n1(cost1[j]*r1, sint1[j]*r1, z1);
1.229 + Vertex3F n2(cost1[j]*r0, sint1[j]*r0, z0);
1.230 + iStackNormals[stackIndex*2] = n1;
1.231 + iStackNormals[(stackIndex*2)+1] = n2;
1.232 + stackIndex++;
1.233 + }
1.234 + }
1.235 + //* The bottom stack is covered with a triangle fan
1.236 + z0 = z1;
1.237 + r0 = r1;
1.238 +
1.239 + iBottomVertices = new (ELeave) Vertex3F[iSlices+2];
1.240 + iBottomNormals = new (ELeave) Vertex3F[iSlices+2];
1.241 +
1.242 + iBottomVertices[0] = Vertex3F(0,0,-aRadius);
1.243 + iBottomNormals[0] = Vertex3F(0,0,-1);
1.244 + for(j=0; j<=iSlices; j++)
1.245 + {
1.246 + Vertex3F vertex(cost1[j]*r0*aRadius, sint1[j]*r0*aRadius, z0*aRadius);
1.247 + iBottomVertices[j+1] = vertex;
1.248 + Vertex3F normal(cost1[j]*r0, sint1[j]*r0, z0);
1.249 + iBottomNormals[j+1] = normal;
1.250 + }
1.251 + //* Release sin and cos tables
1.252 + CleanupStack::PopAndDestroy(4);
1.253 + }
1.254 +