(1)使用貝殼專案
程式碼--
#include <stdio.h>
FILE * fout = NULL,*fin = NULL;
int main(int argc,char**argv)
{
if(fout==NULL)
fout = fopen("output.txt","wt");
fprintf(fout,"Hello World\n");
return 0;
}
課堂作業二--茶壺讀取
(1).加入檔案
(2).
程式碼--
#include <GL/glut.h>
#include <stdio.h>
FILE * fout = NULL,*fin = NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void motion(int x, int y)
{
if(fout==NULL) fout=fopen("motion.txt","w+");
fprintf(fout, "%d %d\n",x,y);
printf("%d %d\n",x,y);
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
(3).缺少freeglut.dll檔,放置目錄位置。
(4).檔案開啟成功
Motion
(5).紀錄位置(還未記錄)
(6).關掉執行檔,顯示紀錄。
課堂作業三:以一鍵移動
(1).先以滑鼠左鍵紀錄位置
再案r鍵移動(從最後位置到初始位置)
程式碼--
#include <GL/glut.h>
#include <stdio.h>
FILE * fout = NULL,*fin = NULL;
int nowX=0,nowY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((nowX-150)/150.0,(nowY-150)/150.0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
if(fout==NULL) fout=fopen("motion.txt","w+");
fprintf(fout, "%d %d\n",x,y);
printf("%d %d\n",x,y);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r')
{
if(fin==NULL) fin=fopen("motion.txt","r");
fscanf(fin ,"%d %d\n", &nowX,&nowY);
printf("%d %d\n",nowX, nowY);
}
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
課堂作業四
(1)
程式碼--
#include <stdio.h>
#include <GL/glut.h>
FILE *fout=NULL, *fin=NULL;
float rot[10]={0};
int rotID=1, oldX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutWireCube(0.3);
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[3],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
oldX=x;
}
}void motion(int x, int y)
{
rot[rotID] += (x-oldX);
oldX=x;
glutPostRedisplay();
if(fout==NULL) fout=fopen("motion.txt","w+");
for(int i=0;i<10;i++){
fprintf( fout, "%.1f ", rot[i]);;
printf( "%.1f ", rot[i]);;
}
fprintf(fout, "\n");
printf("\n");
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') rotID=1;
if(key=='2') rotID=2;
if(key=='3') rotID=3;
if(key=='4') rotID=4;
if(key=='5') rotID=5;
if(key=='6') rotID=6;
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt","r");
for(int i=0;i<10;i++){
fscanf( fin, "%f ", &rot[i]);;
printf( "%f ", rot[i]);;
}
fprintf(fin, "\n");
printf("\n");
}
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
/*#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL, *fin=NULL;
int nowX=0, nowY=0;//, oldX=0, oldY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((nowX-150)/150.0, -(nowY-150)/150.0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
if(fout==NULL) fout=fopen("motion.txt","w+");
fprintf( fout, "%d %d\n", x, y);
printf("%d %d\n", x, y);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt","r");
fscanf( fin, "%d %d\n", &nowX, &nowY);
printf("%d %d\n", nowX, nowY);
}
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
int main(int argc, char**argv)
{
for(int i=0;i<argc; i++){
printf("argv[%d] is %s\n", i, argv[i]);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
*/
#include <GL/glut.h>
#include <stdio.h>
FILE * fout = NULL,*fin = NULL;
int nowX=0,nowY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((nowX-150)/150.0,(nowY-150)/150.0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
if(fout==NULL) fout=fopen("motion.txt","w+");
fprintf(fout, "%d %d\n",x,y);
printf("%d %d\n",x,y);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r')
{
if(fin==NULL) fin=fopen("motion.txt","r");
fscanf(fin ,"%d %d\n", &nowX,&nowY);
printf("%d %d\n",nowX, nowY);
}
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
課堂作業四
(1)
程式碼--
#include <stdio.h>
#include <GL/glut.h>
FILE *fout=NULL, *fin=NULL;
float rot[10]={0};
int rotID=1, oldX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutWireCube(0.3);
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[3],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
oldX=x;
}
}void motion(int x, int y)
{
rot[rotID] += (x-oldX);
oldX=x;
glutPostRedisplay();
if(fout==NULL) fout=fopen("motion.txt","w+");
for(int i=0;i<10;i++){
fprintf( fout, "%.1f ", rot[i]);;
printf( "%.1f ", rot[i]);;
}
fprintf(fout, "\n");
printf("\n");
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') rotID=1;
if(key=='2') rotID=2;
if(key=='3') rotID=3;
if(key=='4') rotID=4;
if(key=='5') rotID=5;
if(key=='6') rotID=6;
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt","r");
for(int i=0;i<10;i++){
fscanf( fin, "%f ", &rot[i]);;
printf( "%f ", rot[i]);;
}
fprintf(fin, "\n");
printf("\n");
}
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
/*#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL, *fin=NULL;
int nowX=0, nowY=0;//, oldX=0, oldY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((nowX-150)/150.0, -(nowY-150)/150.0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
if(fout==NULL) fout=fopen("motion.txt","w+");
fprintf( fout, "%d %d\n", x, y);
printf("%d %d\n", x, y);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt","r");
fscanf( fin, "%d %d\n", &nowX, &nowY);
printf("%d %d\n", nowX, nowY);
}
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
int main(int argc, char**argv)
{
for(int i=0;i<argc; i++){
printf("argv[%d] is %s\n", i, argv[i]);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
*/











沒有留言:
張貼留言