Discussion:
C# with directx viewport problem
(too old to reply)
Ricardo Furtado
2009-01-07 14:55:01 UTC
Permalink
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
legalize+ (Richard [Microsoft Direct3D MVP])
2009-01-08 00:55:18 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by Ricardo Furtado
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.
First, you should understand that Managed DirectX is not really
supported by Microsoft and is deprecated. You should consider using
SlimDX as a replacement.

Secondly, with Transformed vertices, the fixed-function vertex
processing (or vertex shader) doesn't come into play, so you have to
specify positions in screen space.

With Positioned vertices, the vertex processing *does* come into play,
so the positions have to be specified in some model coordinate system
that when processed by your vertex processing configuration results in
them being visible within the view frustum, and thus visible within the
viewport.

It sounds like you haven't configured the vertex processing properly and
your vertices don't have positions that fall into the default viewable
range of [-1,1].
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.com/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission.com/legalize/>
Ricardo Furtado
2009-01-08 08:15:01 UTC
Permalink
First of all, thank you for your answer

When you say
"the positions have to be specified in some model coordinate system
that when processed by your vertex processing configuration results in
them being visible within the view frustum"

i belive you are refering to the line:
graphics.Transform.View = Matrix.OrthoOffCenterLH(0, this.Width,
this.Height, 0, 10, -10);

in the
InitializeGraphics() function

to be honest i don't really know if this line is correct and if i'm using it
well. I used to use OpenGL, i know how to use glOtho or gluOrtho2d, but i'm
new to directx, so i don't really now how to configure my 3d space in DirectX.

best regards

Ricardo Furtado
Post by legalize+ (Richard [Microsoft Direct3D MVP])
[Please do not mail me a copy of your followup]
Post by Ricardo Furtado
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.
First, you should understand that Managed DirectX is not really
supported by Microsoft and is deprecated. You should consider using
SlimDX as a replacement.
Secondly, with Transformed vertices, the fixed-function vertex
processing (or vertex shader) doesn't come into play, so you have to
specify positions in screen space.
With Positioned vertices, the vertex processing *does* come into play,
so the positions have to be specified in some model coordinate system
that when processed by your vertex processing configuration results in
them being visible within the view frustum, and thus visible within the
viewport.
It sounds like you haven't configured the vertex processing properly and
your vertices don't have positions that fall into the default viewable
range of [-1,1].
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.com/~legalize/book/download/index.html>
Legalize Adulthood! <http://blogs.xmission.com/legalize/>
Ricardo Furtado
2009-01-08 09:24:19 UTC
Permalink
Ok, i've tested to clear the viewport with the color blue.
A rectangle is being drawn, but the texture is not being set. So the problem
seems to be the texture. So, in the
graphics.SetTexture(0, texture);

what is the first parameter? stage? what does this means?
I've changed this value, but still, doesn't work
Post by Ricardo Furtado
First of all, thank you for your answer
When you say
"the positions have to be specified in some model coordinate system
that when processed by your vertex processing configuration results in
them being visible within the view frustum"
graphics.Transform.View = Matrix.OrthoOffCenterLH(0, this.Width,
this.Height, 0, 10, -10);
in the
InitializeGraphics() function
to be honest i don't really know if this line is correct and if i'm using it
well. I used to use OpenGL, i know how to use glOtho or gluOrtho2d, but i'm
new to directx, so i don't really now how to configure my 3d space in DirectX.
best regards
Ricardo Furtado
Post by legalize+ (Richard [Microsoft Direct3D MVP])
[Please do not mail me a copy of your followup]
Post by Ricardo Furtado
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.
First, you should understand that Managed DirectX is not really
supported by Microsoft and is deprecated. You should consider using
SlimDX as a replacement.
Secondly, with Transformed vertices, the fixed-function vertex
processing (or vertex shader) doesn't come into play, so you have to
specify positions in screen space.
With Positioned vertices, the vertex processing *does* come into play,
so the positions have to be specified in some model coordinate system
that when processed by your vertex processing configuration results in
them being visible within the view frustum, and thus visible within the
viewport.
It sounds like you haven't configured the vertex processing properly and
your vertices don't have positions that fall into the default viewable
range of [-1,1].
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.com/~legalize/book/download/index.html>
Legalize Adulthood! <http://blogs.xmission.com/legalize/>
legalize+ (Richard [Microsoft Direct3D MVP])
2009-01-09 03:59:15 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by Ricardo Furtado
Ok, i've tested to clear the viewport with the color blue.
A rectangle is being drawn, but the texture is not being set. So the problem
seems to be the texture. So, in the
graphics.SetTexture(0, texture);
what is the first parameter? stage? what does this means?
I've changed this value, but still, doesn't work
Yes, its the stage number. Referring back to my pipeline diagram as I
mentioned in my other post, there are a maximum of 8 texture stages in
the fixed-function pipeline in D3D9.

Check how you're configuring lighting. Lighting is another element of
vertex processing that's ignored if you use transformed vertices. Try
disabling lighting altogether and see if your geometry is no longer
black.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.com/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission.com/legalize/>
legalize+ (Richard [Microsoft Direct3D MVP])
2009-01-09 03:57:14 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by Ricardo Furtado
First of all, thank you for your answer
When you say
"the positions have to be specified in some model coordinate system
that when processed by your vertex processing configuration results in
them being visible within the view frustum"
graphics.Transform.View = Matrix.OrthoOffCenterLH(0, this.Width,
this.Height, 0, 10, -10);
Well, vertex processing is more than just the View matrix. Take a look
at the dataflow poster I made showing the whole pipeline:
<http://www.xmission.com/~legalize/book/preview/poster/index.html>

Vertex processing is the portion of the pipeline before the block
labelled "primitive assembly".

Vertex processing is describes in Part II: Geometry Processing of my
book: <http://www.xmission.com/~legalize/book/download/>
Post by Ricardo Furtado
to be honest i don't really know if this line is correct and if i'm using it
well. I used to use OpenGL, i know how to use glOtho or gluOrtho2d, but i'm
new to directx, so i don't really now how to configure my 3d space in DirectX.
The main difference between OGL and D3D in terms of vertex processing is
that D3D uses a left-handed coordinate system, so Z increases into the
screen, not away from it. (OGL defaults to a right-handed coordinate
system.) Also, I think the view frustum is aligned with the Z axis
slightly differently, D3D uses [0,1], while I think OGL uses [-1,1]
range of Z in the default view frustum.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.com/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission.com/legalize/>
Loading...