Week15_Camera、投影、運鏡
解壓縮win32&data,將data拉到win32資料夾裡
並開啟Projection.exe觀察參數
視角改變eye向量
中心改變center向量
橫拍直拍up向量
開啟codeblock命名為week15-1_glutPerspective
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); ///week15_step02-02 切換成投影矩陣
glLoadIdentity(); ///weeek15_step02_2 還原成單位矩陣
///glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
///glOrtho(-ar*3, ar*3, -1*3, 1*3, -100, +100);
gluPerspective(60, ar, 0.01, 1000);
///week15_step02_02 角度,比例,近的,遠的
glMatrixMode(GL_MODELVIEW); ///week15_step02_02 切換成 Model View
glLoadIdentity() ; ///week15_step02_02 還原成單位矩陣
}
float eyeX = 0,eyeY = 0;
void motion(int x, int y){
eyeX = (x-150.0)/150.0;
eyeY = (150.0-y)/150.0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX, eyeY, 1, 0,0,0, 0,1,0);
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4] = {1, 1, 1, 1};
int ID = 3; ///0:頭 , 1:身體 , 2:上手臂 , 3:下手臂
FILE * fout = NULL;
FILE * fin = NULL;
float teapotX=0, teapotY=0;
float angle[20] = {};
void keyboard(unsigned char key, int x, int y){
if(key=='0') ID = 0;
if(key=='1') ID = 1;
if(key=='2') ID = 2;
if(key=='3') ID = 3;
if(key=='s'){
if(fout==NULL) fout = fopen("motion.txt", "w");
for(int i=0; i<20; i++){
fprintf(fout, "%.2f ", angle[i] );
}
fprintf(fout, "\n");
}else if(key=='r'){
if(fin==NULL) fin = fopen("motion.txt", "r");
for(int i=0; i<20; i++){
fscanf(fin, "%f", &angle[i] );
}
glutPostRedisplay();
}
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(0.2, 0.2, 0.2);
if(body==NULL){
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");
uparmR = glmReadOBJ("model/uparmR.obj");
lowarmR = glmReadOBJ("model/lowarmR.obj");
}
if(ID==0) glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[0]) glmDraw(head, GLM_MATERIAL);
if(ID==1) glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[1]) glmDraw(body, GLM_MATERIAL);
glPushMatrix();
glTranslatef(-1.273334, +0.540000, 0);
glRotatef(angle[2], 0, 0, 1);
glTranslatef(1.273334, -0.540000, 0);
if(ID==2) glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[2]) glmDraw(uparmR, GLM_MATERIAL);
glPushMatrix();
glTranslatef(-1.933332, +0.106667, 0);
glRotatef(angle[3],0,0,1);
glTranslatef(1.933332, -0.106667, 0);
//glTranslatef(teapotX, teapotY, 0);
if(ID==3) glColor3f(1,0,0);
else glColor3f(1,1,1);
if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glColor3f(0, 1, 0);
glutSolidTeapot( 0.02 );
glutSwapBuffers();
}
int oldX = 0, oldY = 0;
void motion(int x,int y)
{
teapotX += (x - oldX)/150.0;
teapotY -= (y - oldY)/150.0;
angle[ID]+= x-oldX;
oldX = x;
oldY = y;
printf("glTranslatef(%f, %f, 0;\n",teapotX, teapotY);
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
oldX = x;
oldY = y;
///angle = x;
///angle = x;
}
display();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言