Thread: D3D - Perspective Confusion

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    D3D - Perspective Confusion

    I'm a little confused because I am not getting results from my attempts to change my perspective in D3D

    I am trying stuff like

    Code:
    int DirectXWindow::RenderDxScene(void)
    {
        HRESULT hr = S_OK;
        hr = m_pd3dDevice->TestCooperativeLevel();
    
        switch(hr)
        {
            // ...
        }
    
        m_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
            D3DCOLOR_COLORVALUE(0, 0, 0, 1.0f), 1.0f, 0);
    
        m_pd3dDevice->BeginScene();
    
        // D3DXMATRIX matWorld; // Either this
        // D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 5000.0f);
        // m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
    
    	D3DXMATRIX matView; // Or this
    	D3DXMatrixLookAtLH(&matView, 
    		&D3DXVECTOR3(0.0f, 3.0f, -5000.0f), 
    		&D3DXVECTOR3(0.0f, 0.0f, 0.0f), 
    		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
    	m_pd3dDevice->SetTransform(D3DTS_VIEW, &matView); 
    
        ScenePrimitive * s = triangle;
    
        m_pd3dDevice->SetTexture(0, 0);
        m_pd3dDevice->SetMaterial(&s->material);
    
        m_pd3dDevice->SetFVF(s->fvf);
        m_pd3dDevice->SetStreamSource(0, s->buffer, 0, sizeof(Vertex));
        m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
    
        m_pd3dDevice->EndScene();
    
        if(m_pd3dDevice->Present(0, 0, 0, 0) == D3DERR_DEVICELOST)
        {
            dout << "Present\n";
        }
    
        return 1;
    }
    Neither do anything, and my triangle just sits there, large and unaffected by my attempts to move it. Is there any obvious reason :?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Even stranger, my transformations on the meshes work fine!

    Code:
    int DirectXWindow::RenderDxScene(void)
    {
    	HRESULT hr = S_OK;
    	hr = m_pd3dDevice->TestCooperativeLevel();
    
    	switch(hr)
    	{
    		// ...
    	}
    
    	m_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
    		D3DCOLOR_COLORVALUE(0, 0, 0, 1.0f), 1.0f, 0);
    
    	m_pd3dDevice->BeginScene();
    
    	D3DXMATRIX matWorld, matView;
    
    	D3DXMatrixLookAtLH(&matView,
    		&D3DXVECTOR3(0.0f, 6.0f, -6.0f),	// Eye
    		&D3DXVECTOR3(0.0f, 0.0f, 0.0f),		// At
    		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));	// Up
    
    	m_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
    
    	ScenePrimitive * s = square;
    
    	static float i = 0;
    	i += 0.5;
    
    	D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 100 - i);
    	m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
    
    	for(size_t i = 0; i < spaceship_mesh.materials.size(); ++i)
    	{
    		m_pd3dDevice->SetMaterial(&spaceship_mesh.materials[i]);
    		spaceship_mesh.mesh->DrawSubset(i);
    	}
    
    	m_pd3dDevice->SetTexture(0, 0);
    	m_pd3dDevice->SetMaterial(&s->material);
    
    	m_pd3dDevice->SetFVF(s->fvf);
    	m_pd3dDevice->SetStreamSource(0, s->buffer, 0, sizeof(Vertex));
    	m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
    
    	m_pd3dDevice->EndScene();
    	m_pd3dDevice->Present(0, 0, 0, 0);
    
    	return 1;
    }
    Neither matrix operations, the view or the world, seem to do anything to my view of the triangle. However, I see that the mesh is affected by it, as it zooms around in my view, it just fine, but the triangle is unaffected! I cannot see what would cause such a discrepancy.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    So I realized I was using precalculated vertices D3DFVF_XYZTRHW. Stopped that. Here is where I am now, I can still not see the triangle but can see the spaceship mesh in the center. Am I not looking at my triangle correctly?


    Renderin' Rooteen
    Code:
    	m_pd3dDevice->BeginScene();
    
    	D3DXMATRIX matWorld, matView;
    
    	D3DXMatrixLookAtLH(&matView,
    		&D3DXVECTOR3(0.0f, 0.0f, -5.0f),	// Eye
    		&D3DXVECTOR3(0.0f, 0.0f, 0.0f),		// At
    		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));	// Up
    
    	m_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
    
    	ScenePrimitive * s = square;
    
    	static float x = 0;
    	x += 0.1;
    
    	D3DXMatrixTranslation(&matWorld, 0, 0, 0);
    	D3DXMatrixRotationY(&matWorld, x);
    
    	m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
    	
    	for(size_t i = 0; i < spaceship.materials.size(); ++i)
    	{
    		m_pd3dDevice->SetMaterial(&spaceship.materials[i]);
    		spaceship.mesh->DrawSubset(i);
    	}
    
    	m_pd3dDevice->SetTexture(0, 0);
    	m_pd3dDevice->SetMaterial(&s->material);
    
    	m_pd3dDevice->SetFVF(s->fvf);
    	m_pd3dDevice->SetStreamSource(0, s->buffer, 0, sizeof(Vertex));
    	m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
    
    	m_pd3dDevice->EndScene();
    Inishillization Rooteen
    Code:
    	D3DXMATRIX mProjection;
    
    	::D3DXMatrixPerspectiveFovLH(&mProjection, D3DX_PI * 0.5f, 
    		double(width) / double(height), 0.1f, 100.0f);
    
    	m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mProjection);
    
    	D3DLIGHT9 light;
    	D3DMATERIAL9 material;
    
    	ZeroMemory(&light, sizeof(light));
    	light.Type = D3DLIGHT_DIRECTIONAL;
    	light.Diffuse.r = 0.5f;
    	light.Diffuse.g = 0.5f;
    	light.Diffuse.b = 0.5f;
    	light.Diffuse.a = 1.0f;
    	light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);
    
    	m_pd3dDevice->SetLight(0, &light);
    	m_pd3dDevice->LightEnable(0, true);
    
    	ZeroMemory(&material, sizeof(D3DMATERIAL9));
    	material.Diffuse.r = material.Ambient.r = 1.0f;
    	material.Diffuse.g = material.Ambient.g = 1.0f;
    	material.Diffuse.b = material.Ambient.b = 1.0f;
    	material.Diffuse.a = material.Ambient.a = 1.0f;
    
    	ScenePrimitive * p = new ScenePrimitive;
    
    	struct Vertex
    		triangle[] = 
    	{
    		{0, 1, 0, D3DCOLOR_XRGB(0, 0, 255),},
    		{1, -1, 0, D3DCOLOR_XRGB(0, 255, 0),},
    		{-1, -1, 0, D3DCOLOR_XRGB(255, 0, 0),},
    	};
    
    	p->fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;
    	p->bufsize = lengthof(triangle);
    
    	m_pd3dDevice->CreateVertexBuffer(sizeof(triangle),
    		D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, p->fvf, D3DPOOL_DEFAULT, 
    		&p->buffer, 0);
    
    	Vertex * vertices;
    
    	p->buffer->Lock(0, 0, (void**) &vertices, 0);
    	memcpy(vertices, triangle, sizeof(triangle));
    	p->buffer->Unlock();
    
    	square = p;
    	spaceship = loadMesh("C:\\Proijektz\\Directx\\Spaceship 2.x");

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Okay I added ambient lighting and now the triangle is present but two things:

    1) I can not see it from 'behind'
    2) It is not a rainbow gradient

    Does the first prob. got something to do with the 'winding order' opr the vertices and 'backface culling'? How can I turn that off?

    Does the second have to do with the material I use? I mean, how do I fix that

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Is there any info that I could give you guys that would help you, uh, help me? Here's the project, the key stuff is in directxwindow

    http://zxcvbn.googlecode.com/svn/trunk/directx/
    http://zxcvbn.googlecode.com/svn/tru...ectXWindow.cpp

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    the winding order determines the backface culling. You can turn off backface culling, but it isnt recommended, since you take a performance hit and most objects, no matter how thin, are rarely 1 pixel in thickness. As for the rainbow effect, you need to specify diffrent colors for each vertex inteh triangle to get it to produce that.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I wouldn't worry too much about vertex colors since most of your lighting later on will be based on other properties. Once you texture the model the material color will most likely be one solid color and will be used in the final lighting equation in your system.

    Direct3D will interpolate between colors at the vertices. This means if you have a simple 2D triangle with the vertex colors as follows:

    top - 255,0,0 (red)
    bottom right - 0,255,0 (green)
    bottom left - 0,0,255 (blue)

    The triangle will be red at the top and gradually fade into green as you go down and right.
    The triangle will be red at the top and gradually fade into blue and you go down and left.
    The triangle will be red at the top and will possibly be white in the middle and then a mixture of green and blue at bottom center.

    Keep in mind Direct3D uses linear interpolation for colors so you are essentially drawing a straight line from red to blue and red to green on the color cube. All colors that lie on these lines will be in your triangle.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Sweet, combination of adding normals and also turning off backface culling solved this one. Thabnks guuys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-23-2005, 11:39 AM
  2. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  3. Replies: 5
    Last Post: 02-25-2005, 04:48 PM
  4. D3D Windowed Application
    By Magos in forum Game Programming
    Replies: 6
    Last Post: 03-14-2004, 02:26 PM
  5. Compressed Earth Perspective
    By DISGUISED in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 11-22-2003, 01:27 PM