glDrawElements 绘制立方体共用顶点的法线和UV处理
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
第一个参数指定了我们绘制的模式,这个和glDrawArrays的一样。第二个参数是我们打算绘制顶点的个数,这里填6,也就是说我们一共需要绘制6个顶点。第三个参数是索引的类型,这里是GL_UNSIGNED_INT。最后一个参数里我们可以指定EBO中的偏移量(或者传递一个索引数组,但是这是当你不在使用索引缓冲对象的时候),但是我们会在这里填写0。基础的使用可参考该教程:https://learnopengl-cn.github.io/01%20Getting%20started/04%20Hello%20Triangle/
在不启用索引缓冲对象的时候,传递一个索引数组,顶点坐标共用没有问题,但是UV和法线公用的话就会出错,因为每个面上的顶点UV属性和法线属性可能不同。所以要以面为单位
鸣谢文章:https://www.cnblogs.com/hefee/p/3822844.html
顶点v0为正面、右面与顶面所共用,不过法线并不能共用。正面的法线为n0,右面的法线为n1,顶面的法线为n2。对于这种情况,法线并不与共用顶点相同,顶点就不能够仅在顶点数组中指定一次。为了匹配法向数组中元素的数量,顶点数组中必须多次指定该顶点坐标。具有法线的典型立方体需要24个单一顶点:6个面×每面4个顶点。参考示例代码中的实际实现。
备份源码链接:https://pan.baidu.com/s/10OJH54YliiJwYsxM9Ht55A
// cube ///
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3// vertex coords array for glDrawArrays() =====================================
// A cube has 6 sides and each side has 2 triangles, therefore, a cube consists
// of 36 vertices (6 sides * 2 tris * 3 vertices = 36 vertices). And, each
// vertex is 3 components (x,y,z) of floats, therefore, the size of vertex
// array is 108 floats (36 * 3 = 108).
GLfloat vertices1[] = { 1, 1, 1, -1, 1, 1, -1,-1, 1, // v0-v1-v2 (front)-1,-1, 1, 1,-1, 1, 1, 1, 1, // v2-v3-v01, 1, 1, 1,-1, 1, 1,-1,-1, // v0-v3-v4 (right)1,-1,-1, 1, 1,-1, 1, 1, 1, // v4-v5-v01, 1, 1, 1, 1,-1, -1, 1,-1, // v0-v5-v6 (top)-1, 1,-1, -1, 1, 1, 1, 1, 1, // v6-v1-v0-1, 1, 1, -1, 1,-1, -1,-1,-1, // v1-v6-v7 (left)-1,-1,-1, -1,-1, 1, -1, 1, 1, // v7-v2-v1-1,-1,-1, 1,-1,-1, 1,-1, 1, // v7-v4-v3 (bottom)1,-1, 1, -1,-1, 1, -1,-1,-1, // v3-v2-v71,-1,-1, -1,-1,-1, -1, 1,-1, // v4-v7-v6 (back)-1, 1,-1, 1, 1,-1, 1,-1,-1 }; // v6-v5-v4// normal array
GLfloat normals1[] = { 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2 (front)0, 0, 1, 0, 0, 1, 0, 0, 1, // v2-v3-v01, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4 (right)1, 0, 0, 1, 0, 0, 1, 0, 0, // v4-v5-v00, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6 (top)0, 1, 0, 0, 1, 0, 0, 1, 0, // v6-v1-v0-1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7 (left)-1, 0, 0, -1, 0, 0, -1, 0, 0, // v7-v2-v10,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3 (bottom)0,-1, 0, 0,-1, 0, 0,-1, 0, // v3-v2-v70, 0,-1, 0, 0,-1, 0, 0,-1, // v4-v7-v6 (back)0, 0,-1, 0, 0,-1, 0, 0,-1 }; // v6-v5-v4// color array
GLfloat colors1[] = { 1, 1, 1, 1, 1, 0, 1, 0, 0, // v0-v1-v2 (front)1, 0, 0, 1, 0, 1, 1, 1, 1, // v2-v3-v01, 1, 1, 1, 0, 1, 0, 0, 1, // v0-v3-v4 (right)0, 0, 1, 0, 1, 1, 1, 1, 1, // v4-v5-v01, 1, 1, 0, 1, 1, 0, 1, 0, // v0-v5-v6 (top)0, 1, 0, 1, 1, 0, 1, 1, 1, // v6-v1-v01, 1, 0, 0, 1, 0, 0, 0, 0, // v1-v6-v7 (left)0, 0, 0, 1, 0, 0, 1, 1, 0, // v7-v2-v10, 0, 0, 0, 0, 1, 1, 0, 1, // v7-v4-v3 (bottom)1, 0, 1, 1, 0, 0, 0, 0, 0, // v3-v2-v70, 0, 1, 0, 0, 0, 0, 1, 0, // v4-v7-v6 (back)0, 1, 0, 0, 1, 1, 0, 0, 1 }; // v6-v5-v4///
// draw cube at upper-right corner with glDrawArrays
// A cube has only 8 vertices, but each vertex is shared for 3 different faces,
// which have different normals. Therefore, we need more than 8 vertex data to
// draw a cube. Since each face has 2 triangles, we need 6 vertices per face.
// (2 * 3 = 6) And, a cube has 6 faces, so, the total number of vertices for
// drawing a cube is 36 (= 6 faces * 6 vertices).
// Note that there are some duplicated vertex data for glDrawArray() because
// the vertex data in the vertex array must be sequentially placed in memory.
// For a cube, there are 24 unique vertex data and 12 redundant vertex data in
// the vertex array.
///
void draw2()
{// enble and specify pointers to vertex arraysglEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_COLOR_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);glNormalPointer(GL_FLOAT, 0, normals1);glColorPointer(3, GL_FLOAT, 0, colors1);glVertexPointer(3, GL_FLOAT, 0, vertices1);glPushMatrix();glTranslatef(2, 2, 0); // move to upper-right cornerglDrawArrays(GL_TRIANGLES, 0, 36);glPopMatrix();glDisableClientState(GL_VERTEX_ARRAY); // disable vertex arraysglDisableClientState(GL_COLOR_ARRAY);glDisableClientState(GL_NORMAL_ARRAY);
}// vertex array for glDrawElements() and glDrawRangeElement() =================
// Notice that the sizes of these arrays become samller than the arrays for
// glDrawArrays() because glDrawElements() uses an additional index array to
// choose designated vertices with the indices. The size of vertex array is now
// 24 instead of 36, but the index array size is 36, same as the number of
// vertices required to draw a cube.
GLfloat vertices2[] = { 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // v0,v1,v2,v3 (front)1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // v0,v3,v4,v5 (right)1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // v0,v5,v6,v1 (top)-1, 1, 1, -1, 1,-1, -1,-1,-1, -1,-1, 1, // v1,v6,v7,v2 (left)-1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // v7,v4,v3,v2 (bottom)1,-1,-1, -1,-1,-1, -1, 1,-1, 1, 1,-1 }; // v4,v7,v6,v5 (back)// normal array
GLfloat normals2[] = { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0,v1,v2,v3 (front)1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0,v3,v4,v5 (right)0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0,v5,v6,v1 (top)-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1,v6,v7,v2 (left)0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7,v4,v3,v2 (bottom)0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 }; // v4,v7,v6,v5 (back)// color array
GLfloat colors2[] = { 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, // v0,v1,v2,v3 (front)1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, // v0,v3,v4,v5 (right)1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, // v0,v5,v6,v1 (top)1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, // v1,v6,v7,v2 (left)0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // v7,v4,v3,v2 (bottom)0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1 }; // v4,v7,v6,v5 (back)// index array of vertex array for glDrawElements() & glDrawRangeElement()
GLubyte indices[] = { 0, 1, 2, 2, 3, 0, // front4, 5, 6, 6, 7, 4, // right8, 9,10, 10,11, 8, // top12,13,14, 14,15,12, // left16,17,18, 18,19,16, // bottom20,21,22, 22,23,20 }; // back// In a cube, the number of vertex data in the vertex array can be reduced to
// 24 vertices for glDrawElements().
// Note that you need an additional array (index array) to store how to traverse
// the vertext data. For a cube, we need 36 entries in the index array.
///
void draw3()
{// enable and specify pointers to vertex arraysglEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_COLOR_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);glNormalPointer(GL_FLOAT, 0, normals2);glColorPointer(3, GL_FLOAT, 0, colors2);glVertexPointer(3, GL_FLOAT, 0, vertices2);glPushMatrix();glTranslatef(-2, -2, 0); // move to bottom-left cornerglDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);glPopMatrix();glDisableClientState(GL_VERTEX_ARRAY); // disable vertex arraysglDisableClientState(GL_COLOR_ARRAY);glDisableClientState(GL_NORMAL_ARRAY);
}
glDrawElements 绘制立方体共用顶点的法线和UV处理相关推荐
- php绘制正方体,Three.js利用顶点绘制立方体方法
本文主要给大家介绍了关于Three.js利用顶点绘制立方体的方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧.希望能帮助到大家. 前言 之前我们在学些WebGL基础的时候 ...
- Android OpenGLES2.0(五)——绘制立方体
上篇博客中我们提到了OpenGLES中绘制的两种方法,顶点法和索引法.之前我们所使用的都是顶点法,这次绘制立方体使用索引法来绘制立方体. 构建立方体 上篇博客讲到正方形的绘制,立方体是是由六个正方形组 ...
- Panda3D绘制立方体
看一下Panda3D的Tut-Procedural-Cube示例: 一个旋转的立方体: 可以在六个面上添加纹理: 单独看一下绘制立方体这部分代码: from direct.showbase.ShowB ...
- android平台下OpenGL ES 3.0绘制立方体的几种方式
OpenGL ES 3.0学习实践 android平台下OpenGL ES 3.0从零开始 android平台下OpenGL ES 3.0绘制纯色背景 android平台下OpenGL ES 3.0绘 ...
- 【OpenGL ES】绘制立方体
1 前言 本文主要介绍使用 OpenGL ES 绘制立方体,读者如果对 OpenGL ES 不太熟悉,请回顾以下内容: 绘制三角形 绘制彩色三角形 绘制正方形 绘制圆形 在绘制立方体的过程中,主要用到 ...
- OpenGL ES for Android 绘制立方体
立方体有6个面,8个顶点,因此绘制立方体其实就是绘制6个面. 顶点shader attribute vec4 a_Position; attribute vec4 a_color; varying v ...
- python画正方体_python绘制立方体的方法
本文实例为大家分享了python绘制立方体的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python # This is (almost) a direct C++ to Pyt ...
- WebGL绘制立方体-每个面一种颜色
WebGL绘制立方体-每个面一种颜色 本文是WebGL电子书的1.8节内容 思路很简单,在线框模式的立方体源码基础上直接进行更改,添加varying变量,引入顶点数据颜色,立方体6个面,每个面可以分为 ...
- 采用QT进行OpenGL开发(二)绘制立方体
在OpenGL中三维实体会被拆分成一个个小的平面图形来进行绘制.比如我们绘制的立方体,可以拆分成六个平面来进行绘制.这里我们以一个带纹理的正方体来说明一下三维实体的绘制方法. 绘制立方体 由于正方体的 ...
最新文章
- 根据内容来产生一个二维码
- Efficient Graph-Based Image Segmentation
- dockerfile COPY
- 机器学习课程笔记【五】- 支持向量机(1)
- svn汉化失败解决方法
- FireMonkey ListView 绑定数据显示多个图片
- html 画xyz坐标,xyz坐标轴怎么画要图谢谢?
- 含泪整理最优质立秋海报设计素材,你想要的这里都有
- 大数据常见面试题总结,有问必答
- 小学计算机兴趣小组计划书,小学科技兴趣小组活动计划书
- Swift教程-视频拍摄教程
- 关于T51的25C°电池曲线合成(MTK)
- nginx配置和优化详解
- HTML-CSS笔记
- 如何将网页变成桌面屏保
- 数据库系统之幻影读现象项目练习
- IA-32寄存器基本介绍
- 数据的距离度量 二、余弦距离,汉明距离,测地距离,布雷柯蒂斯距离
- 《读大学,究竟读什么?》 笔记
- 用IDEA打包springboot项目报错:--- maven-compiler-plugin:3.8.1:compile (default-compile) @ springboot_homewor