Hello,

I've got it!
I initialise all in the first draw implementation call and then all works fine. 
I have to set all members to "mutable", that was the tricky part!

Cheers

Martin


-------- Original-Nachricht --------
> Datum: Tue, 24 Apr 2012 15:48:53 +0200
> Von: "Martin Großer" <grosser.mar...@gmx.de>
> An: osg-users@lists.openscenegraph.org
> Betreff: [osg-users] Drawable and OpenGL Extensions

> Hello,
> 
> I have tried to implement transform feedback in openscenegraph. I posted
> this few weeks ago. Now I have a nice OpenGL example and want to transfer
> this to an osg implementation. In a first (more simple) example, I want to
> create a vertex array object and a vertex buffer object. I create a OpenGL
> version and a OpenSceneGraph version. But my OSG version doesn't work. In the
> OSG version I create my own extension class with the VAO and VBO stuff.
> After this I create my own drawable type, which should draw four vertices,
> but I see nothing. :-(
> 
> What is my mistake?
> 
> Cheers
> 
> Martin
> 
> 
> PS: Here is the OpenGL code:
> ------------------------------------------
> 
> #include <GL/glew.h>
> #include <GL/gl.h>
> #include <GL/glut.h>
> 
> // Buffers
> GLuint myVAO;
> GLuint myVBO;
> 
> // Vertex Data
> unsigned int m,n,numVerts; // number of horizontal verts
> float* pm_data; // data pointer
> 
> void myInit(void)
> {
>   // Vertex Data
>   m = 2; // number of horizontal verts
>   n = 2; // number of vertical verts
>   numVerts = m*n;
>   pm_data = new float[numVerts*4];
> 
>   // generate position mass data
>   float x = 0.0;
>   float y = 0.0;
>   float dx = 1.0/(m-1);
>   float dy = 1.0/(n-1);
>   for(unsigned int i=0; i < numVerts; i++)
>   {
>     pm_data[i*4] = x; // x
>     pm_data[i*4+1] = y; // y
>     pm_data[i*4+2] = 0.0; // z
>     pm_data[i*4+3] = 1.0; // mass
> 
>     x += dx;
> 
>     if(!((i+1)%m))
>     {
>       y += dy;
>       x = 0.0;
>     }
>   } // position data are finished
> 
> 
>   // init VAO
>   glGenVertexArrays(1,&myVAO);
> 
>   // init VBO
>   glGenBuffers(1,&myVBO);
> 
>   // bind VAO and push data into VBO
>   glBindVertexArray(myVAO);
>   glBindBuffer(GL_ARRAY_BUFFER,myVBO);
>   glBufferData(GL_ARRAY_BUFFER, numVerts*sizeof(float)*4, &(pm_data[0]),
> GL_DYNAMIC_COPY);
>   glEnableVertexAttribArray(0);
>   glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,0,0);
>   glBindVertexArray(0);
> }
> 
> void myDrawImplementation(void)
> {
>   glPointSize(5);
>   glBindVertexArray(myVAO);
>   glDrawArrays(GL_POINTS,0,numVerts);
>   glBindVertexArray(0);
> }
> 
> void RenderScene(void)
> {
>   // Draw myVBO
>   myDrawImplementation();
> 
>   // Flush drawing commands (and swap)
>   glutSwapBuffers();
> }
> 
> int main (int argc, char* argv[])
> {
>   // initialise glut
>   glutInit(&argc, argv);
>   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
>   glutInitWindowSize(512,512);
>   glutCreateWindow("Simple (Hello World)");
>   glutDisplayFunc(RenderScene);
> 
>   // initialise glew
>   glewInit();
> 
>   // initialise VAO
>   myInit();
> 
>   // execution loop
>   glutMainLoop();
> 
>   return 0;
> }
> 
> 
> 
> Here is my osg code:
> ------------------------------------
> 
> #include <osgViewer/Viewer>
> #include <osg/Geode>
> #include <osg/Drawable>
> #include <osg/GLExtensions>
> #include <GL/gl.h>
> 
> class MyExtensions
> {
> public:
>   MyExtensions()
>   {
>     // Generate Vertex Array Object
>    
> if(!osg::setGLExtensionFuncPtr(_glGenVertexArrays,"glGenVertexArrays"))
>       osg::setGLExtensionFuncPtr(_glGenVertexArrays,"glGenVertexArrays");
> 
>     // Bind Vertex Array Object
>    
> if(!osg::setGLExtensionFuncPtr(_glBindVertexArray,"glBindVertexArray"))
>       osg::setGLExtensionFuncPtr(_glBindVertexArray,"glBindVertexArray");
> 
>     // Generate Buffer Object
>     if(!osg::setGLExtensionFuncPtr(_glGenBuffers,"glGenBuffers"))
>       osg::setGLExtensionFuncPtr(_glGenBuffers,"glGenBuffers");
> 
>     // Bind Buffer Object
>     if(!osg::setGLExtensionFuncPtr(_glBindBuffer,"glBindBuffer"))
>       osg::setGLExtensionFuncPtr(_glBindBuffer,"glBindBuffer");
> 
>     //  Buffer Data
>     if(!osg::setGLExtensionFuncPtr(_glBufferData,"glBufferData"))
>       osg::setGLExtensionFuncPtr(_glBufferData,"glBufferData");
> 
>     // Enable Vertex Attribute Array
>    
> if(!osg::setGLExtensionFuncPtr(_glEnableVertexAttribArray,"glEnableVertexAttribArray"))
>      
> osg::setGLExtensionFuncPtr(_glEnableVertexAttribArray,"glEnableVertexAttribArray");
> 
>     // Vertex Attribute Pointer
>    
> if(!osg::setGLExtensionFuncPtr(_glVertexAttribPointer,"glVertexAttribPointer"))
>      
> osg::setGLExtensionFuncPtr(_glVertexAttribPointer,"glVertexAttribPointer");
>   }
> 
>   // Vertex Array generate function
>   void glGenVertexArrays(GLsizei n, GLuint* arrays) const
>   { _glGenVertexArrays(n,arrays); }
> 
>   // Vertex Array bind function
>   void glBindVertexArray(GLuint array) const
>   { _glBindVertexArray(array); }
> 
>   // Enable Vertex Attribute Array
>   void glEnableVertexAttribArray(GLuint index) const
>   { _glEnableVertexAttribArray(index); }
> 
>   // define an array of generic vertex attribute data
>   void glVertexAttribPointer(GLuint index, GLint size, GLenum type,
>                              GLboolean normalized,GLsizei stride,
>                              const GLvoid* pointer) const
>   { _glVertexAttribPointer(index,size,type,normalized,stride,pointer); }
> 
> 
>   // Buffer generate function
>   void glGenBuffers(GLsizei n, GLuint* buffers) const
>   { _glGenBuffers(n,buffers); }
> 
>   // Buffer bind function
>   void glBindBuffer(GLenum target, GLuint buffer) const
>   { _glBindBuffer(target,buffer); }
> 
>   // Buffer data function
>   void glBufferData(GLenum target, GLsizeiptr size, const GLvoid* data,
> GLenum usage) const
>   { _glBufferData(target,size,data,usage); }
> 
> protected:
> 
>   typedef void (APIENTRY* GenVertexArrays)(GLsizei, GLuint*);
>   typedef void (APIENTRY* BindVertexArray)(GLuint);
>   typedef void (APIENTRY* GenBuffers)(GLsizei, GLuint*);
>   typedef void (APIENTRY* BindBuffer)(GLenum, GLuint);
>   typedef void (APIENTRY* BufferData)(GLenum, GLsizeiptr, const GLvoid*,
> GLenum);
>   typedef void (APIENTRY* EnableVertexAttribArray)(GLuint index);
>   typedef void (APIENTRY* VertexAttribPointer)(GLuint index, GLint size,
> GLenum type,
>                                                GLboolean normalized,
> GLsizei stride,
>                                                const GLvoid* pointer);
> 
>   GenVertexArrays _glGenVertexArrays;
>   BindVertexArray _glBindVertexArray;
>   GenBuffers _glGenBuffers;
>   BindBuffer _glBindBuffer;
>   BufferData _glBufferData;
>   EnableVertexAttribArray _glEnableVertexAttribArray;
>   VertexAttribPointer _glVertexAttribPointer;
> };
> 
> 
> class MyDrawable : virtual public osg::Drawable
> {
> public:
>   MyDrawable()
>   {
>     ext = new MyExtensions;
>     myInit();
>   }
> 
>  // I can't say much about the methods below, but OSG seems to expect
>   // that we implement them.
>   MyDrawable(const MyDrawable& pg,
>              const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY)
>   {;}
> 
>   virtual osg::Object* cloneType() const
>   { return new MyDrawable(); }
> 
>   virtual osg::Object* clone(const osg::CopyOp& copyop) const
>   { return new MyDrawable(*this, copyop); }
> 
>   void myInit()
>   {
>     // Vertex Data
>     m = 2; // number of horizontal verts
>     n = 2; // number of vertical verts
>     numVerts = m*n;
>     pm_data = new float[numVerts*4];
> 
>     // generate position mass data
>     float x = 0.0;
>     float y = 0.0;
>     float dx = 1.0/(m-1);
>     float dy = 1.0/(n-1);
>     for(unsigned int i=0; i < numVerts; i++)
>     {
>       pm_data[i*4] = x; // x
>       pm_data[i*4+1] = y; // y
>       pm_data[i*4+2] = 0.0; // z
>       pm_data[i*4+3] = 1.0; // mass
> 
>       x += dx;
> 
>       if(!((i+1)%m))
>       {
>         y += dy;
>         x = 0.0;
>       }
>     } // position data are finished
> 
>     // init VAO
>     ext->glGenVertexArrays(1,&myVAO);
> 
>     // init VBO
>     ext->glGenBuffers(1,&myVBO);
> 
>     // bind VAO and push data into VBO
>     ext->glBindVertexArray(myVAO);
>     ext->glBindBuffer(GL_ARRAY_BUFFER,myVBO);
>     ext->glBufferData(GL_ARRAY_BUFFER, numVerts*sizeof(float)*4,
> &(pm_data[0]), GL_DYNAMIC_COPY);
>     ext->glEnableVertexAttribArray(0);
>     ext->glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,0,0);
>     ext->glBindVertexArray(0);
>   }
> 
>   virtual void drawImplementation (osg::RenderInfo& renderInfo) const
>   {
>     glPointSize(5);
>     ext->glBindVertexArray(myVAO);
>     glDrawArrays(GL_POINTS,0,numVerts);
>     ext->glBindVertexArray(0);
>   }
> 
> protected:
>   ~MyDrawable()
>   {
>     delete ext;
>     delete[] pm_data;
>   }
> 
>   // data
>   unsigned int m,n,numVerts;
>   float* pm_data;
> 
>   GLuint myVAO;
>   GLuint myVBO;
> 
>   MyExtensions* ext;
> };
> 
> 
> int main(int argc, char **argv)
> {
>   // my special drawable
>   MyDrawable* drawable = new MyDrawable;
> 
>   // geometry node
>   osg::Geode* geode = new osg::Geode;
>   geode->addDrawable(drawable);
> 
>   // Viewer
>   ::osg::ref_ptr< osgViewer::Viewer > viewer = new osgViewer::Viewer;
>   viewer->setSceneData(geode);
>   viewer->realize();
>   viewer->run();
> 
>   return 0;
> }
> 
> 
> -- 
> Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
> belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

-- 
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!                          
        
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to