Re: [osg-users] Creating shapes using Geometry
Hi, Paul. Not in any way did your code insulted my intelligence. I haven't been to any Computer Graphics course in my whole life, so keep feeding me with the basics! : ) Well. I've got a question here. I've addapted that code you've given me so that it was supposed to create a sphere. You can deduce that it didn't (it just draws the original circle). I was wondering if you could point out my mistake. It doesn't seem to be in the logics of computer graphics, but something I don't know about OSG. Thanks for your help. CODE/// #include osg/LineWidth #include osg/Geometry #include osg/math #include math.h #include osgViewer/Viewer osg::Vec3Array* circleVerts( int plane, int approx, float radius ) { const double angle( osg::PI * 2. / (double) approx ); osg::Vec3Array* v = new osg::Vec3Array; int idx, count; double x(0.), y(0.), z(0.); double height; double original_radius = radius; for(count = 0; count = approx/4; count++) { height = original_radius*sin(count*angle); radius = cos(count*angle)*radius; switch(plane) { case 0: // X x = height; break; case 1: //Y y = height; break; case 2: //Z z = height; break; } for( idx=0; idxapprox; idx++) { double cosAngle = cos(idx*angle); double sinAngle = sin(idx*angle); switch (plane) { case 0: // X y = radius*cosAngle; z = radius*sinAngle; break; case 1: // Y x = radius*cosAngle; z = radius*sinAngle; break; case 2: // Z x = radius*cosAngle; y = radius*sinAngle; break; } v-push_back( osg::Vec3( x, y, z ) ); } } return v; } osg::Geode* circles( int plane, int approx, float radius ) { osg::Geode* geode = new osg::Geode; osg::LineWidth* lw = new osg::LineWidth( 3. ); geode-getOrCreateStateSet()-setAttributeAndModes( lw, osg::StateAttribute::ON ); osg::Geometry* geom = new osg::Geometry; osg::Vec3Array* v = circleVerts( plane, approx, radius ); geom-setVertexArray( v ); osg::Vec4Array* c = new osg::Vec4Array; c-push_back( osg::Vec4( 1., 1., 1., 1. ) ); geom-setColorArray( c ); geom-setColorBinding( osg::Geometry::BIND_OVERALL ); geom-addPrimitiveSet( new osg::DrawArrays( GL_LINE_LOOP, 0, approx ) ); geode-addDrawable( geom ); return geode; } int main(int argc, char **argv) { // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(argc,argv); osg::DisplaySettings::instance()-setMinimumNumAlphaBits(8); // construct the viewer. osgViewer::Viewer viewer; osg::Group* root = new osg::Group(); root-addChild(circles(0, 200, 1.0)); viewer.setSceneData(root); return viewer.run(); } ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Hi Renan, Quoting Renan Mendes [EMAIL PROTECTED]: Well. I've got a question here. I've addapted that code you've given me so that it was supposed to create a sphere. You can deduce that it didn't (it just draws the original circle). I was wondering if you could point out my ... osg::Vec3Array* circleVerts( int plane, int approx, float radius ) { ... for(count = 0; count = approx/4; count++) { ... for( idx=0; idxapprox; idx++) { ... v-push_back( osg::Vec3( x, y, z ) ); } } } ... osg::Geode* circles( int plane, int approx, float radius ) { osg::Geometry* geom = new osg::Geometry; osg::Vec3Array* v = circleVerts( plane, approx, radius ); ... geom-addPrimitiveSet( new osg::DrawArrays( GL_LINE_LOOP, 0, approx ) ); As far as I can see you're creating 'approx/4' circles each with 'approx' vertices. This means that the vertex array contains '(approx/4)+1 * approx' vertices but the 'DrawArrays' only displays the first 'approx' vertices. If you change that last line to: geom-addPrimitiveSet(new osg::DrawArrays(GL_LINE_LOOP, 0, approx*(approx/4)+1)); you should start to see more what you expect. /Ulrich ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Very nice of you, Paul. Thanks very much. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Restez tranquille, donc! Je parle un peu le français. Peut-être ça ne sera pas le problème plus grand... Thanks, once again. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
_ From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Renan Mendes Sent: Sunday, December 02, 2007 3:40 PM To: OpenSceneGraph Users Subject: Re: [osg-users] Creating shapes using Geometry Hey, Jean-Sébastien. Now I'm asking for that example code you've offered me, if that still stands... I haven't tried hard enough, to be honest, but I sure have no extra time to deal with this properly right now. Not to mention the guy I work with, which is breathing down my neck... I would appreciate that favor very much. Thanks. Renan M Z Mendes Hi Renan -- Not sure exactly what aspect of this you're having trouble with. I've attached some very simple code for drawing a unit circle into one of the three coordinate planes; if this simple code insults your intelligence, I apologize. Expanding this code to draw spheres, cones, cylinders, etc., is an exercise in trig and use of the OpenGL primitive types. Hope this helps get you started... -Paul #include osg/LineWidth #include osg/Geometry #include osg/math #include math.h osg::Vec3Array* circleVerts( int plane, int approx ) { const double angle( osg::PI * 2. / (double) approx ); osg::Vec3Array* v = new osg::Vec3Array; int idx; for( idx=0; idxapprox; idx++) { double cosAngle = cos(idx*angle); double sinAngle = sin(idx*angle); double x(0.), y(0.), z(0.); switch (plane) { case 0: // X y = cosAngle; z = sinAngle; break; case 1: // Y x = cosAngle; z = sinAngle; break; case 2: // Z x = cosAngle; y = sinAngle; break; } v-push_back( osg::Vec3( x, y, z ) ); } return v; } osg::Geode* circles( int plane, int approx ) { osg::Geode* geode = new osg::Geode; osg::LineWidth* lw = new osg::LineWidth( 3. ); geode-getOrCreateStateSet()-setAttributeAndModes( lw, osg::StateAttribute::ON ); osg::Geometry* geom = new osg::Geometry; osg::Vec3Array* v = circleVerts( plane, approx ); geom-setVertexArray( v ); osg::Vec4Array* c = new osg::Vec4Array; c-push_back( osg::Vec4( 1., 1., 1., 1. ) ); geom-setColorArray( c ); geom-setColorBinding( osg::Geometry::BIND_OVERALL ); geom-addPrimitiveSet( new osg::DrawArrays( GL_LINE_LOOP, 0, approx ) ); geode-addDrawable( geom ); return geode; } ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Hey, Jean-Sébastien. Now I'm asking for that example code you've offered me, if that still stands... I haven't tried hard enough, to be honest, but I sure have no extra time to deal with this properly right now. Not to mention the guy I work with, which is breathing down my neck... I would appreciate that favor very much. Thanks. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
hi renan, i think he meant that you should look at the examples included in the osg package. quote: [..]and the examples that come with the OSG are a treasure trove of code and information. cheers, daniel Renan Mendes wrote: Hey, Jean-Sébastien. Now I'm asking for that example code you've offered me, if that still stands... I haven't tried hard enough, to be honest, but I sure have no extra time to deal with this properly right now. Not to mention the guy I work with, which is breathing down my neck... I would appreciate that favor very much. Thanks. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Hello Renan, Now I'm asking for that example code you've offered me, if that still stands... I haven't tried hard enough, to be honest, but I sure have no extra time to deal with this properly right now. Not to mention the guy I work with, which is breathing down my neck... I would appreciate that favor very much. As I said, for the part about feeding the data to OSG, there are good resources and I can't really step you through it, unless you give us the specific part you're having trouble with. On the other hand, if you're having trouble calculating the data itself, I attached a source file that might help for spheres and cylinders. Now, it's not a silver bullet, as it's pretty old, straight OpenGL code, and some of the comments are in French (you might use a translation web site for that ;-) ). But still, it may help you. In a nutshell, to construct a sphere, I first build a display list with 1/6 of a sphere, pointing in one direction (say positive X). Then, I call that display list 6 times with a rotation each time. The reason for doing it that way is to avoid the singularity at the poles of the sphere. For the cylinder, it's basically just making as many circles as needed, and then capping the tube. (come to think of it, I'm not even sure these cylinders were capped, as they were meant to represent a quadric...) In both cases, I guess the more complicated part is figuring out how to make faces out of the vertices, rather than calculating the position of the vertices themselves. So the code may help you out with that part. As I said, it's old code, so if you have trouble figuring it out, I'm afraid I won't be able to help much. But it may be what you need to nudge you in the right direction. Good luck, J-S -- __ Jean-Sebastien Guay [EMAIL PROTECTED] http://whitestar02.webhop.org/ This message was sent using IMP, the Internet Messaging Program. /*--*/ /* */ /**** INF4702 COMPLEMENT D'INFOGRAPHIE *** */ /*Travaux Pratiques */ /* */ /*fichier b_quad.cpp*/ /* */ /*Traitement des surfaces quadriques*/ /* */ /*Rev:07/01/2002y.m.*/ /* */ /*--*/ #includeclass.h #includeif540.h #includeVector3.h #includeQuaternion.h #includetexture.h #includetexture_image.h #includetexture_procedural.h using namespace math; using namespace std; /*--*/ /* */ /*PreProc(Pre-processing: differents calculs...)*/ /* */ /*Params:(void) */ /* */ /*Return:(void) */ /* */ /*Note: Cette methode est appelee apres la lecture des donnees et avant */ /* le calcul des images. C'est l'endroit ideal pour effectuer */ /* les transformations de la surface et calculer des constantes. */ /* */ /*--*/ #ifdefOPEN_GL extern General general; voidQuadric::PreProc( void ) { /* Matrix Q( this-quad.x,this-mix.z / 2, this-mix.y / 2, this-lin.x / 2, this-mix.z / 2, this-quad.y,this-mix.x / 2, this-lin.y / 2, this-mix.y / 2, this-mix.x / 2, this-quad.z,this-lin.z / 2, this-lin.x / 2, this-lin.y / 2, this-lin.z / 2, this-cst ); Matrix S = this-transfo; Matrix Q2 = !S * Q * ~(!S); this-quad.x = Q2[0][0]; this-quad.y = Q2[1][1]; this-quad.z = Q2[2][2]; this-cst= Q2[3][3]; this-mix.x = Q2[1][2] + Q2[2][1]; this-mix.y = Q2[2][0] + Q2[0][2]; this-mix.z = Q2[1][0] + Q2[0][1]; this-lin.x = Q2[0][3] +
Re: [osg-users] Creating shapes using Geometry
Hi, Jean-Sébastien. I've read the Tutorial and while compiling, there has been detected that I don't have the osg::DrawElementsUInt in my computer. Would you mind attaching this file in your next email. I really have no idea why it hasn't come along with the other library files... Well, besides that I've got another question. In the above-mentioned tutorial, there is a part of the example code in which the programmer uses the DrawElementsUInt class to somehow unite the group of points for each face. Will I need to instantiate one object from DrawElementUInt for each of the awful lot of faces I have in our approximation of a sphere? Is the a simpler way to do that? And what about changing the color of the shape, does it have to be as laborious as it's taught in that Tutorial? Thanks. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
hi, the class DrawElementsUInt is defined in the file osg/PrimitiveSet.cpp. hope this helps, daniel Renan Mendes wrote: Hi, Jean-Sébastien. I've read the Tutorial and while compiling, there has been detected that I don't have the osg::DrawElementsUInt in my computer. Would you mind attaching this file in your next email. I really have no idea why it hasn't come along with the other library files... Well, besides that I've got another question. In the above-mentioned tutorial, there is a part of the example code in which the programmer uses the DrawElementsUInt class to somehow unite the group of points for each face. Will I need to instantiate one object from DrawElementUInt for each of the awful lot of faces I have in our approximation of a sphere? Is the a simpler way to do that? And what about changing the color of the shape, does it have to be as laborious as it's taught in that Tutorial? Thanks. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include osg/PrimitiveSet #include osg/BufferObject #include osg/State #include osg/Notify using namespace osg; unsigned int PrimitiveSet::getNumPrimitives() const { switch(_mode) { case(POINTS): return getNumIndices(); case(LINES): return getNumIndices()/2; case(TRIANGLES): return getNumIndices()/3; case(QUADS): return getNumIndices()/4; case(LINE_STRIP): case(LINE_LOOP): case(TRIANGLE_STRIP): case(TRIANGLE_FAN): case(QUAD_STRIP): case(POLYGON): return 1; } return 0; } void DrawArrays::draw(State, bool) const { glDrawArrays(_mode,_first,_count); } void DrawArrays::accept(PrimitiveFunctor functor) const { functor.drawArrays(_mode,_first,_count); } void DrawArrays::accept(PrimitiveIndexFunctor functor) const { functor.drawArrays(_mode,_first,_count); } unsigned int DrawArrayLengths::getNumPrimitives() const { switch(_mode) { case(POINTS): return getNumIndices(); case(LINES): return getNumIndices()/2; case(TRIANGLES): return getNumIndices()/3; case(QUADS): return getNumIndices()/4; case(LINE_STRIP): case(LINE_LOOP): case(TRIANGLE_STRIP): case(TRIANGLE_FAN): case(QUAD_STRIP): case(POLYGON): return size(); } return 0; } void DrawArrayLengths::draw(State, bool) const { GLint first = _first; for(vector_type::const_iterator itr=begin(); itr!=end(); ++itr) { glDrawArrays(_mode,first,*itr); first += *itr; } } void DrawArrayLengths::accept(PrimitiveFunctor functor) const { GLint first = _first; for(vector_type::const_iterator itr=begin(); itr!=end(); ++itr) { functor.drawArrays(_mode,first,*itr); first += *itr; } } void DrawArrayLengths::accept(PrimitiveIndexFunctor functor) const { GLint first = _first; for(vector_type::const_iterator itr=begin(); itr!=end(); ++itr) { functor.drawArrays(_mode,first,*itr); first += *itr; } } unsigned int DrawArrayLengths::getNumIndices() const { unsigned int count = 0; for(vector_type::const_iterator itr=begin(); itr!=end(); ++itr) { count += *itr; } return count; } DrawElementsUByte::~DrawElementsUByte() { releaseGLObjects(); } void DrawElementsUByte::draw(State state, bool useVertexBufferObjects) const { if (useVertexBufferObjects) { const ElementBufferObject* ebo = getElementBufferObject(); state.bindElementBufferObject(ebo); if (ebo) { glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, getElementBufferObjectOffset()); } else { glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, front()); } } else { glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, front()); } } void DrawElementsUByte::accept(PrimitiveFunctor functor) const { if (!empty()) functor.drawElements(_mode,size(),front()); } void
Re: [osg-users] Creating shapes using Geometry
Hello Renan, I've read the Tutorial and while compiling, there has been detected that I don't have the osg::DrawElementsUInt in my computer. You should include osg/Geometry, that should bring osg::DrawElementsUInt. It's actually defined in osg/PrimitiveSet, but you shouldn't need to include that directly as you should be including osg/Geometry anyways. And it should definitely be part of your OSG distribution, or else lots of OSG examples won't compile. Well, besides that I've got another question. In the above-mentioned tutorial, there is a part of the example code in which the programmer uses the DrawElementsUInt class to somehow unite the group of points for each face. Will I need to instantiate one object from DrawElementUInt for each of the awful lot of faces I have in our approximation of a sphere? No, if you want your whole sphere to be a single list of faces you can have a single DrawElementsUInt (of type osg::PrimitiveSet::TRIANGLES) and each group of 3 indices will form a face. In the tutorial I think they wanted to show you can have multiple PrimitiveSets if you want, but they could have used one for the base (QUADS) and one for the sides (TRIANGLES, all four triangles together in the same PrimitiveSet) and it would have worked too. And what about changing the color of the shape, does it have to be as laborious as it's taught in that Tutorial? It all depends on what you want to do. The tutorial uses vertex coloring to assign a color to each vertex of the geometry. You can assign a single color to the whole geometry by doing: osg::Vec4Array* colors = new osg::Vec4Array; colors-push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); // white // ... pyramidGeometry-setColorArray(colors); pyramidGeometry-setColorBinding(osg::Geometry::BIND_OVERALL); And then you could change the appearance of your sphere by assigning a texture to it, or by giving it a material (see osg::Material). Again for that there are tutorials, and the examples that come with the OSG are a treasure trove of code and information. Good luck, J-S -- __ Jean-Sebastien Guay [EMAIL PROTECTED] http://whitestar02.webhop.org/ This message was sent using IMP, the Internet Messaging Program. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
I'll try and do it on my on, at least at first ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Hello Renan, Sorry that I sounded a bit desperate for quick answers, but I really appreciate the hint you've given me. I didn't think about using vectors, having the excuse that I haven't attended any Computer Graphics course. This project I'm participating in is from my Research Internship, and the software is only a small tool ina bigger picture. No problem. I sometimes assume if someone is using OSG then they must have some bases, which is not the case. Sorry about that. Have you made any progress? That offer for example code is still open if you're on a deadline, but as I said, trying to do it will probably be beneficial too. Good luck, J-S -- __ Jean-Sebastien Guay [EMAIL PROTECTED] http://whitestar02.webhop.org/ This message was sent using IMP, the Internet Messaging Program. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Hi, thanks for the interest in helping. But the thing is: I need these shapes to be created from the osg::Geometry class, as a matter of compatibility with another class. Do you know how to create the specific shapes I told you about (sphere, cylinder and box)? Thanks. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Hello Renan, But the thing is: I need these shapes to be created from the osg::Geometry class, as a matter of compatibility with another class. Do you know how to create the specific shapes I told you about (sphere, cylinder and box)? Your question could be two things: a) If you're asking how to specify vertex coordinates and indices in order to make a model in an osg::Geometry, then look at the tutorials that were mentioned. It's basically down to filling vertex and index arrays, and those tutorials have example code. You can also look at the osggeometry example. b) If you're asking how to calculate the appropriate vertices and indices to make a sphere, a cylinder or a box, then it's a purely mathematical exercise. For a box it's simple... For a sphere and a cylinder, there are a few ways, I urge you to try it yourself (hint: a bunch of vectors going in different directions around the origin, when normalized, will give a coarse or fine approximation of a circle, depending on how many vectors you used). If you're really in a hurry I'm sure I can dig up some old code, but really, it's an exercise that's given in any basic Computer Graphics course (and the old code I would give you is exactly that, the answers I gave to that exercise in my basic Computer Graphics course :-) ). You could also load up Blender, make a sphere or a cylinder, and export it to .osg with osgExporter (or any other format you can read - or any other modeling tool you have). But doing it mathematically is pretty easy too. Good luck, J-S -- __ Jean-Sebastien Guay [EMAIL PROTECTED] http://whitestar02.webhop.org/ This message was sent using IMP, the Internet Messaging Program. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Hi, You can take a look at the osg::ShapeDrawable and osg::Shape classes to draw simple objects such as spheres, cylinders, and boxes easily. For more complicated objects, you can build them using vertex arrays and osg::Geometry. Hope this helps. Will On Nov 21, 2007 4:58 PM, Renan Mendes [EMAIL PROTECTED] wrote: Hi, How do I create simple shapes, specifically spheres, cylinders and boxes using osg::Geometry? Can't find tutorials for that. Thanks. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Creating shapes using Geometry
Also I still use these tutorials to remind me.. even on 2.0 and onwards they work. http://www.nps.navy.mil/cs/sullivan/osgtutorials/ Hope that helps. Stephen. On Nov 22, 2007, at 6:58 AM, Renan Mendes wrote: Hi, How do I create simple shapes, specifically spheres, cylinders and boxes using osg::Geometry? Can't find tutorials for that. Thanks. Renan M Z Mendes ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org