Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

DirectX in C# problem (viewport)

I'm developing an application with Visual Studio 2005, using C# and managed DirectX9.
My problem is that, in the application, i need several viewports (for now, i have just two), and one of then is not working.
The viewport that is working uses TransformedTextured vertexes, but the one that is not working uses PositionedTestured vertexes, once that i need to transform everything in this viewport.
I'm after this bug for several weeks.
One of the thing i thought that could be the problem is the World of the Microsoft.DirectX.Direct3D.Device.Transform.World. I thought that the current world could be other than the world 0, but i tryed all the others, and they didn't work either.
Can you help me, please?

Here is part of the code (i've selected what i think its important. hope everything realy important goes here):

public delegate void KeyFunction(Microsoft.DirectX.DirectInput.Key k);
KeyFunction press;
KeyFunction release;

const int divs=7;

const int perfilSuperior = 7;
const int pontosPerfilSuperior = (perfilSuperior-1) * divs;

static string gametitle = "GestCeph - Ajuste dos pontos e linhas";
static int screenwidth = 800; //800
static int screenheight = 600; //600
static bool windowed =true;
static bool graphicslost = false;
static Timer gametimer = null;
static bool paused = false;
float m_bound_image_height = 0;
float m_bound_image_width = 0;
float pos_left=0;
float pos_top=0;
int the_Z=0;


private frmTools m_Tools;


Direct3D.Device graphics = null;
Direct3D.Viewport m_Viewport;
Direct3D.Viewport m_Viewport_amplificado;
DirectSound.Device sound = null;
DirectInput.Device keyboard = null;
DirectInput.Device mouse = null;
DirectInput.Device gameinput = null;
Direct3D.Mesh mesh = null;
Direct3D.Material mtrl;
private String m_Connection;


comunication clsGestCeph;
Imagem m_imagem;

bool m_tenho_imagem = false;
bool m_desenha_perfil_superior = false;
bool m_hold_point=false; //true se o mouse estiver a "segurar" um ponto
bool bool_tmp;


float Medicao_Actual;
float Medicao_Real = 100;
//float zPos = 10;
int contour_color = Color.Red.ToArgb(); //Color.BlueViolet.ToArgb();
int count_pontos = 0;
int m_camera_Z_pos = 0;
Int32 px, py, pz;
String m_cur_img;
String m_str_selected="";
Point defPnt;
Point ptnAjuda;
PointF temp_pointf;
Vector3 m_rato;
//Vector3 m_camera_pos_viewport_amplificado;
//Vector3 m_camera_up_vector_viewport_amplificado;

Microsoft.DirectX.DirectInput.Key[] lastpressed = new Microsoft.DirectX.DirectInput.Key[0];



Hashtable m_pontos_coordenadas;
Hashtable m_pontos_nome;
Hashtable m_pontos_abreviatura;
Hashtable m_pontos_bezier_left;
Hashtable m_pontos_bezier_right;

// --------------------------------------------------------------------
// Resources
// --------------------------------------------------------------------
Font windowsfont = null;
Direct3D.Font dxfont = null;


// --------------------------------------------------------------------
// Geometry
// --------------------------------------------------------------------
/
Direct3D.CustomVertex.TransformedTextured[] vertexes_mini_ampliado = null; //The one that works_________________

Direct3D.CustomVertex.PositionColored[] vertexColored = null;
Direct3D.CustomVertex.PositionColored[] lineColored = null;
Direct3D.CustomVertex.PositionTextured[] vertexes = null; //The one that doesnt work_________________

Direct3D.CustomVertex.PositionTextured[] vertexes_rect_points = null;

Direct3D.Texture texture = null;
Direct3D.Texture point_texture = null;
Direct3D.Texture mini_texture = null;




// Form
public frmImagem()
{

m_Viewport = new Direct3D.Viewport();
m_Viewport.Height = screenheight;
m_Viewport.Width = screenwidth - 150;
m_Viewport.X = 0;
m_Viewport.Y = 0;
m_Viewport.MaxZ = 0;
m_Viewport.MinZ = 10;

m_Viewport_amplificado = new Direct3D.Viewport();
m_Viewport_amplificado.Height = 200;
m_Viewport_amplificado.Width = 150;
m_Viewport_amplificado.X = screenwidth - 150;
m_Viewport_amplificado.Y = 401;
m_Viewport_amplificado.MaxZ = 0;
m_Viewport_amplificado.MinZ = 10;


Int32 lRet;
this.Left = 0;
this.Top = 0;
lRet = Win32.SetCursorPos(0, 0);
defPnt = new Point();


ptnAjuda=this.PointToScreen(new Point(1,1));

//if novo
m_pontos_coordenadas=new Hashtable();
m_pontos_nome = new Hashtable();
m_pontos_abreviatura = new Hashtable();
m_pontos_bezier_left = new Hashtable();
m_pontos_bezier_right = new Hashtable();


m_imagem = new Imagem();
clsGestCeph=new comunication();
frmImagem m_form;
m_form = this;

readXML();
m_Tools = new frmTools(ref m_form);
m_Tools.Show(this);
m_Tools.TopMost = true;

m_imagem.clicked_change_image += new ClickQuerNovaImagem(this.onChanging_Imagem2);


ClientSize = new System.Drawing.Size(screenwidth, screenheight);
Text = gametitle;
gametimer = new Timer();
}



// changes the image in every viewport
public void onChanging_Imagem(object source, EventArgs e,String the_image)
{
m_cur_img = the_image;
m_tenho_imagem = true;
InitializeGeometry();
this.Show();

}



public void InitializeGraphics()
{
// set up the parameters
Direct3D.PresentParameters p = new Direct3D.PresentParameters();
p.SwapEffect = Direct3D.SwapEffect.Discard;



if (windowed == true)
{
p.Windowed = true;
}
else
{
// get the current display mode:
Direct3D.Format current = Direct3D.Manager.Adapters[0].CurrentDisplayMode.Format;

// set up a fullscreen display device
p.Windowed = false; // fullscreen
p.BackBufferFormat = Direct3D.Format.R5G6B5; //current; // use current format
p.BackBufferWidth = screenwidth;
p.BackBufferHeight = screenheight;
}

p.EnableAutoDepthStencil = true;
p.AutoDepthStencilFormat = Direct3D.DepthFormat.D16;

// create a new device:
graphics = new Direct3D.Device(0, Direct3D.DeviceType.Hardware, this, Direct3D.CreateFlags.SoftwareVertexProcessing, p);


// Setup the event handlers for the device
graphics.DeviceLost += new EventHandler(this.InvalidateDeviceObjects);
graphics.DeviceReset += new EventHandler(this.RestoreDeviceObjects);
graphics.Disposing += new EventHandler(this.DeleteDeviceObjects);
graphics.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);

m_imagem.changed_Image += new ClickImagemAlterada(this.onChanging_Imagem);

graphics.Transform.View = Matrix.OrthoOffCenterLH(0, this.Width, this.Height, 0, 10, -10);
// set up various drawing options
graphics.RenderState.CullMode = Direct3D.Cull.None;
graphics.RenderState.AlphaBlendEnable = true;
graphics.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Add;
graphics.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha;
graphics.RenderState.SourceBlend = Direct3D.Blend.SourceAlpha;


mesh = Direct3D.Mesh.Teapot(graphics);
mtrl = new Direct3D.Material();
mtrl.Diffuse = Color.White;
graphics.Material = mtrl;

}


