Android Lesson Five: An Introduction to Blending

使用QGIS提取天地图遥感影像中的建筑物轮廓,及AI方案 用QGIS的插件提取遥感影像中的建筑轮廓,并提供了几个AI提取建筑轮廓的方案。 阅读详情

原文链接地址:http://www.learnopengles.com/android-lesson-five-an-introduction-to-blending/

Android Lesson Five: An Introduction to Blending

In this lesson we’ll take a look at the basics of blending in OpenGL. We’ll look at how to turn blending on and off, how to set different blending modes, and how different blending modes mimic real-life effects. In a later lesson, we’ll also look at how to use the alpha channel, how to use the depth buffer to render both translucent and opaque objects in the same scene, as well as when we need to sort objects by depth, and why.

We’ll also look at how to listen to touch events, and then change our rendering state based on that.Basic blending (additive blending of RGB cubes).

Assumptions and prerequisites

Each lesson in this series builds on the one before it. However, for this lesson it will be enough if you understand Android Lesson One: Getting Started. Although the code is based on the preceding lesson, the lighting and texturing portion has been removed for this lesson so we can focus on the blending.

Blending

Blending is the act of combining one color with a second in order to get a third color. We see blending all of the time in the real world: when light passes through glass, when it bounces off of a surface, and when a light source itself is superimposed on the background, such as the flare we see around a lit streetlight at night.

OpenGL has different blending modes we can use to reproduce this effect. In OpenGL, blending occurs in a late stage of the rendering process: it happens once the fragment shader has calculated the final output color of a fragment and it’s about to be written to the frame buffer. Normally that fragment just overwrites whatever was there before, but if blending is turned on, then that fragment is blended with what was there before.

By default, here’s what the OpenGL blending equation looks like when glBlendEquation() is set to the default, GL_FUNC_ADD:

output = (source factor * source fragment) + (destination factor * destination fragment)

There are also two other modes available in OpenGL ES 2, GL_FUNC_SUBTRACT and GL_FUNC_REVERSE_SUBTRACT. These may be covered in a future tutorial, however, I get an UnsupportedOperationException on the Nexus S when I try to call this function so it’s possible that this is not actually supported on the Android implementation. This isn’t the end of the world since there is plenty you can do already with GL_FUNC_ADD.

The source factor and destination factor are set using the function glBlendFunc(). An overview of a few common blend factors will be given below; more information  as well as an enumeration of the different possible factors is available at the Khronos online manual:

The documentation appears better in Firefox or if you have a MathML extension installed.

Clamping

OpenGL expects the input to be clamped in the range [0, 1] , and the output will also be clamped to the range [0, 1]. What this means in practice is that colors can shift in hue when you are doing blending. If you keep adding red (RGB = 1, 0, 0) to the frame buffer, the final color will stay red. However, if you add in just a little bit of green so that you are adding (RGB = 1, 0.1, 0) to the frame buffer, you will end up with yellow even though you started with a reddish hue! You can see this effect in the demo for this lesson when blending is turned on: the colors become oversaturated where different colors overlap.

Different types of blending and how they relate to different effects

 

Additive Color. Source: http://commons.wikimedia.org/wiki/File:AdditiveColor.svg

The RGB additive color model. Source: Wikipedia

 

Additive blending

Additive blending is the type of blending we do when we add different colors together and add the result. This is the way that our vision works together with light and this is how we can perceive millions of different colors on our monitors — they are really just blending three different primary colors together.

This type of blending has many uses in 3D rendering, such as in particle effects which appear to give off light or overlays such as the corona around a light, or a glow effect around a light saber.

Additive blending can be specified by calling glBlendFunc(GL_ONE, GL_ONE). This results in the blending equation output = (1 * source fragment) + (1 * destination fragment), which collapses into output = source fragment + destination fragment.

 

A lightmap applied to the first texture from http://pdtextures.blogspot.com/2008/03/first-set.html

An example of lightmapping.

 

Multiplicative blending

Multiplicative blending (also known as modulation) is another useful blending mode that represents the way that light behaves when it passes through a color filter, or bounces off of a lit object and enters our eyes. A red object appears red to us because when white light strikes the object, blue and green light is absorbed. Only the red light is reflected back toward our eyes. In the example to the left, we can see a surface that reflects some red and some green, but very little blue.

When multi-texturing is not available, multiplicative blending is used to implement lightmaps in games. The texture is multiplied by the lightmap in order to fill in the lit and shadowed areas.

