OpenGL Shader Key Points (2)

本文深入探讨了OpenGL中Uniform变量的作用与用法,包括如何在C/C++程序中为Uniform变量赋值,并介绍了Uniform Blocks的概念及其在多个Shader程序间共享Uniform数据的方法。

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


1.  Uniform

1.1.  Uniform变量

不是所有的变量都是跟顶点一一对应的,如变换矩阵,光源位置等。

Uniform变量可以在任何类型的shader中使用,但只能作为输入值,不能在shader中给它赋值,只能在C/C++程序中指定。

 

 

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;
out vec3 Color;
uniform mat4 RotationMatrix;
void main()
{
Color = VertexColor;
gl_Position = RotationMatrix *vec4(VertexPosition,1.0);
}
 


在程序中指定uniform的值:

GLuint location =glGetUniformLocation(programHandle,  "RotationMatrix");
if( location >= 0 )
{
glUniformMatrix4fv(location, 1, GL_FALSE,  &rotationMatrix[0][0]);
}


 

API:

         遍历shader program所有的uniform变量

         glGetProgramiv

glGetActiveUniform

glGetActiveUniformName

glGetUniformLocation

 

1.2.  Uniform blocks

单独的uniform变量是跟shader program绑定的,因而不能在多shader program中共享。

Uniform blocks用来在不同的shaderprogram中共享uniform数据。

 

API:

glGetUniformBlockIndex //根据名称找到uniformblocks的索引

glGetActiveUniformBlockiv //根据索引找到blocks的大小

glGetUniformIndices//根据uniform blocks的各分量的名字找到索引

glGetActiveUniformsiv//找到各分量的便宜量,这样就可以分配block并设置分量的值。

 

1.2.1.  创建uniform buffer object (UBO):

GL_UNIFORM_BUFFER

glGenBuffers

glBindBuffer

glBufferData

glBindBufferBase // UBO与uniformblocks绑定

### OpenGL Fixed Function Pipeline vs Programmable Pipeline Differences In traditional OpenGL versions prior to 2.0, the **fixed function pipeline** was utilized where developers had limited control over how vertices were transformed or fragments shaded; this process relied on predefined functions set by the API itself[^1]. The architecture included stages like transformation, lighting, clipping, rasterization, texture application, and finally pixel operations. With the advent of more advanced GPUs, OpenGL introduced the **programmable pipeline**, starting from version 2.0 onwards with specifications such as OpenGL ES 2.0 which emphasized creating shader programs using GLSL (OpenGL Shading Language). This shift allowed for greater flexibility in defining custom behavior at various points within the rendering pipeline through user-defined shaders—specifically vertex shaders that manipulate geometric data before it reaches later stages of processing, and fragment shaders responsible for determining final colors per-pixel after rasterization has occurred. The key distinctions between these two approaches are summarized below: #### Control Over Processing Stages - In the fixed-function approach, each stage operates according to pre-established rules without any direct intervention possible beyond setting parameters. - Conversely, under the programmable model, one writes code directly controlling what happens during both vertex transformations and fragment shading processes via shaders written in GLSL. #### Flexibility & Customizability - Limited customization options exist when working strictly inside a rigid framework provided solely by built-in functionalities offered by earlier APIs. - Enhanced capabilities allow artists/developers full access to modify nearly every aspect related to visual effects generation including but not limited to implementing non-standard algorithms unachievable otherwise due to constraints imposed previously upon them. #### Performance Considerations - While simpler applications may benefit slightly better performance-wise out-of-the-box because they do not require compilation time associated with writing new shaders, - Complex scenes requiring intricate interactions among multiple elements often see significant improvements once optimized specifically tailored towards target hardware characteristics thanks largely in part owing much credit given hereafter attributed primarily toward leveraging modern GPU architectures effectively. ```cpp // Example Vertex Shader Code Snippet #version 330 core layout(location = 0) in vec3 position; void main() { gl_Position = vec4(position, 1.0); } ``` ```cpp // Example Fragment Shader Code Snippet #version 330 core out vec4 FragColor; void main(){ FragColor = vec4(1.0f); // White color output } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值