开始绘制立体图形立方体正四面体

刚刚学会绘制B曲线和曲面,突然想学习绘制立体图形了,说干就干,百度了一下,从立方体开始

做了好好长时间终于完成了。。实现的代码如下

#include  
#include       
#include       
#include       

static const GLfloat vertex_list[][3] = {
	-0.5f, -0.5f, -0.5f,
	0.5f, -0.5f, -0.5f,
	0.5f, 0.5f, -0.5f,
	-0.5f, 0.5f, -0.5f,
	-0.5f, -0.5f, 0.5f,
	0.5f, -0.5f, 0.5f,
	0.5f, 0.5f, 0.5f,
	-0.5f, 0.5f, 0.5f,
};

static const GLint index_list[][4] = {
	0, 1, 2, 3,//bottem
	0, 3, 7, 4,//left
	2, 3, 7, 6,//front
	1, 2, 6, 5,//right
	0, 1, 5, 4,//back
	4, 5, 6, 7//top
};



void myDisplay(void){
	glClear(GL_COLOR_BUFFER_BIT);
	glRotatef(45, 1, 1, 1);
	glFrontFace(GL_CCW);

	for (int i = 0; i < 6; ++i)      // 有六个面,循环六次
	{
		glBegin(GL_LINE_LOOP);
		for (int j = 0; j < 4; ++j)     // 每个面有四个顶点,循环四次
			glVertex3fv(vertex_list[index_list[i][j]]);
		glEnd();
	}


	glFlush();
}


int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("opengl1");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}  
只是画出了线,目前还没有面,效果如下

开始绘制立体图形立方体正四面体_第1张图片

下面我打算利用数学公式绘制一些简单的几何体,最简单的就是正四面体

知道几个点的坐标就很容易地可以绘制出正四面体

#include  
#include       
#include       
#include       

#define pi 3.1415927f;
float a= 0.5f;
float u = sqrtf(2.0) / 3;
float v = float(1 / 3);
float t = sqrtf(6.0) / 3;

static const GLfloat vertex_list[][3] = {
	0.0f   , 0.0f  ,  a   ,
	-2*a*u , 0,0   , -a*v ,
	a*u    , a*t   , -a*v ,
	a*u    , -a*t  , -a*v ,
};

static const GLint index_list[][3] = {
	0, 1, 2, 
	0, 1, 2, 
	0, 1, 3,
	1, 2, 3,
};



void myDisplay(void){
	glClear(GL_COLOR_BUFFER_BIT);
	glRotatef(45, 1, 1, 1);
	glFrontFace(GL_CCW);

	for (int i = 0; i < 4; ++i)      // 有四个面,循环六次
	{
		glBegin(GL_LINE_LOOP);
		for (int j = 0; j < 3; ++j)     // 每个面有四个顶点,循环四次
			glVertex3fv(vertex_list[index_list[i][j]]);
		glEnd();
	}


	glFlush();
}


int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("opengl1");
	glutDisplayFunc(&myDisplay);
	glutMainLoop();
	return 0;
}  
绘制出的图为

开始绘制立体图形立方体正四面体_第2张图片

下一篇中我开始探索绘制圆形以及球体,再一次进阶,哦也

你可能感兴趣的:(opengl初步学习)