• Source
    1. #include <GL/glut.h>
    2. #include "glm.h"
    3. GLMmodel* pmodel = NULL;
    4. /*打光陣列開始*/
    5. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
    6. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    7. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    8. const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };/*改了光的方向*/
    9.  
    10. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
    11. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    12. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    13. const GLfloat high_shininess[] = { 100.0f };
    14. /*打光陣列結束*/
    15. float angle=0;///自動旋轉A
    16. void display(){
    17. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    18. /*glutSolidTeapot(0.3);茶壺*/
    19. angle+=0.1;///自動旋轉B,這行要在Matrix外面
    20. glPushMatrix();
    21. glScalef(5,5,5);/*縮放*/
    22. glRotatef(angle,0,1,0);///自動旋轉C
    23. if (!pmodel) {
    24. pmodel = glmReadOBJ("data/chair.obj");/*讀我自己的模型*/
    25. if (!pmodel) exit(0);
    26. glmUnitize(pmodel);
    27. glmFacetNormals(pmodel);
    28. glmVertexNormals(pmodel, 90.0);
    29. }
    30.  
    31. glmDraw(pmodel, GLM_SMOOTH|GLM_MATERIAL);/*要有material才會有顏色*/
    32.  
    33. glPopMatrix();
    34.  
    35. glutSwapBuffers();
    36. }
    37. int main(int argc,char **argv){
    38. glutInit(&argc,argv);
    39. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    40. glutCreateWindow("GLUT");
    41. ///glutDisplayFunc(display);這行要移到mainloop面前
    42. /*打光開始*/
    43. glClearColor(1,1,1,1);
    44. /*glEnable(GL_CULL_FACE);///這行要拿掉才不會很透光*/
    45. glCullFace(GL_BACK);
    46.  
    47. glEnable(GL_DEPTH_TEST);
    48. glDepthFunc(GL_LESS);
    49.  
    50. glEnable(GL_LIGHT0);
    51. glEnable(GL_NORMALIZE);
    52. glEnable(GL_COLOR_MATERIAL);
    53. glEnable(GL_LIGHTING);
    54.  
    55. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    56. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    57. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    58. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    59.  
    60. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    61. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    62. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    63. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    64. /*打光結束*/
    65. glutDisplayFunc(display);///自動旋轉D
    66. glutIdleFunc(display);///自動旋轉E
    67. glutMainLoop();
    68. }
    69.  
    70.  
    71.  
    72.  
    73.  
    74.  
    75.