Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include "GLRenderer.h"
- #include "GL\gl.h"
- #include "GL\glu.h"
- #include "GL\glaux.h"
- #include "GL\glut.h"
- #include "DImage.h"
- #define M_PI 3.141592653589793238
- //#pragma comment(lib, "GL\\glut32.lib")
- CGLRenderer::CGLRenderer(void)
- : rotH(0)
- , rotV(0)
- {
- }
- CGLRenderer::~CGLRenderer(void)
- {
- }
- 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 = 24;
- 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(0, 0, 0, 1);
- glEnable(GL_DEPTH_TEST);
- texture = LoadTexture(L"texture.bmp");
- //---------------------------------
- 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(10, 10, 5, 0, 0, 0, 0, 1, 0);
- glPushMatrix();
- glRotated(rotV, 1, 0, 0);
- glRotated(rotH, 0, 1, 0);
- EnableLight();
- //CODE
- //CODE
- //CODE
- DrawAxis();
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture);
- DrawCube(40);
- glDisable(GL_TEXTURE_2D);
- DrawTube(2, 2, 10, 36);
- //CODE
- //CODE
- //CODE
- glPopMatrix();
- glFlush();
- SwapBuffers(pDC->m_hDC);
- //---------------------------------
- wglMakeCurrent(NULL, NULL);
- }
- void CGLRenderer::Reshape(CDC *pDC, int w, int h)
- {
- wglMakeCurrent(pDC->m_hDC, m_hrc);
- //---------------------------------
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, w, h);
- gluPerspective(40, (double)w / (double)h, 1, 1000);
- glMatrixMode(GL_MODELVIEW);
- //---------------------------------
- wglMakeCurrent(NULL, NULL);
- }
- void CGLRenderer::DestroyScene(CDC *pDC)
- {
- wglMakeCurrent(pDC->m_hDC, m_hrc);
- // ...
- glDeleteTextures(1, &texture);
- wglMakeCurrent(NULL,NULL);
- if(m_hrc)
- {
- wglDeleteContext(m_hrc);
- m_hrc = NULL;
- }
- }
- void CGLRenderer::OnKeyDown(unsigned char nChar)
- {
- if (nChar == VK_LEFT) rotV -= 10;
- if (nChar == VK_RIGHT) rotV += 10;
- if (nChar == VK_UP) rotH -= 10;
- if (nChar == VK_DOWN) rotH += 10;
- }
- void CGLRenderer::DrawAxis()
- {
- glBegin(GL_LINES);
- glColor3d(1, 0, 0); glVertex3d(0, 0, 0); glVertex3d(5, 0, 0);
- glColor3d(0, 1, 0); glVertex3d(0, 0, 0); glVertex3d(0, 5, 0);
- glColor3d(0, 0, 1); glVertex3d(0, 0, 0); glVertex3d(0, 0, 5);
- glEnd();
- }
- unsigned int CGLRenderer::LoadTexture(CString fileName)
- {
- unsigned int id;
- DImage img;
- img.Load(fileName);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- glGenTextures(1, &id);
- glBindTexture(GL_TEXTURE_2D, id);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.Width(), img.Height(), 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, img.GetDIBBits());
- return id;
- }
- void CGLRenderer::DrawCube(double a)
- {
- glPushMatrix();
- glTranslated(-a/2, -a/2, -a/2);
- glBegin(GL_QUADS);
- //back
- glVertex3d(0, 0, 0); glTexCoord2d(1, 0);
- glVertex3d(0, a, 0); glTexCoord2d(0, 0);
- glVertex3d(a, a, 0); glTexCoord2d(0, 1);
- glVertex3d(a, 0, 0); glTexCoord2d(1, 1);
- //front
- glVertex3d(0, 0, a); glTexCoord2d(1, 0);
- glVertex3d(0, a, a); glTexCoord2d(0, 0);
- glVertex3d(a, a, a); glTexCoord2d(0, 1);
- glVertex3d(a, 0, a); glTexCoord2d(1, 1);
- //left
- glVertex3d(0, 0, 0); glTexCoord2d(1, 0);
- glVertex3d(0, a, 0); glTexCoord2d(0, 0);
- glVertex3d(0, a, a); glTexCoord2d(0, 1);
- glVertex3d(0, 0, a); glTexCoord2d(1, 1);
- //right
- glVertex3d(a, 0, 0); glTexCoord2d(1, 0);
- glVertex3d(a, a, 0); glTexCoord2d(0, 0);
- glVertex3d(a, a, a); glTexCoord2d(0, 1);
- glVertex3d(a, 0, a); glTexCoord2d(1, 1);
- //bottom
- glVertex3d(0, 0, 0); glTexCoord2d(1, 0);
- glVertex3d(a, 0, 0); glTexCoord2d(0, 0);
- glVertex3d(a, 0, a); glTexCoord2d(0, 1);
- glVertex3d(0, 0, a); glTexCoord2d(1, 1);
- //top
- glVertex3d(0, a, 0); glTexCoord2d(1, 0);
- glVertex3d(a, a, 0); glTexCoord2d(0, 0);
- glVertex3d(a, a, a); glTexCoord2d(0, 1);
- glVertex3d(0, a, a); glTexCoord2d(1, 1);
- glEnd();
- glPopMatrix();
- }
- void CGLRenderer::DrawCone(double r, double h, double n)
- {
- double alfa = 360 / n;
- double str = 2 * r * sin( (alfa / 2) / 180 * M_PI);
- double H = r * cos( (alfa / 2) / 180 * M_PI);
- glPushMatrix();
- for (int i = 0; i < n; i++)
- {
- glRotated(alfa, 0, 1, 0);
- glBegin(GL_TRIANGLES);
- glVertex3d(H, 0, -str / 2);
- glVertex3d(H, 0, str / 2);
- glVertex3d(0, h, 0);
- glEnd();
- }
- glPopMatrix();
- }
- void CGLRenderer::DrawTube(double r1, double r2, double h, double n)
- {
- double alfa = 360 / n;
- double str = 2 * r1 * sin((alfa / 2) / 180 * M_PI);
- double H = r1 * cos((alfa / 2) / 180 * M_PI);
- double x = r1*r2 / h;
- double clip[] = { 0,-1,0, h - x };
- glEnable(GL_CLIP_PLANE0);
- glClipPlane(GL_CLIP_PLANE0, clip);
- glPushMatrix();
- for (int i = 0; i < n; i++)
- {
- glRotated(alfa, 0, 1, 0);
- if (r1 != r2)
- {
- glBegin(GL_TRIANGLES);
- glVertex3d(H, 0, -str / 2);
- glVertex3d(H, 0, str / 2);
- glVertex3d(0, h, 0);
- glEnd();
- }
- else
- {
- glBegin(GL_QUADS);
- glVertex3d(H, 0, -str / 2);
- glVertex3d(H, h, -str / 2);
- glVertex3d(H, h, +str / 2);
- glVertex3d(H, 0, str / 2);
- glEnd();
- }
- }
- glPopMatrix();
- glDisable(GL_CLIP_PLANE0);
- }
- void CGLRenderer::EnableLight()
- {
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- float ambinet[4] = { 1, 1, 1, 1 };
- float diffuse[4] = { 1, 1, 1, 1 };
- float specular[4] = { 1, 1, 1, 1 };
- float position[4] = { 10, 10, 10, 0 };
- glLightfv(GL_LIGHT0, GL_AMBIENT, ambinet);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
- glLightfv(GL_LIGHT0, GL_POSITION, position);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement