当前位置:网站首页>OpenGL - Textures
OpenGL - Textures
2022-06-22 06:39:00 【Farmer er】
textures( texture ) It can be mapped as a picture to the triangle drawn in the previous section , However, you need to specify the corresponding for each vertex texture coordinate( Texture coordinates ):
- (0, 0) In the lower left corner of the texture
- (1, 1) In the upper right corner of the texture
- s、t、r representative x、y、z Axis
Using texture coordinates to obtain color is called sampling (Sampling), Other positions will be interpolated (Fragment Interpolation), The texture coordinates are specified below :
float texCoords[] = {
0.0f, 0.0f, // lower-left corner
1.0f, 0.0f, // lower-right corner
0.5f, 1.0f // top-center corner
};
Texture Wrapping
If the texture coordinates are out of bounds , You can set the following policies for sampling :
// GL_REPEAT: The default behavior for textures. Repeats the texture image.
// GL_MIRRORED_REPEAT: Same as GL_REPEAT but mirrors the image with each repeat.
// GL_CLAMP_TO_EDGE: Clamps the coordinates between 0 and 1. The result is that higher coordinates become clamped to the edge, resulting in a stretched edge pattern.
// GL_CLAMP_TO_BORDER: Coordinates outside the range are now given a user-specified border color.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
Different strategies take the form of :
GL_MIRRORED_REPEAT: It can be seen that the left, right, up and down of each picture are mirror symmetryGL_CLAMP_TO_BORDER: You need to specify an additional boundary color
float borderColor[] = {
1.0f, 1.0f, 0.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
Texture Filtering
When you zoom in, you will find pixels , Texture pixels (Texture Pixel), When sampling by texture coordinates , It will inevitably involve the problem of resolution , There are several pixels around a coordinate , There are two strategies to obtain pixel color :
GL_NEAREST: Get the pixel color whose center point is closest to the texture coordinates , Will produce granular patterns , Clearly see the composition of some pixels ,8-bit Commonly usedGL_LINEAR: Get the mixed color of pixels near the texture coordinates , The closer the distance, the greater the effect , Produce a smoother picture , It is not easy to see a single pixel , Will be more real
When zooming , You can set the filtering method . Linear filtering for amplification , Avoid solid colors , Zoom out with proximity filter , Ensure clarity :
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Mipmaps
Imagine an object far from the observer , With high definition materials , It is difficult to sample at this time , Because a point has a large span , And waste memory .
So , Use mipmaps Solve this problem , There will be a series of texture images , The former is the latter 2 times . According to the distance of the observer , Use the most appropriate texture image .
After creating the texture, you can call glGenerateMipmaps , Will only take effect if the texture is reduced .
Loading and creating textures
// Loading pictures
int width, height, nrChannels;
unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// 2、 Appoint mipmap The level of , If you create it manually
// 3、 Store the form of the texture , The only pictures selected are RGB Information
// 6、border, Has always been a 0
// 7、8 Specify the form and data type of picture pixel data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
Apply texture :
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// texture units
uniform sampler2D ourTexture;
void main()
{
FragColor = texture(ourTexture, TexCoord);
}
ourShader.use(); // don't forget to activate the shader before setting uniforms!
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0); // set it manually
glActiveTexture(GL_TEXTURE0); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, texture);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
If the picture is found along y Axis flipped callable stbi_set_flip_vertically_on_load(true);
边栏推荐
- Which is the best agency mode or decoration mode
- MySQL-IFNULL处理N/A
- 成功解决raise KeyError(f“None of [{key}] are in the [{axis_name}]“)KeyError: “None of [Index([‘age.in.y
- OpenGL - Draw Triangle
- SQL injection vulnerability (XI) wide byte injection
- Databricks from open source to commercialization
- SQL 注入漏洞(十三)base64注入
- Lock lock (key)
- Four functional interfaces (required)
- Tp6 links to sqlserver, PHP links to sqlserver, and Linux offline installation and deployment
猜你喜欢

You are using PIP version 19.0.3, however version 22.1.2 is available

Four functional interfaces (required)

Leetcode the shortest path of three (eight) charts per week

Seven parameters of thread pool and custom thread pool

SQL 注入漏洞(十四)xff 注入攻击

Unsafe concurrency of collection classes
![[openairinterface5g] rrcsetuprequest for RRC NR resolution](/img/de/34e71154941f977546362f10a19929.jpg)
[openairinterface5g] rrcsetuprequest for RRC NR resolution

Introduction to PMOD interface of kv260

Event preview edgex developer summit @ Nanjing station is coming!

MySQL ifnull processing n/a
随机推荐
东郊到家开发技术服务
Overview of coherent sonar Geoswath
[php] composer installation
Seven parameters of thread pool and custom thread pool
Use of stopwatch
OpenGL - Textures
In depth analysis of 20million OP events stolen by optimization (including code)
Dijin introduces digi connectcore voice control software for connectcore system module
keil C关于switch语句问题导致芯片不能正常运行
[5g NR] UE registration management status
Lock lock (key)
Blog add mailbox private message shortcut
[openairinterface5g] ITTI messaging mechanism
Tableau 连接mysql详细教程
【M32】单片机 xxx.map 文件简单解读
WPS文档目录更新产生的问题记录
Shengxin literature learning (Part1) -- precision: a approach to transfer predictors of drug response from pre-clinical ...
SQL injection vulnerability (XIV) XFF injection attack
CGIC file upload - rookie notes
iframe框架,,原生js路由