Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
30 replies to this topic

#21 Nik02   Crossbones+   -  Reputation: 2631

Like
0Likes
Like

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


Sponsor:

#22 xDarkice   Members   -  Reputation: 112

Like
0Likes
Like

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.

#23 Nik02   Crossbones+   -  Reputation: 2631

Like
1Likes
Like

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


#24 Nik02   Crossbones+   -  Reputation: 2631

Like
1Likes
Like

Posted 12 January 2012 - 08:01 AM

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

Niko Suni


#25 xDarkice   Members   -  Reputation: 112

Like
0Likes
Like

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.LoadIdentity();
			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

#26 xDarkice   Members   -  Reputation: 112

Like
0Likes
Like

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?

#27 xDarkice   Members   -  Reputation: 112

Like
0Likes
Like

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?

#28 xDarkice   Members   -  Reputation: 112

Like
0Likes
Like

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);
			    _D3DTextures.Add(t);

		    }

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

#29 xDarkice   Members   -  Reputation: 112

Like
0Likes
Like

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?

Attached Thumbnails

  • clipping..PNG


#30 L. Spiro   Crossbones+   -  Reputation: 11939

Like
0Likes
Like

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#31 xDarkice   Members   -  Reputation: 112

Like
0Likes
Like

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.



PARTNERS