OpenGl 基本函数 glDrawArrays 详解

本文章是转载:下面的几张图一目了然,很不多。

https://www.cnblogs.com/lxb0478/p/6381677.html

glDrawArrays的功能:提供绘制功能,从数组数据中提取数据渲染基本图元。

 

定义

void glDrawArrays(  GLenum mode,    GLint first,    GLsizei count);  

 

参数

mode

    需要渲染的图元类型,包括 GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN ,GL_TRIANGLES。

  • GL_POINTS:把每一个顶点作为一个点进行处理,顶点n即定义了点n,共绘制N个点
  • GL_LINES:连接每两个顶点作为一个独立的线段,顶点2n-1和2n之间共定义了n条线段,总共绘制N/2条线段
  • GL_LINE_STRIP:绘制从第一个顶点到最后一个顶点依次相连的一组线段,第n和n+1个顶点定义了线段n,总共绘制n-1条线段
  • GL_LINE_LOOP:绘制从第一个顶点到最后一个顶点依次相连的一组线段,然后最后一个顶点和第一个顶点相连,第n和n+1个顶点定义了线段n,总共绘制n条线段
  • GL_TRIANGLES:把每三个顶点作为一个独立的三角形,顶点3n-2、3n-1和3n定义了第n个三角形,总共绘制N/3个三角形
  • GL_TRIANGLE_STRIP:绘制一组相连的三角形,对于奇数n,顶点n、n+1和n+2定义了第n个三角形;对于偶数n,顶点n+1、n和n+2定义了第n个三角形,总共绘制N-2个三角形
  • GL_TRIANGLE_FAN:绘制一组相连的三角形,三角形是由第一个顶点及其后给定的顶点确定,顶点1、n+1和n+2定义了第n个三角形,总共绘制N-2个三角形

 

first   

    从数组缓存中的哪一位开始绘制,一般为0.

count

    数组中顶点的数量.

 

功能

glDrawArrays specifies multiple geometric primitives with very few subroutine calls. It is possible to prespecify separate arrays of attributes and use them to construct a sequence of primitives with a single call to glDrawArrays.

When glDrawArrays is called, it uses count sequential elements from each enabled array to construct a sequence of geometric primitives, beginning with element first. mode specifies what kind of primitives are constructed and how the array elements construct those primitives.

To enable and disable a generic vertex attribute array, call glEnableVertexAttribArray and glDisableVertexAttribArray.

If an array corresponding to a generic attribute required by a vertex shader is not enabled, then the corresponding element is taken from the current generic attribute state. Errors

GL_INVALID_ENUM is generated if mode is not an accepted value.

GL_INVALID_VALUE is generated if count is negative.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to an enabled array and the buffer object's data store is currently mapped.

GL_INVALID_FRAMEBUFFER_OPERATION is generated if the currently bound framebuffer is not framebuffer complete (i.e. the return value from glCheckFramebufferStatus is not GL_FRAMEBUFFER_COMPLETE).

GL_INVALID_OPERATION is generated if recording the vertices of a primitive to the buffer objects being used for transform feedback purposes would result in either exceeding the limits of any buffer object’s size, or in exceeding the end position offset + size - 1, as set by glBindBufferRange.

  • 15
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用顶点数组指定的顶点数据存储在客户端内存中。 此数据必须从客户端内存复制到图形内存时 绘制调用,如glDrawArraysglDrawElements。 和光栅化。” 然而,如果我们不这样做会好得多 在每个绘制调用上复制顶点数据,但可以缓存 图形存储器中的数据。 这种方法可以显著地改进 同时也降低了内存带宽和渲染性能 功耗要求,这两者都是非常重要的 手持设备。 这就是顶点缓冲对象可以提供帮助的地方。 顶点 缓存对象允许OpenGL ES 3.0应用程序分配和缓存 顶点数据在高性能图形内存和渲染从这 内存,从而避免每次绘制原语时重新发送数据。 不仅是顶点数据,还有用来描述的元素索引 原语的顶点索引,并作为参数传递给 glDrawElements,可以被缓存。 OpenGL ES 3.0支持两种类型的缓冲对象 指定顶点和原始数据:数组缓冲对象和元素数组 缓冲区对象。 GL_ARRAY_BUFFER指定的数组缓冲区对象 令牌用于创建存储顶点数据的缓冲区对象。 的 GL_ELEMENT_ARRAY_BUFFER指定的元素数组缓冲区对象 顶点缓冲对象141 令牌用于创建存储原语索引的缓冲区对象。 OpenGL ES 3.0中的其他缓冲区对象类型将在本文的其他部分进行描述 关注用于指定顶点的缓冲对象 属性和元素数组。 注意:为了获得最好的性能,我们建议使用OpenGL ES 3.0 应用程序为顶点属性数据和 元素指标。 在我们可以使用缓冲区对象渲染之前,我们需要分配 缓冲对象,并上传顶点数据和元素索引到 合适的缓冲区对象
OpenGL本身并没有提供直接绘制数字的函数,但可以通过绘制纹理贴图的方式来实现绘制数字的效果。 以下是一个简单的绘制数字的函数示例: ```c++ void drawNumber(int num, GLuint textureID, int width, int height) { int digits[10]; int count = 0; if (num == 0) { digits[count++] = 0; } else { while (num != 0) { digits[count++] = num % 10; num /= 10; } } glPushMatrix(); glTranslatef((count - 1) * width * 0.5f, 0, 0); for (int i = count - 1; i >= 0; i--) { int digit = digits[i]; float u = (float)digit / 10.0f; float u2 = u + 0.1f; GLfloat vertices[] = { (GLfloat)i * width, 0.0f, 0.0f, u, 1.0f, (GLfloat)i * width + width, 0.0f, 0.0f, u2, 1.0f, (GLfloat)i * width + width, (GLfloat)height, 0.0f, u2, 0.0f, (GLfloat)i * width, (GLfloat)height, 0.0f, u, 0.0f }; glBindTexture(GL_TEXTURE_2D, textureID); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices + 3); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glTranslatef(-width, 0, 0); } glPopMatrix(); } ``` 该函数接受三个参数:要绘制的数字、数字的纹理贴图ID、数字的宽度和高度。函数内部将数字拆分成各个位的数字,并使用绑定的纹理贴图绘制每个数字。 在主循环中,可以使用该函数来绘制数字到屏幕上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值