gluPerspectiveを置き換える

gluPerspectiveを置き換えてみます。
gluPerspective(fovy, aspect, near, far) の作り出す行列は

となっています。
そして

と定義されます。
cotangent(余接、コタンジェント)は math.h にありませんので
自作します。

#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#include <GL/freeglut/freeglut.h>
#include <math.h>

#define PI 3.1415926

#define WIDTH 320
#define HEIGHT 240

//平行移動用
float x = 0.0f;
bool flag = false;
//緑
GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
//ライトの位置
GLfloat lightpos[] = { 200.0, 150.0, -500.0, 1.0 };
//単位行列
GLfloat mat[]={
 1,0,0,0,
 0,1,0,0,
 0,0,1,0,
 0,0,0,1
};
//平行移動用
GLfloat move[]={
 1,0,0,0,
 0,1,0,0,
 0,0,1,0,
 0,0,0,1
};

GLfloat modelview[16];
GLfloat multi[16];

//合成 glMultMatrixfの代わり
void multiplication(GLfloat* src1,GLfloat* src2,GLfloat* dst)
{
 for(int y=0;y<4;y++){
  for(int x=0;x<4;x++){
   dst[y*4+x]=src2[y*4]*src1[x]+src2[y*4+1]*src1[x+4]+src2[y*4+2]*src1[x+8]+src2[y*4+3]*src1[x+12];
  }
 }
}

//gluPerspectiveと同じ
void perspective(float fovy,float aspect,float znear,float zfar)
{
 float radian=2*PI*fovy/360.0;
 float t = (float)(1.0 / tan(radian/2));
 GLfloat m[]={
 t / aspect,0,0,0,
 0,t,0,0,
 0,0,(zfar + znear) / (znear - zfar),-1,
 0,0,(2 * zfar * znear) / (znear - zfar),0
 };
 glLoadMatrixf(m);
}

void display(void)
{

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glViewport(0, 0, WIDTH, HEIGHT);
 glMatrixMode(GL_PROJECTION);

 //glLoadIdentity();と同じ↓
 glLoadMatrixf(mat);

 //視野角,アスペクト比(ウィンドウの幅/高さ),描画する範囲(最も近い距離,最も遠い距離)
 //gluPerspective(30.0, (double)WIDTH / (double)HEIGHT, 1.0, 1000.0);と同じ↓
 perspective(30.0, (double)WIDTH / (double)HEIGHT, 1.0, 1000.0);
 glMatrixMode(GL_MODELVIEW);

 //glLoadIdentity();と同じ↓
 glLoadMatrixf(mat);

 //視点の設定
 gluLookAt(150.0,100.0,-200.0, //カメラの座標
      0.0,0.0,0.0, // 注視点の座標
     0.0,1.0,0.0); // 画面の上方向を指すベクトル
 //ライトの設定
 glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
 //マテリアルの設定
 glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
 //平行移動
 //glTranslatef(x,0.0f,0.0f);と同じ↓
 move[12]=x; //X軸の平行移動成分を設定
 //glMultMatrixf(move);と同じ↓
 glGetFloatv(GL_MODELVIEW_MATRIX,modelview);
 multiplication(modelview,move,multi);
 glLoadMatrixf(multi);

 glutSolidSphere(40.0,16,16);

 glutSwapBuffers();
}
void idle(void)
{
 if(flag){x-=1.0f;}else{x+=1.0f;}
 if(x>50.0f)flag=true;
 if(x<-50.0f)flag=false;
 Sleep(1);
 glutPostRedisplay();
}
void Init(){
 glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
 glEnable(GL_DEPTH_TEST);
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
}
int main(int argc, char *argv[])
{
 glutInitWindowPosition(100, 100);
 glutInitWindowSize(WIDTH, HEIGHT);
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
 glutCreateWindow("gluPerspectiveを置き換える");
 glutDisplayFunc(display);
 glutIdleFunc(idle);
 Init();
 glutMainLoop();
 return 0;
}

 

最終更新:2014年04月16日 18:37
添付ファイル