今天上課一開始我們做了一個像甜甜圈的線圖
移動大黑板甜甜圈就會轉動
程式碼如下:
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(angle, 0,1,0);
glutWireTorus(0.3, 0.7, 30, 30);
glFlush();
angle+=0.01;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160535haha");
glutDisplayFunc(display);
glutMainLoop();
}
兩個方塊面對面旋轉
程式碼:
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0,1,0);
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2, 0.2, 0.8);
glEnd();
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2,-0.8);
glVertex3f(-0.2, 0.2,-0.8);
glEnd();
glPopMatrix();
glFlush();
angle+=0.01;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160535haha");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
我第一次做兩個方塊旋轉的時候意外做出一個方塊和甜甜圈旋轉哈哈哈哈
程式碼:
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0,1,0);
glutWireTorus(0.3, 0.7, 30, 30);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2, 0.2, 0.8);
glEnd();
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2,-0.8);
glVertex3f(-0.2, 0.2,-0.8);
glEnd();
glPopMatrix();
glFlush();
angle+=0.01;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160535haha");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
然後再做甜甜圈的時候我又做錯了做出一個鳳梨片哈哈哈
程式碼:
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(angle, 0,1,0);
glutWireTorus(0.3, 0.7, 03, 30);
glFlush();
angle+=0.01;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160535haha");
glutDisplayFunc(display);
glutMainLoop();
}
自轉紅色長方形立體條
程式碼:
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1, 0.8);
glColor3f(1,0,0);///前面畫線哦
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2, 0.2, 0.8);
glEnd();
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2,-0.8);
glVertex3f(-0.2, 0.2,-0.8);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glVertex3f(-0.2, 0.2, 0.8);
glVertex3f(-0.2, 0.2,-0.8);
glEnd();
glColor3f(0, 0, 0);///後面畫線哦
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2, 0.2, 0.8);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2,-0.8);
glVertex3f(-0.2, 0.2,-0.8);
glEnd();
glBegin(GL_LINES);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glVertex3f(-0.2, 0.2, 0.8);
glVertex3f(-0.2, 0.2,-0.8);
glEnd();
glPopMatrix();
glFlush();
angle+=0.01;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160535haha");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
可拉式綠色三角柱
程式碼:
#include <GL/glut.h>
#include <stdio.h> ///別忘記他不然printf程式不認識噢
float angle=0, angle2=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glRotatef(angle2, 1, 0, 0);
glColor3f(0,1,0);///前面畫線哦
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glEnd();
glBegin(GL_POLYGON);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glBegin(GL_QUAD_STRIP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glColor3f(0, 0, 0);///後面畫線哦
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2, 0.8);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glBegin(GL_LINES);
glVertex3f( 0.2, 0.2, 0.8);
glVertex3f( 0.2, 0.2,-0.8);
glVertex3f( 0.2,-0.2, 0.8);
glVertex3f( 0.2,-0.2,-0.8);
glVertex3f(-0.2,-0.2, 0.8);
glVertex3f(-0.2,-0.2,-0.8);
glEnd();
glPopMatrix();
glFlush();
///angle+=0.01;
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
angle = x;
angle2 =y;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("02160535haha");
glutDisplayFunc(display);
glutIdleFunc(display);
///glutMouseFunc(mouse);///mouse的按鈕的按下去DOWN/彈起來UP
glutMotionFunc(motion);///mouse的motion
glutMainLoop();
}
我們加了這行程式碼glutInitDisplayMode(GLUT_DEPTH);
讓顯示的window有3D深度的功能
修改的程式碼:
float angle=0, angle2=0;
void display()
{
///要在main()裡面, glutInit()之後, glutCreateWindow()之前,
///加一行glutInitDisplayMode(GLUT_DEPTH); ///讓顯示的window有3D深度的功能
glEnable(GL_DEPTH_TEST); ///開啟Depth Test深度測試, 近的會擋到遠的, 遠的不會擋到近的
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH);///讓顯示的window有3D深度的功能
glutCreateWindow("02160535haha");
glutDisplayFunc(display);
glutIdleFunc(display);
///glutMouseFunc(mouse);///mouse的按鈕的按下去DOWN/彈起來UP
glutMotionFunc(motion);///mouse的motion
glutMainLoop();
}
沒有留言:
張貼留言