• Advertisement
Sign in to follow this  

Problem using panel control to render c#

This topic is 2646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i am trying to use a panel control to render my scene when i used a form to render i made the next to avoid watch the form blinking

this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.Opaque, true);



now that i am using a panel control to render, my panel is blinking and i can´t see my scene, i think that panel controls doesn´t have Setstyle method to make the same i did when i used a form so i don´t know how to solve this problem

Share this post


Link to post
Share on other sites
Advertisement
Yes you are right, now that i am trying to use that method in this way:

panel1.Setstyle

and i get this error:

it can´t get access to the protected member 'System.Windows.Forms.Control.Setstyle(System.Windows.Forms.Controlstyles, bool)'
using a certificate of the type 'System.Windows.Forms.Panel'; the certificate must be of the type 'MyTestProject.Form1' or derived from it.



What must i do?

Share this post


Link to post
Share on other sites
Since it's a protected method, the obvious answer would be to create a class derived from Panel.

Share this post


Link to post
Share on other sites
I have created a derived class from panel like this:


public class RenderPanel : System.Windows.Forms.Panel
{

}



Is it right? Now i would like to know if i can set the setstyle method infot this class or i must do it after i create an instance of this class?

Share this post


Link to post
Share on other sites
You can call it inside the class. Ex: put it in the constructor.

Share this post


Link to post
Share on other sites
If you try something and it doesn't work, I'll help you fix it, but I won't write it for you.

Share this post


Link to post
Share on other sites
what api? xna? directx?

i have used xna to render to a c# panel before and had the blinking you are refering to as well. I derived a class from it and override the OnPaint and OnPaintBackground and just did nothing in both of the classes. not even call the base class methods and it fixed my problem.

Share this post


Link to post
Share on other sites
i have made this:


public class RenderPanel : System.Windows.Forms.Panel
{
public RenderPanel():base()
{
base.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
}





And my renderloop function is this:

Here is the function:


public void PaintLoop(Control miObjeto)
{
myDevice.SetRenderState(RenderState.CullMode, true);
myDevice.SetRenderState(RenderState.FillMode, FillMode.Solid);
myDevice.SetRenderState(RenderState.Lighting, false);

myDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(Color.Gray), 1, 0);
myDevice.MultiplyTransform(TransformState.Projection,Matrix.PerspectiveFovLH(3.1415F/4,miObjeto.Width/miObjeto.Height,1.0F,500000F));
myDevice.MultiplyTransform(TransformState.View,Matrix.LookAtLH(new Vector3(0,170,70),new Vector3(0,170,0),new Vector3(0,1,0)));


myDevice.BeginScene();

myDevice.VertexFormat = VertexFormat.Position | VertexFormat.Texture1|VertexFormat.Diffuse;

for (int i = 0; i < slimdxMeshList.Count; i++)
{
slimdxMeshList.DrawSubset(0);
}

myDevice.EndScene();
myDevice.Present();

miObjeto.Invalidate();
}





Now my only doubt is that when i create the renderpanel into my form i have to made this:

renderPanel = new RenderPanel();
renderPanel.Parent = this;


Are there anyway to make into my renderpanel class definition has as Parent the form into which a new renderpanel instance is created?

[Edited by - jor1980 on October 24, 2010 12:22:50 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by jor1980
Are there anyway to make into my renderpanel class definition has as Parent the form into which a new renderpanel instance is created?


I have no clue what you just put there.

Why do you need a reference to the parent?

from where do you call the paintloop function ?

just override the OnPaint method and do all your drawing there.

override the OnPaintBackground (i think that is the name) and do absolutely nothing in that function.

from both of these methods that you override, do NOT call the base class version of it.

Not sure really why you are setting the style, i never had to do that to get it to work nicely.

Share this post


Link to post
Share on other sites
The reference to the parent i want is because the form in what i create this renderpanel is not in this namespace and to create the renderpanel i have to made this:


public Form1()
{
InitializeComponent();
renderPanel = new RenderPanel();
renderPanel.Parent = this;
}



If i don´t put renderPanel.Parent = this; the panel does not appear in the form, the only reason i wanted a reference to the parent is to avoid have to declare that line when i create a renderpanel in a form.

My final render panel class is this:


public class RenderPanel : System.Windows.Forms.Panel
{
public RenderPanel():base()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
Visible = true;

}

protected override void OnPaint(PaintEventArgs e)
{

this.Invalidate();
}
}





i call the paintLoop from another funtction when i want to start the render, and the render loop begins.

i set the style because if i don´t do it the render panel is blinking


Which will be the diferent between:


public RenderPanel():base()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
Visible = true;

}



and

public RenderPanel()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
Visible = true;

}

