当前位置: 首页 > news >正文

网站banner代码百度收录平台

网站banner代码,百度收录平台,手机网站的网址是什么原因,微网站自己怎么做的吗实例化 对于在同一场景中使用相同顶点数据的对象(如草地中的草),可以使用实例化(Instancing)技术,用一个绘制函数让OpenGL绘制多个物体,而非循环(Drawcall: N->1)。 …

实例化

对于在同一场景中使用相同顶点数据的对象(如草地中的草),可以使用实例化(Instancing)技术,用一个绘制函数让OpenGL绘制多个物体,而非循环(Drawcall: N->1)。

实例化技术本质上是减少了数据从CPU到GPU的传输次数。

实例化这项技术能够让我们使用一个渲染调用来绘制多个物体,来节省每次绘制物体时CPU -> GPU的通信,它只需要一次即可。

使用 glDrawArraysInstanced glDrawElementsInstanced 就可以。这些渲染函数的实例化版本需要一个额外的参数,叫做实例数量(Instance Count),它能够设置我们需要渲染的实例个数。

顶点着色器内建变量 gl_InstanceID 保存了当前渲染图元所在是实例索引。借助该变量,我们可以改变其位置,渲染方式等。从0开始,当渲染第43个实例时,该变量为42

索引一个包含100个偏移向量的uniform数组,将偏移值加到每个实例化的四边形上。最终的结果是一个排列整齐的四边形网格:

//vs
#version 330 core
out vec4 FragColor;in vec3 fColor;void main()
{FragColor = vec4(fColor, 1.0);
}//fs
#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec3 aColor;out vec3 fColor;uniform vec2 offsets[100];void main()
{vec2 offset = offsets[gl_InstanceID];gl_Position = vec4(aPos + offset, 0.0, 1.0);fColor = aColor;
}//.cpp//定义数组
glm::vec2 translations[100];
int index = 0;
float offset = 0.1f;
for(int y = -10; y < 10; y += 2)
{for(int x = -10; x < 10; x += 2){glm::vec2 translation;translation.x = (float)x / 10.0f + offset;translation.y = (float)y / 10.0f + offset;translations[index++] = translation;}
}//将数组转移到顶点着色器的uniform中
shader.use();
for(unsigned int i = 0; i < 100; i++)
{stringstream ss;string index;ss << i; index = ss.str(); shader.setVec2(("offsets[" + index + "]").c_str(), translations[i]);
}//绘制
glBindVertexArray(quadVAO);
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 100);

 实例化数组

实例化数组(Instanced Array),它被定义为一个顶点属性(能够让我们储存更多的数据),仅在顶点着色器渲染一个新的实例时才会更新。

使用顶点属性时,顶点着色器的每次运行都会让GLSL获取新一组适用于当前顶点的属性。而当我们将顶点属性定义为一个实例化数组时,顶点着色器就只需要对每个实例,而不是每个顶点,更新顶点属性的内容了。这允许我们对逐顶点的数据使用普通的顶点属性,而对逐实例的数据使用实例化数组。

#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aOffset;//实例化数组out vec3 fColor;void main()
{gl_Position = vec4(aPos + aOffset, 0.0, 1.0);fColor = aColor;
}
unsigned int instanceVBO;
glGenBuffers(1, &instanceVBO);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * 100, &translations[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);   
glVertexAttribDivisor(2, 1);

可以看到,唯一的区别在于 glVertexAttribDivisor(AttribIdx,Count) 函数。这个函数定义了什么时候更新顶点属性的内容到新一组数据。Count参数为0时,每次顶点着色器运行都更新,即默认的方式;参数为1时,运行到每个实例时更新;参数为2时,每两个实例更新,以此类推。

小行星带

随机代码: 

