最終更新: mikk_ni3_92 2009年10月05日(月) 17:12:53履歴
現在地:メニュー >> NVIDIA_SDK >> NVIDIA_SDK_05 >> NVIDIA_SDK_05::まとめ2
関連:NVIDIA_SDK_05::まとめ1、NVIDIA_SDK_05::まとめ3
nvModelで「objファイル」を読み込んで、それをVBOを使って描画せよ。
その他条件:
関連:NVIDIA_SDK_05::まとめ1、NVIDIA_SDK_05::まとめ3
nvModelで「objファイル」を読み込んで、それをVBOを使って描画せよ。
その他条件:
- 「GL_ARRAY_BUFFER」を指定したVBO → glDrawArraysでの描画
- 描画は「GL_POINTS」でよい
- nvidia sdk付属サンプルの「cow.obj」を読み込む
#include <iostream> #include "nvModel.h"//内部でglewをインクルードしている #include <GL/glut.h> #include <nvGlutManipulators.h> #pragma comment(lib,"nvModel.lib") //------- 各種外部変数 --------// nv::GlutExamine manipulator; //マウス操作用マニピュレータ nv::Model model; GLuint VboId[1];//本体とインデックス //------------- プロトタイプ宣言 ------------------// 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 With VBO 1"); GLUT_CALL_FUNCs(); OtherMyInit(); } //--------- glewの初期化 ----------// bool GLEW_INITs() { GLenum err; err = glewInit(); if (err != GLEW_OK){ std::cerr << glewGetErrorString(err) << "\n"; return false; } return true; } //------------ nvidia SDKの初期設定 --------------// bool Init_NvidiaSDK() { manipulator.setTrackballScale(1.0);//トラックボール(回転処理の速度) manipulator.setDollyActivate( GLUT_LEFT_BUTTON, GLUT_ACTIVE_CTRL);//左ドラッグ+ctl → dolly移動処理 manipulator.setPanActivate( GLUT_LEFT_BUTTON, GLUT_ACTIVE_SHIFT);//左ドラッグ+Shfit → pan移動処理 //objファイル読み込み std::cout << "now Loading Obj File...\n"; if(!model.loadModelFromFile("cow.obj")) { std::cerr << "Can't Load OBJ file.\n"; return false; } model.computeNormals(); model.compileModel(); model.rescale(1); return true; } //--------- VBO作成 ------------// void BuildVBO() { glGenBuffers(1,&VboId[0]);//VBOを1つ作成 glBindBuffer(GL_ARRAY_BUFFER, VboId[0]); glBufferData(GL_ARRAY_BUFFER,3*model.getPositionCount()*sizeof(float),model.getPositions(),GL_STATIC_DRAW); //※何も変更しないので「GL_STATIC_DRAW」を指定 } //----------- メイン関数 -----------------// int main(int argc, char **argv) { GLUT_INITs(&argc,argv); if( GLEW_INITs() == false){ return -1; } if( Init_NvidiaSDK() == false) { return -1; } BuildVBO();//VBO作成 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.f,0.f,0.f); glBindBuffer(GL_ARRAY_BUFFER, VboId[0]); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(model.getPositionSize(), GL_FLOAT, 0, 0); glDrawArrays(GL_POINTS , 0 , model.getPositionCount()); glDisableClientState(GL_VERTEX_ARRAY); glBindBufferARB(GL_ARRAY_BUFFER, 0); glColor3f(1.f,1.f,1.f); 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(); }