2013年3月29日 星期五

第六週上課內容及HW

1. 上週複習















2.















3.


#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>

float rot=0; //要用的角度  //step 2
float rotX=0,rotY=0;
float oldx=0,oldy=0;
void display();
void mouse();
void motion();
void lighting();
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//glRotatef(rot,1,0,0); //要用這個角度旋轉   //step 2
glRotatef(rotX/10,0,1,0);
glRotatef(rotY/10,1,0,0);
glColor3ub(8,3,253);
glutSolidSphere(0.3,40,40);
glTranslatef(0,-0.4,0); //step 1.5
glutSolidSphere(0.3,40,40); //step 1.7
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int event, int x, int y)
{
printf("%d %d %d %d\n",button,event,x,y);
if(button==GLUT_LEFT_BUTTON && event==GLUT_DOWN)
{
oldx=x; oldy=y;
}
}
void motion(int x,int y)
{
printf("%d %d\n",x,y);
rot=y;
//rotX=x; rotY=y;
rotX+=(x-oldx);
rotY+=(y-oldy);
display();
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Doraemon 00160236");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion); //drag拖的動作 
lighting();
glutMainLoop();
return 0;
}
void lighting() //打光 
{
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 };
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);
}




沒有留言:

張貼留言