os/graphics/graphicstest/uibench/s60/src/geometrystructs.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 /*
    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:
    20  * 
    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
    25  *
    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:
    32  *
    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.   
    38  */
    39 
    40 
    41 #include "geometrystructs.h"
    42 
    43 #include <GLES/gl.h>
    44 #include <e32math.h>
    45 
    46 
    47 float sin(float x)
    48 	{
    49 	double t = x;
    50 	double r;
    51 	TInt error = Math::Sin(r,t);
    52 	ASSERT(error==KErrNone);
    53 	return r;
    54 	}
    55 
    56 float cos(float x)
    57 	{
    58 	double t = x;
    59 	double r;
    60 	TInt error = Math::Cos(r,t);
    61 	ASSERT(error==KErrNone);
    62 	return r;
    63 	}
    64 
    65 int abs(int x)
    66 	{
    67 	return Abs(x);
    68 	}
    69 
    70 Vertex3F::Vertex3F(float x, float y, float z)
    71 	:iX(x), iY(y), iZ(z)
    72 	{
    73 	}
    74 
    75 Vertex3F::Vertex3F()
    76 :iX(0), iY(0), iZ(0)
    77 	{
    78 	}
    79 
    80 int fghCircleTable(float **sint,float **cost,const int n)
    81 {
    82     int i;
    83 
    84     /* Table size, the sign of n flips the circle direction */
    85     const int size = abs(n);
    86 
    87     /* Determine the angle between samples */
    88     const float angle = 2*KPi/(float)( ( n == 0 ) ? 1 : n );
    89 
    90     /* Allocate memory for n samples, plus duplicate of first entry at the end */
    91     *sint = new float[sizeof(float) * size+1];
    92     
    93     if(*sint==NULL)
    94 		{
    95 		return KErrNoMemory;
    96 		}
    97 		
    98     *cost = new float[sizeof(float) * size+1];
    99     
   100     if(*cost==NULL)
   101 		{
   102 		delete[] sint;
   103 		return KErrNoMemory;
   104 		}
   105     
   106     /* Compute cos and sin around the circle */
   107     (*sint)[0] = 0.0;
   108     (*cost)[0] = 1.0;
   109 
   110     for (i=1; i<size; i++)
   111     {
   112     (*sint)[i] = sin(angle*i);
   113     (*cost)[i] = cos(angle*i);
   114     }
   115 
   116     /* Last sample is duplicate of the first */
   117 
   118     (*sint)[size] = (*sint)[0];
   119     (*cost)[size] = (*cost)[0];
   120     return KErrNone;
   121 }
   122 
   123 CSolidSphere* CSolidSphere::NewLC(TReal aRadius, TInt aSlices, TInt aStacks)
   124 	{
   125 	CSolidSphere* self = new(ELeave) CSolidSphere(aSlices, aStacks);
   126 	CleanupStack::PushL(self);
   127 	self->ConstructL(aRadius);
   128 	return self;
   129 	}
   130 
   131 CSolidSphere::~CSolidSphere()
   132 	{
   133 	delete[] iTopVertices;
   134 	delete[] iTopNormals;
   135 	delete[] iBottomVertices;
   136 	delete[] iBottomNormals;
   137 	delete[] iStackVertices;
   138 	delete[] iStackNormals;
   139 	}
   140 
   141 void CSolidSphere::Draw() const
   142 	{
   143 //top fan
   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);
   150 	TInt offset = 0;
   151 	TInt verticesPerStack = (iSlices+1)*2;
   152 	for(TInt i=1; i<iStacks-1; i++ )
   153 		{
   154 		glDrawArrays(GL_TRIANGLE_STRIP, offset, verticesPerStack);
   155 		offset+=verticesPerStack;
   156 		}
   157 //bottom fan
   158 	glVertexPointer(3, GL_FLOAT, 0, iBottomVertices);
   159 	glNormalPointer(GL_FLOAT, 0, iBottomNormals);
   160 	glDrawArrays(GL_TRIANGLE_FAN, 0, iSlices+2);
   161 	}
   162 
   163 CSolidSphere::CSolidSphere(TInt aSlices, TInt aStacks)
   164 	:iSlices(aSlices), iStacks(aStacks)
   165 	{
   166 	}
   167 
   168 void CSolidSphere::ConstructL(TReal aRadius)
   169 	{
   170 	//* Pre-computed circle 
   171 	float* sint1 = NULL;
   172 	float* cost1 = NULL;
   173 	float* sint2 = NULL;
   174 	float* cost2 = NULL;
   175 	
   176 	
   177 	fghCircleTable(&sint1,&cost1,-iSlices);
   178 	CleanupArrayDeletePushL(sint1);
   179 	CleanupArrayDeletePushL(cost1);
   180 		
   181 	fghCircleTable(&sint2,&cost2,iStacks*2);
   182 	CleanupArrayDeletePushL(sint2);
   183 	CleanupArrayDeletePushL(cost2);
   184 
   185 	//* The top stack is covered with a triangle fan 
   186 	int i,j;
   187 	//* Adjust z and radius as stacks are drawn. 
   188 	float z0,z1;
   189 	float r0,r1;
   190 	
   191 	z0 = 1.0;
   192 	z1 = cost2[(iStacks>0)?1:0];
   193 	r0 = 0.0;
   194 	r1 = sint2[(iStacks>0)?1:0];
   195 	
   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--)
   202 	  	{
   203 	  	topVerticesIndex++;
   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;
   208 	    }
   209 	
   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];	
   214 	TInt stackIndex = 0;
   215 	for( i=1; i<iStacks-1; i++ )
   216 	    {
   217 	    z0 = z1; z1 = cost2[i+1];
   218 	    r0 = r1; r1 = sint2[i+1];
   219 	    for(j=0; j<=iSlices; j++)
   220 	       {
   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;
   229 		   stackIndex++;
   230 		   }
   231 	    }
   232 	//* The bottom stack is covered with a triangle fan 
   233 	z0 = z1;
   234 	r0 = r1;
   235 
   236 	iBottomVertices = new (ELeave) Vertex3F[iSlices+2];
   237 	iBottomNormals = new (ELeave) Vertex3F[iSlices+2];
   238 	
   239 	iBottomVertices[0] = Vertex3F(0,0,-aRadius);
   240 	iBottomNormals[0] = Vertex3F(0,0,-1);
   241     for(j=0; j<=iSlices; j++)
   242     	{
   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;
   247 	    }
   248     //* Release sin and cos tables 
   249     CleanupStack::PopAndDestroy(4);
   250 	}
   251