Homework1:绘制一个三角形
#define _STDCALL_SUPPORTED
#define _M_IX86
#define GLUT_DISABLE_ATEXIT_HACK
#define GLUT_NO_WARNING_DISABLE
#include
#include
void myDisplay(void)
{
glClearColor(0.75f,0.75f,0.75f,1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,1.0);
glVertex3f(-0.8,-0.6,0);
glColor3f(0.0,0.9,0.0);
glVertex3f(0.8,-0.6,0);
glColor3f(0.0,0.0,1.0);
glVertex3f(0,0.7,0);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Homework1.1(A Triangle)");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
执行结果如下
Homework2:
使用GL_TRIANGLE_STRIP,代码如下
#define _STDCALL_SUPPORTED
#define _M_IX86
#define GLUT_DISABLE_ATEXIT_HACK
#define GLUT_NO_WARNING_DISABLE
#include
#include
const float top = 0.7 , bottom = -0.6 , left = -0.9 ,width = 0.2;
//顶点
struct Vertex{
float x,y,z;
};
void myDisplay(void)
{
Vertex v[10];
for(int i = 0; i < 10; i++){
v[i].x = left + width*i;
v[i].y = i%2*top + ((i+1)%2)*bottom;
v[i].z = 1.0;
}
glClearColor(0.01f,0.01f,0.01f,0.0f);//背景颜色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLE_STRIP);
for(int i = 0; i < 10; i++){
glColor3f(0.1+i*0.1,0.9-i*0.1,0.1+i*0.1);
glVertex3f(v[i].x,v[i].y,v[i].z);//此处不能使用glVertex3fv 原因在于其参数应为const类型
}
glEnd();
glLineWidth(3); //设置线宽
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//使用线框模式,默认为GL_full
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(20, 50);
glutInitWindowSize(1200, 400);
glutCreateWindow("Homework1.2(A single Triangle Strip)");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
效果图如下:
Homework3:多个Triangle_Strip 可形成三角网,代码如下(省略main函数)
const float top = 0.8 , left = -0.9 ,width = 0.2,height = 0.3;
const int colum = 20,row =5;
//顶点
struct Vertex{
float x,y,z;
};
void myDisplay(void)
{
Vertex v[row][colum];
for(int i = 0; i < row; i++){
for(int j = 0; j < colum; j++){
v[i][j].x = left + (j/2)*width;
v[i][j].y = j%2*(top-height*i) + ((j+1)%2)*(top-(i+1)*height);
v[i][j].z = 1.0;
}
}
glClearColor(0.01f,0.01f,0.01f,0.0f);//背景颜色
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
for(int i = 0; i < row; i++){
glBegin(GL_TRIANGLE_STRIP);
for(int j = 0; j < colum; j++){
glColor3f(0.05+j*0.05,0.9-j*0.05,0.05+j*0.05);
glVertex3f(v[i][j].x,v[i][j].y,v[i][j].z);
}
glEnd();
}
glLineWidth(3); //设置线宽
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//使用线框模式,默认为GL_full
glFlush();
}
效果如下