天天看点

opengl-第7章:多张Texture的使用

目录

    • 1. 版本1.7 QKEnging介绍
      • 1. 增加了静态初始化函数
      • 2. 增加了动态元素
      • 3. 增加了Element初始化附加操作的lambda表达
      • 测试代码
    • 2. 实现多张Texture的使用
      • 1. 将texture绑定gl状态机不同的TextureCode
      • 2. 在调用shader之后,设置shader虚拟机里面的纹理符号 为对应纹理槽
        • 产生纹理
        • shader纹理符号
        • 设置操作
      • 一个简单的证明

1. 版本1.7 QKEnging介绍

1. 增加了静态初始化函数

现在Shader,Texture 元素将不会被循环call use,而是在窗口循环call之前调用一次
           

2. 增加了动态元素

现在,SimpleData将会在窗口循环事件中循环call use()
           

3. 增加了Element初始化附加操作的lambda表达

opengl-第7章:多张Texture的使用

所有的附加lambda表达都将会在use()之后执行

测试代码

#include <BaseWindow.h>
#include <Shader.h>
#include <SimpleData.h>
#include <TextureData.h>
#include <Texture.h>
int main(int argc, char *argv[])
{
	BaseWindow window;

	Shader shader("vertex","fragment");
	TextureData data1("test");

	Texture texture1("pp.jpg",0);
	Texture texture2("p2.jpg",1);
	Texture texture3("p3.png", 2);

	shader.addOp([=](shared_ptr<Element> sb) {

		shared_ptr<Shader> sd = static_pointer_cast<Shader>(sb);
		sd->setInt("ourTexture1", 0);
		sd->setInt("ourTexture2", 2);
	});

	window.addStaticElement(make_shared<Texture>(texture1));
	window.addStaticElement(make_shared<Texture>(texture2));
	window.addStaticElement(make_shared<Texture>(texture3));

	window.addStaticElement(make_shared<Shader>(shader));
	window.addDynamicElement(make_shared<TextureData>(data1));

	window.run();
	return 0;
}


           

2. 实现多张Texture的使用

1. 将texture绑定gl状态机不同的TextureCode

opengl-第7章:多张Texture的使用

在gl状态机里面提供的纹理槽有很多

opengl-第7章:多张Texture的使用

第一步你需要先激活某个纹理槽,然后再在这个纹理槽里面绑定数据

2. 在调用shader之后,设置shader虚拟机里面的纹理符号 为对应纹理槽

产生纹理

opengl-第7章:多张Texture的使用

shader纹理符号

opengl-第7章:多张Texture的使用

设置操作

opengl-第7章:多张Texture的使用
opengl-第7章:多张Texture的使用

值得注意的是,你必须得有一个活着的shader虚拟机,你才能向里面设置这些状态,也就是说你必须use()

opengl-第7章:多张Texture的使用

一个简单的证明

我们直接使用mix函数来进行测试
           
opengl-第7章:多张Texture的使用
opengl-第7章:多张Texture的使用

Nice!

继续阅读