Sign in to follow this  
Nachiketa

Rotation about y-axis

Recommended Posts

Can somebody pls hlp me with this piece of code ? What I want the code to do is to make walls of a 3D room. The following piece of code is trying to make a wall (left side) but as you can see the wall is not tilted in the right directon. Can someone tell me how to rotate the wall (cube) about the y-axis ?? I m using DirectX 9.0.
// GDPass.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "GDPass.h"
#include <d3d9.h>
#include <d3dx9.h>

LPDIRECT3D9 D3DNew=NULL;
LPDIRECT3DDEVICE9 D3DDevice=NULL;
void InitialiseDirect3D();
void InitialiseTexturing(void);

// Global Variables:
HINSTANCE hInst;								// current instance
HWND hWnd;


struct squarevertex{ //Structure used to hold data on corners(vertices) of polygon.
    FLOAT x, y, z; 
    FLOAT tu,tv;        
};

squarevertex sidewallleft[] ={
  //Front
	{ -1.0f,  -1.0f, -1.0f,0.0f,1.0f },
   	{ -1.0f,  1.0f,  -1.0f, 0.0f,0.0f },
   	{ -0.75f,  1.0f, -1.0f, 1.0f,0.0f },
   	{ -0.75f,  1.0f,  -1.0f, 1.0f,0.0f },
	{ -0.75f,  -1.0f, -1.0f, 1.0f,1.0f },
	{ -1.0f,  -1.0f, -1.0f,0.0f,1.0f },
//Back
	{ -1.0f,  -1.0f, 1.0f,0.0f,1.0f },
   	{ -1.0f,  1.0f,  1.0f, 0.0f,0.0f },
   	{ -0.75f,  1.0f, 1.0f, 1.0f,0.0f },
   	{ -0.75f,  1.0f,  1.0f, 1.0f,0.0f },
	{ -0.75f,  -1.0f, 1.0f, 1.0f,1.0f },
	{ -1.0f,  -1.0f, 1.0f,0.0f,1.0f },
//Top
	{ -1.0f, 1.0f, -1.0f,0.0f,1.0f },
	{ -1.0f, 1.0f, 1.0, 0.0f,0.0f },
    { 1.0f,  1.0f, 1.0f, 1.0f,0.0f },
    { 1.0f,  1.0f, 1.0f, 1.0f,0.0f },
	{ 1.0f,  1.0f, -1.0f, 1.0f,1.0f },
	{ -1.0f, 1.0f, -1.0f,0.0f,1.0f },
//Bottom
	{ 1.0f, -1.0f, -1.0f,0.0f,1.0f },
    { 1.0f, -1.0f, 1.0, 0.0f,0.0f },
    { -1.0f,  -1.0f, 1.0f, 1.0f,0.0f },
    { -1.0f,  -1.0f, 1.0f, 1.0f,0.0f },
	{ -1.0f, -1.0f, -1.0f, 1.0f,1.0f },
	{ 1.0f, -1.0f, -1.0f,0.0f,1.0f },
//Left
	{ -1.0f, -1.0f, 1.0f,0.0f,1.0f },
    { -1.0f, 1.0f, 1.0, 0.0f,0.0f },
    { -1.0f,  1.0f, -1.0f, 1.0f,0.0f },
    { -1.0f,  1.0f, -1.0f, 1.0f,0.0f },
	{ -1.0f, -1.0f, -1.0f, 1.0f,1.0f },
	{ -1.0f, -1.0f, 1.0f,0.0f,1.0f },
//Right
	{ -0.75f, -1.0f, -1.0f,0.0f,1.0f },
	{ -0.75f, 1.0f, -1.0, 0.0f,0.0f },
    { -0.75f,  1.0f, 1.0f, 1.0f,0.0f },
    { -0.75f,  1.0f, 1.0f, 1.0f,0.0f },
	{ -0.75f, -1.0f, 1.0f, 1.0f,1.0f },
	{ -0.75f, -1.0f, -1.0f,0.0f,1.0f }
};

LPDIRECT3DVERTEXBUFFER9 D3DVertexBuffer=NULL;
LPDIRECT3DTEXTURE9 D3DTexture=NULL; //Create new instance of a Texture
#define D3D9T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1) 

void InitialiseVertexBuffers(void); //function to initialise the vertex buffers
void DisplaySquare(void); //Function to actually display the square


BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style		= CS_OWNDC;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon		= LoadIcon(hInstance, (LPCTSTR)IDI_GDPASS);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+2);
	wcex.lpszMenuName	= (LPCTSTR)IDC_GDPASS;
	wcex.lpszClassName	= "Getting Started With Windows";
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	RegisterClassEx(&wcex);

	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	InitialiseDirect3D();
	InitialiseVertexBuffers();

	while (true) 
	{
		MSG msg;
		if (PeekMessage(&msg,0,0,0,PM_REMOVE)) 
		{
			if(msg.message==WM_QUIT)
				return msg.wParam;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		DisplaySquare();
	}
	return -1;

}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindowEx(WS_EX_TOPMOST,"Getting Started With Windows", TEXT("Getting Started WIth Windows"), 
	   WS_POPUP,0, 0, 640, 480, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, SW_SHOWDEFAULT);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	RECT rect;
	HDC hdc;

	switch (message) 
	{
	case WM_PAINT:
		
		hdc = BeginPaint(hWnd, &ps);
		GetClientRect (hWnd,&rect);
		SetTextColor(hdc,255);
		SetBkColor(hdc,0);
		DrawText(hdc,"BULL'S EYE",-1,&rect, DT_SINGLELINE | DT_CENTER|DT_VCENTER);
		EndPaint(hWnd, &ps);
		break;

	case WM_KEYDOWN:
		PostQuitMessage(0);
		break;

	case WM_SETCURSOR:
		SetCursor(NULL);
		break;

	case WM_DESTROY:
		D3DDevice->Release(); 
		D3DNew->Release();
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

void InitialiseDirect3D(void) 
{
	D3DPRESENT_PARAMETERS D3DParams;
	D3DDISPLAYMODE D3DDisplayMode; //Basically gets the users display mode from there desktop
	D3DNew = Direct3DCreate9( D3D_SDK_VERSION );//Ensures that the correct version of the SDK is being used
	D3DNew->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&D3DDisplayMode);
	ZeroMemory(&D3DParams,sizeof(D3DParams)); 
	D3DParams.SwapEffect     = D3DSWAPEFFECT_DISCARD; 
	D3DParams.hDeviceWindow  = hWnd;  
	D3DParams.BackBufferCount= 1;  //1 Back buffer being used
    	D3DParams.Windowed          = FALSE; //This is a full screen application
    	D3DParams.BackBufferWidth   = D3DDisplayMode.Width; //Set our buffer to the size of 
    	D3DParams.BackBufferHeight  = D3DDisplayMode.Height; //the screen
    	D3DParams.BackBufferFormat  = D3DDisplayMode.Format;

	//Actual creation of a direct3D Instance ready for use.  
	//Hardware Processing can be changed to software for compatability.
	
D3DNew->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
                          D3DCREATE_HARDWARE_VERTEXPROCESSING,&D3DParams,&D3DDevice);
//Next line fills the screen with blue
	D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
 //This is the code that actually shows the blue screen
	D3DDevice->Present( NULL, NULL, NULL, NULL );
}

void InitialiseVertexBuffers(void)
{
	VOID* vb_vertices;
	D3DXMATRIX view_matrix;
	D3DXMATRIX matProj;
    InitialiseTexturing();//Call texturing procedure
	D3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
    D3DDevice->CreateVertexBuffer(36*sizeof(squarevertex),0,
		D3D9T_CUSTOMVERTEX,D3DPOOL_DEFAULT,&D3DVertexBuffer,NULL);

    D3DVertexBuffer->Lock(0,sizeof(sidewallleft), (void**)&vb_vertices,0);   
		memcpy(vb_vertices, sidewallleft, sizeof(sidewallleft) );
    D3DVertexBuffer->Unlock();
	D3DXMatrixLookAtLH(&view_matrix,&D3DXVECTOR3(0.0f,0.0f,-3.0f),
									&D3DXVECTOR3(0.0f,0.0f,0.0f),
									&D3DXVECTOR3(0.0f,1.0f,0.0f));
	D3DDevice->SetTransform(D3DTS_VIEW,&view_matrix);
	D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.33,1.0f,100.0f);
	D3DDevice->SetTransform(D3DTS_PROJECTION,&matProj);

}

void DisplaySquare(void)
{
	D3DXMATRIX matWorld;
	D3DXMATRIX trans_matrix;
    D3DXMATRIX rot_matrixx;
    D3DXMATRIX rot_matrixy;
    D3DXMATRIX rot_matrix;

	//First line simply blankets the screen with a colour of our choice
	D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );//Changed Lines
	D3DDevice->BeginScene();//lets device know we are about to begin rendering
		D3DDevice->SetFVF(D3D9T_CUSTOMVERTEX);
		D3DDevice->SetStreamSource(0,D3DVertexBuffer,0,sizeof(squarevertex));
		
		D3DXMatrixRotationY(&rot_matrixy,0.5);
		D3DXMatrixRotationX(&rot_matrixx,0.00);
		D3DXMatrixMultiply(&rot_matrix,&rot_matrixx,&rot_matrixy);
		D3DXMatrixTranslation(&trans_matrix,0.0f,0.0f,0.0f);
		D3DXMatrixMultiply(&matWorld,&rot_matrix,&trans_matrix);
     	D3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
		D3DDevice->SetTexture(0,D3DTexture); //Incorporate the texture
		D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,12);  

   D3DDevice->EndScene();
   D3DDevice->SetTexture(0,NULL);//Switch the texture off
	//The next line of code flips the contents of the back buffer to the screen.
	D3DDevice->Present( NULL, NULL, NULL, NULL );//Show what we have drawn
}

