2021年5月5日 星期三

week11電腦圖學

 1.先去下載3個範例並解壓縮

source.zip ,windows,zip,data,zip

2.我們要建立自己的Transformation

3.先開起CodeBlocks把GLUT設定好


4.把檔案加進去


5.把三個放進去

6.將glm.c改成點cpp
7.將tramsformation放入程式碼

8.把檔案放進去
9.把date.zip裡的檔案,放入執行目錄


10.程式碼

#include "glm.h"

GLMmodel* pmodel = NULL;

void drawmodel(void)

{

      if (!pmodel) 

          {

       pmodel = glmReadOBJ("data/porsche.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();

}

int main(int argc, char** argv)

{

     glutInit(&argc,argv);

     glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

     glutCreateWindow("week11 我的3D模型");

     glutDisplayFunc(display);

     glutMainLoop();

}

11.打光程式碼

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 };

把上面打光的程式碼丟在int main上面

 glEnable(GL_DEPTH_TEST);//如果少了這行3D深度測試就會破圖

    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();上面

 

因為打光後發現圖片是背面的 我們要把模型轉方向

 

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glRotatef(180,0,1,0);

        drawmodel();

   glPopMatrix();

    glutSwapBuffers();

}

 




沒有留言:

張貼留言

距地表面160 Week11

 #include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) {     if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...