OpenGL ES3.0 《学习笔记 八》Vertex Shaders

这篇博客详细介绍了OpenGL ES3.0中的顶点着色器,包括内置特殊变量、统一状态、常量、精度限定符、顶点着色器中的uniform限制以及各种示例。重点讨论了矩阵变换、光照计算、纹理坐标生成、顶点皮肤ning和位移映射等关键概念,并给出了相应的顶点着色器代码示例。

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



Vertex Shader Built-In Variables

1.Built-In Special Variables
gl_VertexID

gl_InstanceID

gl_Position

gl_PointSize

gl_FrontFacing

2.Built-In Uniform State
gl_DepthRange : 

struct gl_DepthRangeParameters
{
highp float near; // near Z
highp float far; // far Z
highp float diff; // far – near
}
uniform gl_DepthRangeParameters gl_DepthRange;

3.Built-In Constants
const mediump int gl_MaxVertexAttribs = 16;

const mediump int gl_MaxVertexUniformVectors = 256;

const mediump int gl_MaxVertexOutputVectors = 16;

const mediump int gl_MaxVertexTextureImageUnits = 16;

const mediump int gl_MaxCombinedTextureImageUnits = 32;

查询:

glGetIntegerv ( GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs );
glGetIntegerv ( GL_MAX_VERTEX_UNIFORM_VECTORS, &maxVertexUniforms );
glGetIntegerv ( GL_MAX_VARYING_VECTORS, &maxVaryings );
glGetIntegerv ( GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &maxVertexTextureUnits );
glGetIntegerv ( GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxCombinedTextureUnits );


Precision Qualifiers

三种精度:

(1)lowp

(2)mediump

(3)highp

定义默认精度,如:

precision highp float;

precision mediump int;

如果没有定义默认精度,则 int 和 float 的默认精度都是 highp


Number of Uniforms Limitations in a Vertex Shader

gl_MaxVertexUniformVectors

一般来说常数是参与计数的,所以下面两段同样逻辑的vertex shader,后者优于前者 :

《《《《《《《《《《《《《《《《《《《《《《《《

#version 300 es
#define NUM_TEXTURES 2

uniform mat4 tex_matrix[NUM_TEXTURES];
uniform bool enable_tex[NUM_TEXTURES];
uniform bool enable_tex_matrix[NUM_TEXTURES];

in vec4 a_texcoord0;
in vec4 a_texcoord1;
out vec4 v_texcoord[NUM_TEXTURES];

void main {
v_texcoord[0] = vec4(0.0, 0.0, 0.0, 0.0);
if(enable_tex[0]) {
if(enable_tex_matrix[0]) {
v_texcoord[0] = tex_matrix[0] * a_texcoord0;
} else {
v_texcoord[0] = a_texcoord0;
}


v_texcoord[1] = vec4(0.0, 0.0, 0.0, 0.0);
if(enable_tex[1]) {
if(enable_tex_matrix[1]) {
v_texcoord[1] = tex_matrix[1] * a_texcoord1;
} else {
v_texcoord[1] = a_texcoord1;
}
}

// set gl_Position
}

《《《《《《《《《《《《《《《《《《《《《《《《


《《《《《《《《《《《《《《《《《《《《《《《《

#version 300 es
#define NUM_TEXTURES 2

const int c_zero = 0;
const int c_one = 1;

uniform mat4 tex_matrix[NUM_TEXTURES];
uniform bool enable_tex[NUM_TEXTURES];
uniform bool enable_tex_matrix[NUM_TEXTURES];

in vec4 a_texcoord0; 
in vec4 a_texcoordl;
out vec4 v_texcoord[NUM_TEXTURES];

void main() {
v_texcoord[c_zero] = vec4 (float(c_zero), float(c_zero), float(c_zero), float(c_one));
if(enable_tex[c_zero]) {
if ( enable_tex_matrix[c_zero] ) {
v_texcoord[c_zero] = tex_matrix[c_zero] * a_texcoord0;
} else {
v_texcoord[c_zero] = a_texcoord0;
}
}

v_texcoord[c_one] = vec4(float(c_zero), float(c_zero), float(c_zero), float(c_one));
i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值