void InitialiseTexturing(void)
{
	//A few functions for controlling aspects of the texture.
	//The last 2 are important and relate to the quality of the image 
	//especially if you use point instead of line.
	//Change them around with a detailed texture and see what happens.
	D3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
	D3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
	D3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,   D3DTOP_DISABLE);
	D3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
	//Easily done here we simply create our texture from our file.
	D3DXCreateTextureFromResource(D3DDevice, NULL,MAKEINTRESOURCE(IDB_BITMAP1),&D3DTexture);
}
[Edited by - Coder on March 28, 2005 12:21:16 PM]

Share this post


Link to post
Share on other sites
You want to multiply each point (or set the world matrix), by the following matrix

cos(angle) 0 sin(angle) 0
0 1 0 0
-sin(angle) 0 cos(angle) 0
0 0 0 1

For directX.

Setting the world matrix will apply this transformation to each of the triangles you draw. You could also do the transformation in your code.

Remember this does not rotate around the "y-axis" of the object you're rotating, but by the absolute y-axis of your scene.

Matt

Share this post


Link to post
Share on other sites
Try replacing this part:


D3DXMatrixRotationY(&rot_matrixy,0.5);
D3DXMatrixRotationX(&rot_matrixx,0.00);
D3DXMatrixMultiply(&rot_matrix,&rot_matrixx,&rot_matrixy);
D3DXMatrixTranslation(&trans_matrix,0.0f,0.0f,0.0f);
D3DXMatrixMultiply(&matWorld,&rot_matrix,&trans_matrix);
D3DDevice->SetTransform(D3DTS_WORLD,&matWorld);



with this:


D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationY(&rot_matrixy,D3DX_PI * 0.5f); //Angle in radians
D3DXMatrixMultiply(&matWorld,&matWorld,&rot_matrixy);
D3DDevice->SetTransform(D3DTS_WORLD,&matWorld);



This is assuming, of course, that the problem lies in the rotation code and everything else is all right. ;)

See if that helps!

Share this post


Link to post
Share on other sites
It's all a matter of rotating and translating geometry on the screen. If you've got a wall on the left side and want a wall on the right running parallel to it, then just translate by some offset to the right and render the same geometry again. Something like this:

BeginScene()
1) Rotate model matrix by 90 degrees (MatrixRotationY)
2) Render model

(this is what you've done for the left wall so far)
3) Translate model matrix to the right by some offset (MAtrixTranslation)
4) Render model
EndScene()

Hope this helps!

Share this post


Link to post
Share on other sites
hi
i tried to do what you said, it's still not working,,,
it is translating as expected
all u can see is something weird on the right side, i wish i could send you the image of the problem... but there is no option to upload on this forum,,,
can i mail it to you....
any ways u have been a big help






// Left wall
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationY(&rot_matrixy,0.5);
D3DXMatrixMultiply(&rot_matrix,&rot_matrixx, &rot_matrixy);
D3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
D3DDevice->SetTexture(0,D3DTexture); //Incorporate the texture
D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,12);

// Right wall
D3DXMatrixTranslation(&trans_matrix,100.0f,5.0f,0.0f);
D3DXMatrixMultiply(&matWorld,&rot_matrix,&trans_matrix);
D3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
D3DDevice->SetTexture(0,D3DTexture); //Incorporate the texture
D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,12);

Share this post


Link to post
Share on other sites
There seems to be some prob with your code as far as I can see. Try this:


// Left wall
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationY(&rot_matrixy,D3DX_PI * 0.5f); //Angle in radians
D3DXMatrixMultiply(&matWorld,&matWorld,&rot_matrixy);
D3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
D3DDevice->SetTexture(0,D3DTexture); //Incorporate the texture
D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,12);

// Right wall
D3DXMatrixTranslation(&matWorld,20.0f,0.0f,0.0f); //No rotation reqd.
D3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
D3DDevice->SetTexture(0,D3DTexture); //Incorporate the texture
D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,12);



Mind you, the translation value will depend on the scale of your model. Try changing value of 20 as required.

If this still doesn't work, let me know!

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