#include "stdafx.h"
#include "GLRenderer.h"
#include <gl\GL.h>
#include <gl\GLU.h>
CGLRenderer::CGLRenderer()
{
look_x = 2.0;
look_y = 1.5;
look_z = 3.0;
postolje = 5.0;
zglob = -40.0;
sijalica = 0.0;
fioka_z = -3.4;
}
CGLRenderer::~CGLRenderer()
{
}
bool CGLRenderer::CreateGLContext(CDC* pDC)
{
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
int nPixelFormat = ChoosePixelFormat(pDC->m_hDC, &pfd);
if (nPixelFormat == 0) return false;
BOOL bResult = SetPixelFormat(pDC->m_hDC, nPixelFormat, &pfd);
if (!bResult) return false;
m_hrc = wglCreateContext(pDC->m_hDC);
if (!m_hrc) return false;
return true;
}
void CGLRenderer::PrepareScene(CDC* pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
wglMakeCurrent(NULL, NULL);
}
void CGLRenderer::DrawScene(CDC* pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(look_x, look_y, look_z, 1.0, 0.5, 1.0, 0.0, 1.0, 0.0);
//DrawXYZ();
DrawTable();
DrawWalls();
DrawLampBasics();
DrawFioka();
glFlush();
SwapBuffers(pDC->m_hDC);
wglMakeCurrent(NULL, NULL);
}
void CGLRenderer::Reshape(CDC* pDC, int w, int h)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40, (double)w / (double)h, 1, 100);
glMatrixMode(GL_MODELVIEW);
wglMakeCurrent(NULL, NULL);
}
void CGLRenderer::DestroyScene(CDC* pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
wglMakeCurrent(NULL, NULL);
if (m_hrc)
{
wglDeleteContext(m_hrc);
m_hrc = NULL;
}
}
void CGLRenderer::DrawXYZ()
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(6.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 3.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 5.0);
glEnd();
}
void CGLRenderer::DrawBox(double a, double b, double c)
{
glBegin(GL_QUAD_STRIP);
glVertex3d(-a / 2, b / 2, c / 2);
glVertex3d(-a / 2, -b / 2, c / 2);
glVertex3d(a / 2, b / 2, c / 2);
glVertex3d(a / 2, -b / 2, c / 2);
glVertex3d(a / 2, b / 2, -c / 2);
glVertex3d(a / 2, -b / 2, -c / 2);
glVertex3d(-a / 2, b / 2, -c / 2);
glVertex3d(-a / 2, -b / 2, -c / 2);
glVertex3d(-a / 2, b / 2, c / 2);
glVertex3d(-a / 2, -b / 2, c / 2);
glEnd();
glBegin(GL_QUADS);
glVertex3d(-a / 2, b / 2, c / 2);
glVertex3d(a / 2, b / 2, c / 2);
glVertex3d(a / 2, b / 2, -c / 2);
glVertex3d(-a / 2, b / 2, -c / 2);
glVertex3d(-a / 2, -b / 2, -c / 2);
glVertex3d(a / 2, -b / 2, -c / 2);
glVertex3d(a / 2, -b / 2, c / 2);
glVertex3d(-a / 2, -b / 2, c / 2);
glEnd();
}
void CGLRenderer::DrawTable()
{
double a = 3.0;
double b = 0.1;
double c = 2.0;
double osnova_a = a - 0.4;
double osnova_b = b * 4;
double osnova_c = c - 0.4;
double set_a = -2.5;
double set_b = -1.7;
double set_c = -4.0;
glPushMatrix();
glTranslatef(set_a, set_b, set_c);
glColor4f(0.77, 0.62, 0.43, 1.0);
DrawBox(a, b, c);
glTranslatef(0.0, -0.2, 0.0);
glColor4f(0.67, 0.52, 0.33, 1.0);
DrawBox(osnova_a, osnova_b, osnova_c);
glPopMatrix();
glPushMatrix();
//Legs
glColor3f(0.0, 1.0, 0.0);
glTranslatef( set_a-osnova_a/2, set_b -1.0, -3.2);
DrawBox(0.08, 2.0, 0.08);
glTranslatef(0.0, 0.0, -1.6);
DrawBox(0.08, 2.0, 0.08);
glTranslatef(2.6, 0.0, 0.0);
DrawBox(0.08, 2.0, 0.08);
glTranslatef(0.0, 0.0, 1.6);
DrawBox(0.08, 2.0, 0.08);
glPopMatrix();
}
void CGLRenderer::DrawWall(double a)
{
glBegin(GL_QUADS);
glVertex2d(-a / 2, a / 2);
glVertex2d(-a / 2, -a / 2);
glVertex2d(a / 2, -a / 2);
glVertex2d(a / 2, a / 2);
glEnd();
}
void CGLRenderer::DrawWalls()
{
double a = 15.0;
glColor4f(0.8, 0.8, 0.8, 1.0);
glPushMatrix();
glTranslatef(a/12, a/4, -a/2);
DrawWall(a);
glPopMatrix();
glPushMatrix();
glColor4f(0.6, 0.6, 0.6, 1.0);
glTranslatef(-a/2.4, a/4, 0.0);
glRotatef(90, 0.0, 1.0, 0.0);
DrawWall(a);
glPopMatrix();
glPushMatrix();
glColor4f(0.4, 0.4, 0.4, 1.0);
glTranslatef(a / 12, -a/4, 0.0);
glRotatef(90, 0.0, 1.0, 0.0);
glRotatef(-90, 1.0, 0.0, 0.0);
DrawWall(a);
glPopMatrix();
}
void CGLRenderer::OnKeyDown(UINT nChar)
{
if (nChar == VK_UP)
look_x -= 0.1;
else if (nChar == VK_DOWN)
{
look_x += 0.1;
}
else if (nChar == VK_RIGHT)
look_z += 0.1;
else if (nChar == VK_LEFT)
look_z -= 0.1;
else if (nChar == VK_DELETE)
look_y += 0.1;
else if (nChar == VK_SPACE)
look_y -= 0.1;
else if (nChar == VK_ADD)
{
look_x = 2.0;
look_y = 1.5;
look_z = 3.0;
}
else if (nChar == 'Q')
{
if (postolje < 8.0)
postolje = postolje + 0.5;
}
else if (nChar == 'W')
{
if (postolje > -5.0)
postolje = postolje - 0.5;
}
else if (nChar == 'A')
{
if (zglob > -44.0)
zglob = zglob - 0.5;
}
else if (nChar == 'S')
{
if (zglob < -36.0)
zglob = zglob + 0.5;
}
else if (nChar == 'Z')
{
sijalica += 1.0;
}
else if (nChar == 'X')
{
sijalica -= 1.0;
}
else if (nChar == 'P')
{
if(fioka_z < -3.0)
fioka_z += 0.2;
}
else if (nChar == 'L')
{
if(fioka_z > -3.4)
fioka_z -= 0.2;
}
}
void CGLRenderer::DrawLampBasics()
{
glColor4f(1.0, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(-3.5, -1.7, -3.6);
glBegin(GL_LINE_LOOP);
GLUquadricObj *quadric;
quadric = gluNewQuadric();
gluQuadricDrawStyle(quadric, GLU_FILL);
gluSphere(quadric, 0.2, 36, 18);
gluDeleteQuadric(quadric);
glEndList();
glEnd();
glTranslatef(0.0, 0.5, 0.0);
glRotatef(postolje, 1.0, 0.0, 1.0);
DrawBox(0.03, 1.0, 0.03);
glColor4f(0.0, 0.0, 1.0, 1.0);
glTranslatef(0.0, 0.5, 0.0);
glBegin(GL_LINE_LOOP);
GLUquadricObj *quadric1;
quadric1 = gluNewQuadric();
gluQuadricDrawStyle(quadric1, GLU_FILL);
gluSphere(quadric, 0.05, 36, 18);
gluDeleteQuadric(quadric1);
glEndList();
glEnd();
glPushMatrix();
glTranslatef(0.13, 0.2, -0.12);
glRotatef(zglob, 1.0, 0.0, 1.0);
glColor4f(1.0, 0.0, 0.0, 1.0);
DrawBox(0.03, 0.5, 0.03);
glTranslatef(0.04, 0.25, 0.0);
glRotatef(-90, 1.0, 0.0, 1.0);
glRotatef(sijalica, 0.0, 0.0, 1.0);
glColor4f(0.0, 0.0, 1.0, 1.0);
DrawBox(0.08, 0.2, 0.08);
glTranslatef(0.0, 0.2, 0.0);
DrawBox(0.15, 0.2, 0.15);
glPopMatrix();
glPopMatrix();
}
void CGLRenderer::DrawFioka()
{
glTranslatef(-2.0, -2.05, fioka_z);
glPushMatrix();
glColor3f(0.2, 0.2, 0.2);
DrawBox(0.5, 0.02, 0.5);
glColor3f(0.9, 0.9, 0.3);
glRotatef(90, 1.0, 0.0, 0.0);
glTranslatef(-0.25, 0.0, -0.1);
DrawBox(0.02, 0.5, 0.2);
glTranslatef(0.5, 0.0, 0.0);
DrawBox(0.02, 0.5, 0.2);
glRotatef(-90, 0.0, 0.0, 1.0);
glTranslatef(0.25, -0.25, 0.0);
DrawBox(0.02, 0.5, 0.2);
glColor3f(0.4, 0.7, 0.3);
glTranslatef(-0.5, 0.0, 0.0);
DrawBox(0.02, 0.5, 0.2);
glTranslatef(-0.03, 0.0, 0.0);
glColor3f(0.9, 0.9, 0.3);
glBegin(GL_LINE_LOOP);
GLUquadricObj *quadric;
quadric = gluNewQuadric();
gluQuadricDrawStyle(quadric, GLU_FILL);
gluSphere(quadric, 0.04, 36, 18);
gluDeleteQuadric(quadric);
glEndList();
glEnd();
glPopMatrix();
}
Comments