• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Drawing coordinates system axes in corner of screen

4 posts in this topic

Hi! I'd like to start off saying i don't have any programming training and very few knowledge, but i have messed around with several programming languages, designing tools to aid my everyday job (so i am a total amateur).


This time i'm working on something harder - it's a direct3d - mfc software and i am having lots of problems with it, since directx seems a bit hard.


Anyways... I have a direct3d environment in a mfc window and i would like to draw the coordinate system axes in the corner of the screen, as any 3d software has. I thought this would be no problem, since i found this topic, and that worked great but problems  occurred  when i started moving the camera around. I need the object to appear to be in the same spot, no matter how i pan, zoom or rotate the world (actually, i do these operations on the view matrix).


But it seems that i'm doing something wrong, and i was hoping someone could point me on the right direction, since the object i'm drawing is not scaled accordingly when i zoom, but it works perfect when panning or rotating.


I have also put up a youtube video to show you the symptoms.


This is my code for drawing the object:


void CDEMView::DrawSomeBox()
	// Define the needed matrices - object world, view and project 
	D3DXMATRIX matObjectWorld;
        D3DXMatrixIdentity (&matObjectWorld);	// object world matrix
	D3DXMATRIX matView;				
        D3DXMatrixIdentity (&matView);		// view matrix
	D3DXMATRIX matProjection;	
        D3DXMatrixIdentity (&matProjection);	// projection matrix
	// Get the needed matrices
	_device->GetTransform(D3DTS_VIEW, &matView);
	_device->GetTransform(D3DTS_PROJECTION, &matProjection);

	// Get the viewport
	D3DVIEWPORT9 viewport;

	// Get the center point of the object		
	D3DXVECTOR3* p_centerPoint = BoxCenterVector; // this is from an external variable

	// Get the point on the creen that is the screen projection of the object
	D3DXVECTOR3 projectPoint;
	D3DXVec3Project(&projectPoint, p_centerPoint ,&viewport, &matProjection, &matView, &matObjectWorld);

	// choose the screen point where the object is to be drawn, relative to the Viewport's dimensions
	D3DXVECTOR3 screenPoint;
	screenPoint.x = 0.1*viewport.Width;	// x position (horizontal) is 10% of the width of the screen (0% is left, 100% is right)
	screenPoint.y = 0.9*viewport.Height;	// y position (vertical) is 90% of the height of the screen (0% is top, 100% is bottom)
	screenPoint.z = projectPoint.z;		// 1-projectPoint.z*60/(-zoom);

	//transform the screen position to a world position
	D3DXVECTOR3 worldPoint;
	D3DXVec3Unproject( &worldPoint, &screenPoint, &viewport, &matProjection, &matView, &matObjectWorld );

	// now define how much to translate the box in order to get it to the point we want it to be (WorldPoint)
	float transX, transY, transZ;
	transX = worldPoint.x;
	transY = worldPoint.y;
	transZ = worldPoint.z;
	// define a mesh to store the object into and create the object
	ID3DXMesh* _SomeBox;
	float boxSize = 2.0f;

	// define a material and set its color

	// Set the RGBA for diffuse reflection.
	mat.Diffuse.r = 255;
	mat.Diffuse.g = 0;
	mat.Diffuse.b = 0;
	mat.Diffuse.a = 0.5;


	// apply the translation matrix
	D3DXMatrixTranslation(&matObjectWorld, transX, transY, transZ);
	_device->SetTransform(D3DTS_WORLD, &matObjectWorld);

	// draw the object
	// release the mesh

	// some debugging variables
	debug1 = transX;
	debug2 = transY;
	debug3 = transZ;
	debug4 = boxSize;
	//debug5 = scaleFactor;
	manole = 0;


Any help would be greatly appreciated. Thanks!


Edit: made a small change to the code to reflect my software.

Edited by adi0149

Share this post

Link to post
Share on other sites

To draw the red cube:

#1: Set D3DTS_VIEW back to identity.

#2: Create an orthographic projection matrix.

#3: Set the scale of the cube to x = (1.0f / ScreenWidth), y = (1.0f / ScreenHeight).

#4: Set the rotation of the cube to equal that of the rotation of the world-view matrix that you used to render the first time (copy the 3×3 part of the world-view matrix).

#5: Position the cube in the lower-left of the screen (screen coordinates).

#6: Profit.



L. Spiro


