Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problem with width and height of quads


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
14 replies to this topic

#1 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 06 September 2012 - 10:56 AM

Hello dudes,

I've got a little problem, but maybe I am only too silly to solve it. unsure.pngbiggrin.png

I've got a window (1280x720) and a perspective from 0.01f till 1000.0f.
So, when I will render a normal quad with the width and height of the whole screen, I don't know where the vertices of the quad must be.
I try with (-1/-1), (-1/1), (1/-1), (1/1) and for each z-Axis coordinate 1 (I'm rendering with trianglestrips!), but then it is way too big! dry.png

So my question is, how I could know the true coordinates of each vertex for rendering a quad which is as big as the screen? wacko.png
Are there any mathematical tricks or something like that? mellow.png

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13317

Like
0Likes
Like

Posted 06 September 2012 - 11:20 AM

We are missing a lot of context here...

#3 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 06 September 2012 - 11:32 AM

I'm so sorry. I thought, it was enough information.

My plan is to make a little 3D Game like Little Big Planet.
I'm coding in C++ and DirectX 9 with Vertex- and Pixelshader 3.0
This is a part of my Initialisationcode:
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.hDeviceWindow    = hWnd;
d3dpp.SwapEffect    = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed	 = windowed;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.BackBufferFormat   = D3DFMT_R5G6B5;
d3dpp.BackBufferHeight   = height;
d3dpp.BackBufferWidth   = width;
d3dpp.MultiSampleType   = D3DMULTISAMPLE_NONE;
d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3d9Device);
D3DXMATRIXA16 projection;
D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI/4, (float)width / (float)height, 0.01f, 100.0f);
d3d9Device->SetTransform(D3DTS_PROJECTION,&projection);

If you miss other things, please tell me and I will edit this post. Posted Image
Also I excuse me for my terrible english... I'm from Germany and my english mark isn't so good... >.<

#4 Brother Bob   Moderators   -  Reputation: 8196

Like
0Likes
Like

Posted 06 September 2012 - 11:45 AM

Is there a specific reason you have to do this with a perspective projection? It becomes much easier to determine the necessary coordinates if you use an orthographic projection instead since you can directly construct a coordinate system that matches exactly what you expect and want.

#5 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 06 September 2012 - 12:01 PM

I'm not really sure, but I think I could remember from Blender, that perspective means, that you can see a wall, which is lightly inclined to the back, but in orthographic mode you couldn't see it. Or am I wrong? Posted Image
If so, could you explain me the difference between perspective and orthographic projection?
Because in Little Big Planet you also could see three dimensional objects through perspective projection, could you? Posted Image

#6 Brother Bob   Moderators   -  Reputation: 8196

Like
0Likes
Like

Posted 06 September 2012 - 12:30 PM

Perspective projection has perspective, orthographic projection does not. Perspective is how you see reality yourself; objects appear smaller by distance. Since you're probably quite familiar with perspective, you should look for orthographic projection specifically and see how they behave and are different from what you would expect reality to look like. I know this is for OpenGL, but you can have a look at the Red Book, chapter 3. The theory is universal and the functions maps pretty much directly to Direct3D equivalents. It discusses projections a bit along with general viewpoint transforms.

Since it appears that you have problems getting the coordinates correct due to the perspective effect, my question was basically; why not eliminate that factor in the first place and use a projection that doesn't have perspective?

#7 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 06 September 2012 - 12:55 PM

Thank you.
This is what I meant, I know it from the 3D modelling programm Blender. Posted Image
Maybe my question wasn't very explicit , so I try it once more:
How can I get the correct position of a vertex in perspective projection?

