2017年10月19日 星期四

師大柯雷吉歐的計圖筆記-Week6

一、延續上禮拜的進度

1.上禮拜strdup的問題,進入setting->compiler裡面將第二行的打勾取消就OK了
why? 因為這是舊式的寫法,2011年的C++無法編譯

2.這樣就可以執行了

二、打光及轉動

1.以下是projection.cpp的程式碼
藍色是打光程式碼

#include <GL/glut.h>

#include "glm.h"
GLMmodel* pmodel = NULL;
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);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///3D深度測試,清depth buffer 才能正確繪圖
    drawmodel();
    glutSwapBuffers();
}
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 }; /// 控制打光角度方向,-1.0會從背後照
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");

    glEnable(GL_DEPTH_TEST); ///3D深度測試,打開Enable
    glEnable(GL_LIGHT0); ///打開Enable
    glEnable(GL_LIGHTING); ///打開Enable
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos); 
    glutDisplayFunc(display);

    glutMainLoop();
}

2.以下是目前的結果
3.再來我們要輸入mouse、keyboard函式
綠色是新增的程式碼
#include <GL/glut.h>
#include <stdio.h>  ///呼叫printf
#include "glm.h"
void keyboard(unsigned char key,int x,int y)
{
    printf("Now:%c (%d %d)\n",key,x,y);
}
void mouse(int button,int state,int x,int y)
{

}
GLMmodel* pmodel = NULL;
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);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawmodel();
    glutSwapBuffers();
}
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

這樣就可以用鍵盤找圖的座標
4.
以下紅色為鍵盤轉動的程式碼
以下青色為滑鼠轉動的程式碼
#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
GLMmodel* pmodel= NULL;
int rotateX=0,rotateY=0,rotateZ=0;
int oldX=0,oldY=0,oldZ=0;
void keyboard(unsigned char key,int x,int y)
{
    printf("Now:%c (%d %d)\n",key,x,y);
    if(key=='1') rotateX++;
    if(key=='2') rotateY++;
    if(key=='3') rotateZ++;
    glutPostRedisplay(); ///貼出公告,請電腦有空時,把畫面重新display
}
void mouse(int button,int state,int x,int y)
{
    printf("button:%d state:%d (%d %d)\n",button,state,x,y);

///mouse drag:(1)mouse down  (2)drag  (3)mouse up

    if(state==GLUT_DOWN)
    {
        oldX=x; oldY=y;
    }
}
void motion(int x,int y)
{
    rotateY+= -(x-oldX);
    rotateX+= -(y-oldY);
    oldX=x,oldY=y;
    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);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();    
    glRotatef(rotateX, 1,0,0);   ///更改旋轉角度
    glRotatef(rotateY, 0,1,0);   ///更改旋轉角度
    glRotatef(rotateZ, 0,0,1);   ///更改旋轉角度
    drawmodel();
    glPopMatrix();

    glutSwapBuffers();
}
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

沒有留言:

張貼留言