📄 entityview.cpp
字号:
// EntityView.cpp : implementation file
//
#include "stdafx.h"
#include "SlsPrj.h"
#include "EntityView.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "TransPropertySheet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEntityView
double CEntityView::m_colorBlue = 0.0;
double CEntityView::m_colorGreen = 1.0;
double CEntityView::m_colorRed = 0.0;
IMPLEMENT_DYNCREATE(CEntityView, CBaseView)
CEntityView::CEntityView()
{
m_rotationAngle = 90;
m_zoomRatio = 1.0;
m_randomRotationAngle = 0.0;
m_bRandomRotation = FALSE;
}
CEntityView::~CEntityView()
{
}
BEGIN_MESSAGE_MAP(CEntityView, CBaseView)
//{{AFX_MSG_MAP(CEntityView)
ON_COMMAND(ID_ROTATION, OnRotation)
ON_COMMAND(ID_COLOR, OnColor)
ON_COMMAND(ID_ROTATION_X, OnRotationX)
ON_COMMAND(ID_ROTATION_Y, OnRotationY)
ON_COMMAND(ID_ROTATION_Z, OnRotationZ)
ON_COMMAND(ID_TRANSFORM, OnTransform)
ON_COMMAND(ID_ZOOM, OnZoom)
//}}AFX_MSG_MAP
ON_MESSAGE(ID_MSG_ENTITYVIEWCREATED, OnEntityViewCreated)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEntityView drawing
void CEntityView::OnDraw(CDC* pDC)
{
CBaseView::OnDraw(pDC);
post_gl_shade( );
}
/////////////////////////////////////////////////////////////////////////////
// CEntityView diagnostics
#ifdef _DEBUG
void CEntityView::AssertValid() const
{
CBaseView::AssertValid();
}
void CEntityView::Dump(CDumpContext& dc) const
{
CBaseView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEntityView message handlers
void CEntityView::OnInitialUpdate()
{
CBaseView::OnInitialUpdate();
PostMessage(ID_MSG_ENTITYVIEWCREATED);
}
LONG CEntityView::OnEntityViewCreated(UINT, LONG)
{
CSlsApp* pSlsApp=(CSlsApp* )AfxGetApp();
if(!CreateNewWindow(pSlsApp->m_pCurrentLayerViewDocTemplate))
return -1;
return 0;
}
void CEntityView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
if((lHint == ID_HINT_ONEVIEW) || (lHint == ID_HINT_TWOVIEWS))
{
CBaseView::OnUpdate(pSender, lHint, pHint);
}
}
void CEntityView::post_gl_shade( )
{
CStlDoc* pDoc = GetDocument();
ASSERT(pDoc);
int sizeX = m_clientRect.Width();
int sizeY = m_clientRect.Height();
CSTLSolid* pSTLSolid = &(pDoc->m_pathData.m_Solid);
// Set the pixel format of the device context
CDC* pDC = new CClientDC(this);
HDC hDC = pDC->GetSafeHdc();
SetDCPixelFormat(hDC);
// create a rendering context
m_hglrc = wglCreateContext(hDC);
// make it the calling thread's current rendering context
wglMakeCurrent (hDC, m_hglrc);
// Set Properties for materials and light source
SetLightingProperties();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double Post_model_radius;
double Post_model_center[3];
double xDist = pSTLSolid->m_xMax - pSTLSolid->m_xMin;
double yDist = pSTLSolid->m_yMax - pSTLSolid->m_yMin;
double zDist = pSTLSolid->m_zMax - pSTLSolid->m_zMin;
Post_model_center[0]=(pSTLSolid->m_xMin+pSTLSolid->m_xMax)/2;
Post_model_center[1]=(pSTLSolid->m_yMin+pSTLSolid->m_yMax)/2;
Post_model_center[2]=(pSTLSolid->m_zMin+pSTLSolid->m_zMax)/2;
Post_model_radius=sqrt(xDist*xDist + yDist*yDist + zDist*zDist)/2.0;
glViewport(m_clientRect.left+5, m_clientRect.top+5,
m_clientRect.Width()-5, m_clientRect.Height()-5);
if(sizeX < sizeY)
{
glOrtho(-Post_model_radius, Post_model_radius,
-Post_model_radius * (GLfloat)sizeY / (GLfloat)sizeX,
Post_model_radius * (GLfloat)sizeY / (GLfloat)sizeX,
-10.0 * Post_model_radius, 100.0 * Post_model_radius);
}
else
{
glOrtho(-Post_model_radius * (GLfloat)sizeX / (GLfloat)sizeY,
Post_model_radius * (GLfloat)sizeX / (GLfloat)sizeY,
-Post_model_radius, Post_model_radius,
-10.0 * Post_model_radius, 100.0 * Post_model_radius);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0 + Post_model_center[0], 1.0 + Post_model_center[1],
1.0 + Post_model_center[2], Post_model_center[0],
Post_model_center[1], Post_model_center[2],
0.0, 0.0, 1.0);
if(m_bRandomRotation)
{
glTranslated(Post_model_center[0], Post_model_center[1], Post_model_center[2]);
glRotated(m_randomRotationAngle, 1.0, 0.0, 0.0);
glRotated(m_randomRotationAngle, 0.0, 1.0, 0.0);
glRotated(m_randomRotationAngle, 0.0, 0.0, 1.0);
glTranslated(-Post_model_center[0], -Post_model_center[1], -Post_model_center[2]);
}
post_shade_3d_model(pSTLSolid);
}
void CEntityView::SetDCPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int iPixelFormat;
// get the best available match of pixel format for the device context
iPixelFormat = ChoosePixelFormat(hdc, &pfd);
// make that the pixel format of the device context
SetPixelFormat(hdc, iPixelFormat, &pfd);
}
void CEntityView::SetLightingProperties()
{
float light_pos0[]={ (float)1.0, (float)1.0, (float)1.0, (float)0.0};
float light_pos1[]={(float)-1.0, (float)-1.0, (float)-1.0, (float)0.0};
float light_ambient0[] = {(float)0.6, (float)0.6, (float)0.6, (float)1.0};
float light_ambient1[] = {(float)0.4, (float)0.4, (float)0.4, (float)1.0};
float light_diffuse0[] = {(float)0.6, (float)0.9, (float)0.6, (float)1.0};
float light_diffuse1[] = {(float)0.2, (float)0.4, (float)0.2, (float)1.0};
float mat_shininess0[] = {(float)100.0};
float mat_shininess1[] = {(float)50.0};
float mat_specular0[] = {(float)1.0, (float)1.0, (float)1.0, (float)1.0};
float mat_specular1[] = {(float)0.0, (float)0.5, (float)0.0, (float)1.0};
float mat_diffuse0[] = {(float)m_colorRed, (float)m_colorGreen, (float)m_colorBlue, (float)1.0};
float mat_diffuse1[] = {(float)0.0, (float)0.5, (float)0.0, (float)1.0};
float model_ambient[] = {(float)0.2, (float)0.5, (float)0.2, (float)1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse0);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess0);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glLightfv(GL_LIGHT1, GL_POSITION, light_pos1);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
glEnable(GL_LIGHT1);
glMaterialfv(GL_BACK, GL_SHININESS, mat_shininess1);
glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular1);
glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse1);
glColorMaterial(GL_BACK, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void CEntityView::post_shade_3d_model(CSTLSolid* pSTLSolid)
{
CStlDoc* pDoc = GetDocument();
ASSERT(pDoc);
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable (GL_DEPTH_TEST);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
int vertex_index;
double normal_array[3];
double vertex_array[3];
float color_array[] = {1.0, 1.0, 1.0};
if(pSTLSolid->m_bInitialized)
{
glBegin(GL_TRIANGLES);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -