技术开发 频道

游戏是这样写成的(四)

  【IT168技术文档】

  有同学们为了怎样用OpenGL ES缩放图像而烦恼,正好我也很久没更新这个教程了,所以把第三篇的代码更新了一下,加了缩放和混色功能。

  我也用了SDK Final的模块,重新建立了一次项目。

  这次的修改,主要是CCSprite的render,大家可以参考一下,怎么用glScalef来做缩放。

  1. void CCSprite::render(float x, float y, float angle, float xScale, float yScale)
  2. {
  3.     y = SCREEN_HEIGHT-y;        // for OpenGL ES, (0,0) is at lower left corner!
  4.    
  5.     GLfloat _minU = mX/mTexture->getTextureWidth();
  6.     GLfloat _maxU = (mX+mWidth)/mTexture->getTextureWidth();
  7.     GLfloat _minV = mY/mTexture->getTextureHeight();
  8.     GLfloat _maxV = (mY+mHeight)/mTexture->getTextureHeight();
  9.    
  10.     GLfloat    coordinates[] =
  11.     {
  12.         _minU,    _maxV,
  13.         _maxU,    _maxV,
  14.         _minU,    _minV,
  15.         _maxU,    _minV
  16.     };
  17.    
  18.     GLfloat    xx = - mWidth/2;
  19.     GLfloat yy = - mHeight/2;
  20.    
  21.     GLfloat    vertices[] =
  22.     {
  23.         xx,            yy,                
  24.         xx+mWidth,    yy,                
  25.         xx,            yy+mHeight,
  26.         xx+mWidth,    yy+mHeight    
  27.     };
  28.    
  29.     mTexture->bind();
  30.    
  31.     glColor4f(mRed, mGreen, mBlue, mAlpha);
  32.    
  33.     glPushMatrix();
  34.     glTranslatef(x, y, 0.0f);
  35.     glRotatef(angle, 0.0f, 0.0f, 1.0f);
  36.     glScalef(xScale, yScale, 1.0f);
  37.     glVertexPointer(2, GL_FLOAT, 0, vertices);
  38.     glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
  39.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  40.     glPopMatrix();
  41.    
  42.     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  43.    
  44. }

     本文例子代码iDemo_4下载:http://www.rayfile.com/files/99bc7214-3bee-11de-8aec-0019d11a795f/

0
相关文章