Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于使用glsl3.3版本报opengl报错问题 #1271

Open
yph025869 opened this issue Nov 15, 2023 · 8 comments
Open

关于使用glsl3.3版本报opengl报错问题 #1271

yph025869 opened this issue Nov 15, 2023 · 8 comments

Comments

@yph025869
Copy link

原因:
image

暂时解决办法:在GraphicsWindowWin32::createContextImplementation函数里面加入
image

@yph025869
Copy link
Author

麻烦,问下原因

@yph025869 yph025869 changed the title 关于使用glsl3.0版本报opengl报错问题 关于使用glsl3.3版本报opengl报错问题 Nov 15, 2023
@yph025869
Copy link
Author

// This is public domain software and comes with
// absolutely no warranty. Use of public domain software
// may vary between counties, but in general you are free
// to use and distribute this software for any purpose.

// Example: OSG using an OpenGL 3.0 context.
// The comment block at the end of the source describes building OSG
// for use with OpenGL 3.x.

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/GraphicsContext>
#include <osg/Camera>
#include <osg/Viewport>
#include <osg/StateSet>
#include <osg/Program>
#include <osg/Shader>
#include <osgUtil/Optimizer>

void configureShaders( osg::StateSet* stateSet )
{
const std::string vertexSource =
"#version 330 core\n"
" \n"
"uniform mat4 osg_ModelViewProjectionMatrix; \n"
"uniform mat3 osg_NormalMatrix; \n"
"uniform vec3 ecLightDir; \n"
"uniform mat4 osg_ModelViewMatrix; \n"
" \n"
"layout(location=0)in vec4 osg_Vertex; \n"
"layout(location=1)in vec3 osg_Normal; \n"
"out vec4 color; \n"
"out vec2 texCoord; \n"
" \n"
"vec2 sphereMap(vec3 normal,vec3 ecPositions) \n"
"{ \n"
"float m; \n"
"vec3 r,u;\n"
"u=normalize(ecPositions); \n"
"r=reflect(u,normal); \n"
"m=2.0sqrt(r.xr.x+r.y*(r.z+1.0)(r.z+1.0)); \n"
"return vec2(r.x/m+0.5,r.y/m+0.5); \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"
"vec4 vVert4=osg_ModelViewMatrix
osg_Vertex; \n"
"vec3 vEyeVertex = vVert4.xyz/vVert4.w; \n"
"texCoord=sphereMap(ecNormal,vEyeVertex); \n"
"} \n";
osg::Shader* vShader = new osg::Shader( osg::Shader::VERTEX, vertexSource );

const std::string fragmentSource =
    "#version 330 core \n"
    " \n"
    "in vec4 color; \n"
    "in vec2 texCoord; \n"
    "uniform sampler2D texture0; \n"
    "out vec4 fragData; \n"
    " \n"
    "void main() \n"
    "{ \n"
    "    fragData = texture2D(texture0,texCoord); \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 ) );

}
void myDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
// 处理调试消息
}

int main( int argc, char** argv )
{
osg::ArgumentParser arguments( &argc, argv );

osg::ref_ptr<osg::Node> root = osgDB::readRefNodeFiles( arguments );
if( root == NULL )
{
    osg::notify( osg::FATAL ) << "Unable to load model from command line." << std::endl;
    return( 1 );
}

osgUtil::Optimizer optimizer;
optimizer.optimize(root.get(), osgUtil::Optimizer::ALL_OPTIMIZATIONS  | osgUtil::Optimizer::TESSELLATE_GEOMETRY); //优化节点开销

configureShaders( root->getOrCreateStateSet() );

const int width( 800 ), height( 450 );
const std::string version( "4.6" );
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 = 1;
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 );
}

osgViewer::Viewer viewer;

// Create a Camera that uses the above OpenGL context.
osg::Camera* cam = viewer.getCamera();
cam->setGraphicsContext( gc.get() );
// Must set perspective projection for fovy and aspect.
cam->setProjectionMatrix( osg::Matrix::perspective( 30., (double)width/(double)height, 1., 100. ) );
// Unlike OpenGL, OSG viewport does *not* default to window dimensions.
cam->setViewport( new osg::Viewport( 0, 0, width, height ) );

viewer.setSceneData( root );

// 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);

return( viewer.run() );

}

