week02
前置作業:
將上週freeglut應用的GLUT打開
*step01:做茶壺
#include <GL/glut.h>
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers(); //交換顯示
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //設定顯示模式
glutCreateWindow("week02"); //視窗名稱
glutDisplayFunc(display); //顯示的函式
glutMainLoop(); //主要迴圈最後
}
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers(); //交換顯示
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //設定顯示模式
glutCreateWindow("week02"); //視窗名稱
glutDisplayFunc(display); //顯示的函式
glutMainLoop(); //主要迴圈最後
}
*step02:把茶壺弄上顏色
#include <GL/glut.h>
void display()
{
glColor3f(1,1,1); //選擇顏色
glutSolidTeapot(0.3); //實心茶壺
glColor3f(1,0,0.6); //粉色
glutSolidTeapot(0.2);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week02");
glutDisplayFunc(display);
glutMainLoop();
}
*step03:放一個三角形
#include <GL/glut.h>
void display()
{
glColor3f(1,1,1);
glBegin(GL_POLYGON);
glVertex2f(0,1); //上面頂點
glVertex2f(-1,-1); //左下角頂點
glVertex2f(1,-1); //右下角頂點
glEnd();
glColor3f(1,0,0.6);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week02");
glutDisplayFunc(display);
glutMainLoop();
}
*step04:加三角形的顏色
#include <GL/glut.h>void display(){ glBegin(GL_POLYGON); glColor3f(1,1,1); glVertex2f(0,1); glColor3f(0.5,0,0.5); glVertex2f(-1,-1); glColor3f(0.8,0,0.8); glVertex2f(1,-1); glEnd(); glColor3f(1,0,0.6); glutSolidTeapot(0.3); glutSwapBuffers();}int main(int argc, char *argv[]){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("week02"); glutDisplayFunc(display); glutMainLoop();}
#include <GL/glut.h>
void display()
{
glBegin(GL_POLYGON);
glColor3f(1,1,1); glVertex2f(0,1);
glColor3f(0.5,0,0.5); glVertex2f(-1,-1);
glColor3f(0.8,0,0.8); glVertex2f(1,-1);
glEnd();
glColor3f(1,0,0.6);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week02");
glutDisplayFunc(display);
glutMainLoop();
}
*step05:用小畫家找圖的位置和顏色
*step06:畫圓
#include <GL/glut.h>#include <math.h>void myCircle(float r, float x, float y){ glBegin(GL_POLYGON); for(float a=0; a<=2*3.141592; a+=0.01) { glVertex2f( r*cos(a)+x, r*sin(a)+y); } glEnd();}void display(){ glColor3f(1,1,1); //大圓 myCircle(0.5,0,0);
glColor3f(1,0,0.5); //左上 myCircle(0.3,-0.4,0.6);
glColor3f(0.8,0,0.8); //右下 myCircle(0.3,0.4,-0.6);
glutSwapBuffers();}int main(int argc, char *argv[]){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("week02"); glutDisplayFunc(display); glutMainLoop();}
#include <GL/glut.h>
#include <math.h>
void myCircle(float r, float x, float y)
{
glBegin(GL_POLYGON);
for(float a=0; a<=2*3.141592; a+=0.01)
{
glVertex2f( r*cos(a)+x, r*sin(a)+y);
}
glEnd();
}
void display()
{
glColor3f(1,1,1); //大圓
myCircle(0.5,0,0);
glColor3f(1,0,0.5); //左上
myCircle(0.3,-0.4,0.6);
glColor3f(0.8,0,0.8); //右下
myCircle(0.3,0.4,-0.6);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week02");
glutDisplayFunc(display);
glutMainLoop();
}
.png)
.png)
.png)
.png)
沒有留言:
張貼留言