電腦圖學
1.鍵盤動關節
#include <GL/glut.h>
float diff=2,angle[20]={};
int angleID=0;
void timer(int t)
{
glutTimerFunc(30,timer,t+1);///設下個timer
angle[angleID]+=diff;
if(angle[angleID]>90||angle[angleID]<0)diff=-diff;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y){
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.3,0,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.3,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.3,0,0);
glRotatef(angle[3],0,0,1);
glTranslatef(0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);///初始化
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///顯示模式
glutCreateWindow("HI");///開視窗
glutDisplayFunc(display);///顯示函式
glutTimerFunc(0,timer,0);
glutKeyboardFunc(keyboard);
glutMainLoop();///主迴圈,卡住他
}
2.滑鼠動關節
#include <GL/glut.h>
float diff=2,angle[20]={};
float diff=2,angle[20]={};
int angleID=0;
void timer(int t)
{
///glutTimerFunc(30,timer,t+1);///設下個timer
///angle[angleID]+=diff;
///if(angle[angleID]>90||angle[angleID]<0)diff=-diff;
///glutPostRedisplay();
}
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y){
oldX=x;
oldY=y;
}
void motion(int x,int y){
angle[angleID]+=x-oldX;
oldX=x;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y){
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.3,0,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.3,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.3,0,0);
glRotatef(angle[3],0,0,1);
glTranslatef(0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);///初始化
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///顯示模式
glutCreateWindow("HI");///開視窗
glutDisplayFunc(display);///顯示函式
glutTimerFunc(0,timer,0);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();///主迴圈,卡住他
}
3.存檔
if(key=='s'){
if(fout==NULL) fout=fopen("hi.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%.2f",angle[i]);
fprintf(fout,"\n");
for(int i=0;i<20;i++)printf("%.2f",angle[i]);
printf("\n");
}
4.讀檔
if(key=='r'){
if(fin==NULL) fin=fopen("ohno.txt","r");
for(int i=0;i<20;i++){
fscanf(fin,"%f",&angle[i]);
glutPostRedisplay();
}
}
5.瘋狂讀存檔
#include <GL/glut.h>
#include <stdio.h>
float diff=2,angle[20]={};
int angleID=0;
FILE*fout=NULL;
FILE*fin=NULL;
void timer(int t)
{
///glutTimerFunc(30,timer,t+1);///設下個timer
///angle[angleID]+=diff;
///if(angle[angleID]>90||angle[angleID]<0)diff=-diff;
///glutPostRedisplay();
}
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y){
oldX=x;
oldY=y;
}
void motion(int x,int y){
angle[angleID]+=x-oldX;
oldX=x;
glutPostRedisplay();
if(fout==NULL) fout=fopen("ohno.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%.2f",angle[i]);
fprintf(fout,"\n");
for(int i=0;i<20;i++)printf("%.2f",angle[i]);
printf("\n");
}
void keyboard(unsigned char key,int x,int y){
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='s'){
if(fout==NULL) fout=fopen("ohno.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%.2f",angle[i]);
fprintf(fout,"\n");
for(int i=0;i<20;i++)printf("%.2f",angle[i]);
printf("\n");
}
if(key=='r'){
if(fin==NULL) fin=fopen("ohno.txt","r");
for(int i=0;i<20;i++){
fscanf(fin,"%f",&angle[i]);
glutPostRedisplay();
}
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.3,0,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.3,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.3,0,0);
glRotatef(angle[3],0,0,1);
glTranslatef(0.3,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);///初始化
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///顯示模式
glutCreateWindow("HI");///開視窗
glutDisplayFunc(display);///顯示函式
glutTimerFunc(0,timer,0);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();///主迴圈,卡住他
}





沒有留言:
張貼留言