// *** OpenGL Program that draws a polygon mesh of a pyramid in 3D ***

// ogl3d03View.cpp : implementation of the COgl3d03View class
//

#include "stdafx.h"
#include "ogl3d03.h"

#include "ogl3d03Doc.h"
#include "ogl3d03View.h"

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

/////////////////////////////////////////////////////////////////////////////
// COgl3d03View

IMPLEMENT_DYNCREATE(COgl3d03View, CView)

BEGIN_MESSAGE_MAP(COgl3d03View, CView)
        //{{AFX_MSG_MAP(COgl3d03View)
        ON_WM_CREATE()
        ON_WM_DESTROY()
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COgl3d03View construction/destruction

COgl3d03View::COgl3d03View()
{
        // TODO: add construction code here

}

COgl3d03View::~COgl3d03View()
{
}

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

        return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// COgl3d03View drawing

void COgl3d03View::OnDraw(CDC* pDC)
{
        COgl3d03Doc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        // TODO: add draw code for native data here
    wglMakeCurrent(pDC->m_hDC, m_hRC);
    DrawWithOpenGL();
    wglMakeCurrent(pDC->m_hDC, NULL);

}

/////////////////////////////////////////////////////////////////////////////
// COgl3d03View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// COgl3d03View message handlers

int COgl3d03View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
        if (CView::OnCreate(lpCreateStruct) == -1)
                return -1;
        
        // TODO: Add your specialized creation code here
    PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR), // Structure size.
        1,                             // Structure version number.
        PFD_DRAW_TO_WINDOW |           // Property flags.
            PFD_SUPPORT_OPENGL,
        PFD_TYPE_RGBA,
        24,                            // 24-bit color.
        0, 0, 0, 0, 0, 0,              // Not concerned with these.
        0, 0, 0, 0, 0, 0, 0,           // No alpha or accum buffer.
        32,                            // 32-bit depth buffer.
        0, 0,                          // No stencil or aux buffer.
        PFD_MAIN_PLANE,                // Main layer type.
        0,                             // Reserved.
        0, 0, 0                        // Unsupported.
    };

    CClientDC clientDC(this);

    int pixelFormat =
        ChoosePixelFormat(clientDC.m_hDC, &pfd);
    BOOL success =
        SetPixelFormat(clientDC.m_hDC, pixelFormat, &pfd);

    m_hRC = wglCreateContext(clientDC.m_hDC);

        
        return 0;
}

void COgl3d03View::OnDestroy() 
{
        CView::OnDestroy();
        
        // TODO: Add your message handler code here
    wglDeleteContext(m_hRC);
        
}

void COgl3d03View::DrawWithOpenGL()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.1f, 0.1f, 0.1f);
    //glScalef(m_xScale, m_yScale, m_zScale);
    glRotatef(20, 1.0f, 0.0f, 0.0f);
    glRotatef(15, 0.0f, 1.0f, 0.0f);
    glRotatef(30, 0.0f, 0.0f, 1.0f);

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f, 0.0f, 0.0f);

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    glBegin(GL_POLYGON);
        glVertex3f(-0.5f, 0.0f, -0.5f);
        glVertex3f(-0.5f, 0.0f, 0.5f);
        glVertex3f(0.5f, 0.0f, 0.5f);
        glVertex3f(0.5f, 0.0f, -0.5f);
    glEnd();

    glBegin(GL_POLYGON);
        glVertex3f(-0.5f, 0.0f, 0.5f);
        glVertex3f(0.5f, 0.0f, 0.5f);
        glVertex3f(0.0f, 0.5f, 0.0f);
    glEnd();

    glBegin(GL_POLYGON);
        glVertex3f(0.5f, 0.0f, 0.5f);
        glVertex3f(0.5f, 0.0f, -0.5f);
        glVertex3f(0.0f, 0.5f, 0.0f);
    glEnd();

    glBegin(GL_POLYGON);
        glVertex3f(0.5f, 0.0f, -0.5f);
        glVertex3f(-0.5f, 0.0f, -0.5f);
        glVertex3f(0.0f, 0.5f, 0.0f);
    glEnd();

    glBegin(GL_POLYGON);
        glVertex3f(-0.5f, 0.0f, -0.5f);
        glVertex3f(-0.5f, 0.0f, 0.5f);
        glVertex3f(0.0f, 0.5f, 0.0f);
    glEnd();

    glFlush();
}