Skip to main content
gamedev.net
🔒 Locked

Help with lighting

Started by crosswind Dec 2, 2008 at 5:03 AM 14 replies 1.5k views
Original Post
crosswind
crosswind
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; }
crosswind
crosswind
i tried to initialize lighting here

#include "DxManager.h"
#include

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;
}

Westie007
Westie007
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.
crosswind
crosswind
first time posting so would appreciate anyone to guide me along and apologies for the mess.
crosswind
crosswind
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?
Westie007
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
crosswind
crosswind
thank you...let me read through......
crosswind
crosswind
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
crosswind
crosswind
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
#include
#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

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;
}

Westie007
Westie007
You're closer than you'd think...

It looks like the compiler doesn't know what 'light' is.
crosswind
crosswind
what you mean it doesnt know what light is? care to elaborate?
Westie007
Westie007
The compiler can't find the 'class light' decloration from the Light.h file.

I'm not sure why but I'd guess that maybe you are including dxmanager.h in light.h still, which could cause a circular dependancy.

try commenting out the '#include "dxmanager.h" line and including the required directx headers for the LPDIRECT3DDEVICE9 object stored in the light class.

crosswind
crosswind
this is what I have in my light.h and light.cpp file

#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;
}
Westie007
Westie007
okay cool, read through my last post again and look at the code you just printed there.

Just have a go at commenting out that line and adding those extra headers. See what happens.
Westie007
Westie007
How did that go for you? Don't give up, you're almost there.

Here is the solution:

Change this (in Light.h):

#ifndef _Light_H_
#define _Light_H_
#include "DxManager.h"

to this:

#ifndef _Light_H_
#define _Light_H_
//#include "DxManager.h"
#include
crosswind
crosswind
thank you. However the system doesnt recognise what d3ddev is....it compiles and breaks halfway stating in my light.cpp, it doesnt recognise d3ddev

[Edited by - crosswind on December 2, 2008 1:05:18 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.