今天我們利用貝殼檔來製作
所以設定可想而知比較不一樣
首先我們還是大開貝殼檔 其餘都跟GLUT檔一樣 只是我們要多做設定
1在自己的檔案旁按右鍵 選build option
2Search>compiler>add(freeglut\include)>否
3Search>linker>add(freeglut\lib)>否
4Linker setting>新增freeglut opengl32 glu32 gdi32 winmm > O K
完成後設定成功!!!!!
後來老師複習了之前關節的東西
float angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0,0,1);
glPushMatrix();
glTranslatef(0.5,0.15,0);
glRotatef(angle1, 0,0,1);
glTranslatef(0.3, 0,0);\\旋轉
glScalef(1, 0.3, 0.3);
glColor3f(1,0,0);
glutSolidSphere(0.3,30,30);
glPopMatrix();
glPopMatrix();
glFlush();
}
void motion(int x,int y)
{
angle1 = x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("WOW");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
float angle[8]={},angleOld[8]={},angleNew[8]={}, oldX=0, oldY=0,a=0;
int angleID=1;
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3);
glutSolidCube(1);
glPopMatrix();
}
void display()
{
glColor3ub(166,132,100);
GLfloat pos[]={0, 0, -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5, 0, 0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0.25, 0,0);
drawArm();
glPushMatrix();
glTranslatef(0.25, 0, 0);
glRotatef(angle[2], 0,0,1);
glTranslatef(0.25, 0,0);
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(-0.25, 0,0);
drawArm();
glPushMatrix();
glTranslatef(-0.25, 0, 0);
glRotatef(-angle[4], 0,0,1);
glTranslatef(-0.25, 0,0);
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void readNew()
{
if(fin==NULL)fin=fopen("02160013.txt","r");
for(int i=0;i<8;i++)
{
angleOld[i]=angleNew[i];
fscanf(fin,"%f",&angleNew[i]);
printf("%f ",angleNew[i]);
}
printf("\n");
}
void motion(int x, int y)
{
angle[angleID] -= y - oldY;
oldY=y; oldX=x;
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timerPlay(int t)
{
glutTimerFunc(20, timerPlay, t+1);
if(t%50==0) readNew();
a=(t%50)/50.0;
for(int i=0;i<8;i++)
{
angle[i]=(1-a)*angleOld[i]+(a)*angleNew[i];
}
glutPostRedisplay();
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1')angleID=1;
if(key=='2')angleID=2;
if(key=='3')angleID=3;
if(key=='4')angleID=4;
if(key=='p')glutTimerFunc(10,timerPlay,0);
if(key=='s')
{
if(fout==NULL)fout=fopen("02160013.txt","w+");
for(int i=0;i<8;i++)
{
printf("%.1f ",angle[i]);
fprintf(fout,"%.1f ",angle[i]);
}
fprintf(fout,"\n");
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160013");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言