顯示具有 01160413_莊博鈞 標籤的文章。 顯示所有文章
顯示具有 01160413_莊博鈞 標籤的文章。 顯示所有文章

2014年5月1日 星期四

01160413 莊博鈞 Week 11

增加三個球
#include <GL/glut.h>
float angleX=0;
void display()
{
  glEnable(GL_DEPTH_TEST);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
  glTranslatef(0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPopMatrix();
 
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(-0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPopMatrix();
 
  glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glTranslatef(0,0.4,0);
  glutSolidSphere(0.2,30,30);
  glPopMatrix();
 
 
  glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
     if(key=='a') angleX+=3;
     if(key=='b') angleX-=3;
     glutPostRedisplay();
        
}
void myLight()
{
  const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
  glEnable(GL_LIGHT0);
  glEnable(GL_NORMALIZE);
  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_LIGHTING); 
  glLightfv(GL_LIGHT0, GL_POSITION, light_position); 
}
int main()
{
  glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
  glutCreateWindow("week11");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);
  myLight();
  glutMainLoop();
}

可控制的機械手臂
#include <GL/glut.h>
float angleX=0 ,angle1=0;
void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,1);
glTranslatef(0.3,0,0);
glScalef(3,1,1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();

glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,-1);
glTranslatef(-0.3,0,0);
glScalef(3,1,1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30); 
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
     if(key=='a') angleX+=3;
     if(key=='b') angleX-=3;
     if(key=='1') angle1+=10;
     if(key=='2') angle1-=10;
     glutPostRedisplay();
        
}
void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
 glEnable(GL_LIGHT0);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_LIGHTING);  
 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}


加入腳
#include <GL/glut.h>
float angleX=0 ,angle1=0;
void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,1);
glTranslatef(0.3,0,0);
glScalef(2,1,1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();

glPushMatrix();
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle1, 0,0,-1);
glTranslatef(-0.3,0,0);
glScalef(2,1,1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30); 
glPopMatrix();
glPushMatrix();//右腳 
glTranslatef(0.2,-0.2,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);
glPushMatrix();
glScalef(0.2,0.36,0.2);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-0.18,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);
glScalef(0.2,0.36,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左腳 
glTranslatef(-0.2,-0.2,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);
glPushMatrix();
glScalef(0.2,0.36,0.2);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-0.18,0);
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);
glScalef(0.2,0.36,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();

glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
     if(key=='a') angleX+=3;
     if(key=='b') angleX-=3;
     if(key=='1') angle1+=3;
     if(key=='2') angle1-=3;
     glutPostRedisplay();
        
}
void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
 glEnable(GL_LIGHT0);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_LIGHTING);  
 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
 glutCreateWindow("week11");
 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);
 myLight();
 glutMainLoop();
}


2014年4月24日 星期四

01160413 莊博鈞 Week 10

讓鍵盤可以控制teapot
讓小黑視窗能夠顯示滑鼠移動中所有的座標
#include <GL/glut.h>
#include<stdio.h>

float teapotX=0, teapotY=0;
void keyboard(unsigned char key, int x,int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a'||key=='A') teapotX+=0.1;
if(key=='b'||key=='B') teapotX-=0.1;
if(key=='c'||key=='C') teapotY+=0.1;
if(key=='d'||key=='D') teapotY-=0.1;

glutPostRedisplay();
}
void mouse (int button,int state,int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
}
void motion (int x, int y)
{
printf("%d %d\n",x,y);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}

讓滑鼠能夠拉teapot讓它轉動

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

float teapotX=0, teapotY=0;
float angleX=0,startX=0;
void mouse (int button,int state,int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
startX=x;
}
void motion (int x, int y)
{
printf("%d %d\n",x,y);
angleX+=(x-startX);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glRotatef(angleX,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}

這是能夠隨意旋轉的程式碼,加上打光
#include <GL/glut.h>
#include<stdio.h>

float teapotX=0, teapotY=0;
float angleX=0,startX=0;
float angleY=0,startY=0;
void mouse (int button,int state,int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
startX=x;
startY=y;
}
void motion (int x, int y)
{
printf("%d %d\n",x,y);
angleX+=(x-startX);startX=x;
angleY+=(y-startY);startY=y;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glRotatef(angleX,0,-1,0);
glRotatef(angleY,-1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void myLight()
{
const GLfloat light_position[]={2.0f,5.0f,-5.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}



2014年2月20日 星期四

01160413 莊博鈞 Week 01



 glRotatef (theta, 0.0f, 0.0f, 1.0f);
把這一行程式碼註解圖形就不會旋轉了