OpenGL深入探索——OpenGL/GLSL數據傳遞小記(3.x) 【包含UBO詳解】
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++代碼
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_SRC1_COLOR);
OpenGL
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。