OpenGL:TexGenの実験
本物のページはこちら→texture-gen:src
Included page "opengl:texture-gen:src" does not exist (create it now)


#pragma once #include <windows.h> #include <GL/glew.h> #include <GL/glfw.h> #include <GL/freeglut.h> #include <sstream> #include <cmath> #include <iostream> #include <iomanip> #include <sstream> #include <string> #include <fstream> #include <list> using namespace std; #include <miffy/math/vec2.h> #include <miffy/math/vec3.h> #include <miffy/math/vec4.h> #include<miffy/math/matrix.h> #include <miffy/math/quaternion.h> #pragma comment(lib,"GLFW.lib") #pragma comment(lib,"glew32.lib") const static double M_PI = 4.0*atan(1.0); using namespace miffy; class OpenGL{ public: mat4<float> m_modelview; mat4<float> m_rotation_matrix; mat4<float> m_proj_matrix; mat4<float> m_inv_modelview; mat4<float> m_inv_next_modelview; mat4<float> m_next_modelview; vec2<float> m_translate; double m_zoom; const vec3<double> m_world_eye_pos;//ワールド座標での視点座標 const float m_near; const float m_far; const float m_fovy; float m_aspect_ratio; public: vec2<int> m_win_size; vec2<int> m_last_pushed; quat<float> m_current_quaternion; quat<float> m_target_quaternion; OpenGL(void) :m_world_eye_pos(vec3<double>(0.0, 0.0, 9.0)) ,m_near(0.1f) ,m_far(100.0f) ,m_fovy(30.0f) ,m_zoom(m_world_eye_pos.z) { //テクスチャ画像ファイルの読み込み glfwLoadTexture2D( "test.tga", GLFW_BUILD_MIPMAPS_BIT ); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); static double genfunc[][4] = { //テクスチャ生成関数のパラメータ { 1.0, 0.0, 0.0, 0.0 },//Sに渡す。0.0-2.0の範囲の画像に渡すなら÷2 { 0.0, 1.0, 0.0, 0.0 },//Tに渡す }; /* テクスチャ座標生成関数の設定 */ glTexGendv(GL_S, GL_OBJECT_PLANE, genfunc[0]); glTexGendv(GL_T, GL_OBJECT_PLANE, genfunc[1]); } void display(){ //描画 glClearColor(0.0,0.0,0.0,1.0); //glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); gluLookAt(m_world_eye_pos.x, m_world_eye_pos.y, m_zoom,0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glTranslated(m_translate.x,m_translate.y,0.0);/* 平行移動(奥行き方向) */ m_target_quaternion.toMat4(const_cast<float*>(m_rotation_matrix.m)); glMultMatrixf(m_rotation_matrix.m);//クォータニオンによる回転 glColor3ub(255,255,255);//白を塗っておく // テクスチャ座標の自動生成を有効にする //glRasterPos3d(-0.75, -0.75, 0);//0,0,0位置をスタート位置にする //glutBitmapString(GLUT_BITMAP_HELVETICA_18,(const unsigned char*)("-0.5-0.5")); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glVertex3d(-0.5,-0.5,0.0); glVertex3d(0.5,-0.5,0.0); glVertex3d(0.5,0.5,0.0); glVertex3d(-0.5,0.5,0.0); glEnd(); glPushAttrib(GL_CURRENT_BIT); //glColor3d(1.0,0.0,0.0); //glRasterPos3d(0, -0.2, 0);//0,0,0位置をスタート位置にする //glutBitmapString(GLUT_BITMAP_HELVETICA_18,(const unsigned char*)("0.0-1.0")); /*glPopAttrib(); glBegin(GL_QUADS); glVertex3d(0,0,0.0); glVertex3d(2.0,-0,0.0); glVertex3d(2.0,2.0,0.0); glVertex3d(0,2.0,0.0); glEnd();*/ glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_GEN_S); glPopMatrix(); }//for display void RotateFromScreen(int _mx,int _my){ float dx=(float)(_mx-m_last_pushed.x)/(float)m_win_size.x; float dy=(float)(_my-m_last_pushed.y)/(float)m_win_size.y; //過去との回転の合成をどうやっていいかわからない。やっぱクオータニオンが必要 vec3<float> rotate_axis=vec3<float>(dy,dx,0.0); float axislength=rotate_axis.length(); if(axislength!=0.0){ float radian=(float)fmod(axislength*(float)M_PI,360.0);//画面いっぱいで調度一周になるようにする。 rotate_axis.normalize();//軸の長さを1にする。 quat<float> difq(cos(radian),rotate_axis.x*sin(radian),rotate_axis.y*sin(radian),0.0); m_target_quaternion=difq*m_current_quaternion; } } void reshape(int _width, int _height){ m_win_size.set(_width,_height); m_translate.x = 0.0; m_translate.y = 0.0; glViewport(0, 0, (GLsizei) _width, (GLsizei) _height); m_aspect_ratio = (float)_width/(float)_height; //ブロック前後クリッピング初期化 glMatrixMode(GL_PROJECTION); /* 投影変換の設定 */ glLoadIdentity(); gluPerspective(m_fovy, m_aspect_ratio, m_near, m_far);//original glGetFloatv(GL_PROJECTION_MATRIX, m_proj_matrix.m); glMatrixMode(GL_MODELVIEW); } void zoom(int _direction){ m_zoom=(double)_direction*0.4+m_world_eye_pos.z; } }; int main(int argc, char *argv[]){ glutInit(&argc,argv); glfwInit(); glfwOpenWindow( 0,0, 0,0,0,0,0,0, GLFW_WINDOW ); glfwSetWindowTitle("Miffy OpenGL"); glewInit(); enum STATE{STATIC,ROTATING,SHIFT,ZOOM}; OpenGL opengl; while (glfwGetWindowParam(GLFW_OPENED))//ウィンドウが開いている場合 { int winx,winy; glfwGetWindowSize(&winx,&winy); opengl.reshape(winx,winy); int mx,my; glfwGetMousePos(&mx,&my); if(glfwGetMouseButton(GLFW_MOUSE_BUTTON_LEFT)){ opengl.RotateFromScreen(mx,my); }else{//回転やめ opengl.m_last_pushed.set(mx,my); opengl.m_current_quaternion=opengl.m_target_quaternion; } opengl.zoom(glfwGetMouseWheel()); opengl.display(); glfwSwapBuffers(); } return 0; }