os/graphics/windowing/windowserver/test/t_integ/src/t_pseudoappopenglanimation.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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  @file
    18  @test
    19  @internalComponent
    20 */
    21 
    22 #include "t_pseudoappopenglanimation.h"
    23 
    24 /** Vertex co-ordinates for a cube */
    25 const GLfloat KVertexArray[] = {	-10.0f,  10.0f,  10.0f,
    26 									 10.0f,  10.0f,  10.0f,
    27 									-10.0f, -10.0f,  10.0f,
    28 									 10.0f, -10.0f,  10.0f,
    29 									-10.0f,  10.0f, -10.0f,
    30 									 10.0f,  10.0f, -10.0f,
    31 									-10.0f, -10.0f, -10.0f,
    32 									 10.0f, -10.0f, -10.0f };
    33 
    34 /** Left, near, top co-ordinate index */
    35 const GLubyte LNT = 0;
    36 /** Right, near, top co-ordinate index */
    37 const GLubyte RNT = 1;
    38 /** Left, far, top co-ordinate index */
    39 const GLubyte LFT = 2;
    40 /** Right, far, top co-ordinate index */
    41 const GLubyte RFT = 3;
    42 /** Left, near, bottom co-ordinate index */
    43 const GLubyte LNB = 4;
    44 /** Right, near, bottom co-ordinate index */
    45 const GLubyte RNB = 5;
    46 /** Left, far, bottom co-ordinate index */
    47 const GLubyte LFB = 6;
    48 /** Right, far, bottom co-ordinate index */
    49 const GLubyte RFB = 7;
    50 /** Number of indicies per face */
    51 const TInt KNumFaceIndices = 6;
    52 
    53 /** Array of indicies into vertex array to define a cube */
    54 const GLubyte KIndexArray[] = {	RNB, LNB, LFB, RNB, LFB, RFB,
    55 								RNB, RFB, RFT, RNB, RFT, RNT,
    56 								LNB, RNB, RNT, LNB, RNT, LNT,
    57 								LFB, LNB, LNT, LFB, LNT, LFT,
    58 								RFB, LFB, LFT, RFB, LFT, RFT,
    59 								LNT, RNT, RFT, LNT, RFT, LFT };
    60 
    61 
    62 COpenGLAnimation* COpenGLAnimation::NewL(RWindow* aWin, const TDisplayMode& aMode, const TSize& aSurfaceSize,
    63 																		  const TInt aHorizontalRate,
    64 																		  const TInt aVerticalRate)
    65 	{
    66 	RDebug::Print(_L("Creating COpenGLAnimation class\n"));
    67     COpenGLAnimation* self = new (ELeave) COpenGLAnimation(aMode,aSurfaceSize,aHorizontalRate,aVerticalRate);
    68     CleanupStack::PushL(self);
    69     self->ConstructL(aWin);
    70     CleanupStack::Pop(); // self;
    71     return self;
    72 	}
    73 
    74 COpenGLAnimation::COpenGLAnimation(const TDisplayMode& aMode, const TSize& aSurfaceSize, const TInt aHorizontalRate, const TInt aVerticalRate)
    75 : CEglBase(aMode,aSurfaceSize,aHorizontalRate,aVerticalRate)
    76 	{
    77 	}
    78 
    79 void COpenGLAnimation::ConstructL(RWindow* aWin)
    80 	{
    81 	BaseConstructL(aWin);
    82 
    83 	// eglBindAPI
    84 	TInt ret = eglBindAPI(EGL_OPENGL_ES_API);
    85 	TestL(ret, _L("eglBindAPI failed"));
    86 
    87 	__ASSERT_DEBUG(iUseConfig>0, User::Panic(_L("COpenGLAnimation"), KErrArgument));
    88 
    89 	// eglCreateContext
    90 	iContext = eglCreateContext(iDisplay, iUseConfig, EGL_NO_CONTEXT, NULL);
    91 	TestL(iContext!=EGL_NO_CONTEXT, _L("eglCreateContext failed"));
    92 
    93 	// Make surface and context current on the display object
    94 	ret = eglMakeCurrent(iDisplay, iSurface, iSurface, iContext);
    95 	TestL(ret, _L("eglMakeCurrent failed"));
    96 
    97 	CheckGlErrorL(_L("GL_Problems creating second surface"));
    98 	}
    99 
   100 COpenGLAnimation::~COpenGLAnimation()
   101 	{
   102 	}
   103 
   104 /** Draws a cube using OpenGLES commands */
   105 void COpenGLAnimation::DrawToEglL()
   106 	{
   107 	// Increment rotation variable
   108 	iRotAngle += 15;
   109 
   110 	// Set half-transparent drawing value
   111 	const GLfloat KAlphaValue = 0.5f;
   112 
   113 	// Remove hidden faces
   114 	glEnable(GL_CULL_FACE);
   115 	glCullFace(GL_FRONT);
   116 
   117 	// Use a flat shading model
   118 	glShadeModel(GL_FLAT);
   119 
   120 	// Set the projection parameters
   121 	glMatrixMode(GL_PROJECTION);
   122 	glLoadIdentity();
   123 	glFrustumf(-0.154f, 0.154f, -0.0577f, 0.0577f, 0.1f, 100.0f);
   124 
   125 	// Set the cube position
   126 	glMatrixMode(GL_MODELVIEW);
   127 	glLoadIdentity();
   128 	// Move position away from viewpoitn
   129 	glTranslatef(iX+=iHorizontalRate, 0.0f, -50.0f);
   130 
   131 	// Rotate 45 degrees about the z-axis
   132 	glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
   133 	// Rotate iX degrees about the y- and z-axes
   134 	glRotatef(iRotAngle, 0.0f, 1.0f, 1.0f);
   135 
   136 	// Clear the buffer
   137 	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   138 	glClear(GL_COLOR_BUFFER_BIT);
   139 
   140 	// Enable vertex processing
   141 	glEnableClientState(GL_VERTEX_ARRAY);
   142 
   143 	// Set the vertex array to use
   144 	glVertexPointer(3, GL_FLOAT, 0, KVertexArray);
   145 
   146 	const GLubyte* faceIndex = KIndexArray;
   147 
   148 	// Process cube faces
   149 	for (TInt face = 1; face <= 6; face++)
   150 		{
   151 		const GLfloat red = (face < 2 || face > 4) ? 1.0f : 0;
   152 		const GLfloat green = (face > 0 && face < 4) ? 1.0f : 0;
   153 		const GLfloat blue = (face > 2) ? 1.0f : 0;
   154 		// Set colour to draw in
   155 		glColor4f(red, green, blue, KAlphaValue);
   156 		// Draw the vertices that make up each face as two triangles
   157 		glDrawElements(GL_TRIANGLES, KNumFaceIndices, GL_UNSIGNED_BYTE, faceIndex);
   158 		faceIndex += KNumFaceIndices;
   159 		}
   160 
   161 	// Flush colour buffer to the window surface
   162 	eglSwapBuffers(iDisplay, iSurface);
   163 	}
   164 
   165 void COpenGLAnimation::DrawL(CTestSurfaceRasterizer*  /*aSurfaceRasterizer*/, SurfaceDetails& /*aSurfDetails*/)
   166 	{
   167 	DrawToEglL();
   168 	}
   169 
   170 void COpenGLAnimation::CheckGlErrorL(const TDesC& aMessage)
   171 	{
   172 	GLenum err=glGetError();
   173 	if ( err != GL_NO_ERROR )
   174 		{
   175 		RDebug::Print(_L("Error:0x%x - %S"),err,&aMessage);
   176 		User::Leave(KErrUnknown);
   177 		}
   178 	}