在群里有同學們為了怎樣用 OpenGL ES 縮放圖像而煩惱,正好我也很久沒更新這個教程了,所以把第三篇的代碼更新了一下,加了縮放和混色的功能。
我也用了 SDK Final 的模塊,重新建立了一次項目。
這次的修改,主要是 CCSprite 的 render, 大家可以參考一下,怎麼用glScalef 來作縮放。
复制代码
- void CCSprite::render(float x, float y, float angle, float xScale, float yScale)
- {
- y = SCREEN_HEIGHT-y; // for OpenGL ES, (0,0) is at lower left corner!
-
- GLfloat _minU = mX/mTexture->getTextureWidth();
- GLfloat _maxU = (mX+mWidth)/mTexture->getTextureWidth();
- GLfloat _minV = mY/mTexture->getTextureHeight();
- GLfloat _maxV = (mY+mHeight)/mTexture->getTextureHeight();
-
- GLfloat coordinates[] =
- {
- _minU, _maxV,
- _maxU, _maxV,
- _minU, _minV,
- _maxU, _minV
- };
-
- GLfloat xx = - mWidth/2;
- GLfloat yy = - mHeight/2;
-
- GLfloat vertices[] =
- {
- xx, yy,
- xx+mWidth, yy,
- xx, yy+mHeight,
- xx+mWidth, yy+mHeight
- };
-
- mTexture->bind();
-
- glColor4f(mRed, mGreen, mBlue, mAlpha);
-
- glPushMatrix();
- glTranslatef(x, y, 0.0f);
- glRotatef(angle, 0.0f, 0.0f, 1.0f);
- glScalef(xScale, yScale, 1.0f);
- glVertexPointer(2, GL_FLOAT, 0, vertices);
- glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- glPopMatrix();
-
- glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-
- }
|