Share this post


Link to post
Share on other sites
I have decided to rebuild again all my RenderPanel class,but now if i put Invalidate into the OnPaint method y only see the background color of my device,
and the code makes a renderloop,and if i don´t put Invalidate into the OnPaint method i can see the meshes of my scene but it doesn´t make a render loop.

Here is the code of my RenderPanel class:

public class RenderPanel : System.Windows.Forms.Panel
{
Device myDevice;
Direct3D myDirect3D = new Direct3D();
List<Mesh> slimdxMeshList;

public RenderPanel()
{
Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);
Visible = true;
InitializeDevice();
}


public void InitializeDevice()
{
PresentParameters myPresentParameters = new PresentParameters();
myPresentParameters.Windowed = true;
myPresentParameters.SwapEffect = SwapEffect.Discard;
myPresentParameters.EnableAutoDepthStencil = true;
myPresentParameters.AutoDepthStencilFormat = Format.D16;

myDevice = new Device(myDirect3D, 0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, myPresentParameters);

}


protected override void OnPaint(PaintEventArgs e)
{

myDevice.SetRenderState(RenderState.CullMode, false);
myDevice.SetRenderState(RenderState.FillMode, FillMode.Solid);
myDevice.SetRenderState(RenderState.Lighting, true);
myDevice.SetRenderState(RenderState.Ambient, true);

Light myLight = new Light();
myLight.Type = LightType.Directional;
myLight.Diffuse = Color.White;
myLight.Direction = new Vector3(0, 170, 0);

myDevice.SetLight(0, myLight);
myDevice.EnableLight(0, true);
myDevice.SetRenderState(RenderState.Ambient, Color.White.ToArgb());


myDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(Color.Gray), 1, 0);
myDevice.MultiplyTransform(TransformState.Projection, Matrix.PerspectiveFovLH(3.1415F / 4, this.Width / this.Height, 1.0F, 500000F));
myDevice.MultiplyTransform(TransformState.View, Matrix.LookAtLH(new Vector3(0, 170, 70), new Vector3(0, 170, 0), new Vector3(0, 1, 0)));


myDevice.BeginScene();//todos los dibujos después de esta línea



for (int i = 0; i < slimdxMeshList.Count; i++)
{
slimdxMeshList.DrawSubset(0);
}

myDevice.EndScene();//todos los dibujos antes de esta línea
myDevice.Present();



this.Invalidate();
}


//create a mesh from vertex and faces lists
public Mesh CreateMesh(Raptor3D.CustomMesh myCustomMesh)
{
Mesh malla = new Mesh(myDevice, myCustomMesh.facesList.Count, myCustomMesh.numberOfVertex, MeshFlags.Managed, VertexFormat.Position | VertexFormat.Diffuse | VertexFormat.Texture1);
DataStream indexBuffer = malla.LockIndexBuffer(LockFlags.None);
DataStream vertexBuffer = malla.LockVertexBuffer(LockFlags.None);

for (int i = 0; i < myCustomMesh.numberOfVertex; i++)
{
vertexBuffer.Write<Vector3>(myCustomMesh.verticesList);
Color myColor = new Color();
myColor = Color.White;
vertexBuffer.Write<int>(myColor.ToArgb());
vertexBuffer.Write<Vector2>(myCustomMesh.uvList);
}


for (int i = 0; i < myCustomMesh.facesList.Count; i++)
{
ushort index;
index = (ushort)myCustomMesh.facesList.X;
indexBuffer.Write<ushort>(index);

index = (ushort)myCustomMesh.facesList.Y;
indexBuffer.Write<ushort>(index);

index = (ushort)myCustomMesh.facesList.Z;
indexBuffer.Write<ushort>(index);
}

ExtendedMaterial[] myExtendedMaterial = new ExtendedMaterial[1];
Material myMaterial = new Material();
myMaterial.Diffuse = Color.Aqua;
myMaterial.Specular = Color.LightGray;
myExtendedMaterial[0].MaterialD3D = myMaterial;
malla.SetMaterials(myExtendedMaterial);

//calcula las normales de la malla
malla = malla.Clone(myDevice, MeshFlags.Dynamic, VertexFormat.Position | VertexFormat.Normal | VertexFormat.Diffuse | VertexFormat.Texture1);
malla.ComputeNormals();
return malla;
}


//convierte todas las mallas de custommesh a slimdx mesh
public void CreateScene(List<Raptor3D.CustomMesh> customMeshList)
{
//inicializo la lista slimdxMesh
slimdxMeshList = new List<Mesh>();

for (int i = 0; i < customMeshList.Count; i++)
{
slimdxMeshList.Add(CreateMesh(customMeshList));

}
}


}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement