sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: /** sl@0: @file sl@0: @test sl@0: @internalComponent sl@0: */ sl@0: sl@0: #include "t_pseudoappopenglanimation.h" sl@0: sl@0: /** Vertex co-ordinates for a cube */ sl@0: const GLfloat KVertexArray[] = { -10.0f, 10.0f, 10.0f, sl@0: 10.0f, 10.0f, 10.0f, sl@0: -10.0f, -10.0f, 10.0f, sl@0: 10.0f, -10.0f, 10.0f, sl@0: -10.0f, 10.0f, -10.0f, sl@0: 10.0f, 10.0f, -10.0f, sl@0: -10.0f, -10.0f, -10.0f, sl@0: 10.0f, -10.0f, -10.0f }; sl@0: sl@0: /** Left, near, top co-ordinate index */ sl@0: const GLubyte LNT = 0; sl@0: /** Right, near, top co-ordinate index */ sl@0: const GLubyte RNT = 1; sl@0: /** Left, far, top co-ordinate index */ sl@0: const GLubyte LFT = 2; sl@0: /** Right, far, top co-ordinate index */ sl@0: const GLubyte RFT = 3; sl@0: /** Left, near, bottom co-ordinate index */ sl@0: const GLubyte LNB = 4; sl@0: /** Right, near, bottom co-ordinate index */ sl@0: const GLubyte RNB = 5; sl@0: /** Left, far, bottom co-ordinate index */ sl@0: const GLubyte LFB = 6; sl@0: /** Right, far, bottom co-ordinate index */ sl@0: const GLubyte RFB = 7; sl@0: /** Number of indicies per face */ sl@0: const TInt KNumFaceIndices = 6; sl@0: sl@0: /** Array of indicies into vertex array to define a cube */ sl@0: const GLubyte KIndexArray[] = { RNB, LNB, LFB, RNB, LFB, RFB, sl@0: RNB, RFB, RFT, RNB, RFT, RNT, sl@0: LNB, RNB, RNT, LNB, RNT, LNT, sl@0: LFB, LNB, LNT, LFB, LNT, LFT, sl@0: RFB, LFB, LFT, RFB, LFT, RFT, sl@0: LNT, RNT, RFT, LNT, RFT, LFT }; sl@0: sl@0: sl@0: COpenGLAnimation* COpenGLAnimation::NewL(RWindow* aWin, const TDisplayMode& aMode, const TSize& aSurfaceSize, sl@0: const TInt aHorizontalRate, sl@0: const TInt aVerticalRate) sl@0: { sl@0: RDebug::Print(_L("Creating COpenGLAnimation class\n")); sl@0: COpenGLAnimation* self = new (ELeave) COpenGLAnimation(aMode,aSurfaceSize,aHorizontalRate,aVerticalRate); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aWin); sl@0: CleanupStack::Pop(); // self; sl@0: return self; sl@0: } sl@0: sl@0: COpenGLAnimation::COpenGLAnimation(const TDisplayMode& aMode, const TSize& aSurfaceSize, const TInt aHorizontalRate, const TInt aVerticalRate) sl@0: : CEglBase(aMode,aSurfaceSize,aHorizontalRate,aVerticalRate) sl@0: { sl@0: } sl@0: sl@0: void COpenGLAnimation::ConstructL(RWindow* aWin) sl@0: { sl@0: BaseConstructL(aWin); sl@0: sl@0: // eglBindAPI sl@0: TInt ret = eglBindAPI(EGL_OPENGL_ES_API); sl@0: TestL(ret, _L("eglBindAPI failed")); sl@0: sl@0: __ASSERT_DEBUG(iUseConfig>0, User::Panic(_L("COpenGLAnimation"), KErrArgument)); sl@0: sl@0: // eglCreateContext sl@0: iContext = eglCreateContext(iDisplay, iUseConfig, EGL_NO_CONTEXT, NULL); sl@0: TestL(iContext!=EGL_NO_CONTEXT, _L("eglCreateContext failed")); sl@0: sl@0: // Make surface and context current on the display object sl@0: ret = eglMakeCurrent(iDisplay, iSurface, iSurface, iContext); sl@0: TestL(ret, _L("eglMakeCurrent failed")); sl@0: sl@0: CheckGlErrorL(_L("GL_Problems creating second surface")); sl@0: } sl@0: sl@0: COpenGLAnimation::~COpenGLAnimation() sl@0: { sl@0: } sl@0: sl@0: /** Draws a cube using OpenGLES commands */ sl@0: void COpenGLAnimation::DrawToEglL() sl@0: { sl@0: // Increment rotation variable sl@0: iRotAngle += 15; sl@0: sl@0: // Set half-transparent drawing value sl@0: const GLfloat KAlphaValue = 0.5f; sl@0: sl@0: // Remove hidden faces sl@0: glEnable(GL_CULL_FACE); sl@0: glCullFace(GL_FRONT); sl@0: sl@0: // Use a flat shading model sl@0: glShadeModel(GL_FLAT); sl@0: sl@0: // Set the projection parameters sl@0: glMatrixMode(GL_PROJECTION); sl@0: glLoadIdentity(); sl@0: glFrustumf(-0.154f, 0.154f, -0.0577f, 0.0577f, 0.1f, 100.0f); sl@0: sl@0: // Set the cube position sl@0: glMatrixMode(GL_MODELVIEW); sl@0: glLoadIdentity(); sl@0: // Move position away from viewpoitn sl@0: glTranslatef(iX+=iHorizontalRate, 0.0f, -50.0f); sl@0: sl@0: // Rotate 45 degrees about the z-axis sl@0: glRotatef(45.0f, 0.0f, 0.0f, 1.0f); sl@0: // Rotate iX degrees about the y- and z-axes sl@0: glRotatef(iRotAngle, 0.0f, 1.0f, 1.0f); sl@0: sl@0: // Clear the buffer sl@0: glClearColor(0.0f, 0.0f, 0.0f, 0.0f); sl@0: glClear(GL_COLOR_BUFFER_BIT); sl@0: sl@0: // Enable vertex processing sl@0: glEnableClientState(GL_VERTEX_ARRAY); sl@0: sl@0: // Set the vertex array to use sl@0: glVertexPointer(3, GL_FLOAT, 0, KVertexArray); sl@0: sl@0: const GLubyte* faceIndex = KIndexArray; sl@0: sl@0: // Process cube faces sl@0: for (TInt face = 1; face <= 6; face++) sl@0: { sl@0: const GLfloat red = (face < 2 || face > 4) ? 1.0f : 0; sl@0: const GLfloat green = (face > 0 && face < 4) ? 1.0f : 0; sl@0: const GLfloat blue = (face > 2) ? 1.0f : 0; sl@0: // Set colour to draw in sl@0: glColor4f(red, green, blue, KAlphaValue); sl@0: // Draw the vertices that make up each face as two triangles sl@0: glDrawElements(GL_TRIANGLES, KNumFaceIndices, GL_UNSIGNED_BYTE, faceIndex); sl@0: faceIndex += KNumFaceIndices; sl@0: } sl@0: sl@0: // Flush colour buffer to the window surface sl@0: eglSwapBuffers(iDisplay, iSurface); sl@0: } sl@0: sl@0: void COpenGLAnimation::DrawL(CTestSurfaceRasterizer* /*aSurfaceRasterizer*/, SurfaceDetails& /*aSurfDetails*/) sl@0: { sl@0: DrawToEglL(); sl@0: } sl@0: sl@0: void COpenGLAnimation::CheckGlErrorL(const TDesC& aMessage) sl@0: { sl@0: GLenum err=glGetError(); sl@0: if ( err != GL_NO_ERROR ) sl@0: { sl@0: RDebug::Print(_L("Error:0x%x - %S"),err,&aMessage); sl@0: User::Leave(KErrUnknown); sl@0: } sl@0: }