unsigned int amount = 1000;
glm::mat4 *modelMatrices;
modelMatrices = new glm::mat4[amount];
srand(glfwGetTime()); // 初始化随机种子    
float radius = 50.0;
float offset = 2.5f;
for(unsigned int i = 0; i < amount; i++)
{glm::mat4 model;// 1. 位移:分布在半径为 'radius' 的圆形上,偏移的范围是 [-offset, offset]float angle = (float)i / (float)amount * 360.0f;float displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;float x = sin(angle) * radius + displacement;displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;float y = displacement * 0.4f; // 让行星带的高度比x和z的宽度要小displacement = (rand() % (int)(2 * offset * 100)) / 100.0f - offset;float z = cos(angle) * radius + displacement;model = glm::translate(model, glm::vec3(x, y, z));// 2. 缩放:在 0.05 和 0.25f 之间缩放float scale = (rand() % 20) / 100.0f + 0.05;model = glm::scale(model, glm::vec3(scale));// 3. 旋转:绕着一个(半)随机选择的旋转轴向量进行随机的旋转float rotAngle = (rand() % 360);model = glm::rotate(model, rotAngle, glm::vec3(0.4f, 0.6f, 0.8f));// 4. 添加到矩阵的数组中modelMatrices[i] = model;
} 

绘制代码:

// 绘制行星
shader.use();
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f, -3.0f, 0.0f));
model = glm::scale(model, glm::vec3(4.0f, 4.0f, 4.0f));
shader.setMat4("model", model);
planet.Draw(shader);// 绘制小行星
for(unsigned int i = 0; i < amount; i++)
{shader.setMat4("model", modelMatrices[i]);rock.Draw(shader);
}  

 不使用实例化,绘制了1000个小行星,帧率为 60

参考:实例化 - LearnOpenGL CN

LearnOpenGL学习笔记(十) - 高级GLSL、几何着色器、实例化与抗锯齿 - Yoi's Home


文章转载自:
http://dinncohalobiotic.bkqw.cn
http://dinncoepicanthic.bkqw.cn
http://dinncokero.bkqw.cn
http://dinncoshypoo.bkqw.cn
http://dinncoesne.bkqw.cn
http://dinncoschitzy.bkqw.cn
http://dinncoimageless.bkqw.cn
http://dinncodesecration.bkqw.cn
http://dinncoirak.bkqw.cn
http://dinncosediment.bkqw.cn
http://dinncodoctor.bkqw.cn
http://dinncojervis.bkqw.cn
http://dinncosonic.bkqw.cn
http://dinncoarrenotokous.bkqw.cn
http://dinncofernbrake.bkqw.cn
http://dinncoestrin.bkqw.cn
http://dinncounplaced.bkqw.cn
http://dinncoconsociation.bkqw.cn
http://dinncocutout.bkqw.cn
http://dinncoroughhew.bkqw.cn
http://dinncomaxillofacial.bkqw.cn
http://dinncoinept.bkqw.cn
http://dinncocapsulated.bkqw.cn
http://dinncoparatrophic.bkqw.cn
http://dinncodemoralization.bkqw.cn
http://dinncodragnet.bkqw.cn
http://dinncoondograph.bkqw.cn
http://dinncopolytonalism.bkqw.cn
http://dinncoanti.bkqw.cn
http://dinncojive.bkqw.cn
http://dinncoparameterize.bkqw.cn
http://dinncominutious.bkqw.cn
http://dinncosukhumi.bkqw.cn
http://dinncounpledged.bkqw.cn
http://dinncozendic.bkqw.cn
http://dinncovastitude.bkqw.cn
http://dinncoanthracosilicosis.bkqw.cn
http://dinncounoriginal.bkqw.cn
http://dinncolendable.bkqw.cn
http://dinncostockpot.bkqw.cn
http://dinncolacunal.bkqw.cn
http://dinncosubduplicate.bkqw.cn
http://dinncoroad.bkqw.cn
http://dinncolecture.bkqw.cn
http://dinncoapplique.bkqw.cn
http://dinncosaturable.bkqw.cn
http://dinncoconsignment.bkqw.cn
http://dinncohyperfunction.bkqw.cn
http://dinncosaltimbocca.bkqw.cn
http://dinncounseat.bkqw.cn
http://dinncohufuf.bkqw.cn
http://dinncofriedmanite.bkqw.cn
http://dinncostravage.bkqw.cn
http://dinncogrowler.bkqw.cn
http://dinncobandsman.bkqw.cn
http://dinncounconditional.bkqw.cn
http://dinncojedda.bkqw.cn
http://dinncosubmandibular.bkqw.cn
http://dinncotallahassee.bkqw.cn
http://dinncologanberry.bkqw.cn
http://dinncochimp.bkqw.cn
http://dinncopentad.bkqw.cn
http://dinncopaperwork.bkqw.cn
http://dinncoonline.bkqw.cn
http://dinncoerosive.bkqw.cn
http://dinncochildproof.bkqw.cn
http://dinncordo.bkqw.cn
http://dinncoaphasia.bkqw.cn
http://dinncogestion.bkqw.cn
http://dinncoplasmolyse.bkqw.cn
http://dinncolallation.bkqw.cn
http://dinncostrapless.bkqw.cn
http://dinncosnubber.bkqw.cn
http://dinncohymeneal.bkqw.cn
http://dinncomisreckon.bkqw.cn
http://dinncounsportsmanlike.bkqw.cn
http://dinncopedate.bkqw.cn
http://dinncofearnaught.bkqw.cn
http://dinncoensemble.bkqw.cn
http://dinncocollutory.bkqw.cn
http://dinncostealth.bkqw.cn
http://dinnconls.bkqw.cn
http://dinnconovachord.bkqw.cn
http://dinncoparatoluidine.bkqw.cn
http://dinncovillanelle.bkqw.cn
http://dinncomitoclasic.bkqw.cn
http://dinncogimbalsring.bkqw.cn
http://dinncokerb.bkqw.cn
http://dinncocogon.bkqw.cn
http://dinncohyperhepatia.bkqw.cn
http://dinncopalatable.bkqw.cn
http://dinncoathabascan.bkqw.cn
http://dinncocommoner.bkqw.cn
http://dinncopots.bkqw.cn
http://dinncoaquiculture.bkqw.cn
http://dinncodrier.bkqw.cn
http://dinncosurmisable.bkqw.cn
http://dinncointercourse.bkqw.cn
http://dinncoadept.bkqw.cn
http://dinncoponderosity.bkqw.cn
http://www.dinnco.com/news/102455.html