(Btw: Why I don't really want to use orthographic projection is the fact, that my "camera" will allways look from the front of the scene to the back. So you don't really see any 3D effect, due to the orthographic "viewmode". E.g. a crate where you have to jump over, isn't really a crate anymore but a simple quad. )

#8 Brother Bob   Moderators   -  Reputation: 8196

Like
0Likes
Like

Posted 06 September 2012 - 01:12 PM

Perhaps I'm not understanding what it is you want then. I thought from your first post that you were trying to display a quad the size of the screen, which to me suggested that you were trying to fit the quad perfectly onto the screen, and you were having problems getting the coordinates that corresponded to the corners of the screen.

Can you explain better what it is you're trying to do? What is the 3D effect you want from the quad? Why do you need screen-specific placement of objects that exists in a 3D world? The two spaces are typically used independently, but sometimes you have to go from 3D coordinates to screen coordinates, but rarely the other way around.

#9 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 06 September 2012 - 01:30 PM

Yeah, you're right. Posted Image
That the quad fits the screen perfectly was just an example to understand what I mean (because my english skill is so limited as you probably see...).

The 3D effect that I want is something like in this video (from 4:03 mins to 4:13 mins as example).
In orthographic projection it doesn't work very well to do so, so I must use perspective projection, or am I wrong? Posted Image
My specific problem is, when I will set up e.g. a crate (e.g for the beginning of the level), I must define, where it has to be. But without any coordinates, it doesn't work.
So I will learn, how to "find" the correct coordinates.

I hope, this time, you can understand what I mean... Posted Image
I excuse me for my bad english, again.

#10 Brother Bob   Moderators   -  Reputation: 8196

Like
0Likes
Like

Posted 06 September 2012 - 01:50 PM

I don't see why you would need screen coordinate for that and why you don't know where the crate is. Isn't the crate located somewhere within the scene, and isn't the crate just like any other 3D object in the 3D scene?

#11 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 06 September 2012 - 02:08 PM

I didn't really need screen coordinate. But I need 3D coordinates, which I don't have. Posted Image
My example was a quad which should perfectly fits the screen.
My screens size is 1280x720 pixels.
The vertices of my quad are A(-1/-1/10), B(1/-1/10), C(1/1/10) and D(-1/1/10) (for point(x/y/z)).
But it's too big (~3 times as big as my screen), so, which numbers I have to choose for x/y/z to have my quad as 1280x720 pixel?

#12 Brother Bob   Moderators   -  Reputation: 8196

Like
0Likes
Like

Posted 06 September 2012 - 02:30 PM

Never mind all these reasons, I guess I'm just confused what you really need here, so may just as well answer the question without asking for any reason :)

Assuming that you have a symmetric view volume, which you do if you don't know what a non-symmetric one is, then you can easily calculate the size of the "screen" at a certain depth:
width = tan(fov/2)*aspect*depth;
height = tan(fov/2)*depth;
Here, fov is the vertical field of view, aspect is the aspect ratio of the dimensions of the screen, and depth is the desired depth at which you want to calculate the coordinate range of the screen. The result is that the coordinates spans from -width to width in the horizontal direction, and from -height to height in the vertical direction.

So if you want the quad to span the entire screen, then the corners should be [-width, -height, depth], [width, -height, depth], [width, height, depth] and [-width, height, depth].

You can also calculate other coordinates based on fractions of the resolution and the width and height values of course. For example, if you want the quad to span the lower quadrant only, then draw from [-width, -height, depth] to [0, 0, depth].

#13 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 07 September 2012 - 06:18 AM

Thank you very much. That is just what I needed to know! Posted Image

How do I calculate the vertically fov ? My Code is now:
float width = tan((D3DX_PI/4)/2)*(float)(1280/720)*(1.0f);
float height = tan((D3DX_PI/4)/2)*(1.0f);
// Set up vertex buffer
D3DVERTEX quad[4] = {
	   {D3DXVECTOR3(-width, -height, -1.0f), D3DXVECTOR2(0.0f, 1.0f)},
	   {D3DXVECTOR3(-width,  height, -1.0f), D3DXVECTOR2(0.0f, 0.0f)},
	   {D3DXVECTOR3( width, -height, -1.0f), D3DXVECTOR2(1.0f, 1.0f)},
	   {D3DXVECTOR3( width,  height, -1.0f), D3DXVECTOR2(1.0f, 0.0f)},
	  };
It has the same height as the screen, but the width is too small to cover the whole screen.

Posted Image

Edited by N1ghtDr34m3r, 07 September 2012 - 07:29 AM.


#14 Brother Bob   Moderators   -  Reputation: 8196

Like
0Likes
Like

Posted 07 September 2012 - 07:09 AM

The vertical field of view is the same field of view parameter you pass to D3DXMatrixPerspectiveFovLH. My numbers should match the window exactly, ignoring the inherent floating point errors, so I cannot say why it doesn't completely span the whole screen.

However, since you were talking about perspective effects, one thing you should keep in mind is that the width and height values are only valid for the single depth value used to calculate them. If you're drawing, say, a cube (as in your crate example you also mentioned), then the cube extends in the depth direction as well. If you want the cube to span the whole window, you have to be clear on what part of the cube has to span the size of the screen; the front, the back or somewhere inside the the cube.

Likewise, if you have a perspective effect you also have varying depth on different places of the quad. If the cube is tilted backwards at the top, then the top of the quad is further away and the height range becomes larger; thus, the quad appears smaller unless you increase its height.

#15 N1ghtDr34m3r   Members   -  Reputation: 152

Like
0Likes
Like

Posted 07 September 2012 - 01:56 PM

D3DXMATRIXA16 projection;
D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI/4, (float)width / (float)height, 0.0f, 100.0f);
d3d9Device->SetTransform(D3DTS_PROJECTION,&projection);
Okay, so I have the same fov like in my Init(). Posted Image

Yeah, thanks for the tipp. Posted Image

EDIT:

I found the problem, namely that for the aspect I don't have to write
(float)(1280/720)
but
(1280.0f/720.0f)
. Posted Image

Now the quad fills the entire screen. Posted Image

Thank you for helping! Posted Image

Edited by N1ghtDr34m3r, 07 September 2012 - 02:14 PM.





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