Android OpenGL ES 3.0 开发 :3D实例化(Instancing 3D)

1.OpenGL ES 实例化(Instancing)
  • OpenGL ES 实例化(Instancing)是一种只调用一次渲染函数就能绘制出很多物体的技术,可以实现将数据一次性发送给 GPU ,告诉 OpenGL ES 使用一个绘制函数,将这些数据绘制成多个物体。

  • 实例化(Instancing)避免了 CPU 多次向 GPU 下达渲染命令(避免多次调用 glDrawArrays 或 glDrawElements 等绘制函数),节省了绘制多个物体时 CPU 与 GPU 之间的通信时间,提升了渲染性能。

2.使用实例化渲染需要使用的绘制接口
//普通渲染
glDrawArrays (GLenum mode, GLint first, GLsizei count);

glDrawElements (GLenum mode, GLsizei count, GLenum type, const void *indices);

//实例化渲染
glDrawArraysInstanced (GLenum mode, GLint first, GLsizei count, GLsizei instancecount);

glDrawElementsInstanced (GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei instancecount);

相对于普通绘制,实例化绘制多了一个参数 instancecount,表示需要渲染的实例数量,调用完实例化绘制函数后,我们便将绘制数据一次性发送给GPU,然后告诉它该如何使用一个函数来绘制这些实例。

实例化(Instancing)的目标并不是实现将同一物体绘制多次,而是能基于某一物体绘制出位置、大小、形状或者颜色不同的多个物体。OpenGL ES 着色器中有一个与实例化绘制相关的内建变量 gl_InstanceID

gl_InstanceID 表示当前正在绘制实例的 ID ,每个实例对应一个唯一的 ID ,通过这个 ID 可以轻易实现基于一个物体而绘制出位置、大小、形状或者颜色不同的多个物体(实例)。

3.3D实例化 实战
3.1 顶点着色器
#version 300 es
precision mediump float;
layout(location = 0) in vec4 a_position;   //顶点
layout(location = 1) in vec2 a_texCoord;   //材质顶点
layout(location = 2) in vec3 a_normal;  
layout(location = 3) in vec3 offset;      //多个实例化的偏移量

out vec3 normal;
out vec3 fragPos;               //输出到片元着色器的 位置信息
out vec2 v_texCoord;            //输出到片元的材质顶点

uniform mat4 u_MVPMatrix;       //MVP矩阵
uniform mat4 u_ModelMatrix;     //模型矩阵


void main() {
    gl_Position = u_MVPMatrix * (a_position + vec4(offset, 1.0));   //投影变换之后,输出的是gl_Position,也就是说你最终画在屏幕里面的哪个位置
    fragPos = vec3(u_ModelMatrix * (a_position + vec4(offset, 1.0))); //当前片元坐标:也就是通过旋转等变换之后的坐标
    //transpose:矩阵求转置矩阵     inverse :求矩阵的逆矩阵
    normal = mat3(transpose(inverse(u_ModelMatrix))) * a_normal;
    v_texCoord = a_texCoord;

}
  • 相对于其他的,多了一个offset ,这个表示多个实例的偏移量
  • 注意MVP矩阵是:观察矩阵x模型矩阵x投影矩阵 然后再跟顶点相乘就是顶点的位置了
  • transpose:这个函数是求转置矩阵
  • inverse:这个是求逆矩阵的
3.2片元着色器
#version 300 es
precision mediump float;

struct Light {
    vec3 position;          //光的位置
    vec3 direction;         //方向
        vec3 color;         //光的颜色
        float cutOff;           //内切光角
        float outerCutOff;      //外切光角
        float constant;     //衰减系数
        float linear;       //一次项
        float quadratic;    //二次项
};

in vec3 normal;
in vec3 fragPos;
in vec2 v_texCoord;

layout(location = 0) out vec4 outColor;
uniform sampler2D s_TextureMap;
uniform vec3 viewPos;
uniform Light light;

