os/graphics/graphicsdeviceinterface/directgdi/test/tdirectgdieglcontent_cube.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "tdirectgdieglcontent_cube.h"
sl@0
    17
#include "tdisplaymode_mapping.h"
sl@0
    18
#include <fbs.h>
sl@0
    19
#include <graphics/sgimage_sw.h>
sl@0
    20
#include <graphics/fbsdefs.h>
sl@0
    21
sl@0
    22
// CONSTANTS
sl@0
    23
/** Camera parameters */
sl@0
    24
const TInt KCameraDistance = 100;
sl@0
    25
const TReal32 KFrustumLeft = -1.f;	//left vertical clipping plane
sl@0
    26
const TReal32 KFrustumRight = 1.f;	//right vertical clipping plane
sl@0
    27
const TReal32 KFrustumBottom = -1.f;//bottom horizontal clipping plane
sl@0
    28
const TReal32 KFrustumTop = 1.f;	//top horizontal clipping plane
sl@0
    29
const TReal32 KFrustumNear = 3.f;	//near depth clipping plane
sl@0
    30
const TReal32 KFrustumFar = 1000.f;	//far depth clipping plane
sl@0
    31
sl@0
    32
/* Define vertice coordinates for the cube
sl@0
    33
   Duplication of vertices needed for texturing every surface of the cube */
sl@0
    34
static const GLbyte KVertices[24 * 3] =
sl@0
    35
	{
sl@0
    36
	/* top */
sl@0
    37
	-1,  1,  1,
sl@0
    38
	1,  1,  1,
sl@0
    39
	1, -1,  1,
sl@0
    40
	-1, -1,  1,
sl@0
    41
sl@0
    42
	/* front */
sl@0
    43
	1,  1,  1,
sl@0
    44
	1,  1, -1,
sl@0
    45
	1, -1, -1,
sl@0
    46
	1, -1,  1,
sl@0
    47
sl@0
    48
	/* right */
sl@0
    49
	-1,  1,  1,
sl@0
    50
	-1,  1, -1,
sl@0
    51
	1,  1, -1,
sl@0
    52
	1,  1,  1,
sl@0
    53
sl@0
    54
	/* left */
sl@0
    55
	1, -1,  1,
sl@0
    56
	1, -1, -1,
sl@0
    57
	-1, -1, -1,
sl@0
    58
	-1, -1,  1,
sl@0
    59
sl@0
    60
	/* back */
sl@0
    61
	-1, -1,  1,
sl@0
    62
	-1, -1, -1,
sl@0
    63
	-1,  1, -1,
sl@0
    64
	-1,  1,  1,
sl@0
    65
sl@0
    66
	/* bottom */
sl@0
    67
	-1,  1, -1,
sl@0
    68
	1,  1, -1,
sl@0
    69
	1, -1, -1,
sl@0
    70
	-1, -1, -1
sl@0
    71
	};
sl@0
    72
sl@0
    73
/**
sl@0
    74
Indices for drawing the triangles.
sl@0
    75
The color of the triangle is determined by 
sl@0
    76
the color of the last vertex of the triangle.
sl@0
    77
*/
sl@0
    78
static const GLubyte KTriangles[12 * 3] =
sl@0
    79
	{
sl@0
    80
	/* top */
sl@0
    81
	1,0,3,
sl@0
    82
	1,3,2,
sl@0
    83
sl@0
    84
	/* front */
sl@0
    85
	5,4,7,
sl@0
    86
	5,7,6,
sl@0
    87
sl@0
    88
	/* right */
sl@0
    89
	9,8,11,
sl@0
    90
	9,11,10,
sl@0
    91
sl@0
    92
	/* left */
sl@0
    93
	13,12,15,
sl@0
    94
	13,15,14,
sl@0
    95
sl@0
    96
	/* back */
sl@0
    97
	17,16,19,
sl@0
    98
	17,19,18,
sl@0
    99
sl@0
   100
	/* bottom */
sl@0
   101
	21,22,23,
sl@0
   102
	21,23,20
sl@0
   103
	};
sl@0
   104
sl@0
   105
/* Macro for changing the input texture coordinate values from
sl@0
   106
   GLubyte [0,255] to GLbyte [-128,127]. See more info below. */
sl@0
   107
#define tex(u,v) (GLbyte)( (u) - 128 ) , (GLbyte)( (v) - 128 )
sl@0
   108
