egl/sfegltest/src/gles1cube.cpp
branchbug235_bringup_0
changeset 215 097e92a68d68
equal deleted inserted replaced
213:deb2534f581f 215:097e92a68d68
       
     1 // Copyright (c) 2010 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 #include "gles1cube.h"
       
    16 
       
    17 _LIT(KGLES1CubeName, "gles1cube");
       
    18 
       
    19 CEGLRendering* CGLES1Cube::NewL(RWindow& aWindow)
       
    20     {
       
    21     CGLES1Cube* self = new (ELeave) CGLES1Cube(aWindow);
       
    22     CleanupStack::PushL(self);
       
    23     self->ConstructL();
       
    24     CleanupStack::Pop(self);
       
    25     return self;
       
    26     }
       
    27 
       
    28 const TDesC& CGLES1Cube::Name()
       
    29 	{
       
    30 	return KGLES1CubeName;
       
    31 	}
       
    32 
       
    33 CGLES1Cube::CGLES1Cube(RWindow& aWindow)
       
    34     :   CEGLRendering(aWindow, EGL_OPENGL_ES_API)
       
    35     {
       
    36 
       
    37     }
       
    38 
       
    39 CGLES1Cube::~CGLES1Cube()
       
    40 	{
       
    41 	RDebug::Printf("[EBT] CGLES1Cube::~CGLES1Cube");
       
    42 	glDeleteBuffers(1, &iCoordinateColorBuffer);
       
    43 	glDeleteBuffers(1, &iIndexBuffer);
       
    44 	}
       
    45 
       
    46 /*
       
    47     v6----- v4
       
    48    /|      /|
       
    49   v0------v2|
       
    50   | |     | |
       
    51   | |v7---|-|v5
       
    52   |/      |/
       
    53   v1------v3
       
    54 
       
    55 v0: -1.0, +1.0, +1.0,
       
    56 v1: -1.0, -1.0, +1.0,
       
    57 v2: +1.0, +1.0, +1.0,
       
    58 v3: +1.0, -1.0, +1.0,
       
    59 v4: +1.0, +1.0, -1.0,
       
    60 v5: +1.0, -1.0, -1.0,
       
    61 v6: -1.0, +1.0, -1.0,
       
    62 v7: -1.0, -1.0, -1.0,
       
    63 */
       
    64 
       
    65 static GLfloat CoordinateData[] =
       
    66 	{
       
    67 	-1.0, +1.0, +1.0,	// v0
       
    68 	-1.0, -1.0, +1.0,	// v1
       
    69 	+1.0, +1.0, +1.0,	// v2
       
    70 	+1.0, -1.0, +1.0,	// v3
       
    71 	+1.0, +1.0, -1.0,	// v4
       
    72 	+1.0, -1.0, -1.0,	// v5
       
    73 	-1.0, +1.0, -1.0,	// v6
       
    74 	-1.0, -1.0, -1.0	// v7
       
    75 	};
       
    76 
       
    77 static const TInt CoordinateElementCount = 3; // XYZ
       
    78 
       
    79 static GLfloat ColorData[] =
       
    80 	{
       
    81 	 0.0,  1.0,  1.0,  1.0,	// v0
       
    82 	 0.0,  0.0,  1.0,  1.0,	// v1
       
    83 	 1.0,  1.0,  1.0,  1.0,	// v2
       
    84 	 1.0,  0.0,  1.0,  1.0,	// v3
       
    85 	 1.0,  1.0,  0.0,  1.0,	// v4
       
    86 	 1.0,  0.0,  0.0,  1.0,	// v5
       
    87 	 0.0,  1.0,  0.0,  1.0,	// v6
       
    88 	 0.0,  0.0,  0.0,  1.0	// v7
       
    89 	};
       
    90 
       
    91 static const TInt ColorElementCount = 4; // RGBA
       
    92 
       
    93 static GLubyte FaceData[] =
       
    94 	{
       
    95 	0, 1, 3, 2, // front
       
    96 	2, 3, 5, 4, // right
       
    97 	4, 5, 7, 6, // left
       
    98 	6, 7, 1, 0, // back
       
    99 	6, 0, 2, 4, // top
       
   100 	1, 7, 5, 3
       
   101 	};
       
   102 
       
   103 static const TInt VerticesPerFace = 4;
       
   104 static const TInt VertexCount = sizeof(FaceData) / sizeof(GLubyte);
       
   105 static const TInt FaceCount = VertexCount / VerticesPerFace;
       
   106 
       
   107 static const TInt TrianglesPerFace = 2;
       
   108 static const TInt VerticesPerTriangle = 3;
       
   109 
       
   110 static const TInt IndexCount = FaceCount * TrianglesPerFace * VerticesPerTriangle;
       
   111 
       
   112 static GLubyte FaceIndexData[] =
       
   113 	{
       
   114 	0, 2, 1,
       
   115 	0, 3, 2
       
   116 	};
       
   117 
       
   118 void CGLES1Cube::KhrSetup()
       
   119     {
       
   120 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup");
       
   121 
       
   122 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup vertexCount %d", VertexCount);
       
   123 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup faceCount %d", FaceCount);
       
   124 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup indexCount %d", IndexCount);
       
   125 
       
   126 	const GLsizeiptr coordinateSize = CoordinateElementCount * sizeof(GLfloat);
       
   127 	const GLsizeiptr coordinateDataSize = VertexCount * coordinateSize;
       
   128 	const GLsizeiptr colorSize = ColorElementCount * sizeof(GLfloat);
       
   129 	const GLsizeiptr colorDataSize = VertexCount * colorSize;
       
   130 
       
   131 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glViewport %d x %d", WindowSize().iWidth, WindowSize().iHeight);
       
   132 	glViewport(0, 0, WindowSize().iWidth, WindowSize().iHeight);
       
   133 	GLCheckError();
       
   134 
       
   135 	glEnable(GL_DEPTH_TEST);
       
   136 	glEnable(GL_CULL_FACE);
       
   137 	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
       
   138 
       
   139 	// Create coordinate/color buffer
       
   140 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glGenBuffers vertex/color");
       
   141 	glGenBuffers(1, &iCoordinateColorBuffer);
       
   142 	GLCheckError();
       
   143 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glBindBuffer vertex/color");
       
   144 	glBindBuffer(GL_ARRAY_BUFFER, iCoordinateColorBuffer);
       
   145 	GLCheckError();
       
   146 
       
   147 	// Allocate memory for coordinate/color buffer
       
   148 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glBufferData coordinate/color");
       
   149 	glBufferData(GL_ARRAY_BUFFER, coordinateDataSize + colorDataSize, 0, GL_STATIC_DRAW);
       
   150 	GLCheckError();
       
   151 
       
   152 	// Upload coordinate/color data
       
   153 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup supply coordinate/color data");
       
   154 	GLubyte indexData[IndexCount];
       
   155 	for (TInt i=0; i<FaceCount; ++i)
       
   156 		{
       
   157 		RDebug::Printf("[EBT] CGLES1Cube::KhrSetup face %d", i);
       
   158 		// Upload coordinate/color data for this face
       
   159 		for (TInt j=0; j<VerticesPerFace; ++j)
       
   160 			{
       
   161 			const TInt vertexIndex = FaceData[i * VerticesPerFace + j];
       
   162 			const TInt destIndex = (i * VerticesPerFace) + j;
       
   163 			const GLfloat *coordSrc = CoordinateData + vertexIndex * CoordinateElementCount;
       
   164 			const TInt coordDest = destIndex * coordinateSize;
       
   165 			const GLfloat *colorSrc = ColorData + vertexIndex * ColorElementCount;
       
   166 			const TInt colorDest = coordinateDataSize + destIndex * colorSize;
       
   167 			RDebug::Printf("[EBT] CGLES1Cube::KhrSetup vertex %d (%d) destIndex %d coordDest %d colorDest %d",
       
   168 					       j, vertexIndex, destIndex, coordDest, colorDest);
       
   169 			RDebug::Printf("[EBT] CGLES1Cube::KhrSetup coord %3.1f %3.1f %3.1f",
       
   170 						   *(coordSrc), *(coordSrc + 1), *(coordSrc + 2));
       
   171 			RDebug::Printf("[EBT] CGLES1Cube::KhrSetup color %3.1f %3.1f %3.1f",
       
   172 						   *(colorSrc), *(colorSrc + 1), *(colorSrc + 2));
       
   173 			glBufferSubData(GL_ARRAY_BUFFER, coordDest, coordinateSize, coordSrc);
       
   174 			GLCheckError();
       
   175 			glBufferSubData(GL_ARRAY_BUFFER, colorDest, colorSize, colorSrc);
       
   176 			GLCheckError();
       
   177 			}
       
   178 
       
   179 		// Store vertex indices for this face
       
   180 		TInt indexDataOffset = i * TrianglesPerFace * VerticesPerTriangle;
       
   181 		TInt indexBase = i * VerticesPerFace;
       
   182 		for (TInt j=0; j<TrianglesPerFace * VerticesPerTriangle; ++j)
       
   183 			{
       
   184 			RDebug::Printf("[EBT] CGLES1Cube::KhrSetup index %d offset %d value %d",
       
   185 						   j, indexDataOffset, indexBase + FaceIndexData[j]);
       
   186 			indexData[indexDataOffset++] = indexBase + FaceIndexData[j];
       
   187 			}
       
   188 		}
       
   189 
       
   190 	// Tell GL engine how to unpack coordinate/color buffer
       
   191 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glVertexPointer");
       
   192 	glVertexPointer(CoordinateElementCount, GL_FLOAT, 0, 0);
       
   193 	GLCheckError();
       
   194 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glColorPointer");
       
   195 	glColorPointer(ColorElementCount, GL_FLOAT, 0, reinterpret_cast<GLvoid*>(coordinateDataSize));
       
   196 	GLCheckError();
       
   197 
       
   198 	// Create index buffer
       
   199 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glGenBuffers index");
       
   200 	glGenBuffers(1, &iIndexBuffer);
       
   201 	GLCheckError();
       
   202 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glBindBuffer index");
       
   203 	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iIndexBuffer);
       
   204 	GLCheckError();
       
   205 
       
   206 	// Allocate memory for index buffer and upload index data
       
   207 	RDebug::Printf("[EBT] CGLES1Cube::KhrSetup glBufferData index");
       
   208 	glBufferData(GL_ELEMENT_ARRAY_BUFFER, IndexCount, indexData, GL_STATIC_DRAW);
       
   209 	GLCheckError();
       
   210 
       
   211 	glEnableClientState(GL_VERTEX_ARRAY);
       
   212 	GLCheckError();
       
   213 	glEnableClientState(GL_COLOR_ARRAY);
       
   214 	GLCheckError();
       
   215 
       
   216 	StartRedrawTimer();
       
   217     }
       
   218 
       
   219 void CGLES1Cube::KhrPaint()
       
   220 	{
       
   221 	RDebug::Printf("[EBT] CGLES1Cube::KhrPaint");
       
   222 
       
   223 	glLoadIdentity();
       
   224 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       
   225 
       
   226 	glMatrixMode(GL_PROJECTION);
       
   227 	glLoadIdentity();
       
   228 	glOrthof(-1.5f, 1.5f, -2.5f, 2.5f, -10.5f, 10.5f);
       
   229 
       
   230 	glMatrixMode(GL_MODELVIEW);
       
   231 	iAngle = (iAngle + 1.0f);
       
   232 	if (iAngle > 360.0f)
       
   233 		{
       
   234 		iAngle -= 360.0f;
       
   235 		}
       
   236 	glRotatef(iAngle, 0.0f, 1.0f, 0.5f);
       
   237 
       
   238 	glDrawElements(GL_TRIANGLES, IndexCount, GL_UNSIGNED_BYTE, 0);
       
   239     }
       
   240