void main() {
    vec4 objectColor = texture(s_TextureMap, v_texCoord);   //拿到材质指定位置的颜色
    vec3 lightDir = normalize(light.position - fragPos);  //归一化

    float theta = dot(lightDir, normalize(-light.direction));  //dot 点乘计算夹角
    float epsilon = light.cutOff - light.outerCutOff;
    float intensity = clamp((theta - light.outerCutOff) / epsilon,0.0, 1.0);  //clamp将一个值截取到两个值之间

    // Ambient  环境光
    float ambientStrength = 0.4;
    vec3 ambient = ambientStrength * light.color;

    // Diffuse 散射光
    vec3 norm = normalize(normal);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * light.color;

    // Specular 镜面光
    vec3 viewDir = normalize(viewPos - fragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
    vec3 specular = spec * light.color;

    float distance    = length(light.position - fragPos);  //计算两个点的距离
    //计算衰减
    float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

    diffuse  *= attenuation;
    specular *= attenuation;

    diffuse *= intensity;
    specular*= intensity;

    vec3 finalColor = (ambient + diffuse + specular) * vec3(objectColor);
    outColor = vec4(finalColor, 1.0f);
}

根据传进来的参数,计算光照以及衰减,最后将光照跟颜色相乘得到最终的颜色

3.2 c++代码实现
void MSInstancing3DSample::PaintGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    value += 1;
    value = value % 360;
    value = value % 360;

    //转化为弧度角
    float radiansX = static_cast<float>(MATH_PI / 180.0f * value);

    glm::mat4 modelMat = glm::mat4(1.0f);  //模型矩阵
    modelMat = glm::scale(modelMat, glm::vec3(1.2f, 1.2f, 1.2f));
    modelMat = glm::rotate(modelMat, radiansX, glm::vec3(1.0f, 0.0f, 0.0f));
    modelMat = glm::rotate(modelMat, radiansX, glm::vec3(0.0f, 1.0f, 0.0f));
    modelMat = glm::translate(modelMat, glm::vec3(0.0f, 0.0f, 0.0f));

    glm::mat4 Projection =glm::perspective(glm::radians(60.0f), (float) 9 / (float) 18, 0.1f,
                                           1000.0f);

    // viewMat matrix
    glm::mat4 viewMat = glm::lookAt(
            glm::vec3(-3, 0, 3), // Camera is at (0,0,1), in World Space
            glm::vec3(0, 0, 0), // and looks at the origin
            glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
    );


    glm::mat4 mvpMatrix = Projection * viewMat * modelMat;


    m_pOpenGLShader->Bind();  //内部调用的就是使用程序
    m_pOpenGLShader->SetUniformValue("u_MVPMatrix", mvpMatrix);
    m_pOpenGLShader->SetUniformValue("u_ModelMatrix", modelMat);


    glBindVertexArray(m_VaoId);

    m_pOpenGLShader->SetUniformValue("viewPos",glm::vec3(0.0f, 0.0f, 3.0f));

    m_pOpenGLShader->SetUniformValue("s_TextureMap",0);
    // Bind the RGBA map
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_texID[3]);

    // 设置光源的位置、颜色和方向
    m_pOpenGLShader->SetUniformValue("light.position",glm::vec3(0.0f, 0.0f, 3.0f));
    m_pOpenGLShader->SetUniformValue("light.color",glm::vec3(1.0f, 1.0f, 1.0f));
    m_pOpenGLShader->SetUniformValue("light.direction",glm::vec3(0.0f, 0.0f, -1.0f));

    // 用于计算边缘的过度,cutOff 表示内切光角,outerCutOff 表示外切光角
    m_pOpenGLShader->SetUniformValue("light.cutOff",glm::cos(glm::radians(10.5f)));
    m_pOpenGLShader->SetUniformValue("light.outerCutOff",glm::cos(glm::radians(11.5f)));


    // 衰减系数,常数项 constant,一次项 linear 和二次项 quadratic。

    m_pOpenGLShader->SetUniformValue("light.constant",1.0f);
    m_pOpenGLShader->SetUniformValue("light.linear",0.09f);
    m_pOpenGLShader->SetUniformValue("light.quadratic",0.032f);


    glDrawArraysInstanced(GL_TRIANGLES, 0, 36, 125);
    glBindVertexArray(0);

}
  • 将MVP矩阵和模型矩阵传给shader
  • 将光线的数据传给shader
  • 将衰减参数传给shader
  • glDrawArraysInstanced 进行绘制
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
3D Graphics with Metal -- 共7分卷,此为分卷6 May 28, 2019 Video 3D Graphics with Metal English | MP4 | AVC 1920&times;1080 | AAC 48KHz 2ch | 3h 12m | 1.85 GB In this course you’ll get an introduction to computer graphics using Metal on the GPU. You’ll render 3D models and even write a simple game using your very own game engine. This course is for developers interested in understanding low-level computer graphics while achieving high performance in rendering 3D scenes. You’ll need to be comfortable with Xcode and Swift and be prepared to understand some math at a conceptual level. Covered concepts The Metal Pipeline 3D Models Coordinate Spaces Lighting Materials and Textures Making a game Table of Contents 1 Introduction 2 Initialize Metal 3 Set Up Metal in Swift 4 Metal on the GPU 5 Shaders 6 Challenge – Render a Quad 7 Metal Buffers 8 Indexed Drawing 9 Vertex Descriptors 10 3D Models 11 Render a Model 12 Challenge – Add Color 13 Conclusion 14 Introduction 15 Position your model 16 Coordinate Spaces 17 Projection and Depth 18 Cameras 19 Normals 20 Ambient and Diffuse Lighting 21 Specular Lighting 22 Scene Graph 23 Create a Scene 24 Conclusion 25 Introduction 26 Materials 27 UV Maps 28 Texture Coordinates 29 Textures 30 Function Specialization 31 Instancing Introduction 32 Instancing 33 Challenge – Render all the trains 34 Conclusion 35 Introduction 36 RayBreak – the Game 37 Build the Arena 38 Collisions 39 Interaction 40 Juice it up 41 Game Over 42 Challenge – Game Over 43 Conclusion
OpenGL® ES™ is the industry’s leading software interface and graphics library for rendering sophisticated 3D graphics on handheld and embedded devices. The newest version, OpenGL ES 3.0, makes it possible to create stunning visuals for new games and apps, without compromising device performance or battery life. In the OpenGL ES 3.0 Programming Guide, 2nd Edition, the authors cover the entire API and Shading Language. They carefully introduce OpenGL ES 3.0 features such as shadow mapping, instancing, multiple render targets, uniform buffer objects, texture compression, program binaries, and transform feedback. Through detailed, downloadable C-based code examples, you’ll learn how to set up and program every aspect of the graphics pipeline. Step by step, you’ll move from introductory techniques all the way to advanced per-pixel lighting and particle systems. Throughout, you’ll find cutting-edge tips for optimizing performance, maximizing efficiency with both the API and hardware, and fully leveraging OpenGL ES 3.0 in a wide spectrum of applications. All code has been built and tested on iOS 7, Android 4.3, Windows (OpenGL ES 3.0 Emulation), and Ubuntu Linux, and the authors demonstrate how to build OpenGL ES code for each platform. Coverage includes EGL API: communicating with the native windowing system, choosing configurations, and creating rendering contexts and surfaces Shaders: creating and attaching shader objects; compiling shaders; checking for compile errors; creating, linking, and querying program objects; and using source shaders and program binaries OpenGL ES Shading Language: variables, types, constructors, structures, arrays, attributes, uniform blocks, I/O variables, precision qualifiers, and invariance Geometry, vertices, and primitives: inputting geometry into the pipeline, and assembling it into primitives 2D/3D, Cubemap, Array texturing: creation, loading, and rendering; texture wrap modes, filtering, and formats; compressed textures, sampler objects, immutable textures, pixel unpack buffer objects, and mipmapping Fragment shaders: multitexturing, fog, alpha test, and user clip planes Fragment operations: scissor, stencil, and depth tests; multisampling, blending, and dithering Framebuffer objects: rendering to offscreen surfaces for advanced effects Advanced rendering: per-pixel lighting, environment mapping, particle systems, image post-processing, procedural textures, shadow mapping, terrain, and projective texturing Sync objects and fences: synchronizing within host application and GPU execution This edition of the book includes a color insert of the OpenGL ES 3.0 API and OpenGL ES Shading Language 3.0 Reference Cards created by Khronos. The reference cards contain a complete list of all of the functions in OpenGL ES 3.0 along with all of the types, operators, qualifiers, built-ins, and functions in the OpenGL ES Shading Language. Table of Contents Chapter 1. Introduction to OpenGL ES 3.0 Chapter 2. Hello Triangle: An OpenGL ES 3.0 Example Chapter 3. An Introduction to EGL Chapter 4. Shaders and Programs Chapter 5. OpenGL ES Shading Language Chapter 6. Vertex Attributes, Vertex Arrays, and Buffer Objects Chapter 7. Primitive Assembly and Rasterization Chapter 8. Vertex Shaders Chapter 9. Texturing Chapter 10. Fragment Shaders Chapter 11. Fragment Operations Chapter 12. Framebuffer Objects Chapter 13. Sync Objects and Fences Chapter 14. Advanced Programming with OpenGL ES 3.0 Chapter 15. State Queries Chapter 16. OpenGL ES Platforms Appendix A. GL_HALF_FLOAT Appendix B. Built-In Functions Appendix C. ES Framework API Book Details Title: OpenGL ES 3.0 Programming Guide, 2nd Edition Author: Aaftab Munshi, Budirijanto Purnomo, Dan Ginsburg, Dave Shreiner Length: 560 pages Edition: 2 Language: English Publisher: Addison-Wesley Professional Publication Date: 2014-03-10 ISBN-10: 0321933885 ISBN-13: 9780321933881
随着智能手机移动嵌入式平台硬件性能的不断提升,3D游戏应用也逐渐普及开来。《Android 3D游戏开发技术宝典:OpenGL ES 2.0》结合作者多年从事3D游戏应用开发的宝贵经验,全面介绍了与Android平台相关的必知必会的基础知识及大型完整3D案例,讲解上由浅入深,循序渐进,起点低、终点高,既适合初学者学习,也适合有一定基础的读者进一步提升之用。另外,由于OpenGL ES 2.0的着色语言通用于各种移动嵌入式平台,因此,《Android 3D游戏开发技术宝典:OpenGL ES 2.0》中与着色器开发相关的60%左右的内容还可供iPhone、Windows Mobile、MeeGoo等平台的开发人员参考。 全书共22章,其中第1章与第2章为Android平台相关的一些基础知识;第3章~第10章介绍了基于OpenGL ES 2.0进行3D应用开发的一些必知必会的基本知识;第11章~第15章介绍了一些高级特效的实现方法;第16章~第17章介绍了3D游戏开发中相关的一些物理、碰撞检测知识以及常用的3D物理引擎JBullet;第19章介绍了3种人机交互的高级技术;第20章~第22章给出了3个完整的大型3D游戏案例,总代码量接近6万行。同时为了便于读者的学习,《Android 3D游戏开发技术宝典:OpenGL ES 2.0》附赠的光盘中包含了书中所有案例的完整源代码,同时给出了最后3个完整大型3D游戏案例的讲解视频,最大限度地帮助读者快速掌握相应的开发技术。 《Android 3D游戏开发技术宝典:OpenGL ES 2.0》适合Android程序员、游戏开发者及Android爱好者学习,也可以作为相关培训学校和大专院校相关专业的教学用书。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值