3dMaterial.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:7k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dMaterial.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- /////////////////////////////////////////////////////////////////////////////
- #include <windows.h>
- #include "stdafx.h"
- #include "glOOP.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dMaterial
- IMPLEMENT_DYNAMIC(C3dMaterial, CObject)
- //IMPLEMENT_SERIAL(C3dMaterial, CObject, 0)
- /////////////////////////////////////////////////////////////////////////////
- // C3dMaterial construction
- C3dMaterial::C3dMaterial()
- {
- // Assign default values to member attributes..
- m_szName.GetBuffer(128); // allocate 128 bytes min.
- m_szName.Format("Shiny");
- m_ColorAmbient.SetColor4f(0.3f, 0.3f, 0.3f, 1.0f);
- m_ColorDiffuse.SetColor4f(0.8f, 0.8f, 0.8f, 1.0f);
- m_ColorSpecular.SetColor4f(1.0f, 1.0f, 1.0f, 1.0f);
- m_ColorEmmissive.SetColor4f(0.0f, 0.0f, 0.0f, 1.0f);
- m_fSpecularPower = 20.0f;
- m_fIOR = 0.0f;
- m_fReflection = 0.0f;
- m_fTranslucency = 0.0f;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dMaterial Destructor
- C3dMaterial::~C3dMaterial()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dMaterial Implimentation
- C3dMaterial* C3dMaterial::Create()
- {
- C3dMaterial* pMaterial = new C3dMaterial;
- ASSERT(pMaterial);
- return pMaterial;
- }
- C3dMaterial* C3dMaterial::Create(CString szName,
- GLfloat ar, GLfloat ag, GLfloat ab, GLfloat aa,
- GLfloat dr, GLfloat dg, GLfloat db, GLfloat da,
- GLfloat sr, GLfloat sg, GLfloat sb, GLfloat sa,
- GLfloat er, GLfloat eg, GLfloat eb, GLfloat ea,
- GLfloat power,
- GLfloat IOR,
- GLfloat reflection,
- GLfloat translucency)
- {
- C3dMaterial* pMaterial = new C3dMaterial;
- ASSERT(pMaterial);
- pMaterial->m_szName = szName;
- pMaterial->m_ColorAmbient.SetColor4f(ar, ag, ab, aa);
- pMaterial->m_ColorDiffuse.SetColor4f(dr, dg, db, da);
- pMaterial->m_ColorSpecular.SetColor4f(sr, sg, sb, sa);
- pMaterial->m_ColorEmmissive.SetColor4f(er, eg, eb, ea);
- pMaterial->m_fSpecularPower = power;
- pMaterial->m_fIOR = IOR;
- pMaterial->m_fReflection = reflection;
- pMaterial->m_fTranslucency = translucency;
- return pMaterial;
- }
- void C3dMaterial::Delete(C3dMaterial* pMaterial)
- {
- if(pMaterial) {
- delete pMaterial;
- }
- }
- void C3dMaterial::SetMaterialAttrib()
- {
- // Set the objects material properties
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_ColorAmbient.m_fColor);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_ColorDiffuse.m_fColor);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_ColorSpecular.m_fColor);
- glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, m_ColorEmmissive.m_fColor);
- glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m_fSpecularPower);
- }
- void C3dMaterial::ResetMaterialAttrib()
- {
- GLfloat ambColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
- GLfloat difColor[] = {0.8f, 0.8f, 0.8f, 1.0f};
- GLfloat spcColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
- GLfloat emmColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
- // Set material properties to default values
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambColor);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, difColor);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spcColor);
- glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emmColor);
- glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
- }
- void C3dMaterial::Serialize(CArchive& ar, int iVersion)
- {
- CString szBuffer;
- if (ar.IsStoring())
- {
- // Save the Material header
- szBuffer.Format("Material {n");
- ar.WriteString(szBuffer);
- // Save the Material Data...
- szBuffer.Format("tName < %s >n", m_szName);
- ar.WriteString(szBuffer);
- szBuffer.Format("tAmbientColor < %f %f %f %f >n", m_ColorAmbient.m_fColor[0], m_ColorAmbient.m_fColor[1], m_ColorAmbient.m_fColor[2], m_ColorAmbient.m_fColor[3]);
- ar.WriteString(szBuffer);
- szBuffer.Format("tDiffuseColor < %f %f %f %f >n", m_ColorDiffuse.m_fColor[0], m_ColorDiffuse.m_fColor[1], m_ColorDiffuse.m_fColor[2], m_ColorDiffuse.m_fColor[3]);
- ar.WriteString(szBuffer);
- szBuffer.Format("tSpecularColor < %f %f %f %f >n", m_ColorSpecular.m_fColor[0], m_ColorSpecular.m_fColor[1], m_ColorSpecular.m_fColor[2], m_ColorSpecular.m_fColor[3]);
- ar.WriteString(szBuffer);
- szBuffer.Format("tSpecularPower < %f >n", m_fSpecularPower);
- ar.WriteString(szBuffer);
- szBuffer.Format("tReflection < %f >n", m_fReflection);
- ar.WriteString(szBuffer);
- szBuffer.Format("tTranslucency < %f >n", m_fTranslucency);
- ar.WriteString(szBuffer);
- szBuffer.Format("tIndexOfRefraction < %f >n", m_fIOR);
- ar.WriteString(szBuffer);
- szBuffer.Format("}nn"); // end of material def
- ar.WriteString(szBuffer);
- }
- else
- {
- // Read the Material data...
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft(); // Remove leading white spaces
- sscanf(szBuffer, "Name < %s >n", m_szName);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "AmbientColor < %f %f %f %f >n", &m_ColorAmbient.m_fColor[0], &m_ColorAmbient.m_fColor[1], &m_ColorAmbient.m_fColor[2], &m_ColorAmbient.m_fColor[3]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "DiffuseColor < %f %f %f %f >n", &m_ColorDiffuse.m_fColor[0], &m_ColorDiffuse.m_fColor[1], &m_ColorDiffuse.m_fColor[2], &m_ColorDiffuse.m_fColor[3]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "SpecularColor < %f %f %f %f >n", &m_ColorSpecular.m_fColor[0], &m_ColorSpecular.m_fColor[1], &m_ColorSpecular.m_fColor[2], &m_ColorSpecular.m_fColor[3]);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "SpecularPower < %f >n", &m_fSpecularPower);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Reflection < %f >n", &m_fReflection);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "Translucency < %f >n", &m_fTranslucency);
- ar.ReadString(szBuffer);
- szBuffer.TrimLeft();
- sscanf(szBuffer, "IndexOfRefraction < %f >n", &m_fIOR);
- }
- }