2017年10月19日 星期四

ray ㄉweek06

(1)複習上週進度
(2)編譯上週 transformation 程式 (小心 compiler設定 vs. strdup() )
(3)作業2 : Keyboard, Mouse, 轉動 ,移動
(4)主軸: 打光Lighting

a.解決上週的問題

把c++11 的勾點掉


















然後就可以正確執行ㄌ

















b.第一步打光

程式碼:
#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);
    drawmodel();
    glutSwapBuffers();
}
GLfloat 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");

    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glutDisplayFunc(display);

    glutMainLoop();
}



























c. 紀錄鍵盤和滑鼠輸入的字

多加  
void keyboard(unsigned char key, int x, int y){
    printf("now: %c (%d %d)\n",key,x,y);
}

void mouse(int botton,int state,int x,int y){
   printf("botton: %d state: %d (%d %d)\n",botton,state,x,y);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");

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

    glutMainLoop();
}















這樣在小黑格就會紀錄所有的輸入拉
















D.用鍵盤讓模型轉動

多加程式碼
int rotateX=0,rotateY=0,rotateZ=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();   //每次輸入馬上更新
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(rotateX, 1,0,0);
        glRotatef(rotateY, 0,1,0);
    drawmodel();
    glutSwapBuffers();
}















這樣就可以順利轉拉















E.用滑鼠讓模型轉動

void mouse(int botton,int state,int x,int y){
    printf("botton: %d state: %d (%d %d)\n",botton,state,x,y);
    if(state==GLUT_DOWN){
        oldX=x;
        oldY=y;
    }
}
void motion(int x,int y){
    rotateX += -(y-oldY);
    rotateY += -(x-oldX);
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}





































沒有留言:

張貼留言