2017年10月19日 星期四

李柏徹的計算機圖學學習紀錄06

一、上Zuvio回答問題,複習上禮拜的學習進度。
二、下載上禮拜未成功的編譯檔,把下圖的C++11勾掉,即可編譯出第二張圖的圖形
\

三、打光:去source資料夾中原始的projection.c找到打光相關程式碼並加入

GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };//決定打光位置

glEnable(GL_DEPTH_TEST);//3D影像深度
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
成果:

四、Keyboard and Mouse
void keyboard(unsigned char key, int x, int y)
{
    printf("now: %c (%d %d)\n",key,x,y);
    if(key=='1')
    {
        rotatex++;
        glutPostRedisplay();
    }
    if(key=='2')
    {
        rotatey++;
        glutPostRedisplay();
    }
    if(key=='3')
    {
        rotatez++;
        glutPostRedisplay();
    }
    //printf("%d %d %d\n", rotatex, rotatey, rotatez);
}
void mouse(int button,int state, int x, int y)
{
    printf("button:%d state:%d (%d %d)\n",button,state,x,y);
    if(state==GLUT_DOWN)
    {
        oldx=x;
        oldy=y;
    }
    //glutPostRedisplay();
}
void motion(int x, int y)
{
    rotatey+= -(x-oldx);
    rotatex+= -(y-oldy);
    oldx=x;
    oldy=y;
    glutPostRedisplay();
}
main中加入
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
讓鍵盤跟滑鼠有一些功能


今日完整程式碼:
#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;
void keyboard(unsigned char key, int x, int y)
{
    printf("now: %c (%d %d)\n",key,x,y);
    if(key=='1')
    {
        rotatex++;
        glutPostRedisplay();
    }
    if(key=='2')
    {
        rotatey++;
        glutPostRedisplay();
    }
    if(key=='3')
    {
        rotatez++;
        glutPostRedisplay();
    }
    //printf("%d %d %d\n", rotatex, rotatey, rotatez);
}
void mouse(int button,int state, int x, int y)
{
    printf("button:%d state:%d (%d %d)\n",button,state,x,y);
    if(state==GLUT_DOWN)
    {
        oldx=x;
        oldy=y;
    }
    //glutPostRedisplay();
}
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();
    //printf("display: %d %d %d\n", rotatex, rotatey, rotatez);
}
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");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);

    glutMainLoop();
}

沒有留言:

張貼留言