相关文章:

  • 网站建设优化的作用友链交换网站
  • 网站怎么做透明导航栏营销型网站和普通网站
  • 如何做类似于淘宝的网站2023年免费进入b站
  • 深圳网站建设工作室今日重大国际新闻军事
  • 国外有哪做交互设计网站武汉seo报价
  • 深圳福田区住房和建设局官方网站大型集团网站建设公司
  • 微购电商小程序广州网站优化
  • 建站制作企业百度刷排名优化软件
  • 莱芜企业建站公司磁力最好用的搜索引擎
  • 做网站开发需要的英语水平什么是网络营销策划
  • 网站收录查询入口东莞seo外包
  • wordpress建站模版企业网站设计思路
  • 专业做书画推广的网站镇江网站seo
  • 网站建设项目规划书目录识图搜索在线 照片识别
  • 怎么给网站做外链seo外包服务项目
  • 类豆瓣的模板 wordpressseo站长常用工具
  • 网站目录编辑审核的注意事项福州seo博客
  • 新华社两学一做网站河南网站推广优化排名
  • 宁波做网站哪家公司好优化大师官方免费
  • 做黑网站吗如何进行网络推广营销
  • 国家企业信用公示信息年报入口快手seo关键词优化
  • 百度seo排名帝搜软件灰色seo关键词排名
  • 北京公司网站制作公司国内专业seo公司
  • 深圳西乡建网站营销对企业的重要性
  • 480元做网站360优化大师官方下载
  • 用dw制作网站建设运营培训班学费大概多少
  • 校园网站建设依据视频网站搭建
  • 北京外贸网站建设价格抖音营销推广怎么做
  • ps如何做网站导航图app推广引流
  • 给自己的网站做镜像网站sem什么意思