EditMaterialDlg.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:16k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // EditMaterialDlg.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 "stdafx.h"
- #include "glOOP.h"
- #include "MyColorPaletteWnd.h"
- #include "EditColorDlg.h"
- #include "EditNameDlg.h"
- #include "EditMaterialDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CEditMaterialDlg dialog
- CEditMaterialDlg::CEditMaterialDlg(C3dMaterialList* pMatlList, C3dColorList* pColorList, CWnd* pParent /*=NULL*/)
- : CDialog(CEditMaterialDlg::IDD, pParent)
- {
- m_pMatlList = pMatlList;
- m_pColorList = pColorList;
- m_pLastMatl = NULL;
- //{{AFX_DATA_INIT(CEditMaterialDlg)
- m_fIndexOfRefraction = 0.0f;
- m_fSpecularPower = 0.0f;
- m_fTranslucency = 0.0f;
- m_szName = _T("");
- //}}AFX_DATA_INIT
- }
- void CEditMaterialDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CEditMaterialDlg)
- DDX_Control(pDX, IDC_COMBO_EMM_COLOR, m_ComboEmm);
- DDX_Control(pDX, IDC_COMBO_SPC_COLOR, m_ComboSpc);
- DDX_Control(pDX, IDC_COMBO_DIF_COLOR, m_ComboDif);
- DDX_Control(pDX, IDC_COMBO_AMB_COLOR, m_ComboAmb);
- DDX_Control(pDX, IDC_MATERIAL_LIST, m_ListBox);
- DDX_Text(pDX, IDC_INDEX_OF_REFRACTION, m_fIndexOfRefraction);
- DDV_MinMaxFloat(pDX, m_fIndexOfRefraction, 0.f, 10.f);
- DDX_Text(pDX, IDC_SPC_POWER, m_fSpecularPower);
- DDV_MinMaxFloat(pDX, m_fSpecularPower, 0.f, 128.f);
- DDX_Text(pDX, IDC_TRANSLUCENCY, m_fTranslucency);
- DDV_MinMaxFloat(pDX, m_fTranslucency, 0.f, 1.f);
- DDX_Text(pDX, IDC_MATERIAL_NAME, m_szName);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CEditMaterialDlg, CDialog)
- //{{AFX_MSG_MAP(CEditMaterialDlg)
- ON_LBN_SELCHANGE(IDC_MATERIAL_LIST, OnSelchangeMaterialList)
- ON_WM_SHOWWINDOW()
- ON_EN_KILLFOCUS(IDC_SPC_POWER, OnKillfocusSpcPower)
- ON_EN_KILLFOCUS(IDC_INDEX_OF_REFRACTION, OnKillfocusIndexOfRefraction)
- ON_EN_KILLFOCUS(IDC_TRANSLUCENCY, OnKillfocusTranslucency)
- ON_BN_CLICKED(ID_NEW, OnNew)
- ON_BN_CLICKED(ID_DELETE, OnDelete)
- ON_WM_DRAWITEM()
- ON_BN_CLICKED(IDC_BUTTON_AMB_COLOR_WND, OnButtonAmbColor)
- ON_BN_CLICKED(IDC_BUTTON_DIF_COLOR_WND, OnButtonDifColor)
- ON_BN_CLICKED(IDC_BUTTON_SPC_COLOR_WND, OnButtonSpcColor)
- ON_BN_CLICKED(IDC_BUTTON_EMM_COLOR_WND, OnButtonEmmColor)
- ON_CBN_SELCHANGE(IDC_COMBO_AMB_COLOR, OnSelchangeComboAmbColor)
- ON_CBN_SELCHANGE(IDC_COMBO_DIF_COLOR, OnSelchangeComboDifColor)
- ON_CBN_SELCHANGE(IDC_COMBO_SPC_COLOR, OnSelchangeComboSpcColor)
- ON_CBN_SELCHANGE(IDC_COMBO_EMM_COLOR, OnSelchangeComboEmmColor)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CEditMaterialDlg message handlers
- BOOL CEditMaterialDlg::OnInitDialog()
- {
- // let the base class do the default work
- CDialog::OnInitDialog();
- // Create the button color windows
- CRect rect;
- GetDlgItem(IDC_BUTTON_AMB_COLOR_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- m_wndAmbButtonColor.Create(NULL, // lpszClassName
- NULL, // lpszWindowName
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_BUTTON_AMB_COLOR_WND,// UINT nID
- 0); // pContext
- GetDlgItem(IDC_BUTTON_DIF_COLOR_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- m_wndDifButtonColor.Create(NULL, // lpszClassName
- NULL, // lpszWindowName
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_BUTTON_DIF_COLOR_WND,// UINT nID
- 0); // pContext
- GetDlgItem(IDC_BUTTON_SPC_COLOR_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- m_wndSpcButtonColor.Create(NULL, // lpszClassName
- NULL, // lpszWindowName
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_BUTTON_SPC_COLOR_WND,// UINT nID
- 0); // pContext
- GetDlgItem(IDC_BUTTON_EMM_COLOR_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- m_wndEmmButtonColor.Create(NULL, // lpszClassName
- NULL, // lpszWindowName
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_BUTTON_EMM_COLOR_WND,// UINT nID
- 0); // pContext
- GetDlgItem(IDC_MATERIAL_PREVIEW_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- m_wndMaterialPreview.Create(NULL, // dwExtStyle
- NULL, // lpszClassName
- NULL, // lpszWindowName
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_MATERIAL_PREVIEW_WND,// UINT nID
- 0); // pContext
- // Load the material list box with the supplied
- // material list
- C3dMaterial* pMatl = NULL;
- if(m_pMatlList)
- {
- pMatl = m_pMatlList->LoadListBox(&m_ListBox);
- if(pMatl)
- // Display the materials values
- LoadDialogData(pMatl);
- }
- // Load the color combo boxes with the supplied
- // color list
- if(m_pColorList)
- {
- if(pMatl)
- {
- m_pColorList->LoadComboBox(&m_ComboAmb, &pMatl->m_ColorAmbient);
- m_pColorList->LoadComboBox(&m_ComboDif, &pMatl->m_ColorDiffuse);
- m_pColorList->LoadComboBox(&m_ComboSpc, &pMatl->m_ColorSpecular);
- m_pColorList->LoadComboBox(&m_ComboEmm, &pMatl->m_ColorEmmissive);
- }
- else
- {
- m_pColorList->LoadComboBox(&m_ComboAmb, NULL);
- m_pColorList->LoadComboBox(&m_ComboDif, NULL);
- m_pColorList->LoadComboBox(&m_ComboSpc, NULL);
- m_pColorList->LoadComboBox(&m_ComboEmm, NULL);
- }
- }
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CEditMaterialDlg::OnSelchangeMaterialList()
- {
- C3dMaterial* pMatl;
- // Get the zero-based index of the item selected
- int index = m_ListBox.GetCurSel();
- // Save the last selected material and reload the list
- // box. (The user may have changed the name of the
- // material..)
- if(m_pLastMatl)
- {
- SaveDialogData(m_pLastMatl);
- // pMatl = LoadListBox();
- }
- // Cast the user selected list box items' lParam to
- // a C3dMaterial pointer..
- pMatl = (C3dMaterial*)m_ListBox.GetItemData(index);
- if(pMatl)
- {
- // Display the materials values
- LoadDialogData(pMatl);
- // Repaint our material's color buttons
- PaintButtonWnd(&m_wndAmbButtonColor, &m_pLastMatl->m_ColorAmbient);
- PaintButtonWnd(&m_wndDifButtonColor, &m_pLastMatl->m_ColorDiffuse);
- PaintButtonWnd(&m_wndSpcButtonColor, &m_pLastMatl->m_ColorSpecular);
- PaintButtonWnd(&m_wndEmmButtonColor, &m_pLastMatl->m_ColorEmmissive);
- }
- }
- void CEditMaterialDlg::OnOK()
- {
- if(m_pLastMatl) {
- // Save the dialog data to the material
- SaveDialogData(m_pLastMatl);
- }
- CDialog::OnOK();
- }
- void CEditMaterialDlg::LoadDialogData(C3dMaterial* pMatl)
- {
- // Set our local values to the materials' values
- m_szName = pMatl->m_szName;
- m_fSpecularPower = pMatl->m_fSpecularPower;
- m_fIndexOfRefraction = pMatl->m_fIOR;
- m_fTranslucency = pMatl->m_fTranslucency;
- // Set our material preview windows material to
- // the currently selected material and repaint
- // the window
- m_wndMaterialPreview.m_pMaterial = pMatl;
- m_wndMaterialPreview.InvalidateRect(NULL, FALSE);
- // Dialog box is being initialized (FALSE)
- // or data is being retrieved (TRUE).
- UpdateData(FALSE);
- // Save the pointer
- m_pLastMatl = pMatl;
- }
- void CEditMaterialDlg::SaveDialogData(C3dMaterial* pMatl)
- {
- // Dialog box is being initialized (FALSE)
- // or data is being retrieved (TRUE).
- UpdateData(TRUE);
- // Set our materials values to the local values
- pMatl->m_szName = m_szName;
- pMatl->m_fSpecularPower = m_fSpecularPower;
- pMatl->m_fIOR = m_fIndexOfRefraction;
- pMatl->m_fTranslucency = m_fTranslucency;
- }
- void CEditMaterialDlg::OnShowWindow(BOOL bShow, UINT nStatus)
- {
- CDialog::OnShowWindow(bShow, nStatus);
- if(bShow) // Reload list box
- {
- // Load the material list box with the supplied
- // material list
- C3dMaterial* pMatl = NULL;
- if(m_pMatlList)
- {
- pMatl = m_pMatlList->LoadListBox(&m_ListBox);
- if(pMatl)
- // Display the materials values
- LoadDialogData(pMatl);
- }
- // Load the color combo boxes with the supplied
- // color list
- if(m_pColorList)
- {
- if(pMatl)
- {
- m_pColorList->LoadComboBox(&m_ComboAmb, &pMatl->m_ColorAmbient);
- m_pColorList->LoadComboBox(&m_ComboDif, &pMatl->m_ColorDiffuse);
- m_pColorList->LoadComboBox(&m_ComboSpc, &pMatl->m_ColorSpecular);
- m_pColorList->LoadComboBox(&m_ComboEmm, &pMatl->m_ColorEmmissive);
- }
- else
- {
- m_pColorList->LoadComboBox(&m_ComboAmb, NULL);
- m_pColorList->LoadComboBox(&m_ComboDif, NULL);
- m_pColorList->LoadComboBox(&m_ComboSpc, NULL);
- m_pColorList->LoadComboBox(&m_ComboEmm, NULL);
- }
- }
- }
- }
- void CEditMaterialDlg::ResetDlg(C3dMaterialList* pMatlList, C3dColorList* pColorList)
- {
- m_pMatlList = pMatlList;
- m_pColorList = pColorList;
- m_pLastMatl = NULL;
- }
- void CEditMaterialDlg::OnKillfocusSpcPower()
- {
- if(m_pLastMatl)
- SaveDialogData(m_pLastMatl);
- }
- void CEditMaterialDlg::OnKillfocusIndexOfRefraction()
- {
- if(m_pLastMatl)
- SaveDialogData(m_pLastMatl);
- }
- void CEditMaterialDlg::OnKillfocusTranslucency()
- {
- if(m_pLastMatl)
- SaveDialogData(m_pLastMatl);
- }
- void CEditMaterialDlg::OnNew()
- {
- // Get the name of the new color
- CEditNameDlg newNameDlg(this);
- if(newNameDlg.DoModal() == IDOK)
- {
- if(m_pLastMatl)
- {
- C3dMaterial* pMatl = NULL;
- pMatl = C3dMaterial::Create();
- pMatl->m_szName = newNameDlg.m_szName;
- if(!m_pMatlList->Find(pMatl))
- {
- // pMatl is not in the list, so add to to list,
- // reload the list box, select the material, reload
- // the dialog data.
- m_pMatlList->Append(pMatl);
- m_pMatlList->LoadListBox(&m_ListBox);
- // Display the materials values
- LoadDialogData(pMatl);
- }
- else
- C3dMaterial::Delete(pMatl);
- }
- }
- }
- void CEditMaterialDlg::OnDelete()
- {
- // TODO: Add your control notification handler code here
- }
- void CEditMaterialDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- // Let the base class draw the control item first
- CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
- if(!m_pLastMatl)
- return;
- if(nIDCtl == IDC_BUTTON_AMB_COLOR_WND)
- PaintButtonWnd(&m_wndAmbButtonColor, &m_pLastMatl->m_ColorAmbient);
- if(nIDCtl == IDC_BUTTON_DIF_COLOR_WND)
- PaintButtonWnd(&m_wndDifButtonColor, &m_pLastMatl->m_ColorDiffuse);
- if(nIDCtl == IDC_BUTTON_SPC_COLOR_WND)
- PaintButtonWnd(&m_wndSpcButtonColor, &m_pLastMatl->m_ColorSpecular);
- if(nIDCtl == IDC_BUTTON_EMM_COLOR_WND)
- PaintButtonWnd(&m_wndEmmButtonColor, &m_pLastMatl->m_ColorEmmissive);
- }
- void CEditMaterialDlg::PaintButtonWnd(CWnd* pWnd, C3dColor* pColor)
- {
- CRect rect; // window rect
- CDC* pDC; // pointer to a device context
- // Get the size of the gradient window
- pWnd->GetWindowRect(&rect);
- // Get a pointer to the windows device context
- pDC = pWnd->GetDC();
- if(pDC)
- pDC->FillSolidRect(0,
- 0,
- rect.right-rect.left,
- rect.bottom-rect.top,
- COLORREF RGB(pColor->m_fColor[0]*255,
- pColor->m_fColor[1]*255,
- pColor->m_fColor[2]*255));
- ReleaseDC(pDC);
- }
- void CEditMaterialDlg::OnButtonAmbColor()
- {
- if(m_pLastMatl)
- {
- // Create the dialog
- CEditColorDlg colorDlg(m_pColorList, &m_pLastMatl->m_ColorAmbient, this);
- // Invoke the modal dialog box and return the
- // dialog-box result when done.
- if(colorDlg.DoModal() == IDOK)
- {
- // Save any changes the user may have made
- m_pLastMatl->m_ColorAmbient.SetColor4fv(&colorDlg.m_Color);
- PaintButtonWnd(&m_wndAmbButtonColor, &m_pLastMatl->m_ColorAmbient);
- }
- }
- else
- AfxMessageBox("A Material has not been selected. Create a new material or select one from the list.", MB_OK);
- }
- void CEditMaterialDlg::OnButtonDifColor()
- {
- if(m_pLastMatl)
- {
- // Create the dialog
- CEditColorDlg colorDlg(m_pColorList, &m_pLastMatl->m_ColorDiffuse, this);
- // Invoke the modal dialog box and return the
- // dialog-box result when done.
- if(colorDlg.DoModal() == IDOK)
- {
- // Save any changes the user may have made
- m_pLastMatl->m_ColorDiffuse.SetColor4fv(&colorDlg.m_Color);
- PaintButtonWnd(&m_wndDifButtonColor, &m_pLastMatl->m_ColorDiffuse);
- }
- }
- else
- AfxMessageBox("A Material has not been selected. Create a new material or select one from the list.", MB_OK);
- }
- void CEditMaterialDlg::OnButtonSpcColor()
- {
- if(m_pLastMatl)
- {
- // Create the dialog
- CEditColorDlg colorDlg(m_pColorList, &m_pLastMatl->m_ColorSpecular, this);
- // Invoke the modal dialog box and return the
- // dialog-box result when done.
- if(colorDlg.DoModal() == IDOK)
- {
- // Save any changes the user may have made
- m_pLastMatl->m_ColorSpecular.SetColor4fv(&colorDlg.m_Color);
- PaintButtonWnd(&m_wndSpcButtonColor, &m_pLastMatl->m_ColorSpecular);
- }
- }
- else
- AfxMessageBox("A Material has not been selected. Create a new material or select one from the list.", MB_OK);
- }
- void CEditMaterialDlg::OnButtonEmmColor()
- {
- if(m_pLastMatl)
- {
- // Create the dialog
- CEditColorDlg colorDlg(m_pColorList, &m_pLastMatl->m_ColorEmmissive, this);
- // Invoke the modal dialog box and return the
- // dialog-box result when done.
- if(colorDlg.DoModal() == IDOK)
- {
- // Save any changes the user may have made
- m_pLastMatl->m_ColorEmmissive.SetColor4fv(&colorDlg.m_Color);
- PaintButtonWnd(&m_wndEmmButtonColor, &m_pLastMatl->m_ColorEmmissive);
- }
- }
- else
- AfxMessageBox("A Material has not been selected. Create a new material or select one from the list.", MB_OK);
- }
- void CEditMaterialDlg::OnSelchangeComboAmbColor()
- {
- C3dColor* pColor;
- // Get the zero-based index of the item selected
- int index = m_ComboAmb.GetCurSel();
- // Cast the user selected list box items' lParam to
- // a C3dColor pointer..
- pColor = (C3dColor*)m_ComboAmb.GetItemData(index);
- if(pColor)
- {
- // Set the material's color value and paint the color
- // button window
- if(m_pLastMatl)
- m_pLastMatl->m_ColorAmbient.SetColor4fv(pColor);
- PaintButtonWnd(&m_wndAmbButtonColor, pColor);
- }
- }
- void CEditMaterialDlg::OnSelchangeComboDifColor()
- {
- C3dColor* pColor;
- // Get the zero-based index of the item selected
- int index = m_ComboDif.GetCurSel();
- // Cast the user selected list box items' lParam to
- // a C3dColor pointer..
- pColor = (C3dColor*)m_ComboDif.GetItemData(index);
- if(pColor)
- {
- // Set the material's color value and paint the color
- // button window
- if(m_pLastMatl)
- m_pLastMatl->m_ColorDiffuse.SetColor4fv(pColor);
- PaintButtonWnd(&m_wndDifButtonColor, pColor);
- }
- }
- void CEditMaterialDlg::OnSelchangeComboSpcColor()
- {
- C3dColor* pColor;
- // Get the zero-based index of the item selected
- int index = m_ComboSpc.GetCurSel();
- // Cast the user selected list box items' lParam to
- // a C3dColor pointer..
- pColor = (C3dColor*)m_ComboSpc.GetItemData(index);
- if(pColor)
- {
- // Set the material's color value and paint the color
- // button window
- if(m_pLastMatl)
- m_pLastMatl->m_ColorSpecular.SetColor4fv(pColor);
- PaintButtonWnd(&m_wndSpcButtonColor, pColor);
- }
- }
- void CEditMaterialDlg::OnSelchangeComboEmmColor()
- {
- C3dColor* pColor;
- // Get the zero-based index of the item selected
- int index = m_ComboEmm.GetCurSel();
- // Cast the user selected list box items' lParam to
- // a C3dColor pointer..
- pColor = (C3dColor*)m_ComboEmm.GetItemData(index);
- if(pColor)
- {
- // Set the material's color value and paint the color
- // button window
- if(m_pLastMatl)
- m_pLastMatl->m_ColorEmmissive.SetColor4fv(pColor);
- PaintButtonWnd(&m_wndEmmButtonColor, pColor);
- }
- }