2020年5月19日 星期二

翰翰的日記12

我是第12周
一開始老師先教.cbp檔用codeblocks開的方式,首先去載source、win32、freeglut(要複製改32),之後到codeblocks的enviroment裡把這個打勾
打勾完後所有資料就可以直接用codeblocks開了


接下來這裡很重要:
先開一個glut專案檔
開完了之後把glm.c改成glm.cpp然後跟glm.h一起丟到專案檔底下
接下來進codeblocks在這裡按add files
然後選glm.cpp

開啟了不要動glm.cpp
在main的程式碼裡
寫上之前匯obj檔的程式碼
再來要把資料都集中到專案資料夾裡
所以要先把freeglut.dill丟到專案底下
再改路徑
先把這個用notepad++開起來


再把路徑改成小數點如下圖
好了以後就可以把obj跟mtl丟到專案底下了
再來再來老師教怎麼匯入多個模型

#include <GL/glut.h>
#include "glm.h"
GLMmodel * pmodelA=NULL;
GLMmodel * pmodelB=NULL;
GLMmodel * pmodelC=NULL;
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, 3.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.5,0.5,0.5);
    glmDraw(pmodelB, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///drawBody();
    glPushMatrix();
    glTranslatef(0,0,0);
    glRotatef(angle,0,0,1);
    glTranslatef(0.25,0,0);
    drawArm();
    glPopMatrix();
    glutSwapBuffers();

}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11我寫的");
    glutDisplayFunc(display);

     ///打光需要3Dㄉdepth test
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    ///打光1:要開啟光線,原來範例就寫好了
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    ///打光2:要設定光的設定值(前面有陣列,這裡拿來用)
    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);
    ///打光2.2:要把物體也設定好它的matreial
    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 * model=NULL;///身體 al
GLMmodel * pmodelB=NULL;///手臂 porsche
GLMmodel * pmodelC=NULL;///手臂 porsche
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, 3.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()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if(model==NULL)
    {
        model = glmReadOBJ("Al.obj");
        glmUnitize(model);
        glmFacetNormals(model);
        glmVertexNormals(model, 90);
    }
    glPushMatrix();
        glRotatef(180,0,1,0);
        glmDraw(model, 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();
        glTranslatef(0.25,0,0);
        glRotatef(90, 0,1,0);
        glScalef(0.5,0.5,0.5);
        glmDraw(pmodelB, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   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("啥都不會怎麼不去死");
    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();
}

沒有留言:

張貼留言