How to create rotating effects and change the colour in SceneControl?

Apr 24, 2012 at 5:00 PM

Hello! I have doubts on how to make the polygon in SceneControl rotate. I also don't know how to change the colour of polygon. I'll appreciate your answer!

Apr 25, 2012 at 3:27 AM

For rotation, I recommend taking a look at the source code for the PolygonLoadingSample.  It automatically rotates all located polygons by a degree every time a frame is drawn.  I've pasted the relevant snippet below. 

With respect to setting polygon color, there doesn't currently seem to be a good way to do so if you're using the SharpGL SceneGraph.  Personally, I've added a Material property to the SceneElement class and modified Polygon.Render so it uses the material I've set.  However, this only works when lighting is enabled.

 

        private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e)
        {
            //  Get the OpenGL object, for quick access.
            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

            //  Clear and load the identity.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            gl.LoadIdentity();

            //  View from a bit away the y axis and a few units above the ground.
            gl.LookAt(-10, 10, -10, 0, 0, 0, 0, 1, 0);

            //  Rotate the objects every cycle.
            gl.Rotate(rotate, 0.0f, 0.0f, 1.0f);

            //  Move the objects down a bit so that they fit in the screen better.
            gl.Translate(0, 0, -1);
            
            //  Draw every polygon in the collection.
            foreach (Polygon polygon in polygons)
            {
                polygon.PushObjectSpace(gl);
                polygon.Render(gl, SharpGL.SceneGraph.Core.RenderMode.Render);
                polygon.PopObjectSpace(gl);
            }
            //  Rotate a bit more each cycle.
            rotate += 1.0f;
        }


        float rotate = 0;


Coordinator
Apr 25, 2012 at 9:46 AM

Hi Guys,

First, Sean, thanks for posting this sample - secondly I'd like to incorporate what you've done with materials to allow scene objects to be painted with materials. In keeping with the current architecture, as not all Scene Objects can have materials (e.g. lights) I was thinking about the below:

public interface IHasMaterial
{
    Material Material { get; set; }
}

Or something along these lines. Under the hood I'd like the material to be stored in the Scene Assets so that a single instance of a material can be shared across items, and they can be serialized with the object just storing the Asset ID.

Any thoughts?

Apr 26, 2012 at 12:39 AM

I think you've got a very good point about using a interface so only nodes in the scene graph that actually have materials have a material available. 

With regards to storing only a single instance of each asset, I think you've gone far toward accomplishing that by giving the Scene class its collection of Assets.  I think one advantage to implementing the IHasMaterial interface with an object reference to a Material versus just a string or int with the ID of the asset is that you can make use of instances of SceneGraph classes independently of scene that way.  As long as the object reference is to the instance of the Material in the Assets collection, it shouldn't make much difference in terms of memory use. 

With regards to making serialization/deserialization easier, perhaps the Material property should be set to XmlIgnore, and an AssetID property could be added. When getting AssetID, it would merely pull the ID of the Material.  When AssetID is set, it sets the Material property to an object reference to the identified instance of a Material in the Scene's Assets collection.  Just thinking aloud.

Later this evening I'll send you what I've got so far via the "Submit a patch" feature.  You can evaluate it and use what you like, if anything.

 

Apr 26, 2012 at 8:51 AM

Thank you very much for you answer, but my question is how to rotate a polygon in sceneControl , instead of in openGLControl.

Coordinator
Apr 26, 2012 at 12:28 PM

Hi Sean,

I agree with your thoughts-  as you have said it'd be great to keep the material property in their, XmlIgnore it and serialize the ID, then let the scene re-set the property on deserialization. I'd really like to get this IHasMaterial implementation in ASAP and get materials/textures working - especially if you've got code to properly load materials from OBJ files. There's a couple of other TODOs in the task list around this area. Let me know when you've submitted the patch and we'll take it from there :)

 

Hi Raulrunner,

For a polygon, use:

polygon.LinearTransformation.RotateX += 10.0;

for example to rotate it :)

Apr 26, 2012 at 2:47 PM
Raulrunner wrote:

Thank you very much for you answer, but my question is how to rotate a polygon in sceneControl , instead of in openGLControl.

 

Ralrunner,

You can follow a similar technique with SceneControl.  Hold the degree of rotation in a private variable.  In an event handler method for either the Elapsed event of a Timer or the OpenGLDraw event of the the SceneControl, increment the rotation variable.  Then update the relevant rotation properties of the Transformation property of the elements in the scene graph.

 

 

Apr 26, 2012 at 2:51 PM
dwmkerr wrote:

Hi Sean,

I agree with your thoughts-  as you have said it'd be great to keep the material property in their, XmlIgnore it and serialize the ID, then let the scene re-set the property on deserialization. I'd really like to get this IHasMaterial implementation in ASAP and get materials/textures working - especially if you've got code to properly load materials from OBJ files. There's a couple of other TODOs in the task list around this area. Let me know when you've submitted the patch and we'll take it from there :)


Hi dwmkerr,

I uploaded the source code via the "Submit a patch" function last night.  But I guess you realize that, because I just noticed the changeset on the Source Code page.  :-)

Jul 9, 2012 at 9:47 PM

Hi Guys, 

Sorry the color part of the answer is unclear to me...

I am using Polygons in a SceneControl, I would like to set a specific color for each of them (r,g,b,a), is it doable? How to proceed?

Thanks!