Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
16
Добавлен:
01.05.2014
Размер:
6.91 Кб
Скачать
// lab3View.cpp : implementation of the CLab3View class
//

#include "stdafx.h"
#include "lab3.h"

#include "lab3Doc.h"
#include "lab3View.h"
#include "mainfrm.h"

#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLab3View

IMPLEMENT_DYNCREATE(CLab3View, CView)

BEGIN_MESSAGE_MAP(CLab3View, CView)
	//{{AFX_MSG_MAP(CLab3View)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLab3View construction/destruction

CLab3View::CLab3View(): delta(0.1)
{
	// TODO: add construction code here
	m_fAction = FALSE;
	x=0;
	y=0;
	z=5;
}

CLab3View::~CLab3View()
{
}

BOOL CLab3View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLab3View drawing

void CLab3View::OnDraw(CDC* pDC)
{
	CLab3Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	DrawScene();
}

/////////////////////////////////////////////////////////////////////////////
// CLab3View diagnostics

#ifdef _DEBUG
void CLab3View::AssertValid() const
{
	CView::AssertValid();
}

void CLab3View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CLab3Doc* CLab3View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLab3Doc)));
	return (CLab3Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLab3View message handlers

int CLab3View::SetWindowPixelFormat(HDC hDC)
{
	int m_GLPixelIndex;
	PIXELFORMATDESCRIPTOR pfd;

	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion = 1;

	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cRedBits = 8;
	pfd.cRedShift = 16;
	pfd.cGreenBits = 8;
	pfd.cGreenShift = 8;
	pfd.cBlueBits = 8;
	pfd.cBlueShift = 0;
	pfd.cAlphaBits = 0;
	pfd.cAlphaShift = 0;
	pfd.cAccumBits = 64;
	pfd.cAccumGreenBits = 16;
	pfd.cAccumBlueBits = 16;
	pfd.cAccumRedBits = 16;
	pfd.cAccumAlphaBits = 0;
	pfd.cDepthBits = 32;
	pfd.cStencilBits = 8;
	pfd.cAuxBuffers = 0;
	pfd.iLayerType = PFD_MAIN_PLANE;
	pfd.bReserved = 0;
	pfd.dwLayerMask = 0;
	pfd.dwVisibleMask = 0;
	pfd.dwDamageMask = 0;

	m_GLPixelIndex = ChoosePixelFormat (hDC, &pfd);

	if (m_GLPixelIndex == 0)
		//Let's choose a default index
	{
		m_GLPixelIndex = 1;
		if (DescribePixelFormat (hDC, m_GLPixelIndex, sizeof (PIXELFORMATDESCRIPTOR), &pfd) == 0)
			return 0;
	}

	if (SetPixelFormat (hDC, m_GLPixelIndex, &pfd) == FALSE)
		return 0;

	return 1;
}

int CLab3View::DrawScene()
{

	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	 glPushMatrix(); //сохранить матрицу в стек

	 DrawCat();

  	 glPopMatrix(); //вытащить матрицу из стека

	 glFinish();
	 SwapBuffers(wglGetCurrentDC());

	 return 0;

}

int CLab3View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

	pdc = new CClientDC(this);

	if (SetWindowPixelFormat(pdc->m_hDC) == FALSE)
		return -1;

	hGLRC = wglCreateContext (pdc->m_hDC);
	if (hGLRC == NULL)
		return -1;

	if (wglMakeCurrent(pdc->m_hDC, hGLRC) == FALSE)
		return -1;

	glEnable(GL_ALPHA_TEST);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
//	glEnable(GL_LIGHT1);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	float pos1[4] = {3,3,5,1};
	float dir1[3] = {-1,-1,-1};
//	float pos2[4] = {-3,3,3,1};
//	float dir2[3] = {1,1,-1};

	glLightfv(GL_LIGHT0, GL_POSITION, pos1);
	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir1);

//	glLightfv(GL_LIGHT1, GL_POSITION, pos2);
//	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, dir2);

	return 0;
}

void CLab3View::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	if (wglGetCurrentContext()!=NULL)
		wglMakeCurrent(NULL,NULL);

	if (hGLRC!=NULL)
	{
		wglDeleteContext(hGLRC);
		hGLRC=NULL;
	};

	delete pdc;

	CView::OnDestroy();

	
}

void CLab3View::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here

	glViewport(0,0,cx,cy);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-5,5, -5,5,2,12);
	gluLookAt(x,y,z,0,0,0,0,1,0);
	glMatrixMode(GL_MODELVIEW);
	
}

int CLab3View::DrawCat()
{
	 //glRotated(90,1,0,0);

	 glPushMatrix(); //сохранить матрицу в стек
 
	 glColor3d(1,0.6,0);
	 auxSolidSphere(2.0);

	 glTranslated(1.43,1.43,0);
	 glRotated(-45,1,0,0);
 	 glRotated(10,0,1,0);
	 auxSolidTetrahedron(1);

 	 glPopMatrix(); //сохранить матрицу в стек
	 glPushMatrix(); //сохранить матрицу в стек

	 glTranslated(-1.43,1.43,0);
  	 glRotated(45,1,0,0);
	 glRotated(10,0,0,1);
	 auxSolidTetrahedron(1);

	 glPopMatrix(); //сохранить матрицу в стек
	 glPushMatrix(); //сохранить матрицу в стек

	 glColor3d(0.6,0.4,0.3);
	 glTranslated(0,-1,1.75);
	 auxSolidSphere(0.3);

	 glPopMatrix();
 	 glPushMatrix(); //сохранить матрицу в стек

	 glColor3d(0,0.8,0.4);
	 glTranslated(0.8,0,1.75);
 	 auxSolidSphere(0.2);

	 glPopMatrix();
  	 glPushMatrix(); //сохранить матрицу в стек

	 glColor3d(0,0.8,0.4);
	 glTranslated(-0.8,0,1.75);
 	 auxSolidSphere(0.2);

	 glPopMatrix();

	 glColor3d(1,1,1);
	 glBegin(GL_LINES);
		glVertex3d(0,0,0);
		glVertex3d(3,-3,3);

		glVertex3d(0,0,0);
		glVertex3d(3.2,-1,3);

		glVertex3d(0,0,0);
		glVertex3d(3.6,-2,2.5);

		glVertex3d(0,0,0);
		glVertex3d(-3,-3,3);

		glVertex3d(0,0,0);
		glVertex3d(-3.2,-1,3);

		glVertex3d(0,0,0);
		glVertex3d(-3.6,-2,2.5);

	 glEnd();

	 return 1;

}

void CLab3View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_fAction)
	{
	};
	
	CView::OnMouseMove(nFlags, point);
}

void CLab3View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_fAction=FALSE;
	
	CView::OnLButtonDown(nFlags, point);
}

void CLab3View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_fAction=TRUE;
	
	CView::OnLButtonUp(nFlags, point);
}
Соседние файлы в папке Лабораторная работа №37