float rotX=0, rotY=0;
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rotY, 1,0,0);
glRotatef(rotX, 0,1,0);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
rotX=x;
rotY=y;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutInitWindowPosition(600,0);
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
第五週作業
加旋轉
請把上上週繳交的、自我發揮的作業, 加上旋轉的功能
我的作業
可以旋轉
-------------------程式碼-------------------
#include <GL/glut.h>
#include <stdio.h>
float rotX=0,rotY=0;
void mouse(int button,int state,int x,int y)
{
if(state == GLUT_DOWN)
printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",x,y);
}
void display()
{
glClearColor(17/255.0,17/255.0,17/255.0,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();///備份Matrix (Matrix裡面有translate,rotate,scale的量值)
glRotatef(rotY, 1,0,0);///Y的移動量,讓他對x軸轉
glRotatef(rotX, 0,1,0);///x的移動量,讓他對y軸轉
glColor3ub(190,27,44);
glBegin(GL_POLYGON);
glVertex2f((80-100)/100.0, -(25-100)/100.0);
glVertex2f((80-100)/100.0, -(35-100)/100.0);
glVertex2f((115-100)/100.0, -(56-100)/100.0);
glVertex2f((58-100)/100.0, -(35-100)/100.0);
glVertex2f((58-100)/100.0, -(45-100)/100.0);
glVertex2f((47-100)/100.0, -(45-100)/100.0);
glVertex2f((47-100)/100.0, -(57-100)/100.0);
glVertex2f((35-100)/100.0, -(57-100)/100.0);
glVertex2f((35-100)/100.0, -(90-100)/100.0);
glVertex2f((25-100)/100.0, -(90-100)/100.0);
glVertex2f((25-100)/100.0, -(175-100)/100.0);
glVertex2f((36-100)/100.0, -(175-100)/100.0);
glVertex2f((36-100)/100.0, -(163-100)/100.0);
glVertex2f((47-100)/100.0, -(163-100)/100.0);
glVertex2f((47-100)/100.0, -(153-100)/100.0);
glVertex2f((57-100)/100.0, -(153-100)/100.0);
glVertex2f((57-100)/100.0, -(163-100)/100.0);
glVertex2f((67-100)/100.0, -(163-100)/100.0);
glVertex2f((67-100)/100.0, -(173-100)/100.0);
glVertex2f((90-100)/100.0, -(173-100)/100.0);
glVertex2f((90-100)/100.0, -(153-100)/100.0);
glVertex2f((110-100)/100.0, -(153-100)/100.0);
glVertex2f((110-100)/100.0, -(173-100)/100.0);
glVertex2f((130-100)/100.0, -(173-100)/100.0);
glVertex2f((130-100)/100.0, -(163-100)/100.0);
glVertex2f((140-100)/100.0, -(163-100)/100.0);
glVertex2f((140-100)/100.0, -(153-100)/100.0);
glVertex2f((153-100)/100.0, -(153-100)/100.0);
glVertex2f((153-100)/100.0, -(163-100)/100.0);
glVertex2f((163-100)/100.0, -(163-100)/100.0);
glVertex2f((163-100)/100.0, -(173-100)/100.0);
glVertex2f((173-100)/100.0, -(173-100)/100.0);
glVertex2f((173-100)/100.0, -(90-100)/100.0);
glVertex2f((163-100)/100.0, -(90-100)/100.0);
glVertex2f((163-100)/100.0, -(60-100)/100.0);
glVertex2f((153-100)/100.0, -(60-100)/100.0);
glVertex2f((153-100)/100.0, -(50-100)/100.0);
glVertex2f((143-100)/100.0, -(50-100)/100.0);
glVertex2f((143-100)/100.0, -(35-100)/100.0);
glVertex2f((120-100)/100.0, -(35-100)/100.0);
glVertex2f((120-100)/100.0, -(25-100)/100.0);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;rotY=y;///(拿來旋轉)
display();///glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hello 3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
----------------程式碼------------------


沒有留言:
張貼留言