//Creates vertexes
public void InitializeGeometry()
{


vertexes_mini_ampliado = new Direct3D.CustomVertex.TransformedTextured[4];


vertexes = new Direct3D.CustomVertex.PositionTextured [4];

vertexColored = new Direct3D.CustomVertex.PositionColored[m_pontos_coordenadas.Count];


vertexes_rect_points = new Direct3D.CustomVertex.PositionTextured[4];

Vector3 v;
PointF tmp_point;
int internal_counter;
int contador=1;



foreach (Int32 key in m_pontos_coordenadas.Keys)
{
tmp_point = (PointF)m_pontos_coordenadas[key];

internal_counter = contador - 1;

v = new Vector3(tmp_point.X, tmp_point.Y, 0);

vertexColored[internal_counter].SetPosition(v);

vertexColored[internal_counter].Color = Color.Red.ToArgb();




contador++;
}


vertexes[0].X = m_Viewport.X;
vertexes[0].Y = m_Viewport.Y;
vertexes[0].Z = 0.0f;
vertexes[0].Tu = 0.0f;
vertexes[0].Tv = 0.0f;


vertexes[1].X =(float)m_imagem.get_ImagemWidth();
m_bound_image_width = vertexes[1].X;
vertexes[1].Y = m_Viewport.Y;
vertexes[1].Z = 0.0f;
vertexes[1].Tu = 1.0f;
vertexes[1].Tv = 0.0f;


vertexes[2].X = m_Viewport.X;
vertexes[2].Y = (float)m_imagem.get_ImagemHeight();
m_bound_image_height = vertexes[2].Y;
vertexes[2].Z = 0.0f;
vertexes[2].Tu = 0.0f;
vertexes[2].Tv = 1.0f;

vertexes[3].X = (float)m_imagem.get_ImagemWidth();
vertexes[3].Y = (float)m_imagem.get_ImagemHeight();
vertexes[3].Z = 0.0f;
vertexes[3].Tu = 1.0f;
vertexes[3].Tv = 1.0f;




vertexes_mini_ampliado[0].X = m_Viewport_amplificado.X;
vertexes_mini_ampliado[0].Y = m_Viewport_amplificado.Y;
vertexes_mini_ampliado[0].Z = 0;
vertexes_mini_ampliado[0].Tu = 0.0f;
vertexes_mini_ampliado[0].Tv = 0.0f;


vertexes_mini_ampliado[1].X =m_Viewport_amplificado.X+ m_Viewport_amplificado.Width; //(float)m_imagem.get_ImagemWidth();
vertexes_mini_ampliado[1].Y = m_Viewport_amplificado.Y;
vertexes_mini_ampliado[1].Z = 0;
vertexes_mini_ampliado[1].Tu = 1.0f;
vertexes_mini_ampliado[1].Tv = 0.0f;


vertexes_mini_ampliado[2].X = m_Viewport_amplificado.X;
vertexes_mini_ampliado[2].Y =m_Viewport_amplificado.Y+ (m_Viewport_amplificado.Height-1); //(float)m_imagem.get_ImagemHeight();
vertexes_mini_ampliado[2].Z = 0;
vertexes_mini_ampliado[2].Tu = 0.0f;
vertexes_mini_ampliado[2].Tv = 1.0f;

vertexes_mini_ampliado[3].X =m_Viewport_amplificado.X + m_Viewport_amplificado.Width; // (float)m_imagem.get_ImagemWidth();
vertexes_mini_ampliado[3].Y = m_Viewport_amplificado.Y + (m_Viewport_amplificado.Height - 1); // (float)m_imagem.get_ImagemHeight();
vertexes_mini_ampliado[3].Z = 0;
vertexes_mini_ampliado[3].Tu = 1.0f;
vertexes_mini_ampliado[3].Tv = 1.0f;

//----------------------------------------------------------------


if (m_tenho_imagem)
{
texture = Direct3D.TextureLoader.FromFile(
graphics, m_cur_img, 0, 0, 0, 0, Direct3D.Format.Unknown,
Direct3D.Pool.Managed, Direct3D.Filter.Linear,
Direct3D.Filter.Linear, 0);
}
else
{
texture = Direct3D.TextureLoader.FromFile(
graphics, "texture.jpg", 0, 0, 0, 0, Direct3D.Format.Unknown,
Direct3D.Pool.Managed, Direct3D.Filter.Linear,
Direct3D.Filter.Linear, 0);
}
}




