当前位置:网站首页>Osgsimplegl3 example analysis
Osgsimplegl3 example analysis
2022-07-29 08:25:00 【longlongway2012】
osgSimplegl3 Example analysis
author: longlongwaytogo
date: 2022/07/12
email: [email protected]
repo: https://github.com/longlongwaytogo/LearnOsgGL3
brief introduction
The front is finished osg GL3 Construction of environment , Here is the main analysis osg How to combine gl3 Carry out development work .
A detailed explanation of the example
- establish osg gl3 Environmental Science
const int width( 800 ), height( 450 );
const std::string version( "3.0" );
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->readDISPLAY();
traits->setUndefinedScreenDetailsToDefaultScreen();
osg::ref_ptr< osg::GraphicsContext > gc = osg::GraphicsContext::createGraphicsContext( traits.get() );
if( !gc.valid() )
{
osg::notify( osg::FATAL ) << "Unable to create OpenGL v" << version << " context." << std::endl;
return( 1 );
}
The above code is mainly for osg::GraphicsContext::traits Object to set parameters , The key is to specify gl Version is 3.0.
take traits Parameters are applied to the camera :
osg::Camera* cam = viewer.getCamera(); cam->setGraphicsContext( gc.get() );take shader Parameters are applied to the scene root node
void configureShaders( osg::StateSet* stateSet ) { const std::string vertexSource = "#version 130 \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 130 \n" " \n" "in vec4 color; \n" "out vec4 fragData; \n" " \n" "void main() \n" "{ \n" " fragData = color; \n" "} \n"; osg::Shader* fShader = new osg::Shader( osg::Shader::FRAGMENT, fragmentSource ); osg::Program* program = new osg::Program; program->addShader( vShader ); program->addShader( fShader ); stateSet->setAttribute( program ); osg::Vec3f lightDir( 0., 0.5, 1. ); lightDir.normalize(); stateSet->addUniform( new osg::Uniform( "ecLightDir", lightDir ) ); }Above code , The use of shader Obviously older , It's best to use it really 3.3 The later version statement is better , This allows you to use renderdoc Perform frame grabbing debugging (renderdoc The use of will be introduced in detail in the following article ).
Set projection matrix and vertex attribute reference , But this code only gl3 Previous versions required , Really develop gl3 Version of osg The program does not need the following code
// 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);summary :
although osgSimplegl3 An example is a demonstration gl3 Characteristic , But the environment in which it is written is not fully open GL3 Run in core mode , It's running on gl3 In compatibility mode , This can run version edition 130 Of shader Code .
This example is just a simple demonstration of how to run gl3 Environment . Yes osg Compilation is not required , Even if osg There is no setting in the compilation settings gl shader The version is higher , You can also use a higher version shader characteristic . But the suggestion is still to set gl3 Operating under the environment of gl3 Program , The main reasons are as follows :
- First of all : You can use higher version features ;
- second : There can be more efficient api Interface , Can improve performance ;
- Third : Third party debugging tools can be used (renderdoc、gpa、NSight etc. ) Debug error . Next article , Will explain osg and gl 3.3 Configuration of the above version .
边栏推荐
- Unity shader learning (VI) achieving radar scanning effect
- ML.NET相关资源整理
- Low cost 2.4GHz wireless transceiver chip -- ci24r1
- Markdown concise grammar manual
- Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
- Back up Google or other browser plug-ins
- Qpalette learning notes
- Day5: PHP simple syntax and usage
- Simple operation of SQL server data table
- Centos7/8 command line installation Oracle11g
猜你喜欢

Random lottery turntable wechat applet project source code

Four pin OLED display based on stm32

Importerror: no module named XX

Week 2: convolutional neural network basics

Flask reports an error runtimeerror: the session is unavailable because no secret key was set
![[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification](/img/0e/e5be0fffb154d081c20b09832530d4.png)
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification

User identity identification and account system practice

Stm8s003 domestic substitute for dp32g003 32-bit microcontroller chip

Ga-rpn: recommended area network for guiding anchors

Chrony 时间同步
随机推荐
Simplefoc parameter adjustment 2- speed and position control
Unity multiplayer online framework mirror learning record (I)
New energy shared charging pile management and operation platform
Low cost 2.4GHz wireless transceiver chip -- ci24r1
leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
Importerror: no module named XX
centos7/8命令行安装Oracle11g
Application of explosion-proof inclination sensor in safe operation of LNG
Four pin OLED display based on stm32
A problem encountered in SQL interview
数字人民币时代隐私更安全
Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
Stm32ff030 replaces domestic MCU dp32g030
torch.Tensor.to的用法
MySQL rownum implementation
深度学习(2):图片文字识别
Day4: SQL server is easy to use
Day6: using PHP to write landing pages
[academic related] why can't many domestic scholars' AI papers be reproduced?
【OpenCV】-算子(Sobel、Canny、Laplacian)学习