2017年10月19日 星期四

余瑞得 week 6

1.先照上周的方法開啟專案
2.利用CPP檔案中的部分code弄出一隻沒有顏色的人
3.增加打光函式 讓角色有陰影深度
/* code 解釋
    GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };///  position 表打燈的位置
    glEnable(GL_DEPTH_TEST);/// 啟用深度測試
    glEnable(GL_LIGHTING);///  啟用燈光功能
    glEnable(GL_LIGHT0);///  啟用第0盞燈
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///  用第0盞燈 後面是打燈的位置  上面有設定從後面打燈
*/
4.增加滑鼠跟鍵盤指令
/*
    void keyboard(unsigned char key,int x,int y)
keyboard 會回傳 鍵值 跟滑鼠位置
    glutMouseFunc(mouse);/// glut的mouse fun
    glutKeyboardFunc(keyboard);/// glut 的 keyboard fun
*/
5.最後有新增根據滑鼠鍵盤做轉動但不太能實際表現出來所以下面只附code

#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
float glx=0,gly=0,glz=0;
void keyboard(unsigned char key,int x,int y) ///
{
    printf("%c (%d %d)\n",key,x,y);
    if(key=='1') glx+=10;
    if(key=='2') gly+=10;
    if(key=='3') glz+=10;
    glutPostRedisplay();
}
float oldx=0,oldy=0,oldz=0;
void mouse(int button,int state,int x,int y,int z) ///
{
    printf("p %d %d\n",button,state);
    if(state==GLUT_DOWN && button==1)
    {
        oldx=x;
        oldy=y;
    }
    if(state==GLUT_DOWN)
    {
        oldx=x;
        oldy=y;
    }
}
void motion(int x,int y,int z) ///
{
   // glx+=(oldy-y);
  //  gly+=(oldx-x);
    glz+=((oldx-x)+(oldy-y))/2;
    oldx=x,oldy=y,oldz=((oldx-x)+(oldy-y))/2;
    glutPostRedisplay();
}
void drawmodel(void)
{
    if (!pmodel) {
pmodel = glmReadOBJ("data/al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
    }

    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };///
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();    //把現在的原點push進stack
        glRotatef(glx,1,0,0);           // (角度,x軸,y軸,z軸)
        glRotatef(gly,0,1,0);
        glRotatef(glz,0,0,1);
        drawmodel();
    glPopMatrix();   //把點pop回來
    glutSwapBuffers();

}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");
    glutMouseFunc(mouse);///
    glutKeyboardFunc(keyboard);///
    glutMotionFunc(motion);
    glEnable(GL_DEPTH_TEST);///
    glEnable(GL_LIGHTING);///
    glEnable(GL_LIGHT0);///
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///
    glutDisplayFunc(display);

    glutMainLoop();
}

沒有留言:

張貼留言