Share this post

Link to post
Share on other sites

Thanks for the reply!


I  applied  your changes as follows:


... everything is unchanged up to the point where i found transX, transY, transZ. Now it follows:


        // #1: Set D3DTS_VIEW back to identity.
        D3DXMATRIX matView2; D3DXMatrixIdentity (&matView2);

        // #2: Create an orthographic projection matrix.
	D3DXMatrixOrthoLH( &P, viewport.Width/20, viewport.Height/20, 1.0f, 100.0f );
	_device->SetTransform(D3DTS_PROJECTION, &P);

	// #3: Set the scale of the cube to x = (1.0f / ScreenWidth), y = (1.0f / ScreenHeight).
	D3DXMATRIX mScale; D3DXMatrixIdentity(&mScale);
	D3DXMatrixScaling( &mScale, (1.0f / viewport.Width), (1.0f / viewport.Height), 1 );

	D3DXMatrixMultiply( &matObjectWorld, &matObjectWorld, &mScale );

	// #4: Set the rotation of the cube to equal that of the rotation of the world-view matrix that you used to render the first time (copy the 3×3 part of the world-view matrix).
	for (int i=1; i<4; i++)
		for (int j=1; j<4; j++)
			matView2.m[i][j] = matView.m[i][j];
	_device->SetTransform(D3DTS_VIEW, &matView);

       // ... i create the cube here ...

	// #5: Position the cube in the lower-left of the screen (screen coordinates).
	D3DXMatrixTranslation(&matObjectWorld, transX, transY, transZ);
	_device->SetTransform(D3DTS_WORLD, &matObjectWorld);


If i do this, it works but all the scene is rendered in an orthographic projection and i lose perspective of things. So i decided to add 

_device->SetTransform(D3DTS_PROJECTION, &matProjection);


just after i render the cube so everything else is kept in the initial perspective projection (i draw the cube just at the end of the scene, so everything is in perspective and just the cube is in ortho). However, if i do this, then the cube changes position when zooming, appearing to go towards the origin along Z axis when zooming in and  vice-versa  when zooming out.


So.. the thing is i would like to keep my perspective projection on all things (maybe except the cube, which looks better when is in ortho), but this doesn't work as supposed to. If you'd like, i can put up a new video to express the symptoms better.


Thanks again for the reply!


Share this post

Link to post
Share on other sites

I think the problem is that you are treating the cube like it is part of your overall scene.  Essentially what you want to do is take the transformation information from the object you are manipulating, and remove the scaling and translation information.  You only want the rotation part of your transform to be applied to the little cube.  So you build the world matrix from only that part of the transform of the manipulated object.


Once you have the transform of the cube built, then you just want to render it in a constant location within the field of view.  This is done like L.Spiro indicated, by setting the view matrix to be identity - only when rendering the cube!  You should not affect the rest of your scene with this change, but instead use a separate rendering pass to make the 'UI' portion of your scene.


Share this post

Link to post
Share on other sites

Wow... that's a lot of words i don't understand... like:

use a separate rendering pass


Anyway... i did solve my problem (in a very  unprofessional way, i'm sure of that). You can check out a video of how it looks like here.


So... what i did was to choose a visible dimension on screen (as a ratio of the viewport's height or width) and compute the size of the object at each frame, so that it appears to have that dimension that i chose. here's the code:


        // choose the size of the object with respect to vieport's width
	float mysize = 0.05*viewport.Width;
	// now find the point on the screen that is offseted with half the chosen size
	screenPoint.x = screenPoint.x - mysize/2;
	screenPoint.y = projectPoint.y - mysize/2;
	screenPoint.z = projectPoint.z;

	// now transform the screen position into a world position
	D3DXVec3Unproject( &worldPoint, &screenPoint, &viewport, &matProjection, &matView, &matObjectWorld );

	// coorect size with rotation
	float boxSize = 2.0f; // assign some default size for the object, in case something goes wrong
	if (cos(angley) == 0) // make sure i don't devide by zero
		boxSize = abs(transY - worldPoint.y);
		boxSize = abs(transX - worldPoint.x) / abs(cos(angley)) ;


and finally... 



Now.. what you've seen in the video was accomplished by drawing 3  cylinders with different  colors  and one cube in the middle. If anyone desires, i could put up my entire function here to have a clearer idea.


Thanks all for the advice. Any further comments would be appreciated.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0