openGL之API学习(二零四)GL_TEXTURE_MIN_FILTER GL_TEXTURE_MAG_FILTER

本文介绍了OpenGL中纹理过滤的两种模式——GL_TEXTURE_MIN_FILTER和GL_TEXTURE_MAG_FILTER的使用,包括其取值和默认设置。GL_TEXTURE_MIN_FILTER用于控制纹理缩小时的过滤方式,而GL_TEXTURE_MAG_FILTER则决定了纹理放大的效果。GL_NEAREST产生更锐利的边缘但速度较快,GL_LINEAR则提供更平滑的过渡但计算量稍大。示例代码展示了如何设置这两种过滤方式为GL_LINEAR。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置纹理过滤方式。

1、GL_TEXTURE_MIN_FILTER

GL_TEXTURE_MIN_FILTER取值GL_NEAREST GL_LINEAR GL_NEAREST_MIPMAP_NEAREST GL_LINEAR_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR,默认GL_NEAREST_MIPMAP_LINEA

2、GL_TEXTURE_MAG_FILTER

GL_TEXTURE_MAG_FILTER取值GL_NEAREST GL_LINEAR,默认GL_LINEAR。

当从纹理采样时使用的“细节级别”功能确定纹理应该被成像时,就会使用“纹理放大”功能。它将纹理放大功能设置为GL_NEAREST或GL_LINEAR。GL_NEAREST通常比GL_LINEAR更快,但它可以生成边缘更锐利的纹理图像,因为纹理元素之间的过渡没有那么平滑。GL_纹理_MAG_过滤器的初始值为GL_线性。

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

import { SceneOption, Scene, SCENE } from '../gl-renderer/types' import Texture from '../texture' // 目前没有额外配置项,先预留配置项类型,方便后续扩展 export type WebcodecNormalSceneOption = SceneOption<object> /** * @description: 顶点着色器 * @return {*} */ export const vertexShaderSource = ` attribute vec2 xy; uniform vec2 scale; uniform vec2 translate; varying highp vec2 uv; void main(void) { vec2 scaledPosition = xy * scale+ translate; gl_Position = vec4(scaledPosition, 0.0, 1.0); // Map vertex coordinates (-1 to +1) to UV coordinates (0 to 1). // UV coordinates are Y-flipped relative to vertex coordinates. uv = vec2((1.0 + xy.x) / 2.0, (1.0 - xy.y) / 2.0); } ` /** * @description: 片元着色器 * @return {*} */ export const fragmentShaderSource = ` varying highp vec2 uv; uniform sampler2D texture; void main(void) { gl_FragColor = texture2D(texture, uv); } ` export class WebcodecNormalScene extends Scene { static sceneType = SCENE.WEBCODEC_NORMAL vertexShaderSource = vertexShaderSource fragmentShaderSource = fragmentShaderSource gl!: WebGLRenderingContext program!: WebGLProgram options!: WebcodecNormalSceneOption yTexture!: Texture uTexture!: Texture vTexture!: Texture get sceneType() { return WebcodecNormalScene.sceneType } private applyOptions(sceneGl: WebGLRenderingContext, sceneProgram: WebGLProgram, sceneOption: WebcodecNormalSceneOption) { this.gl = sceneGl this.program = sceneProgram this.options = sceneOption } public initializer(sceneGl: WebGLRenderingContext, sceneProgram: WebGLProgram, sceneOption: WebcodecNormalSceneOption) { this.applyOptions(sceneGl, sceneProgram, sceneOption) const gl = this.gl const shaderProgram = this.program const vertexBuffer = gl.createBuffer() gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer) gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0]), gl.STATIC_DRAW) const xyLocation = gl.getAttribLocation(shaderProgram, 'xy') gl.vertexAttribPointer(xyLocation, 2, gl.FLOAT, false, 0, 0) gl.enableVertexAttribArray(xyLocation) const scaleLocation = gl.getUniformLocation(shaderProgram, 'scale') const scaleFactor = [1, 1] // 缩放因子为 1.5x gl.uniform2fv(scaleLocation, scaleFactor) const translateLocation = gl.getUniformLocation(shaderProgram, 'translate') const translateFactor = [0, 0] // 平移因子为 0,0 gl.uniform2fv(translateLocation, translateFactor) //纹理 const texture = gl.createTexture() const enableMipMap = this.options.isAntialias ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR gl.bindTexture(gl.TEXTURE_2D, texture) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, enableMipMap) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) } public render(frameData: VideoFrame) { const gl = this.gl gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, frameData) this.options.isAntialias && gl.generateMipmap(gl.TEXTURE_2D) // Configure and clear the drawing area. gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight) gl.clearColor(0.0, 0.0, 0.0, 0.0) gl.clear(gl.COLOR_BUFFER_BIT) gl.drawArrays(gl.TRIANGLE_FAN, 0, 4) } public clear() { const gl = this.gl gl.clearColor(0.0, 0.0, 0.0, 0.0) gl.clear(gl.COLOR_BUFFER_BIT) gl.getExtension('WEBGL_lose_context')?.loseContext() } } 结合这段代码说一下优化的方式
最新发布
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值