// --------------------------------------------------------------------
// Render the current game screen
// --------------------------------------------------------------------
protected virtual void Render()
{

if (graphics != null)
{
// check to see if the device has been lost. If so, try to get
// it back.
if (graphicslost)
{
try
{
graphics.TestCooperativeLevel();
}
catch (Direct3D.DeviceLostException)
{
// device cannot be reaquired yet, just return
return;
}
catch (Direct3D.DeviceNotResetException)
{
// device has not been reset, but it can be reaquired now

graphics.Reset(graphics.PresentationParameters);
}
graphicslost = false;
}



try
{

graphics.Viewport = m_Viewport;
graphics.Clear(Direct3D.ClearFlags.Target | Direct3D.ClearFlags.ZBuffer, Color.Black, 1, 0);
graphics.BeginScene();

graphics.SetTexture(0, texture);


graphics.VertexFormat = Direct3D.CustomVertex.PositionTextured.Format;

if (m_tenho_imagem)
{
graphics.DrawUserPrimitives(Direct3D.PrimitiveType.TriangleList, 2, vertexes);
}




graphics.EndScene();
graphics.Present();



//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//--------------------- Viewport Amplificado ---------------------
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


graphics.Viewport = m_Viewport_amplificado;


graphics.Clear(Direct3D.ClearFlags.Target | Direct3D.ClearFlags.ZBuffer, Color.Black, 1, 0);



graphics.BeginScene();

graphics.SetTexture(0,texture);

graphics.VertexFormat = Direct3D.CustomVertex.TransformedTextured.Format;
if (m_tenho_imagem)
{
graphics.DrawUserPrimitives(Direct3D.PrimitiveType.TriangleStrip, 2, vertexes_mini_ampliado);
//graphics.Viewport = m_Viewport;

}
graphics.EndScene();
graphics.Present();
}

// device has been lost, and it cannot be re-initialized yet
catch (Direct3D.DeviceLostException)
{
graphicslost = true;
}

}
}



// --------------------------------------------------------------------
// Run the game
// --------------------------------------------------------------------
public void Run()
{
// reset the game timer
gametimer.Reset();

// loop while form is valid
while (this.Created)
{


// process one frame of the game
SetupCamera();
ProcessFrame();
SetupLights();



// render the current scene
Render();

// handle all events
Application.DoEvents();
}
}


My thanks in advanced




Sign In or Register to comment.