現在地メニュー >> NVIDIA_SDK >> NVIDIA_SDK_06 >> NVIDIA_SDK_06::Part1 >> NVIDIA_SDK_06::まとめ1
関連:NVIDIA_SDK_06::まとめ2NVIDIA_SDK_06::まとめ3

問題

ラベル、チェックボタン、ボタンを用意し、チェックボタンとボタンを押した時には、
描画物の色が変わるようにせよ。

答え

#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>
#include <NV/nvGlutManipulators.h>
#include <NV/nvGlutWidgets.h>

//libファイルの依存指定
#pragma comment(lib,"nvWidgets.lib")


//------- 各種外部変数 --------//
nv::GlutExamine manipulator; //マウス操作用マニピュレータ
nv::GlutUIContext g_ui;//nvWidget用
bool optionForButtom;//

void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
void timer(int value);
void DrawUI();



//------------- OpenGLの初期設定 -------------------------//
void GLUT_CALL_FUNCs()
{
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(myMouseFunc);
	glutMotionFunc(myMouseMotion);
	glutTimerFunc(0,timer,17);
}

void OtherMyInit()
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glEnable(GL_DEPTH_TEST);
}

void GLUT_INITs(int *argcp, char **argv)
{
	glutInit(argcp,argv);
	glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(640,480);
	glutCreateWindow("NVIDIA SDK nvWidget Sample 1");
	GLUT_CALL_FUNCs();

	OtherMyInit();
}


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

	GLUT_INITs(&argc,argv);

	manipulator.setTrackballScale(2.0);//トラックボール(回転処理の速度)
	manipulator.setDollyActivate( GLUT_LEFT_BUTTON, GLUT_ACTIVE_CTRL);//左ドラッグ+ctl → dolly移動処理
	manipulator.setPanActivate( GLUT_LEFT_BUTTON, GLUT_ACTIVE_SHIFT);//左ドラッグ+Shfit → pan移動処理

	//glewの初期化
	GLenum err;
	err = glewInit();
	if (err != GLEW_OK){
		std::cerr << glewGetErrorString(err) << "\n"; //エラーを出力
		return -1;
	}

	glutMainLoop();

	return 0;
}

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

	manipulator.applyTransform();//移動を適用する(回転、平行、拡大縮小など)

	optionForButtom ? glColor3f(1,0,0) : glColor3f(1,1,0) ;
	glutWireTeapot(1.0);
	DrawUI();

	glutSwapBuffers();
}

void reshape(int w, int h)
{
	g_ui.reshape(w, h);
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(30.0, (double)w / (double)h, 0.1, 1000.0);
	glMatrixMode(GL_MODELVIEW);

	manipulator.reshape(w, h);
}


void myMouseFunc(int button,int state,int x,int y)
{
	//UIの方にフォーカスがない時
	if(!g_ui.isOnFocus()){
		manipulator.mouse(button, state, x, y);
	}
	//UIに焦点が或る時
	g_ui.mouse(button, state, glutGetModifiers(), x, y);    
}

void myMouseMotion(int x,int y)
{
	g_ui.mouseMotion(x, y);

	if(!g_ui.isOnFocus()) manipulator.motion(x, y);
	glutPostRedisplay();
}




void timer(int t)
{
	glutPostRedisplay();
	glutTimerFunc(t,timer,17); //タイマー関数
}


//--------- ユーザインターフェイス関係 ------------//
void DrawUI()
{
	g_ui.begin();
	{
		g_ui.beginGroup();//1つのグループ
		g_ui.doLabel(nv::Rect(),  "This is Label",1); //描画スタイル「0」
		g_ui.doCheckButton(nv::Rect(), "Change Color", &optionForButtom,1);//描画スタイル「1」
		g_ui.doButton(nv::Rect(), "ClickButton", &optionForButtom,1);//ボタン
		g_ui.endGroup();
	}
	g_ui.end();
}

【メモ】
大抵は、アニメーションも一緒に使う。

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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