Hi?  Could you sign with a name so we know how to address you?

I don't use Windows or MFC so can't answer specifics on this, though I
kinda surprised MFC is still be used after all these years... ain't it time
to write portable applications and use a portable UI?

W.r.t using OpenGL, I see that you are calling OpenGL in constructors, and
unless you are constructing your objects from a thread with a valid
graphics context current it will fail, likely crash.  You can only do
OpenGL calls from a thread with a valid graphics context.  Also another
problem is that your approach of allocating texture objects will only ever
work with a single graphics context.

Personally I'd strongly recommend that you avoid using custom OpenGL unless
you really really need to.  The OSG covers pretty well of all useful OpenGL
functionality, so the functionality is there you just need to create the
scene graph objects to utilize it.  If you need have niche piece of OpenGL
functionality that the OSG doens't yet support then you can always extend
the OSG locally for that functionality.  Howerver, for you purposes my
guess is that you don't need to.

The reason to avoid writing your own custom OpenGL are:

 1) Custom OpenGL code requires synchronization of state with osg::State,
both in and out of your custom code, so OSG set OpenGL state doesn't
interfer with your code, and the OpenGL state that you set doesn't bleed
into the OSG causing problems.  It's possible to do this properly but it
extra work and extra skill that really is totally unneccessary if you just
use the OSG.

2) The OSG natively supports multiple contexts, and mulit-threading, making
it far simpler to scale your applications that with custom OpenGL code.

3) The OSG queries all OpenGL extensions for you, you don't need to lift a
finger.

4) The OSG does state sorting and lazy state updating for you to ensure the
best performance from your hardware.

Robert.




Robert.


On 17 September 2013 02:50, 小风子 <[email protected]> wrote:

>    I am using osg in the MFC simple document application,and now I want to
> draw ocean in the window,in fact there is a plugin called osgocean which
> can draw ocean perfectly.but it not satisfied my need,because I should
> treat the ocean as a osg node for the further process. I look for the info
> in the Internet and find many resoults drawing ocean in opengl,I also find
> how to use opengl in the osg. these are my codes:
>
>   first impliment the class for using opengl in OSG:
>
>     class SimpleOceanDrawCallBack
>     :public osg::Drawable::DrawCallback
>     {
>     public:
>     SimpleOceanDrawCallBack(void);
>     ~SimpleOceanDrawCallBack(void);
>
>     virtual void drawImplementation(osg::RenderInfo& renderInfo,const
> osg::Drawable* drawable ) ;
>
>      };
>
>   second use the defined class(SimpleOceanDrawCallBack) when necessary:
>
> osg::ref_ptr<osg::Geometry> geometry=new osg::Geometry;
> geometry->setUseDisplayList(false);
> geometry->setDrawCallback(new OceanDrawCallBack());
> osg::ref_ptr<osg::Geode> geode=new osg::Geode;
> geode->addDrawable(geometry);
>
> geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
>
>     and then the geode node the the "ocean" node I needed ,I can do
> something else on this node, the body of drawImplementation is the
> process of drawing ocean , now is the main issue:
>     when drawImplementation  is simple the class works well:
>
> void SimpleOceanDrawCallBack::drawImplementation(osg::RenderInfo&
> renderInfo,const osg::Drawable* drawable )  {
> static double dColor= 50;//颜色
>
> glColor3f( dColor, 0, 0);
>
> glBegin(GL_TRIANGLES);//在OSG中画一个opengl三角形
>
> glVertex3f( 0.0, 0.0, 0.0);
>
> glVertex3f( 2.0, 0.0, 0.0);
>
> glVertex3f( 0.0, 0.0, 2.0);
>
> glEnd();
>
> dColor += 1;//颜色渐变
>
> if ( dColor > 255)
>
> {
>
> dColor= 0.0;
>
> }
> }
>      It works well and will drawing a triangle in the window ,But when I
> use the drawing ocean example I failed ,these are the drawing ocean
> procedure:
>     first I need to do some initialized things,so I put some codes in the
> construction function:
> OceanDrawCallBack::OceanDrawCallBack(void)
> {
> int argc = 1;
> char *argv[1] = {(char*)"Something"};
> glutInit(&argc, argv);
> glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
> glutInitWindowSize(width, height);
> glutCreateWindow("Ocean");
> glutCreateMenu(NULL);
> // //glutDisplayFunc(redisplayFunc);
> // //glutReshapeFunc(reshapeFunc);
> // //glutKeyboardFunc(keyboardFunc);
> // //glutSpecialFunc(specialKeyFunc);
> // //glutMouseFunc(mouseClickFunc);
> // //glutMotionFunc(mouseMotionFunc);
> // //glutPassiveMotionFunc(mousePassiveMotionFunc);
> // //glutIdleFunc(idleFunc);
> glewInit();
> GLuint transmittanceTex;
> GLuint irradianceTex;
> GLuint inscatterTex;
>
> float *data = new float[16*64*3];
> FILE *f = fopen("data/irradiance.raw", "rb");
> fread(data, 1, 16*64*3*sizeof(float), f);
> fclose(f);
> glActiveTexture(GL_TEXTURE0 + IRRADIANCE_UNIT);
> glGenTextures(1, &irradianceTex);
> glBindTexture(GL_TEXTURE_2D, irradianceTex);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
> glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
> glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, 64, 16, 0, GL_RGB, GL_FLOAT,
> data);
> delete[] data;
>
>         .......
> }
>
> then it can complile well ,but I see nothing in the window.and when I
> delete some code(the glutInit function):
>
> OceanDrawCallBack::OceanDrawCallBack(void)
> {
> // int argc = 1;
> // char *argv[1] = {(char*)"Something"};
> // glutInit(&argc, argv);
> // glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
> // glutInitWindowSize(width, height);
> // glutCreateWindow("Ocean");
> // glutCreateMenu(NULL);
> // //glutDisplayFunc(redisplayFunc);
> // //glutReshapeFunc(reshapeFunc);
> // //glutKeyboardFunc(keyboardFunc);
> // //glutSpecialFunc(specialKeyFunc);
> // //glutMouseFunc(mouseClickFunc);
> // //glutMotionFunc(mouseMotionFunc);
> // //glutPassiveMotionFunc(mousePassiveMotionFunc);
> // //glutIdleFunc(idleFunc);
> glewInit();
> GLuint transmittanceTex;
> GLuint irradianceTex;
> GLuint inscatterTex;
>
> float *data = new float[16*64*3];
> FILE *f = fopen("data/irradiance.raw", "rb");
> fread(data, 1, 16*64*3*sizeof(float), f);
> fclose(f);
> glActiveTexture(GL_TEXTURE0 + IRRADIANCE_UNIT);
> glGenTextures(1, &irradianceTex);
> glBindTexture(GL_TEXTURE_2D, irradianceTex);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
> glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
> glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, 64, 16, 0, GL_RGB, GL_FLOAT,
> data);
> delete[] data;
>
>         .......
> }
>
> it complile well and but runs error in this function of mfc:
> BOOL AFXAPI AfxDeactivateActCtx(DWORD dwFlags, ULONG_PTR ulCookie)
> {
> BOOL rc = pfnDeactivateActCtx != 0 ? pfnDeactivateActCtx(dwFlags,
> ulCookie) : FALSE;
> return rc;
> }
>
> then I debug the program and find the it jump in the error in this line:
>     glActiveTexture(GL_TEXTURE0 + IRRADIANCE_UNIT);
>
> this code is just active the texture unit ,and why result in the
> problem,or is my method is wrong ? I could not use opengl in the osg in
> such way?then what should I do to draw ocean as a node in osg?
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to