現在地メニュー >> NVIDIA_SDK >> NVIDIA_SDK_04 >> NVIDIA_SDK_04::まとめ

問題


「nvImageクラス」を使って、「png形式の画像」を読み込んでポリゴンにテクスチャをはれ。

答え

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


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

//----- テクスチャID -----//
GLuint texture[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);
void DRAW_SQU();


//------------- 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 LoadTexture()
{
	nv::Image imgA;
	if(!imgA.loadImageFromFile("test.png"))
	{
		std::cerr << "Can't Load Image \n";
		return ;
	}

	glGenTextures(1, &texture[0]);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //拡大フィルタ
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //縮小フィルタ

	glTexImage2D(GL_TEXTURE_2D,0,imgA.getInternalFormat(), imgA.getWidth(),imgA.getHeight(),0, imgA.getFormat(),imgA.getType(),imgA.getLevel(0));

}


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

	OtherMyInit();
	LoadTexture();
}


//----------- メイン関数 -----------------//
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移動処理

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

	DRAW_SQU();

	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, 100.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();
}
//----------- ここから各種関数 -----------//
void DRAW_SQU()
{
	glEnable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 1.0);	glVertex2f(-1,1);
	glTexCoord2f(0.0, 0.0);	glVertex2f(-1,-1);
	glTexCoord2f(1.0, 0.0);	glVertex2f(1,-1);
	glTexCoord2f(1.0, 1.0);	glVertex2f(1,1);
	glEnd();
	glDisable(GL_TEXTURE_2D);
}

【メモ】
わかりやすいように、nvGlutManipulatorsを使ってマウス操作ができるようにした。

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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