OpenGL中立方体的VAO和EBO表示方法
作者:互联网
文章目录
1.问题描述
在openGL中下面这个数组的如何理解:
const float vertices[] = { //立方体数组
-0.5f, -0.5f, -0.5f, 1.0f,0.0f,0.0f,
0.5f, -0.5f, -0.5f, 1.0f,0.0f,0.0f,
0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f,
0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f,
-0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f,
-0.5f, -0.5f, -0.5f, 1.0f,0.0f,0.0f,
-0.5f, -0.5f, 0.5f, 0.0f,1.0f,0.0f,
0.5f, -0.5f, 0.5f, 0.0f,1.0f,0.0f,
0.5f, 0.5f, 0.5f, 0.0f,1.0f,0.0f,
0.5f, 0.5f, 0.5f, 0.0f,1.0f,0.0f,
-0.5f, 0.5f, 0.5f, 0.0f,1.0f,0.0f,
-0.5f, -0.5f, 0.5f, 0.0f,1.0f,0.0f,
-0.5f, 0.5f, 0.5f, 0.0f,0.0f,1.0f,
-0.5f, 0.5f, -0.5f, 0.0f,0.0f,1.0f,
-0.5f, -0.5f, -0.5f, 0.0f,0.0f,1.0f,
-0.5f, -0.5f, -0.5f, 0.0f,0.0f,1.0f,
-0.5f, -0.5f, 0.5f, 0.0f,0.0f,1.0f,
-0.5f, 0.5f, 0.5f, 0.0f,0.0f,1.0f,
0.5f, 0.5f, 0.5f, 0.5f,0.0f,0.0f,
0.5f, 0.5f, -0.5f, 0.5f,0.0f,0.0f,
0.5f, -0.5f, -0.5f, 0.5f,0.0f,0.0f,
0.5f, -0.5f, -0.5f, 0.5f,0.0f,0.0f,
0.5f, -0.5f, 0.5f, 0.5f,0.0f,0.0f,
0.5f, 0.5f, 0.5f, 0.5f,0.0f,0.0f,
-0.5f, -0.5f, -0.5f, 0.0f,0.5f,0.0f,
0.5f, -0.5f, -0.5f, 0.0f,0.5f,0.0f,
0.5f, -0.5f, 0.5f, 0.0f,0.5f,0.0f,
0.5f, -0.5f, 0.5f, 0.0f,0.5f,0.0f,
-0.5f, -0.5f, 0.5f, 0.0f,0.5f,0.0f,
-0.5f, -0.5f, -0.5f, 0.0f,0.5f,0.0f,
-0.5f, 0.5f, -0.5f, 0.0f,0.0f,0.5f,
0.5f, 0.5f, -0.5f, 0.0f,0.0f,0.5f,
0.5f, 0.5f, 0.5f, 0.0f,0.0f,0.5f,
0.5f, 0.5f, 0.5f, 0.0f,0.0f,0.5f,
-0.5f, 0.5f, 0.5f, 0.0f,0.0f,0.5f,
-0.5f, 0.5f, -0.5f, 0.0f,0.0f,0.5f
};
2.问题解答
每3行就是一个三角形;
每一行的前三个值是三角形的每个点在空间中的位置,后三个值是颜色或者法向量。
立方体有6个面,每个面需要2个三角形拼接成立方体的一个面,共有12个三角形,
所以代码分开后有12个3行点。
const float vertices[] = { //立方体数组
/*------------------三角形EGC----------------------*/
-0.5f, -0.5f, -0.5f, 1.0f,0.0f,0.0f,//E
0.5f, -0.5f, -0.5f, 1.0f,0.0f,0.0f,//G
0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f,//C
/*------------------三角形CAE----------------------*/
0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f,//C
-0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f,//A
-0.5f, -0.5f, -0.5f, 1.0f,0.0f,0.0f,//E
/*------------------三角形FHD----------------------*/
-0.5f, -0.5f, 0.5f, 0.0f,1.0f,0.0f,//F
0.5f, -0.5f, 0.5f, 0.0f,1.0f,0.0f,//H
0.5f, 0.5f, 0.5f, 0.0f,1.0f,0.0f,//D
/*------------------三角形DBF----------------------*/
0.5f, 0.5f, 0.5f, 0.0f,1.0f,0.0f,//D
-0.5f, 0.5f, 0.5f, 0.0f,1.0f,0.0f,//B
-0.5f, -0.5f, 0.5f, 0.0f,1.0f,0.0f,//F
/*------------------三角形BAE----------------------*/
-0.5f, 0.5f, 0.5f, 0.0f,0.0f,1.0f,//B
-0.5f, 0.5f, -0.5f, 0.0f,0.0f,1.0f,//A
-0.5f, -0.5f, -0.5f, 0.0f,0.0f,1.0f,//E
/*------------------三角形EFB----------------------*/
-0.5f, -0.5f, -0.5f, 0.0f,0.0f,1.0f,//E
-0.5f, -0.5f, 0.5f, 0.0f,0.0f,1.0f,//F
-0.5f, 0.5f, 0.5f, 0.0f,0.0f,1.0f,//B
/*------------------三角形DCG----------------------*/
0.5f, 0.5f, 0.5f, 0.5f,0.0f,0.0f,//D
0.5f, 0.5f, -0.5f, 0.5f,0.0f,0.0f,//C
0.5f, -0.5f, -0.5f, 0.5f,0.0f,0.0f,//G
/*------------------三角形GHD----------------------*/
0.5f, -0.5f, -0.5f, 0.5f,0.0f,0.0f,//G
0.5f, -0.5f, 0.5f, 0.5f,0.0f,0.0f,//H
0.5f, 0.5f, 0.5f, 0.5f,0.0f,0.0f,//D
/*------------------三角形EGH----------------------*/
-0.5f, -0.5f, -0.5f, 0.0f,0.5f,0.0f,//E
0.5f, -0.5f, -0.5f, 0.0f,0.5f,0.0f,//G
0.5f, -0.5f, 0.5f, 0.0f,0.5f,0.0f,//H
/*------------------三角形HFE----------------------*/
0.5f, -0.5f, 0.5f, 0.0f,0.5f,0.0f,//H
-0.5f, -0.5f, 0.5f, 0.0f,0.5f,0.0f,//F
-0.5f, -0.5f, -0.5f, 0.0f,0.5f,0.0f,//E
/*------------------三角形ACD----------------------*/
-0.5f, 0.5f, -0.5f, 0.0f,0.0f,0.5f,//A
0.5f, 0.5f, -0.5f, 0.0f,0.0f,0.5f,//C
0.5f, 0.5f, 0.5f, 0.0f,0.0f,0.5f,//D
/*------------------三角形DBA----------------------*/
0.5f, 0.5f, 0.5f, 0.0f,0.0f,0.5f,//D
-0.5f, 0.5f, 0.5f, 0.0f,0.0f,0.5f,//B
-0.5f, 0.5f, -0.5f, 0.0f,0.0f,0.5f//A
};
3.EBO表示法
如果用EBO的话,vertices数组和之前不一样的地方就是重复的点不需要再写出来,写一行,然后记录他在数组中的位置(哪一行)。
再新开一个indices数组,indices数组中的每个数字,对应的是之前vertice数组中的点(之前记录的位置),每一行表示一个三角形。具体代码如下:
float vertices[] = {
-0.5f,0.5f,-0.5f, 0.0f, 0.0f, 0.0f,//点A 0
-0.5f,0.5f,0.5f, 0.0f, 0.0f, 1.0f,//点B 1
0.5f,0.5f,-0.5f, 0.0f, 1.0f, 0.0f,//点C 2
0.5f,0.5f,0.5f, 0.0f, 1.0f, 1.0f,//点D 3
-0.5f,-0.5f,-0.5f, 1.0f, 0.0f, 0.0f,//点E 4
-0.5f,-0.5f,0.5f, 1.0f, 0.0f, 1.0f,//点F 5
0.5f,-0.5f,-0.5f, 1.0f, 1.0f, 0.0f,//点G 6
0.5f,-0.5f,0.5f, 1.0f, 1.0f, 1.0f//点H 7
};
unsigned int indices[] = {
/*上面ABC,BCD*/
0,1,2,
1,2,3,
/*下面EFG,FGH*/
4,5,6,
5,6,7,
/*左侧面ABF,AEF*/
0,1,5,
0,4,5,
/*右侧面CDH,CGH*/
2,3,7,
2,6,7,
/*前面ACG,AEG*/
0,2,6,
0,4,6,
/*后面BFH,BDH*/
1,5,7,
1,3,7
};
参考文章:
https://www.jianshu.com/p/12b0c198c2e0
https://blog.csdn.net/dcrmg/article/details/53556664
标签:1.0,VAO,OpenGL,0.0,EBO,0.5,数组,立方体,三角形 来源: https://blog.csdn.net/qq_41286360/article/details/90339885