Multiplicative blending can be specified by calling glBlendFunc(GL_DST_COLOR, GL_ZERO). This results in the blending equation output = (destination fragment * source fragment) + (0 * destination fragment), which collapses into output = source fragment * destination fragment.

 

An example of two textures blended together. Textures from http://pdtextures.blogspot.com/2008/03/first-set.html

An example of two textures interpolated together.

 

Interpolative blending

Interpolative blending combines multiplication and addition to give an interpolative effect. Unlike addition and modulation by themselves, this blending mode can also be draw-order dependent, so in some cases the results will only be correct if you draw the furthest translucent objects first, and then the closer ones afterwards. Even sorting wouldn’t be perfect, since it’s possible for triangles to overlap and intersect, but the resulting artifacts may be acceptable.

Interpolation is often useful to blend adjacent surfaces together, as well as do effects like tinted glass, or fade-in/fade-out. The image on the left shows two textures (textures from public domain textures) blended together using interpolation.

Interpolation is specified by calling glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). This results in the blending equation output = (source alpha * source fragment) + ((1 – source alpha) * destination fragment). Here’s an example:

Imagine that we’re drawing a green (0r, 1g, 0b) object that is only 25% opaque. The object currently on the screen is red (1r, 0g, 0b) .

output = (source factor * source fragment) + (destination factor * destination fragment)
output = (source alpha * source fragment) + ((1 – source alpha) * destination fragment)
output = (0.25 * (0r, 1g, 0b)) + (0.75 * (1r, 0g, 0b))
output = (0r, 0.25g, 0b) + (0.75r, 0g, 0b)
output = (0.75r, 0.25g, 0b)

Notice that we don’t make any reference to the destination alpha, so the frame buffer itself doesn’t need an alpha channel, which gives us more bits for the color channels.

Using blending

For our lesson, our demo will show the cubes as if they were emitters of light, using additive blending. Something that emits light doesn’t need to be lit by other light sources, so there are no lights in this demo. I’ve also removed the texture, although it could have been neat to use one. The shader program for this lesson will be simple; we just need a shader that will pass out the color given to it.

Vertex shader
uniform mat4 u_MVPMatrix;       // A constant representing the combined model/view/projection matrix. 
  
attribute vec4 a_Position;      // Per-vertex position information we will pass in. 
attribute vec4 a_Color;         // Per-vertex color information we will pass in. 
  
varying vec4 v_Color;           // This will be passed into the fragment shader. 
  
// The entry point for our vertex shader. 
void main() 
{ 
    // Pass through the color. 
    v_Color = a_Color; 
  
    // gl_Position is a special variable used to store the final position. 
    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. 
    gl_Position = u_MVPMatrix * a_Position; 
} 
Fragment shader
precision mediump float;        // Set the default precision to medium. We don't need as high of a 
                                // precision in the fragment shader. 
varying vec4 v_Color;           // This is the color from the vertex shader interpolated across the 
                                // triangle per fragment. 
  
// The entry point for our fragment shader. 
void main() 
{ 
    // Pass through the color 
    gl_FragColor = v_Color; 
} 
Turning blending on

Turning blending on is as simple as making these function calls:

// No culling of back faces 
GLES20.glDisable(GLES20.GL_CULL_FACE); 
  
// No depth testing 
GLES20.glDisable(GLES20.GL_DEPTH_TEST); 
  
// Enable blending 
GLES20.glEnable(GLES20.GL_BLEND); 
GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE); 

We turn off the culling of back faces, because if a cube is translucent, then we can now see the back sides of the cube. We should draw them otherwise it might look quite strange. We turn off depth testing for the same reason.

Listening to touch events, and acting on them

You’ll notice when you run the demo that it’s possible to turn blending on and off by tapping on the screen. See the article “Listening to Android Touch Events, and Acting on Them” for more information.

Further exercises

The demo only uses additive blending at the moment. Try changing it to interpolative blending and re-adding the lights and textures. Does the draw order matter if you’re only drawing two translucent textures on a black background? When would it matter?

【UE5】Vosk插件实战:离线语音识别与TTS朗读全流程解析 本文详细解析了如何在UE5中利用Vosk插件实现离线实时语音转文字功能。从环境配置、插件与模型准备,到核心C++组件封装与蓝图系统搭建,提供了完整的实战教程。重点解决了离线语音识别在游戏开发中的延迟、隐私和网络依赖问题,并演示了从语音识别到TTS朗读的完整交互闭环,为开发安全、实时的语音交互应用提供可靠方案。 阅读详情

相关推荐

LiveKit 本地部署实战:从零搭建安全实时通信环境(Caddy+HTTPS)

