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

問題


「nvModel.h」、「nvModel.lib」、「nvModel.dll」を用意して、「objファイル」を読み込み表示せよ。
その他条件:
  • ワイヤーフレームで描画(頂点だけの描画でよい)
  • 「nvGlutManipulators.h」を使ってマウス処理ができるようにする

答え

#include <iostream>
#include "nvModel.h"//内部でglewをインクルードしている
#include <GL/glut.h>
#include <nvGlutManipulators.h>


#pragma comment(lib,"nvModel.lib")


//------- 各種外部変数 --------//
nv::GlutExamine manipulator; //マウス操作用マニピュレータ
nv::Model model;


//------------- プロトタイプ宣言 ------------------//
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);



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

void OtherMyInit()
{
	glClearColor(1.0, 1.0, 1.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 nvModel Sample");
	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移動処理

	//objファイル読み込み
	if(!model.loadModelFromFile("cow.obj"))
	{
		std::cerr << "Can't Load OBJ file\n";
		return -1;
	}
	model.computeNormals();
	model.compileModel();
	model.rescale(1);

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);


	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();//移動を適用する(回転、平行、拡大縮小など)


	glColor3f(0,0,0);
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(model.getPositionSize(), GL_FLOAT, 0, model.getPositions());
	glDrawElements(GL_TRIANGLES,model.getIndexCount(), GL_UNSIGNED_INT, model.getPositionIndices());
	glDisableClientState(GL_VERTEX_ARRAY);
	glColor3f(1,1,1);

	glutSwapBuffers();


}

void reshape(int w, int 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)
{
	manipulator.mouse(button, state, x, y);
}

void myMouseMotion(int x,int y)
{
	manipulator.motion(x, y);
	glutPostRedisplay();
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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