author: longlongwaytogo date: 2022/07/18 email: longlongway2012@hotmail.com repo: https://github.com/longlongwaytogo/LearnOsgGL3简介
上一篇文章介绍了osg自带的osgSimple3例子,现在对它进行一定的修改,使其运行在gl 3.3 core模式下。
主要修改有以下几个地方- 设置gl 3.3版本和启用core模式
const std::string version( "3.3" );
osg::ref_ptr< osg::GraphicsContext::Traits > traits = new osg::GraphicsContext::Traits();
traits->x = 20; traits->y = 30;
traits->width = width; traits->height = height;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->glContextVersion = version;
traits->glContextProfileMask = GL_CONTEXT_CORE_PROFILE_BIT;// 0x1;//
- 删除兼容代码:
// for non GL3/GL4 and non GLES2 platforms we need enable the osg_ uniforms that the shaders will use,
// you don't need thse two lines on GL3/GL4 and GLES2 specific builds as these will be enable by default.
gc->getState()->setUseModelViewAndProjectionUniforms(true);
gc->getState()->setUseVertexAttributeAliasing(true);
- 修改shader版本为将#version 330
const std::string vertexSource =
"#version 330 n"
" n"
"uniform mat4 osg_ModelViewProjectionMatrix; n"
"uniform mat3 osg_NormalMatrix; n"
"uniform vec3 ecLightDir; n"
" n"
"in vec4 osg_Vertex; n"
"in vec3 osg_Normal; n"
"out vec4 color; n"
" n"
"void main() n"
"{ n"
" vec3 ecNormal = normalize( osg_NormalMatrix * osg_Normal ); n"
" float diffuse = max( dot( ecLightDir, ecNormal ), 0. ); n"
" color = vec4( vec3( diffuse ), 1. ); n"
" n"
" gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex; n"
"} n";
osg::Shader* vShader = new osg::Shader( osg::Shader::VERTEX, vertexSource );
const std::string fragmentSource =
"#version 330 n"
" n"
"in vec4 color; n"
"out vec4 fragData; n"
" n"
"void main() n"
"{ n"
" fragData = color; n"
"} n";
- 设置投影矩阵和顶点属性引用,但这句代码只在gl3以前的版本中需要,真正开发gl3版本的osg程序不需要以下代码
// for non GL3/GL4 and non GLES2 platforms we need enable the osg_ uniforms that the shaders will use,
// you don't need thse two lines on GL3/GL4 and GLES2 specific builds as these will be enable by default.
gc->getState()->setUseModelViewAndProjectionUniforms(true);
gc->getState()->setUseVertexAttributeAliasing(true);
- 兼容osg原有顶点数据布局位置:
// 兼容osg原有顶点数据布局位置
gc->getState()->resetVertexAttributeAlias(false);
-
使用RenderDoc抓帧分析:
-
Renderdoc支持gl 3.3以后版本的OpenGL,可以实现渲染数据的记录和再现,便于分析和查错。虽然不能支持单步调试,但可以修改着色器实时看到修改后的运行结果。
-
使用RenderDoc抓帧的界面,在程序上会显示额外的ui界面,显示Fame数目,刷新频率,抓帧快捷键F12(printscreen也可以抓帧)等信息,按下F12即可捕获一帧的渲染数据,下图为修改后的osgSimple3抓帧结果:
-
点击查看捕获的数据,选择textureViewer,左侧可以看到渲染阶段的api调用,中间为当前选中缓冲区,右侧显示colorbuffer和depethbuffer资源预览图:
-
点击pipeline state 可以查看不同的渲染阶段,如选中vs查看顶点shader
-
顶点shader预览窗口:
-
顶点Mesh Viewer预览窗口,可以查看渲染模型的网格信息:
-
-
总结:
使用opengl 3.3以后的版本,结合RenderDoc等调试工具可以对开发起到事半功倍的作用,又是百思不得其解的问题,一看就知道错在哪里,感慨工具开发者必然也是被折么的无法忍受,在想着通过捕获渲染api接口和数据的思路,来分析和重现整个渲染流程。



