Jump to content
  • Advertisement
Sign in to follow this  
trick

problem rendering rectangle using C++

This topic is 4235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am attempting to draw a rectangle using direct3d. The rectangle is actually 2D, but I want to draw it using 3d functions. The program compiles fine, and the screen does clear to the correct color (Blue). The problem is that my rectangle isn't being printed. Below I've listed the relevant code. Can anyone see what I'm doing wrong here??? CPP / C++ / C Code:
struct TemplateVertex{
	FLOAT x, y, z, rhw;
	//FLOAT u, v;
	DWORD color;
};

#define TEMPLATEFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) //D3DFVF_TEX1)

LPDIRECT3DVERTEXBUFFER9 buf;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;

void render(){
	if(NULL == pd3dDevice)
		return;
	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
	pd3dDevice->BeginScene();

//	TemplateInterface.render(pd3dDevice);
	pd3dDevice->SetStreamSource(0, buf, 0, sizeof(TemplateVertex));
	pd3dDevice->SetFVF(TEMPLATEFVF);
	pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
	pd3dDevice->EndScene();
	pd3dDevice->Present(NULL, NULL, NULL, NULL);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){
	float last_time = 0.0f;
	if(!initWindow(hInstance))
		return false;
	if(!initDirect3D())
		return false;

	MSG msg;
	ZeroMemory(&msg, sizeof(msg));
	timer.FindFreq();
//starting test code...
	TemplateVertex vex[] = {
		{100.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_ARGB(255, 255, 0, 0),},
		{100.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_ARGB(125, 0, 0, 0),},
		{300.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_ARGB(125, 0, 0, 0),},
		{300.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_ARGB(0, 255, 0, 0),},
	};
	VOID* pVertices;
	pd3dDevice->CreateVertexBuffer(4*sizeof(TemplateVertex), 0, TEMPLATEFVF, D3DPOOL_DEFAULT, &buf, NULL);
	buf->Lock(0, sizeof(vex), (void**)&pVertices, 0);
	memcpy(pVertices, vex, sizeof(vex));
	buf->Unlock();
	while(msg.message != WM_QUIT){
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}else{
			timer.Begin();
			update(last_time);
			render();
			last_time = timer.End();
		}
	}

	cleanup();
	return 0;
}

Thanks in advance, for anyone who might be able to help!

Share this post


Link to post
Share on other sites
Advertisement
You're using pretransformed vertices (the RHW FVF flag). This means you must specify a RHW component (you do) and that you must submit vertices in the range [-1,1] in X and Y (you don't), because pre-transformed vertices are multiplied by the RHW component you specify and expected to be in NDC space (which has the aforementioned range).

Your vertex coordinates are all far too large.

Note that I don't recommend the use of pre-transformed vertices; you can achieve the exact same results (but with more control over final appearances, eventually) by not using the RHW flag and simply setting the world, view and projection matrices to the identity matrix. This will allow shaders to execute on your submitted vertices, which can be great for tweaking and special effects.

Share this post


Link to post
Share on other sites
Thanks alot, I'll get on the adjustments now.

I do have one more question, regarding 3d-Space. With the world, view, and projection matrices set to identity, is the origin 0,0,0 at the lower-left corner of screen (with +Z heading away from camera, +X to the right, and +Y straight up the screen)?

Share this post


Link to post
Share on other sites
jpetrie could be right and I might be wrong on this, but your coords are just fine, if I remember correctly. D3DFVF_XYZRHW takes windows coordinates then transforms them into the (-1,1) range by dividing the W coord.

it seems to me that you are just culling the rectangle, try inserting
pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
if that works, flip the order of the verts.

again, my brain is not 100% today and I havent touched DX in over 6 months. so I could be wrong :)
-Dave

Share this post


Link to post
Share on other sites
This is an uncomfortable way to think of things. this is an excellent book on the subject. The DX SDK also has a passable explanation of transforms and spaces.

However, to answer your direct question, the origin would appear to be at the center of the screen. The edges would be -1 and 1. In D3D, you'd be "looking" down the +Z axis.

This is because the triangles that are eventually rendered must be in viewport space (e.g., [0,width] and [0,height] -- "pixel coordinates"). They get there via a transformation from NDC (normalize device coordinate) space, where the boundaries are [-1,1].

They get to NDC space via the perspective divide (by W), from clip space (which is the space you're in after application of the projection matrix).
They get to clip space via the projection matrix, from view space.
They get to view space via the view matrix, from world space. They get to world space via the world matrix, from object or model space.

So you can see that if these last three matrices are identity, and the w-component is 1, you're effectively "giving" the system vertices that are already in NDC space, which is why the "origin" appears to be in the center of the screen, et cetera.

Share this post


Link to post
Share on other sites
The CULL_MODE did fix the problem. apparently I got flipped around when I started typing up the vertices.

The end result of this project will remove the RHW, and the color. It will be running on a texture with an object I've made that takes a texture, and can split it up for various states of an object, and animate each state. The texture could have all images needed for many different objects on screen.

I couldn't get it to render (and now I know why) so I stripped all the extra stuff out and tried it with just color instead. (removing TEX1, adding DIFFUSE and RHW).

Now that I understand a little more about it, it's on to adding the texture back in and removing RHW. Thanks to all for your help!

The u,v coordinates, when using textures, specify the coordinates in the image to corespond to your vertices, right?

[Edited by - trick on February 13, 2007 10:48:44 AM]

Share this post


Link to post
Share on other sites
If all you want to do is render a textured quad without much hassle, I recommend ID3DXSprite. It's fast and very simple to use. It can render textured quads in both 3D and 2D, depending on what transformation matrix you set.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!