1.一如往常地把GLUT打開,老師教了glutSolidSphere的用法。

2.進入課程寫程式,做出一個白色正方型。

#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidCube(1);
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GL_DOUBLE| GL_DEPTH);
glutCreateWindow("weeK09");
glutDisplayFunc(display);
glutMainLoop();
}
3.變成紅色正方形!!

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0); //加入顏色程式
glutSolidCube(1);
glutSwapBuffers();
4.做長方形

#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///備份矩陣
glColor3f(1,0,0); ///顏色
glScalef(0.5,0.2,0.2); ///縮放
glutSolidCube(1);
glPopMatrix(); ///還原矩陣
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("weeK09");
glutDisplayFunc(display);
glutMainLoop();
}
----------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
void myCube() ///用自己的函式簡化
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5,0.2,0.2); ////下面移上去
glutSolidCube(1);
glPopMatrix();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
myCube();///用自己的函式簡化
myCube();///方塊黏在一起了!!
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("weeK09");
glutDisplayFunc(display);
glutMainLoop();
}
5.方塊重疊
#include <GL/glut.h>
void myCube() ///簡化程式
{
glPushMatrix();
glScalef(0.5,0.2,0.2); ////調大小要放在Push/Pop裡面
glutSolidCube(1);
glPopMatrix();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ////保護
glColor3f(1,0,0);
myCube();
glTranslatef(0.4,0.1,0);
glColor3f(1,1,0); ///加個黃色
myCube();
glPopMatrix(); ///保護
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("weeK09");
glutDisplayFunc(display);
glutMainLoop();
}
6.方塊旋轉

#include <GL/glut.h>
void myCube()
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
}
float angle=0; ///轉動變數 (角度)
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1); ///每一次的增加角度
myCube();
glPopMatrix();
angle++; ///增加角度
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display); ///一定要記得打!!!!有空idle,在重畫畫面
glutDisplayFunc(display);
glutMainLoop();
}
7.做一個中心體再旋轉(用綠色方體作範例)

#include <GL/glut.h>
void myCube()
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
}
float angle=0;
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glutSolidCube(0.6);
glPushMatrix();
glTranslatef(0.3,0.3,0); ///把下面(旋轉中)的手臂,掛到右上角
glRotatef(angle,0,0,1);
glTranslatef(0.22,0,0);
////把要轉動的中心點,放到畫面的中心
myCube();
glPopMatrix();
angle++;
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}
6.方塊旋轉

#include <GL/glut.h>
void myCube()
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
}
float angle=0; ///轉動變數 (角度)
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1); ///每一次的增加角度
myCube();
glPopMatrix();
angle++; ///增加角度
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display); ///一定要記得打!!!!有空idle,在重畫畫面
glutDisplayFunc(display);
glutMainLoop();
}
7.做一個中心體再旋轉(用綠色方體作範例)

#include <GL/glut.h>
void myCube()
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
}
float angle=0;
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glutSolidCube(0.6);
glPushMatrix();
glTranslatef(0.3,0.3,0); ///把下面(旋轉中)的手臂,掛到右上角
glRotatef(angle,0,0,1);
glTranslatef(0.22,0,0);
////把要轉動的中心點,放到畫面的中心
myCube();
glPopMatrix();
angle++;
glutSwapBuffers();
}
int main (int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}

沒有留言:
張貼留言