本文详细介绍了如何从零开始搭建LiveKit本地部署环境,结合Caddy服务器实现HTTPS安全通信。通过环境准备、证书生成、服务配置到前端客户端集成的完整流程,帮助开发者快速构建实时音视频应用,特别适合需要安全实时通信的场景。

weixin_29227585的博客 681

Blend混合模式 与 20余种颜色混合模式代码实现

https://www.bilibili.com/read/cv1874791 【UnityShader】Blend混合模式 与 20余种颜色混合模式代码实现(多图预警) 混合模式 可简单理解为 指上下层图片相互有重叠时如何取色的一种称呼 以下是百科给的解释 但我们今天要说的是Unity中的颜色混合 混合模式是图像处理技术中的一个技术名词,不仅用于广...

N的专栏 1362

Tomcat漏洞——弱口令爆破

靶机:CentOS7 IP:192.168.100.40。攻击机:kali IP:192.168.100.60。版 权 声 明:本博客所有文章除特别声明外,均采用。文章版权归作者所有,未经允许请勿转载!作 者:PeiyuJue。

weixin_57379923的博客 910

Android Visualizer 实战:把声音画出来

是系统提供的音频效果(AudioEffect)之一。它挂在音频链路上,,把数据回吐给你——画波形、画频谱、做响度表。

Android的专栏 299

FAGLB03余额显示异常排查与修复指南

本文针对SAP新总账中FAGLB03报表余额显示异常问题,提供了专业的排查与修复指南。文章深入剖析了数据不一致的核心原因,如FAGLFLEXT汇总表与凭证明细不同步、年度余额结转(FAGLGVTR)失败等,并详细介绍了使用FAGLF03进行精准诊断、通过SE38执行标准调整程序TFC_ADJUST_VZ进行修复的标准操作流程,帮助用户快速恢复数据一致性,确保财务报表的准确性。

weixin_29313423的博客 307

【原创教程】汇川H5U入门教案第二课

定时器指令IN输入能流从OFF变为ON时,定时器启动计时,输出Q为ON,IN输入能流从ON变为OFF时,在IN保持为OFF期间,定时器的运行时间是PT参数指定的时间,定时器定时时间达到PT参数指定的时间后,Q变为OFF。IN输入能流为ON时,ET输出值为0,IN从ON变为OFF时,在定时器计时运行期间,ET输出当前的计时时间,定时器计时时间到达PT参数指定的时间后,ET值保持。IN输入能流为ON时,在定时器计时运行期间,ET输出当前的计时时间,定时器计时时间到达PT参数指定的时间后,ET值保持;

Qinmingyue13的博客 2481

Android 开发入门实践(十三):用 Intent、UiState 和 Reducer 理解 MVI 的状态更新

本文记录第141—145课对MVI架构的入门实践,通过“返回列表”功能逐步构建意图(NewsIntent)、状态(NewsUiState)与状态转换(reduce)机制。基于单向数据流,用户操作生成意图,ViewModel通过Reducer根据旧状态计算新状态,并通过StateFlow更新Compose界面。代码实现清晰分离关注点,强化了对状态管理、事件驱动与数据流的理解。

Easyhood的博客 333

YOLOv8全网首发:CVPR2026 MixerCSeg | DEGConv方向引导边缘门控,破解细长裂缝检测难题

DEGConv通过方向先验嵌入、边缘增强卷积和动态门控三大机制,针对性解决了YOLO在处理裂缝类细长、多方向、低对比度目标时的特征提取不足问题,显著提升了裂缝检测的精度与鲁棒性。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 518

RK-RK3572-Android16- 编译打包优化-OTA整包-差分包打包

这里主要记录一下 RK3572 在Android16 上面进行OTA升级的整包和差分包制作经验的一个分享,整理知识点,方便后续自己和其它同事开发提高工作效率、直接使用。不管哪一个平台,MTK 也好、RK也好 基本上都是不同Android版本各个子系统之间还是有一点点区别的,所以有必要记录整理知识点,方便提高开发效率、生产效率。针对OTA升级问题 ,再一次说明还是要看官方提供的文档,按照文档来。

十年饮冰,难凉热血 164

麒麟服务器操作系统QT系列软件工具手册

对Qt提供的开发工具进行了一系列的优化之后,在日常的修复bug和维护框架的过程中,桌面研发对Qt的基础软件库也进行了部分优化工作,包括qtbase、qtwayland、qtscript、qt4-x11等Qt基础组件,涉及的Qt框架版本主要涉及到5.9和5.12两个版本。qt 的翻译,是由 ts 文件生成 qm 文件然后进行翻译,国产操作系统上qt 的翻译是由单独的qttranslations包中进行维护,其次部分汉化问题也是qt组件维护工作的一部分,qt的翻译机制如图4-7所示。

