⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 render.cpp

📁 wince 3d tutorial, it has various examples
💻 CPP
字号:
#include <stdio.h>
#include "render.h"
#include "fixed.h"
#include "texture.h"
#include "mesh.h"
#include "font.h"
#include "timer.h"
#include "particles.h"

EGLDisplay glesDisplay;
EGLSurface glesSurface;
EGLContext glesContext;

extern HWND hWnd; 
extern HDC hDC;   
extern Timer *timer;

int WindowWidth = 0;
int WindowHeight = 0;

BitmappedFont *font = NULL;
Mesh *mesh = NULL;
ParticleSystem *particles = NULL;
Texture *floorTexture = NULL;

bool InitOGLES()
{  
  EGLConfig configs[10];
  EGLint matchingConfigs;	

  const EGLint configAttribs[] =
  {
      EGL_RED_SIZE,       8,
      EGL_GREEN_SIZE,     8,
      EGL_BLUE_SIZE,      8,
      EGL_ALPHA_SIZE,     EGL_DONT_CARE,
      EGL_DEPTH_SIZE,     16,
      EGL_STENCIL_SIZE,   EGL_DONT_CARE,
      EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
      EGL_NONE,           EGL_NONE
  };
  
  hDC = GetWindowDC(hWnd);
  glesDisplay = eglGetDisplay(hDC);	 
  
  if(!eglInitialize(glesDisplay, NULL, NULL)) 
    return false;
	
  if(!eglChooseConfig(glesDisplay, configAttribs, &configs[0], 10,  &matchingConfigs)) 
   return false;
	
  if(matchingConfigs < 1)  return false;	  

  glesSurface = eglCreateWindowSurface(glesDisplay, configs[0], hWnd, configAttribs);	
  if(!glesSurface) return false;
  
  glesContext=eglCreateContext(glesDisplay,configs[0],0,configAttribs);

  if(!glesContext) return false;

  eglMakeCurrent(glesDisplay, glesSurface, glesSurface, glesContext); 
    
  glClearColorx(FixedFromFloat(0.5f), FixedFromFloat(0.5f), FixedFromFloat(0.5f), 0);
  glShadeModel(GL_SMOOTH);  
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);

  RECT r;
  GetWindowRect(hWnd, &r);  
  WindowWidth = r.right - r.left;
  WindowHeight = r.bottom - r.top;
  glViewport(r.left, r.top, WindowWidth, WindowHeight);
  SetPerspective(); 
  //Load all needed resources and create needed objects
  //25 particles, with emmiter at (0,0,0), with a two-bytes-per-pixel raw texture
  particles = new ParticleSystem("./resources/spot.raw",25,0,0,0); 
  mesh = new Mesh("./resources/plane.gsd");
  bool result = mesh->GetState();
  font = new BitmappedFont("./resources/font.raw"); //Load font texture, as an 1-byte raw file
  result &= font->GetState();
  floorTexture = new Texture("./resources/floor.tga",GL_LINEAR_MIPMAP_LINEAR);
  result &= floorTexture->GetState(); 
  return result;
}
//----------------------------------------------------------------------------
void Render()
{  
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);      
  
  //Setup a good point of view
  SetPerspective();
  glLoadIdentity();  
  glTranslatex(0,FixedFromInt(-20),FixedFromInt(-100));
  glRotatex(FixedFromInt(10),ONE,0,0);
  static GLfixed angle = ZERO;
  glRotatex(angle, 0, ONE,0);     
  GLfixed speed = DivideFixed(FixedFromInt(timer->GetTimeBetweenTwoFrames()), FixedFromInt(100));
  angle += speed;
   
  //draw the mesh
  glEnable(GL_TEXTURE_2D);
  floorTexture->BindTexture();
  mesh->Draw();
  glDisable(GL_TEXTURE_2D);
    
  //draw the particles
  particles->DrawParticles(timer->GetTimeBetweenTwoFrames());

  //draw the fps line. 
  SetOrtho2D();
  glLoadIdentity();  
  glColor4x(ONE,ONE,ONE,0);
  BitmappedFont::EnableStates();
  font->Print(0,0,"FPS: %d",timer->GetFPS());
  BitmappedFont::DisableStates();


  eglSwapBuffers(glesDisplay, glesSurface);  
}
//----------------------------------------------------------------------------
void Clean()
{
  if(mesh) delete mesh;
  if(font) delete font;
  if(particles) delete particles;
  if(floorTexture) delete floorTexture;
  if(glesDisplay)
  {
    eglMakeCurrent(glesDisplay, NULL, NULL, NULL);  
    if(glesContext) eglDestroyContext(glesDisplay, glesContext);
    if(glesSurface) eglDestroySurface(glesDisplay, glesSurface);
    eglTerminate(glesDisplay);
  }  
}
//----------------------------------------------------------------------------
void SetOrtho2D()
{  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();    
  glOrthox(FixedFromInt(0), FixedFromInt(WindowWidth), 
           FixedFromInt(0), FixedFromInt(WindowHeight), 
           FixedFromInt(-1), FixedFromInt(1));
  glMatrixMode(GL_MODELVIEW);  
}
//----------------------------------------------------------------------------
void SetPerspective()
{
  RECT r;
  GetWindowRect(hWnd, &r);    
  float ratio = (float)(r.right - r.left)/(r.bottom - r.top);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();        
  Perspective(45.0f,ratio, 1.0f, 200.0f);
  glMatrixMode(GL_MODELVIEW);
}
//----------------------------------------------------------------------------
void Perspective (GLfloat fovy, GLfloat aspect, GLfloat zNear,  GLfloat zFar)
{
  GLfixed xmin, xmax, ymin, ymax, aspectFixed, znearFixed;     
  
  aspectFixed = FixedFromFloat(aspect);
  znearFixed = FixedFromFloat(zNear);

  ymax = MultiplyFixed(znearFixed, FixedFromFloat((GLfloat)tan(fovy * 3.1415962f / 360.0f)));  
  ymin = -ymax;

  xmin = MultiplyFixed(ymin, aspectFixed);
  xmax = MultiplyFixed(ymax, aspectFixed);  
  glFrustumx(xmin, xmax, ymin, ymax, znearFixed, FixedFromFloat(zFar));
}
//----------------------------------------------------------------------------
void SetOrtho()
{  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();    
  glOrthox(FixedFromInt(-100), FixedFromInt(100), 
           FixedFromInt(-100), FixedFromInt(100), 
           FixedFromInt(-100) , FixedFromInt(100));
  glMatrixMode(GL_MODELVIEW);  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -