×
Namespaces

Variants
Actions

Primitivas gráficas com OpenGL ES

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

Compatibilidade
Plataforma(s): Symbian^1 and later

Platform Security
Capabilities: none

Artigo
Palavras-chave: OpenGL ES
Criado por cabezonxdg em 11 Aug 2008
Última alteração feita por lpvalente em 20 Jan 2012

Este exemplo é baseado no curso "Using OpenGL ES" ministrado por Jani Vaarala. O pdf completo pode ser encontrado em Developing Mobile 3D Applications With OpenGL ES and M3G.

Maiores informações sobre a utilização do OpenGL ES podem ser encontradas em: Introdução à OpenGL ES

TrianguloEs.h

#include <GLES/egl.h>
#include <w32std.h> //RWindow
 
class TTrianguloEs
{
public:
void Construct(RWindow aWin); // Cria e define o contexto, surfaces, etc
void initGLES(); // Define propriedades básicas do OpenGL ES
TTrianguloEs();
~TTrianguloEs();
private:
CPeriodic* iPeriodic; // Temporizador para atualizar a tela
static TInt Desenhar( TAny* aInstance ); // O desenho da tela é realizado aqui
EGLDisplay iEglDisplay;
EGLSurface iEglSurface;
EGLContext iEglContext;
 
RWindow iWin; // Janela do sistema nativo de janelas do Symbian
TInt iFrame;
};

TrianguloEs.cpp

#include "TrianguloEs.h"
 
TTrianguloEs::TTrianguloEs()
{
 
}
 
TTrianguloEs::~TTrianguloEs()
{
eglMakeCurrent (iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface (iEglDisplay, iEglSurface);
eglDestroyContext (iEglDisplay, iEglContext);
eglTerminate (iEglDisplay);
}
 
// vertices do triangulo
 
static const GLbyte vertices[] =
{
-1, 1, 0, // x,y,z
1, -1, 0,
1, 1, 0
};
 
static const GLubyte colors[3 * 4] =
{
255, 0, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255
};
 
void TTrianguloEs::initGLES()
{
glClearColor (0.f,0.f,0.1f,1.f);
glDisable (GL_DEPTH_TEST);
glMatrixMode (GL_PROJECTION);
glViewport (0,0,240,320);
glFrustumf (-1.f,1.f,-1.f,1.f,3.f,1000.f);
glMatrixMode (GL_MODELVIEW);
glShadeModel (GL_SMOOTH);
glVertexPointer (2,GL_BYTE,0,vertices);
glColorPointer (4,GL_UNSIGNED_BYTE,0,colors);
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_COLOR_ARRAY);
}
 
void TTrianguloEs::Construct(RWindow aWin)
{
EGLint major, minor;
iWin = aWin;
 
// define o display
iEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 
if(iEglDisplay == NULL )
User::Exit(-1);
 
if(eglInitialize(iEglDisplay,&major,&minor) == EGL_FALSE)
User::Exit(-1);
 
EGLConfig config,colorDepth;
EGLint numOfConfigs = 0;
 
// define a quantidade de bits p/ cor
switch( iWin.DisplayMode() )
{
case (EColor4K): { colorDepth = 12; break; }
case (EColor64K): { colorDepth = 16; break; }
case (EColor16M): { colorDepth = 24; break; }
default:
colorDepth = 32;
}
 
EGLint attrib_list[] = {
EGL_BUFFER_SIZE, colorDepth,
EGL_DEPTH_SIZE, 15,
EGL_NONE
};
 
// define a configuração e salva na variável config
if(eglChooseConfig(iEglDisplay,attrib_list,&config,1,&numOfConfigs ) == EGL_FALSE)
User::Exit(-1);
 
iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &iWin, NULL );
 
if( iEglSurface == NULL )
User::Exit(-1);
 
iEglContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
 
if( iEglContext == NULL )
User::Exit(-1);
 
if( eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext ) == EGL_FALSE )
User::Exit(-1);
 
// cria o temporizador para atualizar a tela
iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );
iPeriodic->Start( 100, 100, TCallBack( TPontoEs::Desenhar, this ) );
 
initGLES();
iFrame = 0;
}
 
TInt TTrianguloEs::Desenhar( TAny* aInstance )
{
TPontoEs* instance = (TPontoEs*)aInstance;
 
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
glTranslatef (0,0,-5.f);
glDrawArrays (GL_TRIANGLES,0,3);
eglSwapBuffers (instance->iEglDisplay, instance->iEglSurface);
 
// Mantem a luz de fundo do display acesa
if (!(instance->iFrame%100)) User::ResetInactivityTime();
instance->iFrame++;
 
return 0;
}
This page was last modified on 20 January 2012, at 18:59.
94 page views in the last 30 days.