Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


DeadMG

Member Since 01 Jan 2010
Offline Last Active May 10 2012 04:08 PM

Topics I've Started

Cannot compute wNear and wFar from projection matrix

01 April 2012 - 04:41 AM

I'm developing an application in Direct3D9, and the Debug runtime spews the titular error when I attempt to set my projection matrix. I've got the following code:

D3DXMatrixPerspectiveFovLH(
	&Projection,
	D3DXToRadian(90),
	(float)GetDimensions().x / (float)GetDimensions().y,
	NearPlane,
	FarPlane
);
D3DCALL(device->SetTransform(
	D3DTS_PROJECTION,
	&Projection
));


I checked in advance, and I have only one thread, the x and y values are 1920 and 1018 (the dimensions of the client area of my window), and the NearPlane is 0.1f and the FarPlane is 40.0f- I confirmed these values by breakpoint. The only times I've seen this error previously, and the only causes I could find via Google, were when the NearPlane is 0, which is not the case here.

Edit: Never mind, my own silly fault. I accidentally used a bad pointer to call this function.

Using D3DPT_LINELIST

30 March 2012 - 05:45 AM

I've got some code that renders some triangle lists in 3D. Here is my vertex declaration:

D3DVERTEXELEMENT9 BasicMeshVertices[] = {
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{1, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		{1, 16, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
		{1, 32, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
		{1, 48, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
		{1, 64, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
		D3DDECL_END()
	};

I use the code to pass the world matrix and colour to the shader per instance. This code works exactly how it should. However, when I attempt to use this to render a series of 3D lines using D3DPT_LINELIST, the D3D Debug Runtime tells me that my vertex declaration is wrong. I have a shader that works fine with this vertex input for the triangle list. Any suggestions as to what the problem could be? Here is my sample Draw code. The LineVerts are a simple {0, 0, 0} and {1, 0, 0} to represent a unit line on the X axis, and the indices are a simple {0, 1}.

	D3DCALL(device->SetVertexDeclaration(VertexDecl));
	D3DCALL(device->SetStreamSource(1, PerBoneBuffer.get(), 0, sizeof(PerInstanceData)));
	D3DCALL(device->SetStreamSourceFreq(1, D3DSTREAMSOURCE_INSTANCEDATA | 1));
	D3DCALL(device->SetStreamSource(0, LineVerts, 0, sizeof(D3DXVECTOR3)));
	D3DCALL(device->SetStreamSourceFreq(0, D3DSTREAMSOURCE_INDEXEDDATA | lines.size()));
	D3DCALL(device->SetIndices(LineIndices));
	PerInstanceData* data;
	std::vector<Wide::Render::Line*> lines_vec(lines.begin(), lines.end());
	D3DCALL(PerBoneBuffer->Lock(0, lines.size() * sizeof(PerInstanceData), reinterpret_cast<void**>(&data), D3DLOCK_DISCARD));
	std::for_each(lines.begin(), lines.end(), [&](Wide::Render::Line* ptr) {
		data->Color = D3DXColor(ptr->Colour);
		D3DXMATRIXA16 Translate, Scale, Rotate;
		D3DXMatrixTranslation(&Translate, ptr->Start.x, ptr->Start.y, ptr->Start.z);
		D3DXMatrixScaling(&Scale, ptr->Scale, 1, 1);
		D3DXMatrixRotationQuaternion(&Rotate, &D3DQuaternion(ptr->Rotation));
		data->World = Scale * Rotate * Translate;
	});
	D3DCALL(PerBoneBuffer->Unlock());
	D3DCALL(device->DrawIndexedPrimitive(D3DPRIMITIVETYPE::D3DPT_LINELIST, 0, 0, 2, 0, 1));

Depth buffer doesn't write in Direct3D9

09 March 2012 - 07:54 AM

As the title says, my application will not write new depths to the depth buffer in D3D9. I've used PIX to confirm that the depth buffer is cleared and set to the device as it should be, and the depth test is even used- if I clear it to 0, then all the pixels are rejected against buffer. I've checked the return code of all D3D9 functions and got nothing. I even explicitly set the render states in the shader to the appropriate values to no avail. Any suggestions?

In an unfortunately unrelated issue, the Direct3D debug runtime is not functioning correctly and I don't have it's output. I am trying to solve this issue as quickly as possible.

Camera rolling

09 March 2012 - 07:47 AM

I've asked about this before, but the topic appears to have gone doolally.

I have a camera floating in 3D space. I'm trying to implement a relatively simple system where the user moves the mouse and the camera rotates in that direction, and they can push the keyboard to move the camera's position, so allowing free view of whatever's in said 3D space.

Now I've got a small problem. The user can obviously only move the mouse in two dimensions- x and y, and the code only rotates around the x and y axis. To keep the rotations relative I also rotate all three axis by every rotation.

This works fine in the short term- the camera moves exactly as expected. However, once the user starts to rotate the camera often, the camera starts to slowly roll. You can move the mouse in a circle and exaggerate the effect to produce purely rolling. I wish to eliminate this issue. I've used quaternions to represent each rotation in a sequence. I attach some hopefully easy C++ code in case I have made a simple mistake.

    Math::AbsolutePoint previous;
    previous.x = 0;
    previous.y = 0;

    Math::Vector CameraXAxis = Math::Vector(1, 0, 0);
    Math::Vector CameraYAxis = Math::Vector(0, 1, 0);
    Math::Vector CameraZAxis = Math::Vector(0, 0, 1);
    auto RotateAxis = [&](Math::Quaternion q) {
	    CameraXAxis = CameraXAxis * q;
	    CameraYAxis = CameraYAxis * q;
	    CameraZAxis = CameraZAxis * q;
	    Scene3D->camera.LookingAt = CameraZAxis;
	    Scene3D->camera.Up = CameraYAxis;
    };
    window->OnKeyDown = [&](Wide::OS::Key k) {
	    switch(k) {
	    case OS::Key::DownArrow:
		    Scene3D->camera.Position -= CameraYAxis;
		    return;
	    case OS::Key::UpArrow:
		    Scene3D->camera.Position += CameraYAxis;
		    return;
	    case OS::Key::LeftArrow:
		    Scene3D->camera.Position -= CameraXAxis;
		    return;
	    case OS::Key::RightArrow:
		    Scene3D->camera.Position += CameraXAxis;
		    return;
	    }
    };
    window->OnMouseScroll = [&](int scroll, Math::AbsolutePoint position) {
	    if (scroll > 0)
		    Scene3D->camera.Position.z -= 1;
	    if (scroll < 0)
		    Scene3D->camera.Position.z += 1;
    };
    window->OnMouseMove = [&](Math::AbsolutePoint position) {
	    if (previous.x == 0) {
		    previous = position;
		    return;
	    }
	    Math::Quaternion addrotation;
	    if (previous.x > position.x) {
		    addrotation = Math::RotateAxis(3, CameraYAxis);
	    } else if (previous.x < position.x) {
		    addrotation = Math::RotateAxis(-3, CameraYAxis);
	    }
	    if (previous.y > position.y) {
		    addrotation = Math::RotateAxis(3, CameraXAxis);
	    } else if (previous.y < position.y) {
		    addrotation = Math::RotateAxis(-3, CameraXAxis);
	    }
	    RotateAxis(addrotation);
	    previous = position;
    };

Any suggestions?

Rendering silently failing with ID3DXSprite and ID3DXFont

18 January 2011 - 03:42 PM

I'm having some problems with ID3DXSprite and ID3DXFont where text will silently fail to render.

When two fonts are created, even when they are identical, rendering from the second font will silently fail. I'm running with Direct3D in full debug mode and it doesn't report any errors or warnings and I'm using a macro to check the return values of all Direct3D functions. The first font still works correctly (at least, notwithstanding the second issue). If the first font's creation is commented out, the second font will work correctly. I have fully re-entrant code, no global variables.
When I render more than one texture to the sprite, rendering will silently fail. This is most obvious because I have some animated buttons where the texture will change when moused over. Both textures are created beforehand. When mousing over, the texture changes and the text disappears. When the mouse leaves the button, the previous texture and the text re-appear. This is also completely silent from the Direct3D runtime.

I initially thought that it must be a problem with my use of ID3DXFont but when the second problem manifested, I'm looking more at ID3DXSprite. This is my main rendering code (the parts that relate to ID3DXSprite).

D3DCALL(D3DSprite->Begin(
				D3DXSPRITE_ALPHABLEND | 
				D3DXSPRITE_DO_NOT_ADDREF_TEXTURE | 
				D3DXSPRITE_SORT_DEPTH_BACKTOFRONT | 
				D3DXSPRITE_SORT_TEXTURE
			));
			decltype(GetPtrs<D3D9Sprite>()) sprites = GetPtrs<D3D9Sprite>();
			std::for_each(sprites.begin(), sprites.end(), [&, this](D3D9Sprite* sprite) {
				//D3DXVECTOR3 spritepos = sprite->position + sprite->center;
				if (!sprite->Visible())
					return;
				D3DCALL(D3DSprite->Draw(
					sprite->owner->D3DTexture.get(),
					nullptr,
					&sprite->center,
					&sprite->position,
					0xFFFFFFFF
				));
			});
			decltype(GetPtrs<D3D9Text>()) texts = GetPtrs<D3D9Text>();
			std::for_each(texts.begin(), texts.end(), [&, this](D3D9Text* ptr) {
				if (!ptr->Visible())
					return;
				RECT textbox = {0};			
				unsigned int format = 0;
				textbox.top = ptr->PositionY();
				textbox.left = ptr->PositionX();
				textbox.bottom = textbox.top + ptr->SizeY();
				textbox.right = textbox.left + ptr->SizeX();
				D3DCALL(ptr->owner->D3DXFont->DrawTextW(
					D3DSprite.get(),
					ptr->Text().c_str(),
					ptr->Text().size(),
					&textbox,
					format,
					ptr->Colour()
				));
			});
			decltype(GetPtrs<D3D9Button>()) buttons = GetPtrs<D3D9Button>();
			std::for_each(buttons.begin(), buttons.end(), [&, this](D3D9Button* button) {
				if (!button->Visible())
					return;
				D3DCALL(D3DSprite->Draw(
					button->owner->D3DTexture.get(),
					nullptr,
					&button->center,
					&button->position,
					0xFFFFFFFF
				));
				RECT textbox = {0};			
				unsigned int format = DT_CENTER | DT_VCENTER;
				textbox.top = button->PositionY() - (button->Texture()->SizeY() / 2);
				textbox.left = button->PositionX() - (button->Texture()->SizeX() / 2);
				textbox.bottom = button->PositionY() + (button->Texture()->SizeY() / 2);
				textbox.right = button->PositionX() + (button->Texture()->SizeX() / 2);
				D3DCALL(button->font->D3DXFont->DrawTextW(
					D3DSprite.get(),
					button->Text().c_str(),
					button->Text().size(),
					&textbox,
					format,
					button->Colour()
				));
			});
			D3DCALL(D3DSprite->End());


Just a note- I'm using a C++0x compiler so there may be some unfamiliar constructs in here like decltype and lambda functions. If any clarification is needed just ask.

PARTNERS