sl@0
   109
/* Input texture coordinates for each of the object vertices.
sl@0
   110
   The coordinates are given in GLbyte [-128,127] format.
sl@0
   111
   Since the texture picture coordinates are between values
sl@0
   112
   [0,255] for both width and height, we have defined a macro
sl@0
   113
   to change the input coordinates into a appropriate form.
sl@0
   114
   It is easier to think the texture coordinates as corresponding
sl@0
   115
   image pixel coordinates. This alone is not enough because
sl@0
   116
   if texture coordinates are not given between values [0,1],
sl@0
   117
   texture wrapping will occur. Therefore we need to
sl@0
   118
   scale the texture coordinates with appropriate texture matrix,
sl@0
   119
   which is defined in AppInit(). */
sl@0
   120
static const GLbyte KTexCoords[24 * 2] = 
sl@0
   121
	{
sl@0
   122
	/* top */
sl@0
   123
	tex(0,0), 
sl@0
   124
	tex(255,0),
sl@0
   125
	tex(255,255), 
sl@0
   126
	tex(0,255), 
sl@0
   127
sl@0
   128
	/* front */
sl@0
   129
	tex(0,255),
sl@0
   130
	tex(127,255),
sl@0
   131
	tex(127,127),
sl@0
   132
	tex(0,127),
sl@0
   133
sl@0
   134
	/* right */
sl@0
   135
	tex(127,255),
sl@0
   136
	tex(255,255),
sl@0
   137
	tex(255,127),
sl@0
   138
	tex(127,127),
sl@0
   139
sl@0
   140
	/* left */
sl@0
   141
	tex(0,127),
sl@0
   142
	tex(127,127),
sl@0
   143
	tex(127,0),
sl@0
   144
	tex(0,0),
sl@0
   145
sl@0
   146
	/* back */
sl@0
   147
	tex(127,127),
sl@0
   148
	tex(255,127),
sl@0
   149
	tex(255,0),
sl@0
   150
	tex(127,0),
sl@0
   151
sl@0
   152
	/* bottom */
sl@0
   153
	tex(255,255),
sl@0
   154
	tex(255,0),
sl@0
   155
	tex(0,0),
sl@0
   156
	tex(0,255)
sl@0
   157
	};
sl@0
   158
sl@0
   159
/**
sl@0
   160
Static constructor.
sl@0
   161
@param aPixelFormat Pixel format of pixmap buffer.
sl@0
   162
@param aSize Size of pixmap buffer.
sl@0
   163
*/
sl@0
   164
CGLCube* CGLCube::NewL(TUidPixelFormat aPixelFormat, const TSize& aSize)
sl@0
   165
	{
sl@0
   166
	CGLCube* self = NewLC(aPixelFormat, aSize);
sl@0
   167
	CleanupStack::Pop(self);
sl@0
   168
	return self;
sl@0
   169
	}
sl@0
   170
sl@0
   171
CGLCube* CGLCube::NewLC(TUidPixelFormat aPixelFormat, const TSize& aSize)
sl@0
   172
	{
sl@0
   173
	CGLCube* self = new(ELeave) CGLCube();
sl@0
   174
	CleanupStack::PushL(self);
sl@0
   175
	self->ConstructL(aPixelFormat, aSize);
sl@0
   176
	return self;
sl@0
   177
	}
sl@0
   178
sl@0
   179
/**
sl@0
   180
1st phase constructor
sl@0
   181
*/
sl@0
   182
CGLCube::CGLCube()
sl@0
   183
	{
sl@0
   184
	}
sl@0
   185
sl@0
   186
/**
sl@0
   187
2nd phase constructor
sl@0
   188
@param aPixelFormat Pixel format of pixmap buffer.
sl@0
   189
@param aSize Size of pixmap buffer.
sl@0
   190
*/
sl@0
   191
void CGLCube::ConstructL(TUidPixelFormat aPixelFormat, const TSize& aSize)
sl@0
   192
	{
sl@0
   193
	// init graphic environment
sl@0
   194
	User::LeaveIfError(SgDriver::Open());
sl@0
   195
	FbsStartup();
sl@0
   196
	User::LeaveIfError(RFbsSession::Connect());	
sl@0
   197
	InitEglL(aPixelFormat, aSize);
sl@0
   198
	}
sl@0
   199