太极淘的博客 543

行测测评——矩阵、圆形、环形三角图形数列推理解题技巧

写在前面:各大公司的测评做的让人眼花缭乱,很多题型都是第一次见,比如逻辑关系、信息推理,最难的要属找规律的数字推理了。根据B站UP主刘文超老师的课程,记录一下解题技巧。感谢UP主的无私奉献,原地址连接:https://www.bilibili.com/video/BV1it411M7o2?t=528&p=7 解题技巧: 从图形的上下、左右、交叉 角度进行尝试求解 基本法则是“加、减、乘、除、倍数、次方”六种形态。 一、有心数阵 解题技巧:周边数字通过某种运算得到中间数字 1.周边数..

涵盖了计算机专业基础知识、数学建模相关实践、复杂网络论文研究、LeetCode算法刷题经验、C语言开发经验、前端Vue、React框架开发实战相关知识 8643

Android Message源码阅读

本文梳理了Android Handler机制中Message的核心设计:链表结构、消息复用池(sPool)及回收机制,重点解析了异步消息与同步屏障(SyncBarrier)的协同机制。当主线程需优先处理UI绘制等高优先级任务时,系统通过postSyncBarrier插入屏障,使Looper跳过普通同步消息,优先执行标记为异步的UI渲染任务,确保帧率流畅。该机制是保障Android界面响应性的关键技术之一。

u012116089的博客 202

Python实战日志_P4_图书馆Web API服务

目标:使用Flask将图书馆系统升级为RESTful API服务,掌握Web开发基础难度:⭐⭐⭐⭐☆预计学习时间:75分钟前置要求:已完成第3篇《个人图书馆管理系统》

qq_39436086的博客 83

Android Laya 与 Native Bridge:页面识别、双向通信、协议设计和崩溃定位

Laya 与 Native 通信的难点不在于某个或的写法,而在于边界:页面来源是否可信、协议是否明确、线程是否正确、请求是否幂等、页面是否仍然存在、日志是否能关联一次完整请求。建立这些边界后,才能把“页面白屏”“按钮无响应”“重复消息”和“Native 崩溃”拆成可验证的层次,并沿着真实调用链找到根因。

路在脚下,幸福在手中,莫贪!!!!! 162

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

安卓部署Linux性能实测:一颗移动处理器,能否胜任日常 Linux 开发?

本文通过在一加Ace5上部署NimoteCode的ARM64 Linux环境,测试了其在CoreMark、SQLite编译及并行构建中的表现。结果显示,尽管单核性能约2.34GHz,多核扩展比达6.15×,但优化编译耗时仍高达35秒,反映移动处理器虽具备一定开发能力,却受限于文件系统开销与运行环境(如PRoot)。实际开发中,非优化构建效率较高,而并行编译可显著提速。结论:安卓手机可作为轻量级开发辅助工具,但无法替代桌面环境,真正价值在于便携性与即时响应,而非全功能替代。

一个不知名码农的专栏 325

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

BUUCTF-菜刀流量-MISC-学习笔记

从 1970-01-01 00:00:00 UTC 起算的秒数,——Wireshark 直接告诉你它回答的是谁;由表可知道,96帧返回了flag.rar。, 代表删除键,先删了4次,又删了1次。所以密码是:SuperMaster。解析128帧的响应,即133帧。所以这是一个标准抓包文件,用。解压压缩包拿到html代码。:选中 128 帧,展开。:128 和 131 的。能把一问一答放在一起看;是八进制(Oct)的数字。(或 TCP 层)看到。对比一下ASCii 码表。

晓梦林的博客 68

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Android 开发入门实践(九):把新闻数据与页面状态交给 ViewModel

本文记录第110—117课学习成果:将页面标题、新闻列表及选中编号逐步迁移至NewsViewModel,通过ViewModelProvider获取实例并读取数据。页面调用ViewModel方法处理选择与返回,实现状态管理解耦。过程中纠正了对Activity重建机制的误解,并厘清mutableStateOf的可观察性与ViewModel对象生命周期的区别,深化了对Compose状态更新机制的理解。

Easyhood的博客 222

Android 17 全栈性能分析关键字与源码手册

FrameTimeline 找异常帧,ATrace 找阶段,thread_state 找等待类型,ftrace 找内核原因,Simpleperf 找 CPU 热点,驱动源码找最终触发条件;任何根因结论都必须回到同一对象、同一时间窗口和同一条依赖链。

