Advertisement
Vita94

Untitled

Jan 22nd, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.52 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "GLRenderer.h"
  3. #include "GL\gl.h"
  4. #include "GL\glu.h"
  5. #include "GL\glaux.h"
  6. #include "GL\glut.h"
  7. #include "DImage.h"
  8. #define M_PI 3.141592653589793238
  9. //#pragma comment(lib, "GL\\glut32.lib")
  10.  
  11. CGLRenderer::CGLRenderer(void)
  12.     : rotH(0)
  13.     , rotV(0)
  14. {
  15. }
  16.  
  17. CGLRenderer::~CGLRenderer(void)
  18. {
  19. }
  20.  
  21. bool CGLRenderer::CreateGLContext(CDC* pDC)
  22. {
  23.     PIXELFORMATDESCRIPTOR pfd ;
  24.     memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  25.     pfd.nSize  = sizeof(PIXELFORMATDESCRIPTOR);
  26.     pfd.nVersion   = 1;
  27.     pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;  
  28.     pfd.iPixelType = PFD_TYPE_RGBA;
  29.     pfd.cColorBits = 32;
  30.     pfd.cDepthBits = 24;
  31.     pfd.iLayerType = PFD_MAIN_PLANE;
  32.    
  33.     int nPixelFormat = ChoosePixelFormat(pDC->m_hDC, &pfd);
  34.    
  35.     if (nPixelFormat == 0) return false;
  36.  
  37.     BOOL bResult = SetPixelFormat (pDC->m_hDC, nPixelFormat, &pfd);
  38.    
  39.     if (!bResult) return false;
  40.  
  41.     m_hrc = wglCreateContext(pDC->m_hDC);
  42.  
  43.     if (!m_hrc) return false;
  44.  
  45.     return true;   
  46. }
  47.  
  48. void CGLRenderer::PrepareScene(CDC *pDC)
  49. {
  50.     wglMakeCurrent(pDC->m_hDC, m_hrc);
  51.     //---------------------------------
  52.     glClearColor(0, 0, 0, 1);
  53.     glEnable(GL_DEPTH_TEST);
  54.  
  55.     texture = LoadTexture(L"texture.bmp");
  56.  
  57.     //---------------------------------
  58.     wglMakeCurrent(NULL, NULL);
  59. }
  60.  
  61. void CGLRenderer::DrawScene(CDC *pDC)
  62. {
  63.     wglMakeCurrent(pDC->m_hDC, m_hrc);
  64.     //---------------------------------
  65.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  66.     glLoadIdentity();
  67.     gluLookAt(10, 10, 5, 0, 0, 0, 0, 1, 0);
  68.  
  69.     glPushMatrix();
  70.     glRotated(rotV, 1, 0, 0);
  71.     glRotated(rotH, 0, 1, 0);
  72.     EnableLight();
  73.  
  74.     //CODE
  75.     //CODE
  76.     //CODE
  77.     DrawAxis();
  78.     glEnable(GL_TEXTURE_2D);
  79.     glBindTexture(GL_TEXTURE_2D, texture);
  80.     DrawCube(40);
  81.    
  82.     glDisable(GL_TEXTURE_2D);
  83.  
  84.     DrawTube(2, 2, 10, 36);
  85.     //CODE
  86.     //CODE
  87.     //CODE
  88.     glPopMatrix();
  89.     glFlush();
  90.     SwapBuffers(pDC->m_hDC);
  91.     //---------------------------------
  92.     wglMakeCurrent(NULL, NULL);
  93. }
  94.  
  95. void CGLRenderer::Reshape(CDC *pDC, int w, int h)
  96. {
  97.     wglMakeCurrent(pDC->m_hDC, m_hrc);
  98.     //---------------------------------
  99.     glMatrixMode(GL_PROJECTION);
  100.     glLoadIdentity();
  101.     glViewport(0, 0, w, h);
  102.     gluPerspective(40, (double)w / (double)h, 1, 1000);
  103.     glMatrixMode(GL_MODELVIEW);
  104.    
  105.     //---------------------------------
  106.     wglMakeCurrent(NULL, NULL);
  107. }
  108.  
  109. void CGLRenderer::DestroyScene(CDC *pDC)
  110. {
  111.     wglMakeCurrent(pDC->m_hDC, m_hrc);
  112.     // ...
  113.     glDeleteTextures(1, &texture);
  114.     wglMakeCurrent(NULL,NULL);
  115.     if(m_hrc)
  116.     {
  117.         wglDeleteContext(m_hrc);
  118.         m_hrc = NULL;
  119.     }
  120. }
  121.  
  122. void CGLRenderer::OnKeyDown(unsigned char nChar)
  123. {
  124.     if (nChar == VK_LEFT) rotV -= 10;
  125.     if (nChar == VK_RIGHT) rotV += 10;
  126.     if (nChar == VK_UP) rotH -= 10;
  127.     if (nChar == VK_DOWN) rotH += 10;
  128. }
  129.  
  130.  
  131. void CGLRenderer::DrawAxis()
  132. {
  133.     glBegin(GL_LINES);
  134.     glColor3d(1, 0, 0); glVertex3d(0, 0, 0); glVertex3d(5, 0, 0);
  135.     glColor3d(0, 1, 0); glVertex3d(0, 0, 0); glVertex3d(0, 5, 0);
  136.     glColor3d(0, 0, 1); glVertex3d(0, 0, 0); glVertex3d(0, 0, 5);
  137.     glEnd();
  138. }
  139.  
  140.  
  141. unsigned int CGLRenderer::LoadTexture(CString fileName)
  142. {
  143.     unsigned int id;
  144.     DImage img;
  145.     img.Load(fileName);
  146.     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  147.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  148.     glGenTextures(1, &id);
  149.     glBindTexture(GL_TEXTURE_2D, id);
  150.  
  151.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  152.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  153.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  154.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  155.  
  156.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.Width(), img.Height(), 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, img.GetDIBBits());
  157.  
  158.     return id;
  159. }
  160.  
  161. void CGLRenderer::DrawCube(double a)
  162. {
  163.     glPushMatrix();
  164.     glTranslated(-a/2, -a/2, -a/2);
  165.  
  166.     glBegin(GL_QUADS);
  167.     //back
  168.     glVertex3d(0, 0, 0); glTexCoord2d(1, 0);
  169.     glVertex3d(0, a, 0); glTexCoord2d(0, 0);
  170.     glVertex3d(a, a, 0); glTexCoord2d(0, 1);
  171.     glVertex3d(a, 0, 0); glTexCoord2d(1, 1);
  172.  
  173.     //front
  174.     glVertex3d(0, 0, a); glTexCoord2d(1, 0);
  175.     glVertex3d(0, a, a); glTexCoord2d(0, 0);
  176.     glVertex3d(a, a, a); glTexCoord2d(0, 1);
  177.     glVertex3d(a, 0, a); glTexCoord2d(1, 1);
  178.  
  179.     //left
  180.     glVertex3d(0, 0, 0); glTexCoord2d(1, 0);
  181.     glVertex3d(0, a, 0); glTexCoord2d(0, 0);
  182.     glVertex3d(0, a, a); glTexCoord2d(0, 1);
  183.     glVertex3d(0, 0, a); glTexCoord2d(1, 1);
  184.  
  185.     //right
  186.     glVertex3d(a, 0, 0); glTexCoord2d(1, 0);
  187.     glVertex3d(a, a, 0); glTexCoord2d(0, 0);
  188.     glVertex3d(a, a, a); glTexCoord2d(0, 1);
  189.     glVertex3d(a, 0, a); glTexCoord2d(1, 1);
  190.  
  191.     //bottom
  192.     glVertex3d(0, 0, 0); glTexCoord2d(1, 0);
  193.     glVertex3d(a, 0, 0); glTexCoord2d(0, 0);
  194.     glVertex3d(a, 0, a); glTexCoord2d(0, 1);
  195.     glVertex3d(0, 0, a); glTexCoord2d(1, 1);
  196.  
  197.     //top
  198.     glVertex3d(0, a, 0); glTexCoord2d(1, 0);
  199.     glVertex3d(a, a, 0); glTexCoord2d(0, 0);
  200.     glVertex3d(a, a, a); glTexCoord2d(0, 1);
  201.     glVertex3d(0, a, a); glTexCoord2d(1, 1);
  202.  
  203.     glEnd();
  204.     glPopMatrix();
  205. }
  206.  
  207.  
  208. void CGLRenderer::DrawCone(double r, double h, double n)
  209. {
  210.     double alfa = 360 / n;
  211.     double str  = 2 * r * sin( (alfa / 2)  / 180 *  M_PI);
  212.     double H = r * cos( (alfa / 2) / 180 *  M_PI);
  213.     glPushMatrix();
  214.     for (int i = 0; i < n; i++)
  215.     {
  216.         glRotated(alfa, 0, 1, 0);
  217.         glBegin(GL_TRIANGLES);
  218.         glVertex3d(H, 0, -str / 2);
  219.         glVertex3d(H, 0, str / 2);
  220.         glVertex3d(0, h, 0);
  221.         glEnd();
  222.     }
  223.     glPopMatrix();
  224. }
  225.  
  226. void CGLRenderer::DrawTube(double r1, double r2, double h, double n)
  227. {
  228.     double alfa = 360 / n;
  229.     double str = 2 * r1 * sin((alfa / 2) / 180 * M_PI);
  230.     double H = r1 * cos((alfa / 2) / 180 * M_PI);
  231.     double x = r1*r2 / h;
  232.     double clip[] = { 0,-1,0, h - x };
  233.     glEnable(GL_CLIP_PLANE0);
  234.     glClipPlane(GL_CLIP_PLANE0, clip);
  235.     glPushMatrix();
  236.     for (int i = 0; i < n; i++)
  237.     {
  238.         glRotated(alfa, 0, 1, 0);
  239.  
  240.         if (r1 != r2)
  241.         {
  242.             glBegin(GL_TRIANGLES);
  243.             glVertex3d(H, 0, -str / 2);
  244.             glVertex3d(H, 0, str / 2);
  245.             glVertex3d(0, h, 0);
  246.             glEnd();
  247.         }
  248.         else
  249.         {
  250.             glBegin(GL_QUADS);
  251.             glVertex3d(H, 0, -str / 2);
  252.             glVertex3d(H, h, -str / 2);
  253.             glVertex3d(H, h, +str / 2);
  254.             glVertex3d(H, 0, str / 2);
  255.             glEnd();
  256.         }
  257.        
  258.     }
  259.     glPopMatrix();
  260.     glDisable(GL_CLIP_PLANE0);
  261. }
  262.  
  263. void CGLRenderer::EnableLight()
  264. {
  265.     glEnable(GL_LIGHTING);
  266.     glEnable(GL_LIGHT0);
  267.     float ambinet[4] = { 1, 1, 1, 1 };
  268.     float diffuse[4] = { 1, 1, 1, 1 };
  269.     float specular[4] = { 1, 1, 1, 1 };
  270.     float position[4] = { 10, 10, 10, 0 };
  271.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambinet);
  272.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  273.     glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  274.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement