Re: [osg-users] Drawable and OpenGL Extensions

2012-04-24 Thread Sergey Polischuk
Hi Martin

code where are you initialize your extensions and opengl stuff (atm all this 
happens in your drawable constructor) needs active opengl context
It's better to move such initialization out of constructor and do it on first 
draw or some other place where opengl context already created and active.

Cheers,
Sergey

24.04.2012, 17:48, "Martin Groer" :
> 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 
> #include 
> #include 
>
> // 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 
> #include 
> #include 
> #include 
> #include 
>
> 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, 

Re: [osg-users] Drawable and OpenGL Extensions

2012-04-24 Thread Martin Großer
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" 
> 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 
> #include 
> #include 
> 
> // 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 
> #include 
> #include 
> #include 
> #include 
> 
> 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