当前位置:网站首页>[OpenGL learning notes 8] texture
[OpenGL learning notes 8] texture
2022-07-05 01:27:00 【Briant_ ccj】
Catalog
3. Attach a texture image to the texture object
4. Define the sampler in the clip shader and use
5. Relate texture objects to shaders
6. Pass texture objects to texture units , And call glDrawElements mapping
Texture is a 2D picture ( There is even 1D and 3D The texture of ), Generally speaking, painting texture is using OpenGL Drawing pictures .
In addition to images , Textures can also be used to store large amounts of data , This data can be sent to the shader .
To paint a texture
1. Create texture objects
// Generated OpenGL object , Texture is also used ID Refer to the
unsigned int texture;
glGenTextures(1, &texture);
//glGenTextures The function first needs to input the number of generated textures , Then store them in the... Of the second parameter unsigned int Array
2. Bind texture object
After binding, any texture instruction can configure the currently bound texture :
glBindTexture(GL_TEXTURE_2D, texture);
3. Attach a texture image to the texture object
// adopt glTexImage2D Generate a texture on the current texture object
// When calling glTexImage2D when , The currently bound texture object will be attached with a texture image
QImage image1 = QGLWidget::convertToGLFormat(QImage(":/image/image/container.jpg"));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image1.width(), image1.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image1.bits());
glGenerateMipmap(GL_TEXTURE_2D);
When calling glTexImage2D when , The currently bound texture object will be attached with a texture image .
4. Define the sampler in the clip shader and use
GLSL There is a built-in data type for texture objects , It's called a sampler (Sampler), It uses texture type as suffix , such as sampler1D
、sampler3D
, Or in our case sampler2D
. We can simply declare a uniform sampler2D
Add a texture to the clip shader , We'll assign the texture to this later uniform.
#version 330
out vec4 fragColor;
in vec2 TexCord;
uniform sampler2D texture1;
void main()
{
fragColor = texture(texture1, TexCord);
# Use GLSL The built-in texture Function to sample the color of the texture , Its first parameter is the texture sampler ,
The second parameter is the texture coordinates .texture The function samples the corresponding color values using the texture parameters previously set .
The output of this fragment shader is texture ( interpolation ) In texture coordinates ( Filtered ) Color .
}
5. Relate texture objects to shaders
Texture data of texture objects cannot be directly passed to shaders , Texture objects can be passed to texture units .
The sampler can pass glUniform1i To specify which texture unit the sampler corresponds to .
sharedprogram.bind();
// Set the texture unit corresponding to the sampler
sharedprogram.setUniformValue("texture1",0);// Designated sampler texture1 For texture units 0
//sharedprogram.setUniformValue("texture2",1);// Designated sampler texture2 For texture units 1
sharedprogram.release();
6. Pass texture objects to texture units , And call glDrawElements mapping
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);// Transfer texture objects to texture cells
//glActiveTexture(GL_TEXTURE1);
//glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
/********************************************************************/
#version 330
out vec4 fragColor;
in vec2 TexCord;
uniform sampler2D texture1;
uniform sampler2D texture2;
void main()
{
fragColor = mix(texture(texture1, TexCord), texture(texture2, TexCord), 0.2) ;
}
/*********************************************************************/
// Create a texture object
glGenTextures(1, &texture_container);
// Bind texture object , After any texture instruction can configure the currently bound texture
glBindTexture(GL_TEXTURE_2D, texture_container);
// adopt glTexImage2D Generate a texture on the current texture object
// When calling glTexImage2D when , The currently bound texture object will be attached with a texture image
QImage image1 = QGLWidget::convertToGLFormat(QImage(":/image/image/container.jpg"));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image1.width(), image1.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image1.bits());
glGenerateMipmap(GL_TEXTURE_2D);
glGenTextures(1, &texture_awesomeface);
glBindTexture(GL_TEXTURE_2D, texture_awesomeface);
QImage image = QGLWidget::convertToGLFormat(QImage(":/image/image/awesomeface.png"));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
glGenerateMipmap(GL_TEXTURE_2D);
/*********************************************************************/
sharedprogram.bind();
// Set the texture unit corresponding to the sampler
sharedprogram.setUniformValue("texture1",0);// Designated sampler texture1 For texture units 0
sharedprogram.setUniformValue("texture2",1);// Designated sampler texture2 For texture units 1
sharedprogram.release();
/*********************************************************************/
sharedprogram.bind();
glActiveTexture(GL_TEXTURE0);// Activate the texture unit before binding the texture
glBindTexture(GL_TEXTURE_2D, texture_container);// Pass texture to texture unit
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture_awesomeface);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
sharedprogram.release();
Texture coordinates
Texture coordinates in x and y On the shaft , The scope is 0 To 1 Between ( Note that we are using 2D texture image ). Texture coordinates start at (0, 0), The lower left corner of the texture image , Always at (1, 1), The upper right corner of the texture image .
The texture coordinates in the above figure are as follows
float texCoords[] = {
0.0f, 0.0f, // The lower left corner
1.0f, 0.0f, // The lower right corner
0.5f, 1.0f // upper-middle
};
Texture units
The position value of a texture is usually called a texture unit (Texture Unit). The default texture unit for a texture is 0, It is the default active texture unit , So we didn't assign a location value in the previous part of the tutorial .
The main purpose of texture unit is to let us use more than one texture in shader . By assigning texture units to the sampler , We can bind multiple textures at once , As long as we first activate the corresponding texture unit . It's like glBindTexture equally , We can use glActiveTexture Activate texture unit , Pass in the texture unit we need to use :
glActiveTexture(GL_TEXTURE0); // Activate the texture unit before binding the texture
glBindTexture(GL_TEXTURE_2D, texture);
After activating the texture unit , Next glBindTexture The function call binds the texture to the currently active texture unit , Texture units GL_TEXTURE0 Default is always activated , So in the previous example, when we use glBindTexture
When , No texture units need to be activated .
OpenGL At least guarantee 16 Texture units for you to use , That is to say you can activate from GL_TEXTURE0 To GL_TEXTRUE15. They are all defined in order , So we can also pass GL_TEXTURE0 + 8 The way to get GL_TEXTURE8, This is useful when we need to loop some texture elements .
边栏推荐
- Digital DP template
- Global and Chinese market of nutrient analyzer 2022-2028: Research Report on technology, participants, trends, market size and share
- C语音常用的位运算技巧
- 【CTF】AWDP总结(Web)
- Call Huawei order service to verify the purchase token interface and return connection reset
- Grabbing and sorting out external articles -- status bar [4]
- 107. Some details of SAP ui5 overflow toolbar container control and resize event processing
- Query for Boolean field as "not true" (e.g. either false or non-existent)
- Are you still writing the TS type code
- [FPGA tutorial case 10] design and implementation of complex multiplier based on Verilog
猜你喜欢
流批一體在京東的探索與實踐
How to use words to describe breaking change in Spartacus UI of SAP e-commerce cloud
Daily question brushing record (13)
[wave modeling 2] three dimensional wave modeling and wave generator modeling matlab simulation
微信小程序:全新独立后台月老办事处一元交友盲盒
Single step debugging of master data reading of SAP commerce cloud products
【海浪建模1】海浪建模的理论分析和matlab仿真
Pandora IOT development board learning (RT thread) - Experiment 4 buzzer + motor experiment [key external interrupt] (learning notes)
Basic operations of database and table ----- delete index
Four pits in reentrantlock!
随机推荐
每日刷题记录 (十三)
如果消费互联网比喻成「湖泊」的话,产业互联网则是广阔的「海洋」
[wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
[development of large e-commerce projects] performance pressure test - Performance Monitoring - heap memory and garbage collection -39
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
Pycharm professional download and installation tutorial
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
【FPGA教程案例10】基于Verilog的复数乘法器设计与实现
Complex, complicated and numerous: illustration of seven types of code coupling
Global and Chinese markets of emergency rescue vessels (errv) 2022-2028: Research Report on technology, participants, trends, market size and share
Nebula Importer 数据导入实践
【大型电商项目开发】性能压测-优化-中间件对性能的影响-40
Yyds dry goods inventory [Gan Di's one week summary: the most complete and detailed in the whole network]; detailed explanation of MySQL index data structure and index optimization; remember collectio
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Global and Chinese market of optical densitometers 2022-2028: Research Report on technology, participants, trends, market size and share
Wechat applet: exclusive applet version of the whole network, independent wechat community contacts
The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
Poap: the adoption entrance of NFT?
Detailed explanation of multi-mode input event distribution mechanism
[untitled]