Jump to content
  • Advertisement
Sign in to follow this  
crosswind

Help with lighting

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

got this error when compile: Error 1 error C3861: 'init_light': identifier not found #ifndef _Light_H_ #define _Light_H_ #include "DxManager.h" class light { LPDIRECT3DDEVICE9 d3ddev; public: light(); ~light(); void init_light(); }; #endif #include "Light.h" light::light() { } light::~light() { } void light::init_light() { D3DLIGHT9 light; // create the light struct ZeroMemory(&light, sizeof(light)); // clear out the struct for use light.Type = D3DLIGHT_DIRECTIONAL; // make the light type 'directional light' light.Diffuse.r = 0.5f; // .5 red light.Diffuse.g = 0.5f; // .5 green light.Diffuse.b = 0.5f; // .5 blue light.Diffuse.a = 1.0f; // full alpha (we'll get to that soon) D3DVECTOR vecDirection = {-1.0f, -0.3f, -1.0f}; // the direction of the light light.Direction = vecDirection; // set the direction d3ddev->SetLight(0, &light); // send the light struct properties to light #0 d3ddev->LightEnable(0, TRUE); // turn on light #0 return; }

Share this post


Link to post
Share on other sites
Advertisement
i tried to initialize lighting here

#include "DxManager.h"
#include <atlbase.h>

directx::directx(HWND hWnd)
{
t_buffer = 0;
z_buffer = 0;

d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;

// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

init_graphics();
init_light();

d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn on the 3D lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); // ambient light

}

directx::~directx()
{
d3d->Release();
d3ddev->Release();
meshSpaceship->Release(); // close and release the spaceship mesh




}

void directx::render()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

// SET UP THE TRANSFORMS

D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 8.0f, 16.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
SCREEN_WIDTH / SCREEN_HEIGHT, // the aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

static float index = 0.0f; index+=0.03f; // an ever-increasing float value
D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY)); // set the world transform


// draw the spaceship
for(DWORD i = 0; i < numMaterials; i++) // loop through each subset
{
d3ddev->SetMaterial(&material); // set the material for the subset
if(texture != NULL) // if the subset has a texture (if texture is not NULL)
d3ddev->SetTexture(0, texture); // ...then set the texture

meshSpaceship->DrawSubset(i); // draw the subset
}

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);


return;
}

void directx::init_graphics()
{

LPD3DXBUFFER bufShipMaterial;

D3DXLoadMeshFromX(L"d1.x", // load this file
D3DXMESH_SYSTEMMEM, // load the mesh into system memory
d3ddev, // the Direct3D Device
NULL, // we aren't using adjacency
&bufShipMaterial, // put the materials here
NULL, // we aren't using effect instances
&numMaterials, // the number of materials in this model
&meshSpaceship); // put the mesh here

// retrieve the pointer to the buffer containing the material information
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();

// create a new material buffer and texture for each material in the mesh
material = new D3DMATERIAL9[numMaterials];
texture = new LPDIRECT3DTEXTURE9[numMaterials];

for(DWORD i = 0; i < numMaterials; i++) // for each material...
{
material = tempMaterials.MatD3D; // get the material info
material.Ambient = material.Diffuse; // make ambient the same as diffuse
USES_CONVERSION; // allows certain string conversions
// if there is a texture to load, load it
if(FAILED(D3DXCreateTextureFromFile(d3ddev,
CA2W(tempMaterials.pTextureFilename),
&texture)))
texture = NULL; // if there is no texture, set the texture to NULL
}

return;
}

Share this post


Link to post
Share on other sites
You need to create a light object within your directx class.

e.g

light mLight; // put this in the private part of your directx class decloration

mLight.init_light();

init_graphics() works because its a member of your directx class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Westie007
You need to create a light object with your directx class.

light mLight;

mLight.init_light();

init_graphics(); works because its a member of your directx class.


but i included my Light.h into DxManager.cpp?

Share this post


