Rendering primitives, light

Started by
1 comment, last by gerbrost 22 years, 5 months ago
Hello all, i have to step on your nerves again: Im rendering 4 Walls and a floor. The walls do render, but when i try to set a light in the scene, it cant be set to the middle of the room. it appears to jump from corner to corner. I bould the position of the light to the position of the camera to examine the effect. the light appears at the corner of the room where my camera is located. when i move to the middle of the room, the light disappears. when i move closer to one of the corners, the light jumps to one of the corners. im trying to find the problem for days now, so i hope someone can help me. Here is the code(the whole project can be downloaded at http://www.nwmedia.de/nwmedia/download.html):
  
//this is called at scene init:

	device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE );
	device->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
// get textures for walls and floor

	if( FAILED( D3DXCreateTextureFromFile( device, "ground.bmp", &g_pGroundTexture))) {
		LogError("CreateTextureFromFile for ground failed!");
		return false;
	}

	ZeroMemory(&Material,sizeof(Material));
	Material.Diffuse.r = 1.0f;
	Material.Diffuse.g = 1.0f;
	Material.Diffuse.b = 1.0f;

	if( FAILED( D3DXCreateTextureFromFile( device, "wall.bmp", &g_pWallTexture))) {
		LogError("CreateTextureFromFile for wall failed!");
		return false;
	}
	if( FAILED( device->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &g_pVBfloor))) {
		LogError("CreateVertexBuffer failed!");
        return false;
    }
// the other vertex buffers are created the same style, so i cut them out

//fill data into the buffers:

	if(FAILED(device->SetVertexShader(D3DFVF_CUSTOMVERTEX))) {
		LogError("SetVertexshader Failed!");
		return false;
	}

    if( FAILED( g_pVBfloor->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) ) {
		LogError("pVBfloor->Lock failed!");
        return false;
	}

	pVertices[0].position=D3DXVECTOR3(0.0f,0.0f,0.0f);
	pVertices[0].normal=D3DXVECTOR3(0.0f,10.0f,0.0f);
	pVertices[0].tu=0.0f;
	pVertices[0].tv=0.0f;
	pVertices[1].position=D3DXVECTOR3(0.0f,0.0f,100.0f);
	pVertices[1].normal=D3DXVECTOR3(0.0f,1.0f,0.0f);
	pVertices[1].tu=0.0f;
	pVertices[1].tv=8.0f;
	pVertices[2].position=D3DXVECTOR3(100.0f,0.0f,0.0f);
	pVertices[2].normal=D3DXVECTOR3(0.0f,1.0f,0.0f);
	pVertices[2].tu=8.0f;
	pVertices[2].tv=0.0f;
	pVertices[3].position=D3DXVECTOR3(100.0f,0.0f,100.0f);
	pVertices[3].normal=D3DXVECTOR3(0.0f,1.0f,0.0f);
	pVertices[3].tu=8.0f;
	pVertices[3].tv=8.0f;
    g_pVBfloor->Unlock();
//the other buffers are filled the same way, so i will skip that


//this is how the light is set up at scene init:

	//*** Light3: Point Light

  	ZeroMemory( &light3, sizeof(D3DLIGHT8) );
	light3.Position = D3DXVECTOR3(1.0f,1.0f,1.0f);
	light3.Type = D3DLIGHT_POINT;
	light3.Diffuse.r  = 0.0f;
	light3.Diffuse.g  = 0.0f;
	light3.Diffuse.b  = 3.0f;
	light3.Ambient.r  = 0.0f;
	light3.Ambient.g  = 0.0f;
	light3.Ambient.b  = 0.0f;
	light3.Specular.r = 0.0f;
	light3.Specular.g = 0.0f;
	light3.Specular.b = 0.0f;
	light3.Range      = 50.0f;
	light3.Attenuation0	= 0.01f;
	
	device->SetLight( 2, &light3);
	device->LightEnable( 2, TRUE);
	//*


	device->SetRenderState( D3DRS_LIGHTING, TRUE );
