Re: [PyOpenGL-Users] Converting C++ code to python code From: Derakon - 2011-04-17 19:24 ```On Sun, Apr 17, 2011 at 11:44 AM, Abhijeet Rastogi wrote: > > Original author has implemented figureSet as vector, I implemented it as > python list while converting the code.  In the below code snippet, "in the > if-else part", I am not able to make out, how will I convert the code in > python? > > void drawSel() { >   glColor3f(0, 0, 0); >   int size = figureSet.size(); >   Figure *f = figureSet[selected]; > >   /* getPoint() is a virtual method: every figure has at least >       two points (is at least a line) */ >   int *pt1 = f->getPoint(1)->getCoords(); >   int *pt2 = f->getPoint(2)->getCoords(); > >   if (Triangle *t = dynamic_cast(f)) { // triangle: one more > point >      int *pt3 = t->getPoint(3)->getCoords(); >      //something happens here >   } >   else if (Quad *q = dynamic_cast(f)) { // quad: two more point >      int *pt3 = q->getPoint(3)->getCoords(); >      int *pt4 = q->getPoint(4)->getCoords(); >      //Something happens here >   } > } In this code we have a guarantee that there are at least 2 points to draw, but if the figure is a triangle, then there's only one more, while if it's a quad then there's two more. These need different drawing logic, hence the if/else split. The conditionals are basically saying "How many points are in this polygon?" and then drawing differently depending on the result. In other words, dynamic_cast is being used to check if the figure variable is representing a Triangle or a Quad. The direct way to do this in Python would be something along the lines of if type(figure) == Triangle: draw as if it's a triangle elif type(figure) == Quad: draw as if it's a quad Assuming you have Triangle and Quad classes declared, of course. The more elegant way would be to have Triangle and Quad have their own draw functions; then you just do figure.draw() and the appropriate function is automatically invoked. -Chris ```

 Re: [PyOpenGL-Users] Converting C++ code to python code From: Derakon - 2011-04-17 17:14 ```On Sun, Apr 17, 2011 at 10:02 AM, Abhijeet Rastogi wrote: > I have a function in C++ which goes like this . > void selection(int x, int y, int win) { > // Space for selection buffer > static GLuint selectBuff[BUFFER_LENGTH]; > GLint hits, viewport[4]; > > // Setup selection buffer and get the view port > glSelectBuffer(BUFFER_LENGTH, selectBuff); > glGetIntegerv(GL_VIEWPORT, viewport); > > // Switch to projection and save the matrix > glMatrixMode(GL_PROJECTION); > glPushMatrix(); >          ... >          ... > OK. So, now, first of all, in C++ code, it has defined the variables and > then called  glGetIntegerv. How am I supposed to do the same in python > code. Any time that the C version of OpenGL code returns something by reference, the Python version will just return the value directly. So you should be able to do this: selectBuff = glSelectBuffer(BUFFER_LENGTH) viewport = glGetIntegerv(GL_VIEWPORT) ```
 Re: [PyOpenGL-Users] Converting C++ code to python code From: Abhijeet Rastogi - 2011-04-17 17:40 Attachments: Message as HTML ```So, there is no need of defining anything because python will return it during the function call. That helped. Thanks Derakon. On Sun, Apr 17, 2011 at 10:44 PM, Derakon wrote: > > Any time that the C version of OpenGL code returns something by > reference, the Python version will just return the value directly. So > you should be able to do this: > > selectBuff = glSelectBuffer(BUFFER_LENGTH) > viewport = glGetIntegerv(GL_VIEWPORT) > > -- Regards, Abhijeet Rastogi (shadyabhi) http://www.google.com/profiles/abhijeet.1989 ```
 Re: [PyOpenGL-Users] Converting C++ code to python code From: Joshua R. Davis - 2011-04-17 17:55 ```Converting C/C++ OpenGL code to PyOpenGL code takes a little practice, due to small alterations in how PyOpenGL function calls use parameters. Rather than directly answering your question, let me point you to completely abstracted PyOpenGL picking and collision-testing functions on my web site: http://www.joshuadavis.us/software/selection.py You can use this code as-is, or you can view it as a tutorial. Also, please be aware that the selection mechanism is deprecated in OpenGL now. It is preferable to pick using the back buffer, for example. Hope this helps. Josh On 2011 Apr 17, at 12:02 PM, Abhijeet Rastogi wrote: > I have a function in C++ which goes like this . > > void selection(int x, int y, int win) { > // Space for selection buffer > static GLuint selectBuff[BUFFER_LENGTH]; > GLint hits, viewport[4]; > > // Setup selection buffer and get the view port > glSelectBuffer(BUFFER_LENGTH, selectBuff); > glGetIntegerv(GL_VIEWPORT, viewport); > > // Switch to projection and save the matrix > glMatrixMode(GL_PROJECTION); > glPushMatrix(); > ... > ... > > OK. So, now, first of all, in C++ code, it has defined the variables and then called glGetIntegerv. How am I supposed to do the same in python code. > Please help. I am just beginner trying to learn opengl. > > For the full code, you can search for function selection in http://sprunge.us/fAaf > > -- > Regards, > Abhijeet Rastogi (shadyabhi) > http://www.google.com/profiles/abhijeet.1989 > ------------------------------------------------------------------------------ > Benefiting from Server Virtualization: Beyond Initial Workload > Consolidation -- Increasing the use of server virtualization is a top > priority.Virtualization can reduce costs, simplify management, and improve > application availability and disaster protection. Learn more about boosting > the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev_______________________________________________ > PyOpenGL Homepage > http://pyopengl.sourceforge.net > _______________________________________________ > PyOpenGL-Users mailing list > PyOpenGL-Users@... > https://lists.sourceforge.net/lists/listinfo/pyopengl-users ```
 Re: [PyOpenGL-Users] Converting C++ code to python code From: Abhijeet Rastogi - 2011-04-17 18:44 Attachments: Message as HTML ```OK. May be as the topic is same I should continue here only. There is one more function in that cpp file. The query is small, but the code I have given displays more lines. Basically, I have a base class Figure; and from that I have derived classes like Line, Triangle, Quad.... Original author has implemented figureSet as vector, I implemented it as python list while converting the code. In the below code snippet, "in the if-else part", I am not able to make out, how will I convert the code in python? Guyz please help. I need to get the full working python clone by tomorrow & I am stuck at few places. Full c code -> http://sprunge.us/OUKM?cpp & header file is here http://sprunge.us/OUKM?cpp *void drawSel() { glColor3f(0, 0, 0); int size = figureSet.size(); Figure *f = figureSet[selected]; /* getPoint() is a virtual method: every figure has at least two points (is at least a line) */ int *pt1 = f->getPoint(1)->getCoords(); int *pt2 = f->getPoint(2)->getCoords(); if (Triangle *t = dynamic_cast(f)) { // triangle: one more point int *pt3 = t->getPoint(3)->getCoords(); //something happens here } else if (Quad *q = dynamic_cast(f)) { // quad: two more point int *pt3 = q->getPoint(3)->getCoords(); int *pt4 = q->getPoint(4)->getCoords(); //Something happens here } } * On Sun, Apr 17, 2011 at 10:49 PM, Joshua R. Davis Converting C/C++ OpenGL code to PyOpenGL code takes a little practice, due > to small alterations in how PyOpenGL function calls use parameters. Rather > than directly answering your question, let me point you to completely > abstracted PyOpenGL picking and collision-testing functions on my web site: > > http://www.joshuadavis.us/software/selection.py > > You can use this code as-is, or you can view it as a tutorial. Also, please > be aware that the selection mechanism is deprecated in OpenGL now. It is > preferable to pick using the back buffer, for example. Hope this helps. > > Josh > > -- Regards, Abhijeet Rastogi (shadyabhi) http://www.google.com/profiles/abhijeet.1989 ```
 Re: [PyOpenGL-Users] Converting C++ code to python code From: Derakon - 2011-04-17 19:24 ```On Sun, Apr 17, 2011 at 11:44 AM, Abhijeet Rastogi wrote: > > Original author has implemented figureSet as vector, I implemented it as > python list while converting the code.  In the below code snippet, "in the > if-else part", I am not able to make out, how will I convert the code in > python? > > void drawSel() { >   glColor3f(0, 0, 0); >   int size = figureSet.size(); >   Figure *f = figureSet[selected]; > >   /* getPoint() is a virtual method: every figure has at least >       two points (is at least a line) */ >   int *pt1 = f->getPoint(1)->getCoords(); >   int *pt2 = f->getPoint(2)->getCoords(); > >   if (Triangle *t = dynamic_cast(f)) { // triangle: one more > point >      int *pt3 = t->getPoint(3)->getCoords(); >      //something happens here >   } >   else if (Quad *q = dynamic_cast(f)) { // quad: two more point >      int *pt3 = q->getPoint(3)->getCoords(); >      int *pt4 = q->getPoint(4)->getCoords(); >      //Something happens here >   } > } In this code we have a guarantee that there are at least 2 points to draw, but if the figure is a triangle, then there's only one more, while if it's a quad then there's two more. These need different drawing logic, hence the if/else split. The conditionals are basically saying "How many points are in this polygon?" and then drawing differently depending on the result. In other words, dynamic_cast is being used to check if the figure variable is representing a Triangle or a Quad. The direct way to do this in Python would be something along the lines of if type(figure) == Triangle: draw as if it's a triangle elif type(figure) == Quad: draw as if it's a quad Assuming you have Triangle and Quad classes declared, of course. The more elegant way would be to have Triangle and Quad have their own draw functions; then you just do figure.draw() and the appropriate function is automatically invoked. -Chris ```