×
現在地メニュー >> Cg >> CgFX編01 >> CgFX編01::まとめ

問題

Cg編03::まとめのプログラムを修正して、「.cgfxファイル」を使って透視投影を行え。

答え

myCgEffect.cgfx

//cgfxのグローバル変数
//4x4の透視投影変換行列を取得
float4x4 WorldViewProjection;


//-----------------------------------------------------------//
//                ここから頂点シェーダの記述                 //
//-----------------------------------------------------------//
//入力頂点
struct VertexIn
{
        float4 position : POSITION;
        float3 color : COLOR;
};

//出力頂点
struct VertexOut
{
        float4 position : POSITION;
        float3 color : COLOR;
};

//-------- 頂点シェーダメイン関数 ---------//
VertexOut CgVertexMain(in VertexIn input,uniform float4x4 modelViewProj)
{
        VertexOut output;//出力用
        output.position = mul(modelViewProj, input.position);
        output.color = input.color;//色

        return output;
}



//-----------------------------------------------------------//
//            ここからフラグメントシェーダの記述             //
//-----------------------------------------------------------//
//フラグメントの入力
struct FragmentIn
{
        float4 position : POSITION; //ラスタライズ用(使用しない)
        float3 color : COLOR;
};

//フラグメントの出力
struct FragmentOut
{
        float3 color : COLOR;
};

//--------- フラグメントシェーダメイン関数 ------------//
FragmentOut CgFragmentMain(in FragmentIn input)
{
        FragmentOut output;//フラグメント出力

        output.color = input.color;

        return output;
}

//テクニック
technique Technique0
{
	pass Pass0
	{
		Zenable = true;//デプステストON

		VertexShader = compile arbvp1 CgVertexMain( WorldViewProjection );
		PixelShader  = compile arbfp1 CgFragmentMain();
	}

}

main.cpp

#include <iostream>
#include <GL/glut.h>
#include <Cg/cg.h>
#include <Cg/cgGL.h>

#pragma comment (lib,"cg.lib")
#pragma comment (lib,"cgGL.lib")


//-------- 各種外部変数 -------------//
//Cg用変数
CGcontext CgContext; //コンテキスト
CGparameter CgModelViewProj; //CGparameter型(ModelProj行列を取得)

CGeffect effect;//Cgfxのエフェクト変数
CGtechnique techniques;//CgFXのテクニック変数



//-------- プロトタイプ宣言 -----//
void display();
void reshape(int w, int h);
void drawXYZ();

//-------- Cgの初期設定 -----------//
void CgINIT()
{
	//コンテキスト作成
	CgContext = cgCreateContext();
	if(CgContext == NULL){
		std::cerr << "Can't Create Context\n";
		exit(0);
	}

	//ここからCgFXの設定
	cgGLRegisterStates(CgContext);//cgfx用にコンテキストを登録

	//CgFXファイルを読み込む
	effect = cgCreateEffectFromFile(CgContext,"myCgEffect.cgfx",NULL);
	if (!effect) {
		fprintf(stderr, "Unable to create effect!\n");
		const char *listing = cgGetLastListing(CgContext);
		if (listing)
		fprintf(stderr, "%s\n", listing);
		exit(1);
	}

	//テクニックを取り出す
	techniques = cgGetFirstTechnique(effect);


	//関連付け
	CgModelViewProj = cgGetNamedEffectParameter(effect, "WorldViewProjection");

}


//------------ GLUTの初期設定 ---------------//
void GLUT_CALL_FUNC()
{
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
}

void OtherMyInit()
{
	glClearColor(1,1,1,1);
}

void GLUT_INIT(int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(640,480);
	glutCreateWindow("Hello CgFX");
	GLUT_CALL_FUNC();
	OtherMyInit();

}

//---------- メイン関数 ---------------//
int main(int argc,char **argv)
{

	GLUT_INIT(argc,argv);

	CgINIT();//Cgセットアップ

	glutMainLoop();

	//Cg、CgFXオブジェクトの破棄
	cgDestroyEffect(effect);
	cgDestroyContext(CgContext);

	return 0;
}



//----------- ここから各種コールバック -----------------//
void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	gluLookAt(3,4,5,0,0,0,0,1,0);

	//現在のmodelview,projection行列を取得
	cgGLSetStateMatrixParameter(CgModelViewProj,CG_GL_MODELVIEW_PROJECTION_MATRIX,CG_GL_MATRIX_IDENTITY);

	CGpass pass;
	pass = cgGetFirstPass(techniques);
	std::cout << cgGetPassName(pass) << "\n";
	while(pass)
	{
		cgSetPassState(pass);
		glColor3f(0,0,1);
		glutWireSphere(1.0,30,30);
		cgResetPassState(pass);
		pass = cgGetNextPass(pass);
	}


	drawXYZ();
	glutSwapBuffers();
}

void reshape(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
	glMatrixMode(GL_MODELVIEW);
}


void drawXYZ()
{
	glBegin(GL_LINES);

	glColor3f(1,0,0);
	glVertex3f(0,0,0);
	glVertex3f(200,0,0);

	glColor3f(0,1,0);
	glVertex3f(0,0,0);
	glVertex3f(0,200,0);

	glColor3f(0,0,1);
	glVertex3f(0,0,0);
	glVertex3f(0,0,200);

	glEnd();

	glColor3f(1,1,1);
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます