OpenGL深入探索——OpenGL/GLSL数据传递小记(3.x) 【包含UBO详解】

网友投稿 892 2022-05-30

1. attribute变量

glsl3.x代码 (Vertex Program)

#version 330

layout(location = 0) in vec3 attrib_position;

layout(location = 1) in vec2 attrib_texcoord;

layout(location = 2) in vec3 attrib_normal;

layout(location = 3) in int attrib_clustercount;

2.uniform变量

glsl代码

#version 330

uniform sampler2D basetex1;

uniform float fAlphaRestrictVal;

uniform mat4 matModel;

uniform mat4 matView;

uniform mat4 matProj;

Uniform Buffer Object(UBO)

glsl代码

#version 330

layout(std140) uniform matVP

{

mat4 matProj;

mat4 matView;

};

OpenGL代码

GLint nMatVPBlockIndex = glGetUniformBlockIndex(nProgramHandler, "matVP");

//GLint nMatVPBlockIndex = glGetProgramResourceIndex(nProgramHandler, GL_UNIFORM_BLOCK, "matVP");

if (GL_INVALID_INDEX != nMatVPBlockIndex)

{

GLint nBlockDataSize = 0;

glGetActiveUniformBlockiv(nProgramHandler, nMatVPBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSize);

glGenBuffers(1, &m_nUBO);

glBindBuffer(GL_UNIFORM_BUFFER, m_nUBO);

glBufferData(GL_UNIFORM_BUFFER, nBlockDataSize, NULL, GL_DYNAMIC_DRAW);

glBindBufferRange(GL_UNIFORM_BUFFER, 0, m_nUBO, 0, nBlockDataSize);

glUniformBlockBinding(nProgramHandler, nMatVPBlockIndex, 0);

glBindBuffer(GL_UNIFORM_BUFFER, NULL);

}

OpenGL代码

GLint nMatVPBlockIndex = glGetUniformBlockIndex(nProgramHandler, "matVP");

GLint nCloudScaleIndex = glGetUniformBlockIndex(nProgramHandler, "Scale");

if (GL_INVALID_INDEX != nMatVPBlockIndex && GL_INVALID_INDEX != nCloudScaleIndex)

{

int nUniformBufferAlignSize = 0;

glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &nUniformBufferAlignSize);

GLint nBlockDataSize1 = 0, nBlockDataSize2 = 0;

glGetActiveUniformBlockiv(nProgramHandler, nMatVPBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSize1);

glGetActiveUniformBlockiv(nProgramHandler, nCloudScaleIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &nBlockDataSize2);

glGenBuffers(1, &m_nUBO);

glBindBuffer(GL_UNIFORM_BUFFER, m_nUBO);

glBufferData(GL_UNIFORM_BUFFER, nUniformBufferAlignSize + nBlockDataSize2, NULL, GL_DYNAMIC_DRAW);

glBindBufferRange(GL_UNIFORM_BUFFER, 0, m_nUBO, 0, nBlockDataSize1);

glUniformBlockBinding(nProgramHandler, nMatVPBlockIndex, 0);

glBindBufferRange(GL_UNIFORM_BUFFER, 1, m_nUBO, nUniformBufferAlignSize, nBlockDataSize2);

glUniformBlockBinding(nProgramHandler, nCloudScaleIndex, 1);

}

glsl代码

layout(std140) uniform matVP

{

float elapsedTime;

mat4 matProj;

mat4 matView;

};

layout(std140) uniform Scale

{

uniform float cloudScale;

};

C++代码

{

//Render

glBindBuffer(GL_UNIFORM_BUFFER, m_nUBO);

glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GLfloat), &fElapsed);

//current std140 alignment(for base sclaer): 16

glBufferSubData(GL_UNIFORM_BUFFER, 16, sizeof(Matrix16), m_mtProj.mt);

glBufferSubData(GL_UNIFORM_BUFFER, 16 + sizeof(Matrix16), sizeof(Matrix16), m_mtView.mt);

//GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT: 256

glBufferSubData(GL_UNIFORM_BUFFER, 256, sizeof(GLfloat), &fCScale);

glBindBuffer(GL_UNIFORM_BUFFER, NULL);

}

3.varying变量

glsl代码

//vertex shader or geometry shadr

flat out float visibility;

//fragment shader

flat in float visibility;

glsl代码

//Vertex Shader

out vec2 varying_vg_texcoord;

//Geometry Shader

in vec2 varying_vg_texcoord[];

out vec2 varying_gf_texcoord;

//Fragment Shader

in vec2 varying_gf_texcoord;

//Vertex Shader

out Varying

{

vec2 texcoord;

}VaryingOut;

//Geometry Shader

in Varying

{

vec2 texcoord;

}VaryingIn[];

out Varying

{

vec2 texcoord;

}VaryingOut;

//Fragment Shader

in Varying

{

vec2 texcoord;

}VaryingIn;

glsl代码

//Geometry Shader Example

void main(void)

{

for(int i = 0; i < gl_in.length(); ++i)

{

gl_Position = gl_in[i].gl_Position;

VaryingOut.texcoord = VaryingIn[i].texcoord;

EmitVertex();

}

EndPrimitive();

}

glsl代码

//Geometry Shader Example

out Varying

{

vec3 position;

int cloudcount;

vec3 dimension;

}VaryingOut;

C++代码

//OpenGL Code Example

const GLchar *varyingOutCloudFeed[] = {"Varying.position", "Varying.cloudcount", "Varying.dimension"};

glTransformFeedbackVaryings(m_CloudFeedShader.GetProgramHandler(), 3, varyingOutCloudFeed, GL_SEPARATE_ATTRIBS);

m_CloudFeedShader.Link();

4.fragmentShader输出

glsl代码 (fragemnt shader)

//单输出

layout(location = 0) out vec4 fragColor;

//MRT

layout(location = 0) out vec4 fragColor0;

layout(location = 1) out vec4 fragColor1;

glsl代码 (fragemnt shader)

layout(location = 0, index = 0) out vec4 fragColor;

layout(location = 0, index = 1) out vec4 src1Color;

C++代码

OpenGL深入探索——OpenGL/GLSL数据传递小记(3.x) 【包含UBO详解】

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_SRC1_COLOR);

OpenGL

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一个神奇的bug:OOM?优雅终止线程?系统内存占用较高?
下一篇:Oracle高级查询
相关文章