/*

Building OSG for OpenGL 3.x

OSG currently support OpenGL 3.x on Windows. This comment block describes the
necessary configuration steps.

Get the draft gl3.h header file from OpenGL.org and put it in a folder called
“GL3” somewhere on your hard drive. OSG includes this header as <GL3/gl3.h>. Get
gl3.h from here:
http://www.opengl.org/registry/

Open the cmake-gui and load OSG's top-level CmakeLists.txt. You'll need to make
several changes.

  • Add the path to <GL3/gl3.h> to the CMake compiler flags, CMAKE_CXX_FLAGS and
    CMAKE_CXX_FLAGS_DEBUG (for release and debug builds; others if you use other
    build configurations). The text to add should look something like this:
    /I “C:\GLHeader”
    The folder GLHeader should contain a subfolder GL3, which in turn contains
    gl3.h.

  • Enable the following CMake variable:
    OSG_GL3_AVAILABLE

  • Disable the following CMake variables:
    OSG_GL1_AVAILABLE
    OSG_GL2_AVAILABLE
    OSG_GLES1_AVAILABLE
    OSG_GLES2_AVAILABLE
    OSG_GL_DISPLAYLISTS_AVAILABLE
    OSG_GL_FIXED_FUNCTION_AVAILABLE
    OSG_GL_MATRICES_AVAILABLE
    OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE
    OSG_GL_VERTEX_FUNCS_AVAILABLE

Create your project files in cmake-gui as usual, and build OSG as usual.

If you have an external project that will depend on OSG built for OpenGL 3.x,
you'll need to ensure your external project also uses the compiler include
directives to find <GL3/gl3.h>.

To verify your application is using a pure OpenGL 3.x context, set
OSG_NOTIFY_LEVEL=INFO in the environment and check the console output. Context
creation displays output such as the following:
GL3: Attempting to create OpenGL3 context.
GL3: version: 3.1
GL3: context flags: 0
GL3: profile: 0
GL3: context created successfully.

When your app begins rendering, it displays information about the actual context
it is using:
glVersion=3.1, isGlslSupported=YES, glslLanguageVersion=1.4

*/

@yph025869
Copy link
Author

以上代码是例子

@xarray
Copy link

xarray commented Nov 16, 2023

When using OpenGL Core profile, most state attributes and related modes are deprecated and will display warnings as in your terminal. For instance, GLmode 0xb50 means GL_LIGHTING, which only works under fixed pipeline and not recognized anymore in core profile cases.

I would suggest remove those state attributes (e.g. Light, LightModel, Texgen, Texmat, Material) from your scene graph and use GLSL shaders to implement different rendering effects. These attributes are still useful under Compatible Profile (although very very old...) so there is actually no need to change anything on OSG side.

@Yph0258691
Copy link

When using OpenGL Core profile, most state attributes and related modes are deprecated and will display warnings as in your terminal. For instance, GLmode 0xb50 means GL_LIGHTING, which only works under fixed pipeline and not recognized anymore in core profile cases.

I would suggest remove those state attributes (e.g. Light, LightModel, Texgen, Texmat, Material) from your scene graph and use GLSL shaders to implement different rendering effects. These attributes are still useful under Compatible Profile (although very very old...) so there is actually no need to change anything on OSG side.

的确是这个问题,不过,还得要像个办法,兼容固定管线

@xarray
Copy link

xarray commented Nov 17, 2023

When using OpenGL Core profile, most state attributes and related modes are deprecated and will display warnings as in your terminal. For instance, GLmode 0xb50 means GL_LIGHTING, which only works under fixed pipeline and not recognized anymore in core profile cases.
I would suggest remove those state attributes (e.g. Light, LightModel, Texgen, Texmat, Material) from your scene graph and use GLSL shaders to implement different rendering effects. These attributes are still useful under Compatible Profile (although very very old...) so there is actually no need to change anything on OSG side.

的确是这个问题,不过,还得要像个办法,兼容固定管线

When choosing core profile, it means that you deprecate and throw away the fixed pipeline. Why still consider the compatiblity?

@Yph0258691
Copy link

使用 OpenGL Core 配置文件时,大部分状态属性和相关模式均已废弃,请在终端中显示警告。例如,GLmode 0xb50 表示 GL_LIGHTING,它仅在固定管道下工作,并且在核心配置文件情况下不再被识别。
我建议从场景中删除这些状态属性(例如 Light、LightModel、Texgen、Texmat、Material),并使用 GLSL 着色器来实现不同的渲染效果。这些属性在 Compatible Profile 下仍然有用(尽管非常有用)非常旧……),因此实际上不需要在 OSG 方面进行任何更改。

确实是这个问题,不过,还得像个办法,兼容固定桩

当选择核心配置文件时,意味着您放弃并废弃固定管道。为什么还要考虑兼容性呢?

刚刚学习了,兼容是不可能,看样子,只能用3.0的版本已经着色器去使用,否则OSG里面的灯光效果,全部都要修改成glsl

@Yph0258691
Copy link

When using OpenGL Core profile, most state attributes and related modes are deprecated and will display warnings as in your terminal. For instance, GLmode 0xb50 means GL_LIGHTING, which only works under fixed pipeline and not recognized anymore in core profile cases.
I would suggest remove those state attributes (e.g. Light, LightModel, Texgen, Texmat, Material) from your scene graph and use GLSL shaders to implement different rendering effects. These attributes are still useful under Compatible Profile (although very very old...) so there is actually no need to change anything on OSG side.

的确是这个问题,不过,还得要像个办法,兼容固定管线

When choosing core profile, it means that you deprecate and throw away the fixed pipeline. Why still consider the compatiblity?
能不能给一个版本,Light、LightModel、Texgen、Texmat、Material 用GLSL编写的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants