Programlama | Programlama Dilleri | C Programlama Dili |C++

Anasayfa Programlama C++ Builder OpenGL Uygulaması


OpenGL Uygulaması

Bu Program C++ Builder 6 ile uyumludur.

AÇIKLAMA

Builderde OPENGL uygulaması. Programda yön tuşlarını kullanarak cismi hareket ettiriniz.

Q tuşuna bastığınızda cisim büyür,W tuşuna bastığınızda cisim küçülür.

Programın Tamamını Aşağıdaki Linkten İndirebilirsiniz

Linki Görebilmeniz İçin Üye Olmanız Gerekmektedir...

Üye Kayıt

Program Kodu:

Main.cpp

  1. /**************************
  2.   * Includes
  3.   *
  4.   **************************/
  5.  
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8. #include <math.h>
  9. #include "Gemi.cpp"
  10. //#include <gl/glu.h>
  11. //#include <iostream.h>
  12.  
  13.  
  14. /**************************
  15.   * Function Declarations
  16.   *
  17.   **************************/
  18.  
  19. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  20. WPARAM wParam, LPARAM lParam);
  21. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
  22. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
  23. Gemi *gemi;
  24. bool keys[256];
  25.  
  26. /**************************
  27.   * WinMain
  28.   *
  29.   **************************/
  30.  
  31. int WINAPI WinMain (HINSTANCE hInstance,
  32.                     HINSTANCE hPrevInstance,
  33.                     LPSTR lpCmdLine,
  34.                     int iCmdShow)
  35. {
  36.     WNDCLASS wc;
  37.     HWND hWnd;
  38.     HDC hDC;
  39.     HGLRC hRC;       
  40.     MSG msg;
  41.     BOOL bQuit = FALSE;
  42.     float theta ;
  43.  
  44.     /* register window class */
  45.     wc.style = CS_OWNDC;
  46.     wc.lpfnWndProc = WndProc;
  47.     wc.cbClsExtra = 0;
  48.     wc.cbWndExtra = 0;
  49.     wc.hInstance = hInstance;
  50.     wc.hIcon = LoadIcon (NULL, IDI_EXCLAMATION);
  51.     wc.hCursor = LoadCursor (NULL, IDC_ARROW );
  52.     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
  53.     wc.lpszMenuName = NULL;
  54.     wc.lpszClassName = "GLSample";
  55.     RegisterClass (&wc);
  56.  
  57.     /* create main window */
  58.     hWnd = CreateWindow (
  59.       "GLSample", "KTÜ Bilgisayar Mühendisliği Grafikler Lab. OpenGL Deneyi Örnek Program",
  60.       WS_CAPTION | WS_POPUPWINDOW |WS_VISIBLE,
  61.       0, 0, 640, 640,
  62.       NULL, NULL, hInstance, NULL);
  63.       //ShowWindow(hWnd,1);
  64.       //UpdateWindow(hWnd);
  65.  
  66.     /* enable OpenGL for the window */
  67.     EnableOpenGL (hWnd, &hDC, &hRC);
  68.     glEnable(GL_TEXTURE_2D);                        // Enable Texture Mapping ( NEW )
  69.     glShadeModel(GL_SMOOTH);                        // Enable Smooth Shading
  70.     glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                    // Black Background
  71.     glClearDepth(100.0f);                            // Depth Buffer Setup
  72.     glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
  73.     glDepthFunc(GL_LEQUAL);                            // The Type Of Depth Testing To Do
  74.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    //glMatrixMode(GL_PROJECTION);
  75.     glColor4f(1.0f,1.0f,1.0f,0.5f);            // Full Brightness, 50% Alpha ( NEW )
  76.     glBlendFunc(GL_SRC_ALPHA,GL_ONE);        // Blending Function For Translucency Based On Source Alpha Value ( NEW )
  77.  
  78.  
  79.     //glLoadIdentity();
  80.  
  81.     gemi=new Gemi(0.0,0.0,0,0,0.2);
  82.     /* program main loop */
  83.     while (!bQuit)
  84.     {
  85.         /* check for messages */
  86.         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  87.         {
  88.             /* handle or dispatch messages */
  89.             if (msg.message == WM_QUIT)
  90.             {
  91.                 bQuit = TRUE;
  92.             }
  93.             else
  94.             {
  95.                 TranslateMessage (&msg);
  96.                 DispatchMessage (&msg);
  97.             }
  98.         }
  99.         else
  100.         {
  101.             gemi->KlavyeIsle(keys);
  102.             glClearColor (0.309f, 0.474f, 0.745f, 0.0f);
  103.            
  104.             glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  105.             //glClear(GL_COLOR_BUFFER_BIT);
  106.             glPushMatrix ();
  107.             //glClear(GL_ACCUM_BUFFER_BIT);
  108.             //glAccum(GL_MULT,0.2f);
  109.             gemi->Cizdir();
  110.             //glAccum(GL_RETURN,1.0f);
  111.             glPopMatrix ();
  112.             SwapBuffers (hDC);
  113.         }
  114.     }
  115.  
  116.     /* shutdown OpenGL */
  117.     DisableOpenGL (hWnd, hDC, hRC);
  118.  
  119.     /* destroy the window explicitly */
  120.     DestroyWindow (hWnd);
  121.  
  122.     return msg.wParam;
  123. }
  124.  
  125.  
  126. /********************
  127.   * Window Procedure
  128.   *
  129.   ********************/
  130.  
  131. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  132.                           WPARAM wParam, LPARAM lParam)
  133. {
  134.  
  135.     switch (message)
  136.     {
  137.     case WM_CREATE:
  138.         return 0;
  139.     case WM_CLOSE:
  140.         PostQuitMessage (0);
  141.         return 0;
  142.  
  143.     case WM_DESTROY:
  144.         return 0;
  145.  
  146.     case WM_KEYDOWN:
  147.          keys[wParam]=true;
  148.         switch (wParam)
  149.         {
  150.         case VK_ESCAPE:
  151.             PostQuitMessage(0);
  152.             return 0;
  153.         case 82:
  154.              gemi->Sifirla();
  155.              return 0;
  156.  
  157.         }
  158.         return 0;
  159.     case WM_KEYUP:
  160.          keys[wParam]=false;
  161.          return 0;
  162.  
  163.     default:
  164.         return DefWindowProc (hWnd, message, wParam, lParam);
  165.     }
  166. }
  167.  
  168.  
  169. /*******************
  170.   * Enable OpenGL
  171.   *
  172.   *******************/
  173.  
  174. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
  175. {
  176.     PIXELFORMATDESCRIPTOR pfd;
  177.     int iFormat;
  178.  
  179.     /* get the device context (DC) */
  180.     *hDC = GetDC (hWnd);
  181.  
  182.     /* set the pixel format for the DC */
  183.     ZeroMemory (&pfd, sizeof (pfd));
  184.     pfd.nSize = sizeof (pfd);
  185.     pfd.nVersion = 1;
  186.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  187.       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  188.     pfd.iPixelType = PFD_TYPE_RGBA;
  189.     pfd.cColorBits = 24;
  190.     pfd.cDepthBits = 16;
  191.     pfd.iLayerType = PFD_MAIN_PLANE;
  192.     iFormat = ChoosePixelFormat (*hDC, &pfd);
  193.     SetPixelFormat (*hDC, iFormat, &pfd);
  194.  
  195.     /* create and enable the render context (RC) */
  196.     *hRC = wglCreateContext( *hDC );
  197.     wglMakeCurrent( *hDC, *hRC );
  198.  
  199. }
  200.  
  201.  
  202. /******************
  203.   * Disable OpenGL
  204.   *
  205.   ******************/
  206.  
  207. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
  208. {
  209.     wglMakeCurrent (NULL, NULL);
  210.     wglDeleteContext (hRC);
  211.     ReleaseDC (hWnd, hDC);
  212. }
  213.  