sl@0
   200
/**
sl@0
   201
Destructor
sl@0
   202
*/
sl@0
   203
CGLCube::~CGLCube()
sl@0
   204
	{
sl@0
   205
	// deinit gfx environment
sl@0
   206
	DeInitEgl();
sl@0
   207
	SgDriver::Close();
sl@0
   208
	RFbsSession::Disconnect();
sl@0
   209
	}
sl@0
   210
sl@0
   211
/**
sl@0
   212
Egl environment initialisation for pixmap surface rendering.
sl@0
   213
@param aPixelFormat Pixel format of pixmap buffer.
sl@0
   214
@param aSize Size of pixmap buffer.
sl@0
   215
*/
sl@0
   216
void CGLCube::InitEglL(TUidPixelFormat aPixelFormat, const TSize& aSize)
sl@0
   217
	{
sl@0
   218
	// Get the display for drawing graphics
sl@0
   219
	iEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
sl@0
   220
	if(iEglDisplay == NULL)
sl@0
   221
		{
sl@0
   222
		_LIT(KGetDisplayFailed, "eglGetDisplay failed");
sl@0
   223
		User::Panic(KGetDisplayFailed, 0);
sl@0
   224
		}
sl@0
   225
sl@0
   226
	// Initialize display
sl@0
   227
	if(eglInitialize(iEglDisplay, NULL, NULL) == EGL_FALSE)
sl@0
   228
		{
sl@0
   229
		_LIT(KInitializeFailed, "eglInitialize failed");
sl@0
   230
		User::Panic(KInitializeFailed, 0);
sl@0
   231
		}
sl@0
   232
sl@0
   233
	// switch api to GLES
sl@0
   234
	eglBindAPI(EGL_OPENGL_ES_API);
sl@0
   235
	
sl@0
   236
	iImageInfo.iSizeInPixels = aSize;
sl@0
   237
	iImageInfo.iPixelFormat =  aPixelFormat;
sl@0
   238
	iImageInfo.iCpuAccess = ESgCpuAccessNone;
sl@0
   239
	iImageInfo.iUsage = ESgUsageOpenGlesTarget|ESgUsageDirectGdiSource;
sl@0
   240
	iImageInfo.iShareable = ETrue;
sl@0
   241
	iImageInfo.iScreenId = KSgScreenIdMain;
sl@0
   242
sl@0
   243
	for(TInt i=0; i<KEglContentBuffers; i++)
sl@0
   244
		{
sl@0
   245
		User::LeaveIfError(iImages[i].Create(iImageInfo, NULL, 0));
sl@0
   246
sl@0
   247
		EGLint numOfConfigs = 0; 
sl@0
   248
sl@0
   249
		// Define properties for the wanted EGLSurface 
sl@0
   250
		const EGLint attribList[] =
sl@0
   251
			{
sl@0
   252
			EGL_MATCH_NATIVE_PIXMAP, (EGLint)&iImages[i],
sl@0
   253
			EGL_SURFACE_TYPE, EGL_PIXMAP_BIT,
sl@0
   254
			EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
sl@0
   255
			EGL_NONE
sl@0
   256
			};
sl@0
   257
sl@0
   258
		// Choose an EGLConfig that best matches to the properties in attribList
sl@0
   259
		if(eglChooseConfig(iEglDisplay, attribList, &iConfig, 1, &numOfConfigs) == EGL_FALSE)
sl@0
   260
			{
sl@0
   261
			_LIT(KChooseConfigFailed, "eglChooseConfig failed");
sl@0
   262
			User::Panic(KChooseConfigFailed, 0);
sl@0
   263
			}
sl@0
   264
sl@0
   265
		const EGLint ppixmapAttribs[] = { EGL_NONE };
sl@0
   266
		iEglSurfaces[i] = eglCreatePixmapSurface(iEglDisplay, iConfig, &iImages[i], ppixmapAttribs);
sl@0
   267
		if(iEglSurfaces[i] == NULL)
sl@0
   268
			{
sl@0
   269
			_LIT(KCreatePixmapSurfaceFailed, "eglCreatePixmapSurface failed");
sl@0
   270
			User::Panic(KCreatePixmapSurfaceFailed, 0);
sl@0
   271
			}
sl@0
   272
		}
sl@0
   273
	iEglContext = eglCreateContext(iEglDisplay, iConfig, EGL_NO_CONTEXT, NULL);
sl@0
   274
	if(iEglContext == NULL)
sl@0
   275
		{
sl@0
   276
		_LIT(KCreateContextFailed, "eglCreateContext failed");
sl@0
   277
		User::Panic(KCreateContextFailed, 0);
sl@0
   278
		}
sl@0
   279
sl@0
   280
	if(eglMakeCurrent(iEglDisplay, iEglSurfaces[iCurrentImage], iEglSurfaces[iCurrentImage], iEglContext) == EGL_FALSE)
sl@0
   281
		{
sl@0
   282
		_LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
sl@0
   283
		User::Panic(KMakeCurrentFailed, 0);
sl@0
   284
		}
sl@0
   285
sl@0
   286
	// Prepare texture map (shaded chessboard)
sl@0
   287
	GLubyte* texData = new(ELeave) GLubyte[64*64*4];
sl@0
   288
	for(TInt i=0; i<64; i++)
sl@0
   289
		{
sl@0
   290
		for(TInt j=0; j<64; j++)
sl@0
   291
			{
sl@0
   292
			if((i&8)^(j&8)) // switch 'white' and 'black' fields
sl@0
   293
				{
sl@0
   294
				texData[i*64*4+j*4+0] = i*4; // r
sl@0
   295
				texData[i*64*4+j*4+1] = j*4; // g
sl@0
   296
				texData[i*64*4+j*4+2] = (i+j)*2; // b
sl@0
   297
				}
sl@0
   298
			else
sl@0
   299
				{
sl@0
   300
				texData[i*64*4+j*4+0] = 255-i*4; // r
sl@0
   301
				texData[i*64*4+j*4+1] = 255-j*4; // g
sl@0
   302
				texData[i*64*4+j*4+2] = 255-(i+j)*2; // b
sl@0
   303
				}
sl@0
   304
			texData[i*64*4+j*4+3] = 255; // alpha
sl@0
   305
			}
sl@0
   306
		}
sl@0
   307
sl@0
   308
	// Generate texture
sl@0
   309
	glEnable(GL_TEXTURE_2D);
sl@0
   310
	glGenTextures(1, &iTexId);
sl@0
   311
	glBindTexture(GL_TEXTURE_2D, iTexId);
sl@0
   312
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
sl@0
   313
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
sl@0
   314
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
sl@0
   315
	delete [] texData;
sl@0
   316
	}