// this is my render-method:

	ClearScreen();	//my device->clear method

	device->BeginScene();
	ProcessKeyboardInput();
	ProcessMouseInput();
// my light position is updated. this should not be the problem, since when i set the position manually, the same problem occurs


	light3.Position		= D3DXVECTOR3(fCamTransX,fCamTransY,fCamTransZ);
	device->SetLight( 2, &light3);

	SetViewMatrix();
//finally: Here the primitives are drawn

    device->SetTexture( 0, g_pGroundTexture);
    device->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    device->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	device->SetMaterial(&Material);
	D3DXMATRIX GroundZero; // dumb name for a world matrix, sorry

	D3DXMATRIX GroundZeroTemp;
	D3DXMATRIX RestoreMatrix;
	D3DXMatrixIdentity(&GroundZero);

	D3DXMatrixRotationX(&GroundZero,D3DXToRadian(0));
	D3DXMatrixRotationY(&GroundZeroTemp,D3DXToRadian(0));
	D3DXMatrixMultiply(&GroundZero,&GroundZeroTemp,&GroundZero);
	D3DXMatrixRotationZ(&GroundZeroTemp,D3DXToRadian(0));
	D3DXMatrixMultiply(&GroundZero,&GroundZeroTemp,&GroundZero);
	D3DXMatrixTranslation(&GroundZeroTemp,0.0f,0.0f,0.0f);
	D3DXMatrixMultiply(&GroundZero,&GroundZeroTemp,&GroundZero);
	D3DXMatrixScaling(&GroundZeroTemp,1.0f,1.0f,1.0f);
	D3DXMatrixMultiply(&GroundZero,&GroundZeroTemp,&GroundZero);
	
	D3DXMatrixMultiply(&GroundZero,&GroundZeroTemp,&GroundZero);
	device->SetTransform(D3DTS_WORLD,&GroundZero);

	device->SetStreamSource( 0, g_pVBfloor, sizeof(CUSTOMVERTEX) );

    device->SetVertexShader( D3DFVF_CUSTOMVERTEX );
    
    device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0,2);

    device->SetTexture( 0, g_pWallTexture);
    device->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    device->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	device->SetStreamSource( 0, g_pVBwest, sizeof(CUSTOMVERTEX) );
    device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0,2);

	device->SetStreamSource( 0, g_pVBeast, sizeof(CUSTOMVERTEX) );
    device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0,2);

	device->SetStreamSource( 0, g_pVBnorth, sizeof(CUSTOMVERTEX) );
    device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0,2);

	device->SetStreamSource( 0, g_pVBsouth, sizeof(CUSTOMVERTEX) );
    device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0,2);
	
	D3DXMatrixIdentity(&RestoreMatrix);
	device->SetTransform(D3DTS_WORLD,&RestoreMatrix);



  
I tried to comment out the matrix calculations, but that did not seem to be the problem. I know its a lot of messy code, but im really stuck so i hope soemone can give me a hand! thank you!
Advertisement
It sound to me like it is a problem with your polygons (but I might be wrong). Try to increase number of vertecies on your walls. For example add one extra vertex in the middle of every wall and see if that helps. (or you can add light texture and blend it with wall texture)

In DirectX polygons are colored like this. The color of each vertex is calculated and each vertex is colored with its own color. Then the color of the middle area is just a linear transition from one corner vertex to another.

So if you have a huge polygon and a light source in the middle, the vertices will be colored dark, because they are far from the light source so they are almost black (assuming you have only one light source). And the middle area just a linear transition from one vertex to another, so it is also almost black, even though you got your light close to the middle of the polygon.
If i would be able to read the documentation, i would have been able to see that myself *sigh*
If theres no light reaching a vertex, no lighting calulations can be performed...
Thank you, thank you, thank you!!

This topic is closed to new replies.

Advertisement