View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Porting from OpenGL to Directx

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

30 replies to this topic

#21Nik02  Members

Posted 11 January 2012 - 01:02 AM

Yes, you can disable/enable mip lookup between every draw call. Each draw call uses the state that happens to be set before it.

Niko Suni

#22xDarkice  Members

Posted 11 January 2012 - 11:44 AM

Ok, I got this now. Can I force DirectX to use screen positions instead of values from 0 to 1 to set the Z-Positions and the vertexpositions much more precisely?

I think there could be some 'fighting' between the values when they are too near together. In OpenGL I can do this. Ideas?

Edit: I am trying to use this
Matrix.OrthoLH(Screenwidth, Screenheight, zNear, zFar);
This works, but if I am using screenpositions now for drawing, the textures are shifted to the mid of the screen. Basically, position 0,0 is the center of the screen, but I want it to be at the top left.

#23Nik02  Members

Posted 12 January 2012 - 07:59 AM

You could make a matrix that scales and biases coordinates from 0...screenwidth and 0...screenheight to -1...1. This is very simple math.

Niko Suni

#24Nik02  Members

Posted 12 January 2012 - 08:01 AM

Also, floating point precision is very sufficient for today's display resolutions.

Niko Suni

#25xDarkice  Members

Posted 13 January 2012 - 07:41 AM

I got almost everything now, some bugs are left. The biggest one is about resizing the window. In OpenGL I am doing this:

private void RResize()
{
h = control.Height;
w = control.Width;
y = 0;
x = 0;

if ((float)w / (float)h > CSettings.GetRenderAspect())
{
w = (int)Math.Round((float)h * CSettings.GetRenderAspect());
x = (control.Width - w) / 2;
}
else
{
h = (int)Math.Round((float)w / CSettings.GetRenderAspect());
y = (control.Height - h) / 2;
}
GL.MatrixMode(MatrixMode.Projection);
GL.Ortho(0, CSettings.iRenderW, CSettings.iRenderH, 0, (double)CSettings.zNear, (double)CSettings.zFar);
GL.Viewport(x, y, w, h);
}


For DirectX i was trying with:
private void RResize()
{
h = this.Height;
w = this.Width;
y = 0;
x = 0;

if ((float)w / (float)h > CSettings.GetRenderAspect())
{
w = (int)Math.Round((float)h * CSettings.GetRenderAspect());
x = (this.Width - w) / 2;
}
else
{
h = (int)Math.Round((float)w / CSettings.GetRenderAspect());
y = (this.Height - h) / 2;
}
if(_Run)
{
m_Device.Viewport=  new Viewport(x, y, w, h);
}
}


But I am getting
Direct3D9: (ERROR) :Viewport outside the render target surface

Another one is when I open up the task manager or a UAC window appears, my device is lost. I think I have to call Device.Reset(). After I disposed all objects, i need a way to recreate the textures which are lost in this process. I need help doing this

Edit2: I am still stuck at resizing my window with the correct aspect ratio. If I set the viewport like this it will go out of my window, it is shifted the right way though but the width and height is too big

#26xDarkice  Members

Posted 18 January 2012 - 12:47 PM

Can someone give me some tips to increase the performance, especially when much textures are drawn to the screen? I am including my whole source code for my D3D interface. Additionally, this does not work on Intel Graphicscards, I think I am using a mode that Intel graphics cant use, but due to the fact that I got no Intel Graphicscard here, I cant test it with debug runtimes. Does Intel provide a list of supported modes?

http://pastebin.com/QNHABBm5

Edit: I came up with an idea: Could I write all Vertices into a list, which is only rendered once per frame(in my MainLoop) and is discarded after? Is this the way not to lock the VertexBuffer several times a frame? Which size is common for a vertexbuffer? Right now I am using this size
4 * Marshal.SizeOf(typeof(TexturedColoredVertex)
, which is rather small i think. I heard I can draw several vertices a time, so i would be using a factor of my current size?

#27xDarkice  Members

Posted 21 January 2012 - 04:39 PM

I got the performance fixed now, but I am still having issues resizing my window.

Right now I am resizing my viewport but it seems as if the width and height parameters are ignored, the frame will shift by my x and y coordinates but it wont use the width and height i specified so that it is running out of my window.

Do I have to set the backbuffer width and height? Do I have to change something at my orthogonal projection or my translation matrix?

#28xDarkice  Members

Posted 25 January 2012 - 08:09 AM

Due to performance causes I need a new way of creating textures from a bitmap. I am currently using this, but due to the fact that I use Bitmap.Save it isnt that fast.

		    using (MemoryStream stream = new MemoryStream())
{
bmp2.Save(stream, ImageFormat.Png);
stream.Position = 0;
Texture t = Texture.FromStream(_Device, stream, 0, w, h, 0, Usage.None, Format.A8R8G8B8, Pool.Managed, Filter.Default, Filter.Default, 0);

}


Unfortunetely there is no Method for Texture.FromBitmap in SlimDx. Can someone help?

#29xDarkice  Members

Posted 27 January 2012 - 05:33 PM

I got a problem that the textures are clipped when they reach the edges of the viewport. I am creating my VertexBuffer with the DoNotClip Flag and I am setting the RenderState.Clipping to false. Am I missing another flag?

#30L. Spiro  Members

Posted 28 January 2012 - 07:17 AM

OpenGL interprets screen-space positions as the center of the pixel
DirectX interprets screen-space positions as the top-left corner of the pixel
(Or is it the other way round?)

Just because no one else mentioned this, OpenGL and Direct3D 10 assume center-points for pixels.
Direct3D 9, Direct3D 11, and GDI use pixel corners.

Right now I am resizing my viewport but it seems as if the width and height parameters are ignored, the frame will shift by my x and y coordinates but it wont use the width and height i specified so that it is running out of my window.

Do I have to set the backbuffer width and height? Do I have to change something at my orthogonal projection or my translation matrix?

If you are only setting the viewport on a resize, you need to also update your orthogonal matrix.

I got a problem that the textures are clipped when they reach the edges of the viewport. I am creating my VertexBuffer with the DoNotClip Flag and I am setting the RenderState.Clipping to false. Am I missing another flag?

I don’t see the problem.
The viewport should be the size of the screen. If anything goes out of the viewport it would be outside of the window and you would never know if it was clipped or not.
I see no clipping in your screen shot.

L. Spiro

#31xDarkice  Members

Posted 28 January 2012 - 07:29 AM

Thanks for your answer, please take a look at the snow at the bottom. The snow will lose one triangle of the quad. It only appears when I rotate these snow flocks. If they arent rotated they will display properly

By the way this is how I am roting a quad:

if (rect.Rotation != 0)
{
rect.Rotation = rect.Rotation * (float)Math.PI / 180;
float centerX = (rx1 + rx2) / 2f;
float centerY = -(ry1 + ry2) / 2f;

Matrix originTranslation = _Device.GetTransform(TransformState.World);
Matrix translationA = Matrix.Translation(-centerX, -centerY, 0);
Matrix rotation = Matrix.RotationZ(-rect.Rotation);
Matrix translationB = Matrix.Translation(centerX, centerY, 0);

Matrix result = translationA * rotation * translationB * originTranslation;

_Device.SetTransform(TransformState.World, result);
}


This is the full source code: http://pastebin.com/yDDuiZV1
I am happy for every improvement ideas =)

I think its the Orthographic Projection I'm using. If a Point of a Vertex would go over my Projection it wont show up I believe, but in OpenGL it does its really confusing and im stuck here

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.