Gemi.cpp

  1. #include <windows.h>
  2. #include <gl/gl.h>
  3. #include <gl/glu.h>
  4. //#include <gl/glaux.h>
  5. #include "Gemi.h"
  6. #include <Math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. Gemi::Gemi()
  10. {
  11.    Carpan=0.5;
  12.    pozisyonX=0.0;
  13.    pozisyonY=0.0;
  14.    aciX=0.0;
  15.    aciY=0.0;
  16.    init();
  17. }
  18. Gemi::Gemi(double x, double y, double aX, double aY,double carpan)
  19. {
  20.    Carpan=carpan;
  21.    pozisyonX=x;
  22.    pozisyonY=y;
  23.    aciX=aX;
  24.    aciY=aY;
  25.    init();
  26. }
  27. Gemi::Gemi(double carpan)
  28. {
  29.    Carpan=carpan;
  30.    pozisyonX=0.0;
  31.    pozisyonY=0.0;
  32.    aciX=0.0;
  33.    aciY=0.0;
  34.    init();
  35. }
  36. void Gemi::Cizdir()
  37. {
  38.      if(Aktif) {
  39.      RutinDengeleme();
  40.      glLoadIdentity();
  41.      //gluLookAt(0,0,0,0,0,1,1,1,0);
  42.      //gluPerspective(60.0,1.0,0.0,-100.0);
  43.     
  44.      glTranslated(pozisyonX,pozisyonY,0.0);
  45.      glRotated(aciY,0.0,1.0,0.0);
  46.      glRotated(aciX,1.0,0.0,0.0);
  47.      //glBindTexture(GL_TEXTURE_2D,texture[textureIndex]);
  48.     
  49.  
  50.     
  51.     
  52.      glBegin(GL_QUADS);
  53.  
  54.          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  55.          glColor3ub(255,255,255);
  56.         
  57.      //glTexCoord2d(1.0,1.0);
  58.      glVertex2d(0.5*Carpan,0.5*Carpan);
  59.      //glTexCoord2d(1.0,0.0);
  60.      glVertex2d(0.5*Carpan,-0.5*Carpan);
  61.      //glTexCoord2d(0.0,0.0);
  62.      glVertex2d(-0.5*Carpan,-0.5*Carpan);
  63.      //glTexCoord2d(0.0,1.0);
  64.      glVertex2d(-0.5*Carpan,0.5*Carpan);
  65.      glEnd();
  66.     
  67.      //glBindTexture(GL_TEXTURE_2D,texture[2]);
  68.      glBegin(GL_QUADS);
  69.        glColor3ub(127,127,127);
  70.      //glTexCoord2d(1.0,1.0);
  71.      glVertex3d(-0.5*Carpan,0.5*Carpan,0);
  72.      //glTexCoord2d(1.0,0.0);
  73.      glVertex3d(-0.5*Carpan,-0.5*Carpan,0);
  74.      //glTexCoord2d(0.0,1.0);
  75.      glVertex3d(-0.5*Carpan,-0.5*Carpan,0.25*Carpan);
  76.      //glTexCoord2d(0.0,0.0);
  77.      glVertex3d(-0.5*Carpan,0.5*Carpan,0.25*Carpan);
  78.        //glColor3ub(255,255,255);
  79.      //glTexCoord2d(1.0,1.0);
  80.      glVertex3d(-0.5*Carpan,0.5*Carpan,0);
  81.      //glTexCoord2d(1.0,0.0);
  82.      glVertex3d(0.5*Carpan,0.5*Carpan,0);
  83.      //glTexCoord2d(0.0,1.0);
  84.      glVertex3d(0.5*Carpan,0.5*Carpan,0.25*Carpan);
  85.      //glTexCoord2d(0.0,0.0);
  86.      glVertex3d(-0.5*Carpan,0.5*Carpan,0.25*Carpan);
  87.        //glColor3ub(0,0,0);
  88.      //glTexCoord2d(1.0,1.0);
  89.      glVertex3d(0.5*Carpan,0.5*Carpan,0);
  90.      //glTexCoord2d(1.0,0.0);
  91.      glVertex3d(0.5*Carpan,-0.5*Carpan,0);
  92.      //glTexCoord2d(0.0,1.0);
  93.      glVertex3d(0.5*Carpan,-0.5*Carpan,0.25*Carpan);
  94.      //glTexCoord2d(0.0,0.0);
  95.      glVertex3d(0.5*Carpan,0.5*Carpan,0.25*Carpan);
  96.        //glColor3ub(0,0,0);
  97.      //glTexCoord2d(1.0,1.0);
  98.      glVertex3d(0.5*Carpan,-0.5*Carpan,0);
  99.      //glTexCoord2d(1.0,0.0);
  100.      glVertex3d(-0.5*Carpan,-0.5*Carpan,0);
  101.      //glTexCoord2d(0.0,1.0);
  102.      glVertex3d(-0.5*Carpan,-0.5*Carpan,0.25*Carpan);
  103.      //glTexCoord2d(0.0,0.0);
  104.      glVertex3d(0.5*Carpan,-0.5*Carpan,0.25*Carpan);
  105.  
  106.      glEnd();
  107.  
  108.      }
  109. }
  110. void Gemi::KlavyeIsle(bool * tuslar)
  111. {
  112.      if(tuslar[VK_DOWN])
  113.           xAciAzalt(aciArtmaMiktari);
  114.      if(tuslar[VK_UP])
  115.           xAciArtir(aciArtmaMiktari);
  116.      if(tuslar[VK_LEFT])
  117.           yAciArtir(aciArtmaMiktari);
  118.      if(tuslar[VK_RIGHT])
  119.           yAciAzalt(aciArtmaMiktari);
  120.      if(tuslar[49])
  121.           textureIndex=0;
  122.      else if(tuslar[50])
  123.           textureIndex=1;
  124.      if(tuslar[81])
  125.           CarpanArtir();
  126.      else if(tuslar[87])
  127.           CarpanAzalt();
  128. }
  129.  
  130. void Gemi::init()
  131. {
  132.      Aktif=true;
  133.      aciArtmaMiktari=0.7;
  134.      aciAzalmaMiktari=0.1;
  135.      aciMaxDeger=45.0;
  136.      //texture=new GLuint[3];
  137.      //textureIndex=0;
  138.      //TextureYukle("texture/smiley",&texture[1]);
  139.      //TextureYukle("texture/ktu",&texture[0]);
  140.      //TextureYukle("texture/duvar",&texture[2]);
  141. }
  142.  
  143. void Gemi::xAciArtir(double aci)
  144. {
  145.      aciX+=aci;
  146.      if(aciX>aciMaxDeger)
  147.        aciX=aciMaxDeger;
  148. }
  149. void Gemi::xAciAzalt(double aci)
  150. {
  151.      aciX-=aci;
  152.      if(aciX<-aciMaxDeger)
  153.        aciX=-aciMaxDeger;
  154. }
  155. void Gemi::yAciArtir(double aci)
  156. {
  157.      aciY+=aci;
  158.      if(aciY>aciMaxDeger)
  159.        aciY=aciMaxDeger;
  160. }
  161. void Gemi::yAciAzalt(double aci)
  162. {
  163.      aciY-=aci;
  164.      if(aciY<-aciMaxDeger)
  165.        aciY=-aciMaxDeger;
  166. }
  167. void Gemi::RutinDengeleme()
  168. {
  169.      if(abs(aciX)<aciAzalmaMiktari)
  170.        aciX-=aciX/8;
  171.      else {
  172.      if(aciX>0.0)
  173.        aciX-=aciAzalmaMiktari;
  174.      else
  175.        aciX+=aciAzalmaMiktari;
  176.      }
  177.      if(abs(aciY)<aciAzalmaMiktari)
  178.        aciY-=aciY/8;
  179.      else {
  180.      if(aciY>0.0)
  181.        aciY-=aciAzalmaMiktari;
  182.      else
  183.        aciY+=aciAzalmaMiktari;
  184.      }
  185.      double ustSinir=1.0-0.5*Carpan;
  186.      double altSinir=0.5*Carpan-1.0;
  187.      if(pozisyonY<=ustSinir && pozisyonY>=altSinir)
  188.      pozisyonY+=aciX/(180.0*100);
  189.    else if(pozisyonY>ustSinir)
  190.      pozisyonY=ustSinir;
  191.    else
  192.      pozisyonY=altSinir;
  193.      if(pozisyonX<=ustSinir && pozisyonX>=altSinir)
  194.      pozisyonX-=aciY/(180.0*100);
  195.    else if(pozisyonX>ustSinir)
  196.      pozisyonX=ustSinir;
  197.    else
  198.      pozisyonX=altSinir;
  199. }
  200.  
  201.  
  202.  
  203. void Gemi::Sifirla()
  204. {
  205.      aciX=0.0;
  206.      aciY=0.0;
  207.      pozisyonX=0.0;
  208.      pozisyonY=0.0;
  209.      Carpan=0.2;
  210. }
  211. void Gemi::CarpanArtir()
  212. {
  213.      Carpan+=0.001;
  214.      if(Carpan>1.0)
  215.         Carpan=1.0;
  216. }
  217. void Gemi::CarpanAzalt()
  218. {
  219.      Carpan-=0.001;
  220.      if(Carpan<0.05)
  221.         Carpan=0.05;
  222. }
  223.  

 Daha karmaşık bir örnek için : OpenGL Uzay Gemisi Simülasyonu

Yorumlar (0)
Sadece kayıtlı kullanıcılar yorum yazabilir!
Son Güncelleme ( Perşembe, 07 Nisan 2011 23:31 )