Ejemplo n.º 1
0
void Sprite2D::draw()
{
	// 各種レンダリングモードの退避
	glPushAttrib( GL_ENABLE_BIT );
	
	// ライティングを無効にする
	glDisable( GL_LIGHTING );

	// zバッファを無効にする
	glDisable( GL_DEPTH_TEST );

	// 面カリングを無効にする
	glDisable( GL_CULL_FACE );

	// カレントカラーを取得する
	GScolor		CurrentColor;
	glGetFloatv( GL_CURRENT_COLOR, (GLfloat*)&CurrentColor );

	// テクスチャをバインドする
	gsBindTexture( mTextureID );

	// モデルビュー変換行列の退避
	glMatrixMode( GL_PROJECTION );
	glPushMatrix();

	// 透視変換行列の設定
	glLoadIdentity();
	gluOrtho2D( 0, 1024, 768, 0 );

	// 視野変換行列の退避
	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();

	glLoadIdentity();

	// 平行移動量の設定を行う
	glTranslatef( mPosition.x, mPosition.y, 0.0f );

	// 回転角度の設定を行う
	glRotatef( mRotate, 0.0f, 0.0f, 1.0f );

	// 拡大縮小を行う
	glScalef( mScale.x, mScale.y, 0.0f );

	// 中心位置の補正を行う
	glTranslatef( -mCenter.x, -mCenter.y, 0 );

	// テクスチャのカラーを設定する
	glColor4fv( (GLfloat*)&mColor );

	// バインド中のテクスチャの幅と高さを取得
	GSsizei		sTexWidth;
	GSsizei		sTexHeight;
	glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &sTexWidth );
	glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &sTexHeight );

	// テクスチャ表示位置を指定されていればそれに従う
	GSrect	rTexCoord;
	GSfloat	fWidth, fHeight;

	if( mRect.left != NULL || mRect.top != NULL ||
		mRect.right != NULL || mRect.bottom != NULL )
	{
		rTexCoord.left = mRect.left / sTexWidth;
		rTexCoord.top = mRect.top / sTexHeight;
		rTexCoord.right = mRect.right / sTexWidth;
		rTexCoord.bottom = mRect.bottom / sTexHeight;

		// 四角形の幅と高さを求める
		fWidth = ABS( mRect.right - mRect.left );
		fHeight = ABS( mRect.bottom - mRect.top );
	}
	// テクスチャの位置が指定されていない場合はテクスチャを全体表示
	else
	{
		rTexCoord.left		= 0.0f;
		rTexCoord.top		= 0.0f;
		rTexCoord.right		= 1.0f;
		rTexCoord.bottom	= 1.0f;

		fWidth = (GLfloat)sTexWidth;
		fHeight = (GLfloat)sTexHeight;
	}

	// 四角形の描画
	glBegin( GL_QUADS );
		glTexCoord2f( rTexCoord.left, rTexCoord.top );
		glVertex2f( 0, 0 );
		glTexCoord2f( rTexCoord.left, rTexCoord.bottom );
		glVertex2f( 0, fHeight );
		glTexCoord2f( rTexCoord.right, rTexCoord.bottom );
		glVertex2f( fWidth, fHeight );
		glTexCoord2f( rTexCoord.right, rTexCoord.top );
		glVertex2f( fWidth, 0 );
	glEnd();

	// モデルビュー変換行列を復帰
	glPopMatrix();

		// 透視変換行列を復帰する
	glMatrixMode( GL_PROJECTION );
	glPopMatrix();

	// モデルビュー変換行列に設定する
	glMatrixMode( GL_MODELVIEW );

	// カレントカラーを復帰する
	glColor4fv( (GLfloat*)&CurrentColor );

	// レンダリングモードの復帰
	glPopAttrib();
}
Ejemplo n.º 2
0
void Renderer::DrawSprite2D(
	GSuint           uTextureID,
	const GSrect*    pSrcRect,
	const GSvector2* pCenter,
	const GSvector2* pScaling,
	GSfloat          fRotation,
	const GSvector2* pTranslation,
	const GScolor*    pColor
	)
{
	GSrect rTexCoord;
	GLfloat fWidth;
	GLfloat fHeight;
	GLsizei sTexWidth;
	GLsizei sTexHeight;
	GScolor CurrentColor;
	
	glPushAttrib(GL_ENABLE_BIT);
	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);
	glGetFloatv(GL_CURRENT_COLOR, (GLfloat*)&CurrentColor);
	gsBindTexture(uTextureID);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0, WINDOW_WIDTH,WINDOW_HEIGHT, 0);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	if (pTranslation != NULL)
	{
		glTranslatef(pTranslation->x, pTranslation->y, 0);
	}
	glRotatef(fRotation, 0, 0, 1);
	if (pScaling != NULL)
	{
		glScalef(pScaling->x, pScaling->y, 0);
	}
	if (pCenter != NULL)
	{
		glTranslatef(-pCenter->x, -pCenter->y, 0);
	}
	if (pColor != NULL)
	{
		glColor4fv((GLfloat*)pColor);
	}
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &sTexWidth);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &sTexHeight);

	if (pSrcRect != NULL)
	{
		rTexCoord.left = pSrcRect->left / sTexWidth;
		rTexCoord.top = pSrcRect->top / sTexHeight;
		rTexCoord.right = pSrcRect->right / sTexWidth;
		rTexCoord.bottom = pSrcRect->bottom / sTexHeight;

		fWidth = ABS(pSrcRect->right - pSrcRect->left);
		fHeight = ABS(pSrcRect->bottom - pSrcRect->top);
	}
	else
	{
		rTexCoord.left = 0;
		rTexCoord.top = 0;
		rTexCoord.right = 1;
		rTexCoord.bottom = 1;
		fWidth = (GSfloat)sTexWidth;
		fHeight = (GSfloat)sTexHeight;
	}

	glBegin(GL_QUADS);
	glTexCoord2f(rTexCoord.left, rTexCoord.top);
	glVertex2f(0, 0);
	glTexCoord2f(rTexCoord.left, rTexCoord.bottom);
	glVertex2f(0, fHeight);
	glTexCoord2f(rTexCoord.right, rTexCoord.bottom);
	glVertex2f(fWidth, fHeight);
	glTexCoord2f(rTexCoord.right, rTexCoord.top);
	glVertex2f(fWidth, 0);
	glEnd();

	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glColor4fv((GLfloat*)&CurrentColor);
	glPopAttrib();
}