2020年5月19日 星期二

Week13 OBJ模型

複習 GLUT .cbp 專案檔設定
專案檔內需有freeglut.dll、glm.cpp、glm.h、obj模型檔
用Notepad++開啟專案.cbp
將working_dir="位置"改成"."
*注意有兩個位置須改

四個關節+滑鼠拖移
叫出身體模型,設定四個關節,左右肩膀及左右手臂,並加入滑鼠拖移手臂揮動
#include <GL/glut.h>
#include "glm.h"
GLMmodel *pmodelA=NULL;///身體, Al.obj
GLMmodel *pmodelB=NULL;///手臂, porsche.obj
GLMmodel *pmodelC=NULL;///手臂, porsche.obj
GLMmodel *pmodelD=NULL;
GLMmodel *pmodelE=NULL;
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

void drawBody()
{
    if(pmodelA==NULL){
        pmodelA=glmReadOBJ("Al.obj");
        glmUnitize(pmodelA);
        glmFacetNormals(pmodelA);
        glmVertexNormals(pmodelA, 90);
    }
    glPushMatrix();
        glRotatef(180, 0,1,0);
        glmDraw(pmodelA, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
int angle=30;
void drawArm()
{
    if(pmodelB==NULL){
        pmodelB=glmReadOBJ("porsche.obj");
        glmUnitize(pmodelB);
        glmFacetNormals(pmodelB);
        glmVertexNormals(pmodelB, 90);
    }
    glPushMatrix();
        glRotatef(90, 0,1,0);
        glScalef(0.3, 0.3, 0.3);
        glmDraw(pmodelB, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///glutSolidSphere(0.1, 30,30);///原點、圓心
    drawBody();
///右手臂
    glPushMatrix();
        glTranslatef(0.5, 0.4, 0);
        glRotatef(angle, 0,0,1);
        glTranslatef(0.3, 0, 0);
        drawArm();///右上手臂
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle, 0,0,1);
            glTranslatef(0.3, 0, 0);
            drawArm();///右下手臂
        glPopMatrix();
    glPopMatrix();
///左手臂
    glPushMatrix();
        glTranslatef(-0.5, 0.4, 0);
        glRotatef(angle, 0,0,1);
        glTranslatef(-0.3, 0, 0);
        drawArm();///左上手臂
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(angle, 0,0,1);
            glTranslatef(-0.3, 0, 0);
            drawArm();///左下手臂
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{
    oldX=x;
}
void motion(int x, int y)
{
    angle += (x-oldX);
    oldX=x;
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();
}

最終版_加入按鍵控制各關節
#include <GL/glut.h>
#include "glm.h"
GLMmodel *pmodelA=NULL;///身體, Al.obj
GLMmodel *pmodelB=NULL;///手臂, porsche.obj
GLMmodel *pmodelC=NULL;///手臂, porsche.obj
GLMmodel *pmodelD=NULL;
GLMmodel *pmodelE=NULL;
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

void drawBody()
{
    if(pmodelA==NULL){
        pmodelA=glmReadOBJ("Al.obj");
        glmUnitize(pmodelA);
        glmFacetNormals(pmodelA);
        glmVertexNormals(pmodelA, 90);
    }
    glPushMatrix();
        glRotatef(180, 0,1,0);
        glmDraw(pmodelA, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
int angle[4]={30,30,30,30};
int angleID=0;
void drawArm()
{
    if(pmodelB==NULL){
        pmodelB=glmReadOBJ("porsche.obj");
        glmUnitize(pmodelB);
        glmFacetNormals(pmodelB);
        glmVertexNormals(pmodelB, 90);
    }
    glPushMatrix();
        glRotatef(90, 0,1,0);
        glScalef(0.3, 0.3, 0.3);
        glmDraw(pmodelB, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void keyboard(unsigned char key, int x,int y)
{
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///glutSolidSphere(0.1, 30,30);///原點、圓心
    drawBody();
    glPushMatrix();///右手臂
        glTranslatef(0.5, 0.4, 0);
        glRotatef(angle[0], 0,0,1);
        glTranslatef(0.3, 0, 0);
        drawArm();///右上手臂
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0.3, 0, 0);
            drawArm();///右下手臂
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();///左手臂
        glTranslatef(-0.5, 0.4, 0);
        glRotatef(angle[2], 0,0,1);
        glTranslatef(-0.3, 0, 0);
        drawArm();///左上手臂
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(-0.3, 0, 0);
            drawArm();///左下手臂
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{
    oldX=x;
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    oldX=x;
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();
}

沒有留言:

張貼留言