weixin_43228946的博客 375

谷歌相机9.2下载(GCam 安卓通用版 安装与使用说明)

谷歌相机9.2版移植自Pixel原生相机,支持Camera2 API的Android 10+设备安装,凭借多帧合成技术在夜景、HDR+与人像模式表现优异。可提升暗光画质、优化动态范围与背景虚化效果,支持视频防抖及配置文件自定义。安装需确认机型兼容性,建议备份原相机,避免冲突。资源可通过kuake链接获取,仅限学习测试使用。

2611_96675819的博客 179

Android 系统启动与调试中的日志抓取方式全解析

│ 用户操作/系统事件 ││ │▼ ▼│ (驱动/硬件/内存管理) │ │ (App/SystemServer) ││ │ │ ││ │▼ ▼│ dmesg 命令 │ │ logd daemon ││ cat /proc/kmsg │ │ (分类存储到分区) ││ │▼ ▼│ 串口 UART │ │ logcat 命令 ││ (硬件直连,最早输出) │ │ adb logcat ││ │▼│ (汇总所有日志+系统状态) │工具最佳用途关键优势主要局限dmesg。

zhaoshuzhaoshu的专栏 270

go消息推送之极光推送 (JPush REST API + SDK,覆盖安卓与iOS)

使用 gorush有个绕不开的问题:国内安卓设备上 FCM 基本不可达,华为、小米、OPPO、vivo 各自为政——此时可用极光推送(JPush)作解决方案。它的思路和前面几家都不一样:App 集成极光 SDK 后拿到统一的 RegistrationID,服务端发一条 REST 请求,极光云端自动把消息路由到 APNs、各家厂商通道或极光自有长连接。这里给出了:一个用纯标准库直接调用极光的;另一个用社区维护的,类型化参数,接口注释与官方文档逐条对应。两个功能相同,可放在一起对比学习。

qq_40467670的博客 196

Android 开发入门实践(十):用 Kotlin 协程完成模拟加载、取消与异常处理

本文记录第118—125课对NewsApp的协程改造:从viewModelScope.launch启动协程,逐步加入delay模拟等待、suspend函数封装、withContext(Dispatchers.IO)切换线程并观察上下文恢复机制。通过日志验证withContext完成后恢复原执行上下文(非自动回主线程),纠正了初始理解偏差。引入Job管理任务,实现加载取消与重复点击防护,结合异常处理与状态管理,完成可观察、可中断的异步流程。

Easyhood的博客 402

工业智能体:从Codex/Claude到项目实战~系列文章11:Codex 批量重构老系统:激活历史 DCS/PLC 存量代码

本期提出规模化重构工业老系统(如DCS/PLC代码)的可执行方法论:通过AST静态分析量化风险,按“盘点评估→样板重构→批量迁移→回归灰度”四阶段推进;利用Codex实现模块级批量重构,并以“金标准样本”确保数值一致性;接入CI流水线实现自动化守护。强调“小步改、保语义、可回滚”,结合绞杀者模式渐进替代,实现存量代码的安全激活。来自华北理工大学段一凡的博客文章

段一凡的博客 386

android 动画掉帧

动画

yeshen.org 244

Android 开发入门实践(十六):网络新闻请求、Coil 图片加载与失败重试

本文通过接入JSONPlaceholder网络接口,实现新闻数据的远程获取与解析。在Repository中使用withContext(Dispatchers.IO)进行协程挂起请求,通过JSONObject解析title、body、id字段并构建News对象。结合ViewModel与StateFlow更新页面状态,利用AsyncImage加载图片,并添加失败提示与手动重试机制。完整覆盖从网络请求、数据转换到界面更新的全流程,深化对协程调度、状态管理与图片加载的理解。

Easyhood的博客 55

mysql初阶--表的操作

本文系统讲解MySQL表的DDL操作:通过SHOW TABLES查看表,CREATE TABLE创建表(支持临时表、校验语句),DESCRIBE或SHOW CREATE TABLE查看结构,ALTER TABLE修改列名、类型、增删字段及重命名表,以及DROP TABLE删除表。强调这些操作属于数据定义语言(DDL),影响表结构而非数据本身。结合存储引擎、字符集(推荐utf8mb4)、建表规范与常见报错,为后续数据操作打下坚实基础。

2601_97033068的博客 525
上一篇: Android Lesson Four: Introducing Basic Texturing
下一篇: Android Lesson Six: An Introduction to Texture Filtering
incanus
博客等级 码龄15年 15粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值