Link to post
Share on other sites
Hmm, it will probably take a while to explain.

Your best bet is to read this tutorial here: http://www.cprogramming.com/tutorial/lesson12.html

Then come back with any questions you still have, as that tutorial should sort this out for you.

Give it a go and we'll help you if you still have problems/questions. :D

Share this post


Link to post
Share on other sites
Quote:
Original post by Westie007
Hmm, it will probably take a while to explain.

Your best bet is to read this tutorial here: http://www.cprogramming.com/tutorial/lesson12.html

Then come back with any questions you still have, as that tutorial should sort this out for you.

Give it a go and we'll help you if you still have problems/questions. :D


Hi Westie,

I read through that tutorial and gave it a thought but still i am not making any heads or tails regarding the issue

Share this post


Link to post
Share on other sites
tried your way westie however new errors pop up

Error 1 error C2146: syntax error : missing ';' before identifier 'mLight'


Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


#ifndef _DxManager_H_
#define _DxManager_H_
#include <d3d9.h>
#include <d3dx9.h>
#include "Light.h"

class directx
{
static const int SCREEN_WIDTH = 640;
static const int SCREEN_HEIGHT = 480;

struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)


LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 t_buffer;
LPDIRECT3DSURFACE9 z_buffer;

// mesh declarations
LPD3DXMESH meshSpaceship; // define the mesh pointer
D3DMATERIAL9* material; // define the material object
LPDIRECT3DTEXTURE9* texture; // a pointer to a texture
DWORD numMaterials; // stores the number of materials in the mesh


public:
directx(HWND hWnd);
~directx();

void render();
void init_graphics();


private:
light mLight;



};
#endif

#include "DxManager.h"
#include <atlbase.h>

directx::directx(HWND hWnd)
{
t_buffer = 0;
z_buffer = 0;

d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;

// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

init_graphics();
mLight.init_light();

d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE); // turn on the 3D lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer
d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50)); // ambient light

}

directx::~directx()
{
d3d->Release();
d3ddev->Release();
meshSpaceship->Release(); // close and release the spaceship mesh

}

void directx::render()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

// SET UP THE TRANSFORMS

D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 8.0f, 16.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
SCREEN_WIDTH / SCREEN_HEIGHT, // the aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

static float index = 0.0f; index+=0.03f; // an ever-increasing float value
D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY)); // set the world transform


// draw the spaceship
for(DWORD i = 0; i < numMaterials; i++) // loop through each subset
{
d3ddev->SetMaterial(&material); // set the material for the subset
if(texture != NULL) // if the subset has a texture (if texture is not NULL)
d3ddev->SetTexture(0, texture); // ...then set the texture

meshSpaceship->DrawSubset(i); // draw the subset
}

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);


return;
}

void directx::init_graphics()
{

LPD3DXBUFFER bufShipMaterial;

D3DXLoadMeshFromX(L"d1.x", // load this file
D3DXMESH_SYSTEMMEM, // load the mesh into system memory
d3ddev, // the Direct3D Device
NULL, // we aren't using adjacency
&bufShipMaterial, // put the materials here
NULL, // we aren't using effect instances
&numMaterials, // the number of materials in this model
&meshSpaceship); // put the mesh here

// retrieve the pointer to the buffer containing the material information
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();

// create a new material buffer and texture for each material in the mesh
material = new D3DMATERIAL9[numMaterials];
texture = new LPDIRECT3DTEXTURE9[numMaterials];

for(DWORD i = 0; i < numMaterials; i++) // for each material...
{
material = tempMaterials.MatD3D; // get the material info
material.Ambient = material.Diffuse; // make ambient the same as diffuse
USES_CONVERSION; // allows certain string conversions
// if there is a texture to load, load it
if(FAILED(D3DXCreateTextureFromFile(d3ddev,
CA2W(tempMaterials.pTextureFilename),
&texture)))
texture = NULL; // if there is no texture, set the texture to NULL
}

return;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!