sl@0
   317
sl@0
   318
/**
sl@0
   319
Egl environment destroying.
sl@0
   320
*/
sl@0
   321
void CGLCube::DeInitEgl()
sl@0
   322
	{
sl@0
   323
	glDeleteTextures(1, &iTexId);
sl@0
   324
	eglMakeCurrent(iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
sl@0
   325
	eglDestroyContext(iEglDisplay, iEglContext);
sl@0
   326
	for(TInt i=0; i<KEglContentBuffers; i++)
sl@0
   327
		{
sl@0
   328
		eglDestroySurface(iEglDisplay, iEglSurfaces[i]);
sl@0
   329
		iImages[i].Close();
sl@0
   330
		}
sl@0
   331
	eglTerminate(iEglDisplay);
sl@0
   332
	eglReleaseThread();
sl@0
   333
	}
sl@0
   334
sl@0
   335
/**
sl@0
   336
Render frame of spinning cube.
sl@0
   337
@param aFrame Number of frame to render.
sl@0
   338
*/
sl@0
   339
void CGLCube::Render(TInt aFrame)
sl@0
   340
	{
sl@0
   341
	if(eglMakeCurrent(iEglDisplay, iEglSurfaces[iCurrentImage], iEglSurfaces[iCurrentImage], iEglContext) == EGL_FALSE)
sl@0
   342
		{
sl@0
   343
		_LIT(KMakeCurrentFailed, "eglMakeCurrent failed");
sl@0
   344
		User::Panic(KMakeCurrentFailed, 0);
sl@0
   345
		}
sl@0
   346
sl@0
   347
	// Set the screen background color. 
sl@0
   348
	glClearColor(0.f, 0.f, 0.f, 1.f);
sl@0
   349
sl@0
   350
	// Enable back face culling, texturing, and normalization.
sl@0
   351
	glEnable(GL_CULL_FACE);
sl@0
   352
	glEnable(GL_TEXTURE_2D);
sl@0
   353
	glEnable(GL_NORMALIZE);
sl@0
   354
sl@0
   355
	// Initialize viewport and projection. 
sl@0
   356
	glViewport(0, 0, iImageInfo.iSizeInPixels.iWidth, iImageInfo.iSizeInPixels.iHeight);
sl@0
   357
sl@0
   358
	// Calculate the view frustrum
sl@0
   359
	GLfloat aspectRatio = (GLfloat)(iImageInfo.iSizeInPixels.iWidth) / (GLfloat)(iImageInfo.iSizeInPixels.iHeight);
sl@0
   360
	glMatrixMode(GL_PROJECTION);
sl@0
   361
	glLoadIdentity();
sl@0
   362
	glFrustumf(KFrustumLeft * aspectRatio, KFrustumRight * aspectRatio,
sl@0
   363
			KFrustumBottom, KFrustumTop,
sl@0
   364
			KFrustumNear, KFrustumFar);
sl@0
   365
sl@0
   366
	/* Initialize appropriate texture matrix. First we have to translate the
sl@0
   367
       input texture coordinate values to be within a range of [0,255]. Hence
sl@0
   368
       we translate the x- and y-coordinate values by 128. Recall that the 
sl@0
   369
       values in nokTexCoords are between [-128,127], now they are in a range 
sl@0
   370
       of [0,255]. After that we scale the values by 1/255 to make the values 
sl@0
   371
       to be in range [0,1]. */
sl@0
   372
	glMatrixMode(GL_TEXTURE);
sl@0
   373
	glLoadIdentity();
sl@0
   374
	glScalef(1.0f/255.0f, 1.0f/255.0f, 1.0f);
sl@0
   375
	glTranslatef(128.0f, 128.0f, 0.0f);
sl@0
   376
sl@0
   377
	// Remember to change the matrix mode to modelview.
sl@0
   378
	glMatrixMode(GL_MODELVIEW);
sl@0
   379
sl@0
   380
	// Enable vertex and texture arrays.
sl@0
   381
	glEnableClientState(GL_VERTEX_ARRAY);
sl@0
   382
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
sl@0
   383
sl@0
   384
	// Set array pointers. 
sl@0
   385
	glVertexPointer(3, GL_BYTE, 0, KVertices);
sl@0
   386
	glTexCoordPointer(2, GL_BYTE, 0, KTexCoords);
sl@0
   387
sl@0
   388
	// Set the initial shading mode 
sl@0
   389
	glShadeModel(GL_FLAT);
sl@0
   390
sl@0
   391
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
sl@0
   392
sl@0
   393
	glClear(GL_COLOR_BUFFER_BIT);
sl@0
   394
sl@0
   395
	// Animate and draw box 
sl@0
   396
	glLoadIdentity();
sl@0
   397
	glTranslatex(0 , 0 , -KCameraDistance << 16);
sl@0
   398
	glRotatex(aFrame << 18, 1 << 16,    0   ,    0   );
sl@0
   399
	glRotatex(aFrame << 17,    0   , 1 << 16,    0   );
sl@0
   400
	glRotatex(aFrame << 16,    0   ,    0   , 1 << 16);
sl@0
   401
	glScalex(20 << 16, 20 << 16, 20 << 16);
sl@0
   402
sl@0
   403
	glBindTexture(GL_TEXTURE_2D, iTexId);
sl@0
   404
	glDrawElements(GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, KTriangles);
sl@0
   405
sl@0
   406
	iLastImage = iCurrentImage;
sl@0
   407
	}
sl@0
   408
sl@0
   409
/**
sl@0
   410
Get image id of current frame. Current image to render is switch to next.
sl@0
   411
@param aId Reference to drawable id class to store image id.
sl@0
   412
*/
sl@0
   413
void CGLCube::GetImage(TSgDrawableId& aId)
sl@0
   414
	{
sl@0
   415
	// finish rendering
sl@0
   416
	glFinish();
sl@0
   417
sl@0
   418
	aId = iImages[iLastImage].Id();
sl@0
   419
	// switch to next buffer to prevent overdraw of image by asynchronous rendering
sl@0
   420
	if(iLastImage == iCurrentImage)
sl@0
   421
		{
sl@0
   422
		iCurrentImage = (iCurrentImage+1)%KEglContentBuffers;
sl@0
   423
		}
sl@0
   424
	}