Автор: quasist Вторник, 01 июня 2010, 23:52
Учитывая практическое отсутствия RTFM по теме, создаю данный топик.
Конечно, как вариант можно попытаться разобраться в библиотеке DGE, но если вас сезонная алергия на ООП...
Необходимые заголовочные файлы из SDK, относящиеся к запуску OpenGL ES.
CODE
#include "panel/fake_OS.h"
#include "OpenGLES/gl.h"
#include "OpenGLES/egl.h"
#include "OpenGLES/glext.h"
В приложении - 10 килобайтная демка в архиве. 10 спрайтов летят навстречу ещё одному
Left-Right - поворот камеры вокруг оси Z.
A-X-B - вкл\выкл GL_FOG, GL_BLEND, GL_TEXTURE
Исходный код под спойлером
» Нажмите, для открытия спойлера «
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <SDL.h>
#ifdef GP2X
#include "panel/fake_OS.h"
#include "OpenGLES/gl.h"
#include "OpenGLES/egl.h"
#include "OpenGLES/glext.h"
EGLDisplay glDisplay;
EGLConfig glConfig;
EGLContext glContext;
EGLSurface glSurface;
NativeWindowType hNativeWnd = 0;
const char *gl_vendor;
const char *gl_renderer;
const char *gl_version;
const char *gl_extensions;
EGLint attrib_list_fsaa[] =
{
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BUFFER_SIZE, 16,
EGL_DEPTH_SIZE, 16,
EGL_SAMPLE_BUFFERS, 1,
EGL_SAMPLES, 4,
EGL_NONE
};
EGLint attrib_list[] =
{
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BUFFER_SIZE, 16,
EGL_DEPTH_SIZE, 16,
EGL_NONE
};
#endif
/* GP2X button mapping */
enum MAP_KEY
{
VK_UP , // 0
VK_UP_LEFT , // 1
VK_LEFT , // 2
VK_DOWN_LEFT , // 3
VK_DOWN , // 4
VK_DOWN_RIGHT , // 5
VK_RIGHT , // 6
VK_UP_RIGHT , // 7
VK_START , // 8
VK_SELECT , // 9
VK_FL , // 10
VK_FR , // 11
VK_FA , // 12
VK_FB , // 13
VK_FX , // 14
VK_FY , // 15
VK_VOL_UP , // 16
VK_VOL_DOWN , // 17
VK_TAT // 18
};
SDL_Joystick *joy = NULL;
#define NUM 1
int texture_i[16][16]=
{
{0,0,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0},
{0,0,0,0, 1,1,1,0, 0,1,0,0, 0,0,0,0},
{0,0,0,0, 1,1,1,1, 1,1,1,1, 0,0,0,0},
{0,0,0,1, 1,1,2,2, 2,2,2,1, 1,1,0,0},
{0,0,1,1, 1,2,2,2, 2,2,2,1, 1,1,1,0},
{0,0,1,1, 1,1,2,2, 2,2,2,2, 1,1,1,0},
{0,1,1,1, 1,1,1,2, 2,2,2,2, 1,1,1,0},
{1,1,1,1, 1,3,3,3, 1,2,2,2, 1,1,1,1},
{0,0,0,1, 1,1,1,3, 1,1,2,2, 2,0,0,0},
{0,0,1,1, 1,1,1,3, 3,3,1,2, 1,1,1,0},
{0,0,0,0, 0,1,1,1, 1,3,1,1, 1,1,1,0},
{0,0,1,1, 1,1,1,1, 1,1,1,1, 1,0,0,0},
{0,0,0,1, 1,1,1,1, 0,1,1,1, 0,0,0,0},
{0,0,0,0, 1,0,0,0, 0,0,1,0, 0,0,0,0},
{0,0,0,0, 1,0,0,0, 0,0,1,0, 0,0,0,0}
};
GLfixed verts[] = {
-65536, -65536, 0,
-65536, 65536, 0,
65536, 65536, 0,
65536, 65536, 0,
-65536, -65536, 0,
65536, -65536, 0
};
GLfixed tex_c[] = {
0, 0,
0, 65536,
65536, 65536,
65536, 65536,
0, 0,
65536, 0
};
GLubyte colors[] = { 255,0,0,255, 0,255,0,255, 0,0,255,255, 255,0,0,255, 0,255,0,255, 0,0,255,255};
GLubyte tex_RGBA[1024];
GLuint texture[1];
float fogcolor[4];
int i,k,axis;
#define S_O 32
signed int scene[S_O][6][2];
int fog,blend,texx;
int main (int argc, char *argv[])
{
int done,x,y;
SDL_Init (SDL_INIT_JOYSTICK);
if (SDL_NumJoysticks() > 0) joy = SDL_JoystickOpen(0);
#ifdef GP2X
EGLint numConfigs;
EGLint majorVersion;
EGLint minorVersion;
hNativeWnd = OS_CreateWindow();
glDisplay = eglGetDisplay( (NativeDisplayType)0 );
eglInitialize( glDisplay, &majorVersion, &minorVersion );
eglChooseConfig( glDisplay, attrib_list, &glConfig, 1, &numConfigs);
glSurface = eglCreateWindowSurface( glDisplay, glConfig, hNativeWnd, attrib_list);
glContext = eglCreateContext( glDisplay, glConfig, EGL_NO_CONTEXT, attrib_list);
eglMakeCurrent( glDisplay, glSurface, glSurface, glContext );
for (k=0;k<S_O;k++)
scene[k][1][0]+=10*65536;
for (x=0;x<16;x++)
for (y=0;y<16;y++)
{
if (texture_i[x][y]==1) {tex_RGBA[y*64+x*4]=255;tex_RGBA[y*64+x*4+3]=255;}
else
if (texture_i[x][y]==2) {tex_RGBA[y*64+x*4+1]=255;tex_RGBA[y*64+x*4+3]=255;}
else
if (texture_i[x][y]==3) {tex_RGBA[y*64+x*4+2]=255;tex_RGBA[y*64+x*4+3]=255;}
}
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16,16,0,GL_RGBA,GL_UNSIGNED_BYTE,tex_RGBA);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
#endif
done = 0;
i=128;
while (!done)
{
i++;
//glFlush();
#ifdef GP2X
float c=(i %256);
c=c/256;
glClearColor(0.5, 0, c, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
//done=1;
glPushMatrix();
glFrustumx(-65536, 65536, 65536*3/4, -65536*3/4, 65536, 40*65536);
glViewport(0, 0, 320, 240);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glTexCoordPointer(2, GL_FIXED, 0, tex_c);
glVertexPointer(3, GL_FIXED, 0, verts);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
fogcolor[0]=0.5f;
fogcolor[1]=c/2;
fogcolor[2]=c;
fogcolor[3]=1.0f;
glFogfv(GL_FOG_COLOR, fogcolor);
glFogf(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, 0.35f);
glHint(GL_FOG_HINT, GL_NICEST);
glFogx(GL_FOG_START, 35*65536);
glFogx(GL_FOG_END, 40*65536);
if (fog%2==0) glEnable(GL_FOG);
if (blend%2==0) glEnable(GL_BLEND);
if (texx%2==0) glEnable(GL_TEXTURE_2D);
glRotatex(axis*65536,0,0,65536);
glTranslatex(0,0,-1*65536);
glDrawArrays(GL_TRIANGLES, 0, 6);
glTranslatex(0,0,-3*65536);
for (k=0;k<S_O;k++)
{
glPushMatrix();
glTranslatex(scene[k][0][0],scene[k][1][0],scene[k][2][0]);
glRotatex(scene[k][3][0],65536,0,0);
glRotatex(scene[k][4][0],0,65536,0);
glRotatex(scene[k][5][0],0,0,65536);
glDrawArrays(GL_TRIANGLES, 0, 6);
glPopMatrix();
scene[k][0][0]+=scene[k][0][1];
scene[k][1][0]+=scene[k][1][1];
scene[k][2][0]+=scene[k][2][1];
scene[k][3][0]+=scene[k][3][1];
scene[k][4][0]+=scene[k][4][1];
scene[k][5][0]+=scene[k][5][1];
if (scene[k][1][0]>5*65536)
{
scene[k][0][0]=(k-16)*10000;
scene[k][1][0]=-1*65536-k*3000;
scene[k][2][0]=k*5000;
scene[k][2][1]=-500;
scene[k][3][0]=i*65536;
scene[k][4][0]=i*k*65536;
scene[k][5][0]=i*65536+2*k*65536;
/*
scene[k][0][1]=(1*rand()-2)*128;
scene[k][2][1]=(1*rand()-2)*128;
scene[k][3][1]=(1*rand()-2)*128;
scene[k][4][1]=(1*rand()-2)*128;
scene[k][5][1]=(1*rand()-2)*128;
*/
scene[k][1][1]=200+(50*i*(k+1))%1024;
scene[k][3][1]=(200+(50*i*(k+1))%1024)*10;
scene[k][4][1]=(200+(50*i*(k+1))%1024)*10;
scene[k][5][1]=(200+(50*i*(k+1))%1024)*10;
}
}
glPopMatrix();
if (texx%2==0) glDisable(GL_TEXTURE_2D);
if (blend%2==0) glDisable(GL_BLEND);
if (fog%2==0) glDisable(GL_FOG);
eglSwapBuffers(glDisplay, glSurface);
SDL_JoystickUpdate();
if (SDL_JoystickGetButton(joy,VK_START)) done=1;
if (SDL_JoystickGetButton(joy,VK_LEFT)) axis--;
if (SDL_JoystickGetButton(joy,VK_RIGHT)) axis++;
if (SDL_JoystickGetButton(joy,VK_FA)) fog++;
if (SDL_JoystickGetButton(joy,VK_FB)) texx++;
if (SDL_JoystickGetButton(joy,VK_FX)) blend++;
//if (i>1000) done=1;
#endif
}
SDL_Quit();
#ifdef GP2X
eglDestroySurface(glDisplay, glSurface);
eglDestroyContext(glDisplay, glContext);
eglTerminate(glDisplay);
free(hNativeWnd);
chdir("/usr/gp2x");
execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
#endif
return 0;
}