#pragma once#include "d3dApp.h"//=============================================================================class BaseMaterial{protected: ID3DXEffect* mEffect; // the shader associate effect file D3DXHANDLE mTech; //——-- Material Parameters ——- D3DXMATRIX mWorldMat; D3DXMATRIX mViewProjectionMat; //——— Colors ———-- D3DXCOLOR mAmbientColor; D3DXCOLOR mDiffuseColor; D3DXCOLOR mSpecularColor; float m_Shininess; // specualr power //——— Lights ———-- D3DXCOLOR mAmbientLightColor; D3DXCOLOR mDiffuseLightColor; D3DXCOLOR mSpecularLightColor; D3DXCOLOR mAttenuation; //———- Shader Handles ———- // Generic shader handles D3DXHANDLE mWorldMatHandle; D3DXHANDLE mViewProjectionMatHandle; D3DXHANDLE mWorldInvTransHandle; D3DXHANDLE mLightPosWHandle; // Position (spot/point) / Direction (directional) D3DXHANDLE mViewerPosWHandle; // Material specific shader handles D3DXHANDLE mAmbientColHandle; D3DXHANDLE mDIffuseColHandle; D3DXHANDLE mSpecularColHandle; D3DXHANDLE mAmbientLightHandle; D3DXHANDLE mDIffuseLightHandle; D3DXHANDLE mSpecularLightHandle; D3DXHANDLE mAttenuationHandle; D3DXHANDLE mShininessHandle; // Light specific shader handles D3DXHANDLE mLightDirHandle;public: BaseMaterial(void); BaseMaterial(D3DXCOLOR ambient, D3DXCOLOR diffuse, D3DXCOLOR specular); virtual ~BaseMaterial(void); void ConnectToEffect( ID3DXEffect* effect ); void Render(D3DXMATRIX& worldMat, D3DXMATRIX& view, D3DXMATRIX& projection, D3DXVECTOR3 lightDirection, ID3DXMesh* mesh);};
#include "BaseMaterial.cpp"#include "3DClasses\Vertex.h"//=============================================================================BaseMaterial::BaseMaterial(void){ mEffect = NULL; ConnectToEffect(mEffect);}BaseMaterial::BaseMaterial(D3DXCOLOR ambient, D3DXCOLOR diffuse, D3DXCOLOR specular):mAmbientColor(ambient),mDiffuseColor(diffuse),mSpecularColor(specular){ ConnectToEffect(mEffect); mAmbientLightColor = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mDiffuseLightColor = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mSpecularLightColor = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); m_Shininess = 3.2f;}//—————————————————————————--// Relase shader, blah...BaseMaterial::~BaseMaterial(void){ ReleaseCOM(mEffect);}//—————————————————————————--// Connects mEffect to shader file, and associates parametersvoid BaseMaterial::ConnectToEffect( ID3DXEffect* effect ){ ID3DXBuffer* errors = 0; HR(D3DXCreateEffectFromFile(gd3dDevice, "ambientdiffusespec.fx", 0, 0, D3DXSHADER_DEBUG, 0, &mEffect, &errors)); if (errors) MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0); mAmbientColHandle = mEffect->GetParameterByName(0, "gAmbientMtrl"); mDIffuseColHandle = mEffect->GetParameterByName(0, "gDiffuseMtrl"); mSpecularColHandle = mEffect->GetParameterByName(0, "gSpecularMtrl"); mAmbientLightHandle = mEffect->GetParameterByName(0, "gAmbientLight"); mDIffuseLightHandle = mEffect->GetParameterByName(0, "gDiffuseLight"); mSpecularLightHandle = mEffect->GetParameterByName(0, "gSpecularLight"); mShininessHandle = mEffect->GetParameterByName(0, "gSpecularPower"); //mAttenuationHandle = mEffect->GetParameterByName(0, "gAttenuation012");}void BaseMaterial::Render(D3DXMATRIX& worldMat, D3DXMATRIX& view, D3DXMATRIX& projection, D3DXVECTOR3 lightDirection, ID3DXMesh* mesh){ mTech = mEffect->GetTechniqueByName("AmbientDiffuseSpecTech"); HR(mEffect->SetTechnique(mTech)); mWorldMatHandle = mEffect->GetParameterByName(0, "gWorld"); mViewProjectionMatHandle = mEffect->GetParameterByName(0, "gWVP"); mWorldInvTransHandle = mEffect->GetParameterByName(0, "gWorldInverseTranspose"); mLightDirHandle = mEffect->GetParameterByName(0, "gLightVecW"); //lightDirection = D3DXVECTOR3(0, 0, -1); mViewerPosWHandle = mEffect->GetParameterByName(0, "gEyePosW"); D3DXMATRIX W, WIT; D3DXMatrixIdentity(&W); D3DXMatrixInverse(&WIT, 0, &W); D3DXMatrixTranspose(&WIT, &WIT); //Begin passes UINT numPasses = 0; HR(mEffect->Begin(&numPasses, 0)); for (UINT i = 0; i < numPasses; ++i) { HR(mEffect->BeginPass(i)); gd3dDevice->SetVertexDeclaration(VertexPN::Decl); //Matricies HR(mEffect->SetMatrix(mWorldMatHandle, &worldMat)); HR(mEffect->SetMatrix(mViewProjectionMatHandle, &(worldMat * view * projection))); HR(mEffect->SetMatrix(mWorldInvTransHandle, &WIT)); // Light HR(mEffect->SetValue(mLightDirHandle, &lightDirection, sizeof(D3DXVECTOR3))); //Ambient + Diffuse + Specular HR(mEffect->SetValue(mAmbientColHandle, &mAmbientColor, sizeof(D3DXCOLOR))); HR(mEffect->SetValue(mDIffuseColHandle, &mDiffuseColor, sizeof(D3DXCOLOR))); HR(mEffect->SetValue(mSpecularColHandle, &mSpecularColor, sizeof(D3DXCOLOR))); HR(mEffect->SetValue(mAmbientLightHandle, &mAmbientLightColor, sizeof(D3DXCOLOR))); HR(mEffect->SetValue(mDIffuseLightHandle, &mDiffuseLightColor, sizeof(D3DXCOLOR))); HR(mEffect->SetValue(mSpecularLightHandle, &mSpecularLightColor, sizeof(D3DXCOLOR))); //HR(mEffect->SetValue(mAttenuationHandle, &mAttenuation, sizeof(D3DXCOLOR))); HR(mEffect->SetFloat(mShininessHandle, m_Shininess)); HR(mEffect->CommitChanges()); //Mesh HR(gd3dDevice->SetTransform(D3DTS_WORLD, &worldMat)); HR(gd3dDevice->SetTransform(D3DTS_VIEW, &view)); HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &projection)); mesh->DrawSubset(0); HR(mEffect->EndPass()); } mEffect->End();}
Implying half the people here understands that shit
Quote from: Ingloriouswho98 on February 28, 2015, 01:58:42 PMImplying half the people here understands that shitBasically all that code, minus the shader file, which I didn't include, is responsible for making any 3D object you ever see on a screen not look like this:Aka, giving it color, visible depth, making it sensitive to light, changing how it would reflect light based on what angle you're looking at it, etc... (aka aka ambient + diffuse + specular lighting)I think that's pretty crazy.
Quote from: Cocos on February 28, 2015, 02:10:35 PMQuote from: Ingloriouswho98 on February 28, 2015, 01:58:42 PMImplying half the people here understands that shitBasically all that code, minus the shader file, which I didn't include, is responsible for making any 3D object you ever see on a screen not look like this:Aka, giving it color, visible depth, making it sensitive to light, changing how it would reflect light based on what angle you're looking at it, etc... (aka aka ambient + diffuse + specular lighting)I think that's pretty crazy.I figured as much but I still can't decipher it
Good job you made a teapot do you want a sticker
IM IN LOVE WITH THE COCO