Advertisement
Guest User

Untitled

a guest
May 4th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.64 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include<GL/gl.h>
  5. #include<GL/glu.h>
  6.  
  7. const double gamma = 2.0;
  8.  
  9. GLfloat CorrectGamma(double c)
  10. {
  11.     return pow(c, gamma);
  12. }
  13.  
  14.  
  15. void HSVToRGB(float h, float s, float v, float &r, float &g, float &b){
  16.     int i;
  17.     float f, q, p, t;
  18.  
  19.     h /= 60;
  20.     i = floor(h);
  21.     f = h - i;
  22.  
  23.     p = v*(1-s);
  24.     q = v*(1-s*f);
  25.     t = v*(1-s*(1-f));
  26.  
  27.     switch(i)
  28.     {
  29.     case 0:
  30.         r = v;
  31.         g = t;
  32.         b = p;
  33.         break;
  34.     case 1:
  35.         r = q;
  36.         g = v;
  37.         b = p;
  38.         break;
  39.     case 2:
  40.         r = p;
  41.         g = v;
  42.         b = t;
  43.         break;
  44.     case 3:
  45.         r = p;
  46.         g = q;
  47.         b = t;
  48.         break;
  49.     case 4:
  50.         r = t;
  51.         g = p;
  52.         b = v;
  53.         break;
  54.     case 5:
  55.         r = v;
  56.         g = p;
  57.         b = q;
  58.         break;
  59.     }
  60. }
  61.  
  62.  
  63. void DrawScene(GLfloat xRot, GLfloat yRot)
  64. {
  65.     int i;
  66.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67.     glLoadIdentity();
  68.  
  69.  
  70.     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  71.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  72.  
  73.     glBegin(GL_LINES);
  74.         glColor3f(1,1,1);
  75.         glVertex3f(10,0,0);
  76.         glVertex3f(-10,0,0);
  77.         glVertex3f(0,10,0);
  78.         glVertex3f(0,-10,0);
  79.         glVertex3f(0,0,10);
  80.         glVertex3f(0,0,-10);
  81.     glEnd();
  82.  
  83.     glBegin(GL_QUAD_STRIP); //ref bar
  84.         glColor3f(0,0,0);
  85.         glVertex3f(-5,-7,6);
  86.         glVertex3f(-5,-4,6);
  87.        
  88.         for(i=1;i<=10;i++)
  89.         {
  90.             glColor3f(CorrectGamma(0),CorrectGamma(0.1*(GLfloat)i),CorrectGamma(0));
  91.             glVertex3f(-5+(GLfloat)i,-7,6);
  92.             glVertex3f(-5+(GLfloat)i,-4,6);
  93.         }
  94.     glEnd();
  95.  
  96.     glBegin(GL_QUAD_STRIP); //mod bar
  97.         glColor3f(0,0,0);
  98.         glVertex3f(-5,-10,6);
  99.         glVertex3f(-5,-7,6);
  100.        
  101.         for(i=1;i<=10;i++)
  102.         {
  103.             glColor3f(0,0.1*(GLfloat)i,0);
  104.             glVertex3f(-5+(GLfloat)i,-10,6);
  105.             glVertex3f(-5+(GLfloat)i,-7,6);
  106.         }
  107.     glEnd();
  108.  
  109.     // Zadanie 3 - tęcza
  110.     //glBegin(GL_QUAD_STRIP); //mod bar
  111.     //  float r, g, b;
  112.  
  113.     //  for(i=0;i<=10;i++)
  114.     //  {
  115.     //      HSVToRGB(36*i, 1, 1, r, g, b);
  116.     //      //glColor3f(0,0.1*(GLfloat)i,0);
  117.     //      glColor3f(r, g, b);
  118.     //      glVertex3f(-5+(GLfloat)i,0,6);
  119.     //      glVertex3f(-5+(GLfloat)i,-3,6);
  120.     //  }
  121.     //glEnd();
  122.  
  123.     // Zadanie 2 - Kostka
  124.     //glBegin(GL_QUADS);
  125.     //  //left
  126.     //  glColor3f(0,0,0);
  127.     //  glVertex3f(-3,-3,-3);
  128.     //  glColor3f(0,1,0);
  129.     //  glVertex3f(-3,3,-3);
  130.     //  glColor3f(1,1,0);
  131.     //  glVertex3f(3,3,-3);
  132.     //  glColor3f(1,0,0);
  133.     //  glVertex3f(3,-3,-3);
  134.  
  135.     //  //glColor4f(0,1.0,0,0.6);
  136.     //  //glVertex3f(-3,-3,3);
  137.     //  //glVertex3f(-3,3,3);
  138.     //  //glVertex3f(3,3,3);
  139.     //  //glVertex3f(3,-3,3);
  140.  
  141.     //  //front
  142.     //  glColor3f(1,0,1);
  143.     //  glVertex3f(3,-3,3);
  144.     //  glColor3f(0,0,1);
  145.     //  glVertex3f(-3,-3,3);
  146.     //  glColor3f(0,0,0);
  147.     //  glVertex3f(-3,-3,-3);
  148.     //  glColor3f(1,0,0);
  149.     //  glVertex3f(3,-3,-3);
  150.  
  151.     //  //right
  152.     //  glColor3f(1,0,1);
  153.     //  glVertex3f(3,-3,3);
  154.     //  glColor3f(0,0,1);
  155.     //  glVertex3f(-3,-3,3);
  156.     //  glColor3f(0,1,1);
  157.     //  glVertex3f(-3,3,3);
  158.     //  glColor3f(1,1,1);
  159.     //  glVertex3f(3,3,3);
  160.  
  161.     //  //back
  162.     //  glColor3f(1,1,0);
  163.     //  glVertex3f(3,3,-3);
  164.     //  glColor3f(0,1,0);
  165.     //  glVertex3f(-3,3,-3);
  166.     //  glColor3f(0,1,1);
  167.     //  glVertex3f(-3,3,3);
  168.     //  glColor3f(1,1,1);
  169.     //  glVertex3f(3,3,3);
  170.  
  171.     //  //bot
  172.     //  glColor3f(0,0,0);
  173.     //  glVertex3f(-3,-3,-3);
  174.     //  glColor3f(0,1,0);
  175.     //  glVertex3f(-3,3,-3);
  176.     //  glColor3f(0,1,1);
  177.     //  glVertex3f(-3,3,3);
  178.     //  glColor3f(0,0,1);
  179.     //  glVertex3f(-3,-3,3);
  180.  
  181.     //  //top
  182.     //  glColor3f(1,1,0);
  183.     //  glVertex3f(3,3,-3);
  184.     //  glColor3f(1,0,0);
  185.     //  glVertex3f(3,-3,-3);
  186.     //  glColor3f(1,0,1);
  187.     //  glVertex3f(3,-3,3);
  188.     //  glColor3f(1,1,1);
  189.     //  glVertex3f(3,3,3);
  190.  
  191.     //glEnd();
  192.  
  193.     glEnd();
  194.     glFinish();
  195. }
  196.  
  197. void SetMyPixelFormat(HDC hdc)
  198. {
  199.     PIXELFORMATDESCRIPTOR pfd;
  200.     ZeroMemory(&pfd, sizeof(pfd));
  201.     pfd.nSize = sizeof(pfd);
  202.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  203.     pfd.iPixelType = PFD_TYPE_RGBA;
  204.     pfd.cColorBits = 32;
  205.     pfd.cDepthBits = 16;
  206.     pfd.iLayerType = PFD_MAIN_PLANE;
  207.  
  208.     int nPixelFormat = ChoosePixelFormat(hdc, &pfd);
  209.     SetPixelFormat(hdc, nPixelFormat, &pfd);
  210. }
  211. void ResizeWindow(int width, int height)
  212. {
  213.     if (height*width==0) return;
  214.     glViewport(0,0,width,height);
  215.     glMatrixMode(GL_PROJECTION);
  216.     glLoadIdentity();
  217.     glOrtho(-10, 10, -10, 10, -10, 10);
  218.     glMatrixMode(GL_MODELVIEW);
  219.     glLoadIdentity();
  220.     glEnable(GL_DEPTH_TEST);
  221.    
  222.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// set the blend mode
  223.     glEnable(GL_BLEND); //obligatory for blending and transparencies
  224.     glEnable(GL_LINE_SMOOTH);
  225.  
  226.     //glEnable(GL_CULL_FACE);
  227.    
  228.     glShadeModel(GL_FLAT);
  229.     glShadeModel(GL_SMOOTH);
  230. }
  231. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  232. {
  233.     PAINTSTRUCT ps;
  234.     HDC hdc;
  235.     static HGLRC hrc;
  236.     static GLfloat xRot = 0.0f;
  237.     static GLfloat yRot = 0.0f;
  238.  
  239.     switch (message)
  240.     {
  241.     case WM_SIZE:
  242.         ResizeWindow(LOWORD(lParam),HIWORD(lParam));
  243.         break;
  244.     case WM_CREATE:
  245.         hdc = GetDC(hWnd);
  246.         SetMyPixelFormat(hdc);
  247.         hrc = wglCreateContext(hdc);
  248.         wglMakeCurrent(hdc, hrc);
  249.         ReleaseDC(hWnd, hdc);
  250.         break;
  251.     case WM_KEYDOWN:
  252.         if(wParam==VK_UP) xRot-= 5.0f;
  253.         if(wParam==VK_DOWN) xRot += 5.0f;
  254.         if(wParam==VK_LEFT) yRot -= 5.0f;
  255.         if(wParam==VK_RIGHT) yRot += 5.0f;
  256.  
  257.         if(xRot > 356.0f) xRot = 0.0f;
  258.         if(xRot < -1.0f) xRot = 355.0f;
  259.         if(yRot > 356.0f) yRot = 0.0f;
  260.         if(yRot < -1.0f) yRot = 355.0f;
  261.        
  262.         InvalidateRect(hWnd,NULL,FALSE);
  263.         break;
  264.     case WM_PAINT:
  265.         hdc = BeginPaint(hWnd, &ps);
  266.         DrawScene(xRot, yRot);
  267.         SwapBuffers(hdc);
  268.         EndPaint(hWnd, &ps);       
  269.         break;
  270.     case WM_ERASEBKGND:
  271.         return 1;
  272.         break;
  273.     case WM_DESTROY:
  274.         wglMakeCurrent(NULL, NULL);
  275.         wglDeleteContext(hrc);
  276.         PostQuitMessage(0);
  277.         break;
  278.     default:
  279.         return DefWindowProc(hWnd, message, wParam, lParam);
  280.     }
  281.     return 0;
  282. }
  283. ATOM MyRegisterClass(HINSTANCE hInstance)
  284. {
  285.     WNDCLASSEX wcex;
  286.     wcex.cbSize = sizeof(WNDCLASSEX);
  287.     wcex.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  288.     wcex.lpfnWndProc    = (WNDPROC)WndProc;
  289.     wcex.cbClsExtra     = 0;
  290.     wcex.cbWndExtra     = 0;
  291.     wcex.hInstance      = hInstance;
  292.     wcex.hIcon          = NULL;
  293.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  294.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  295.     wcex.lpszMenuName   = NULL;
  296.     wcex.lpszClassName  = "Primitives";
  297.     wcex.hIconSm        = NULL;
  298.     return RegisterClassEx(&wcex);
  299. }
  300. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  301. {
  302.    HWND hWnd;
  303.    hWnd = CreateWindow("Primitives", "OGL color lab", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  304.    if (!hWnd) return FALSE;
  305.    ShowWindow(hWnd, nCmdShow);
  306.    UpdateWindow(hWnd);
  307.    return TRUE;
  308. }
  309. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  310. {
  311.     MSG msg;
  312.     MyRegisterClass(hInstance);
  313.     if (!InitInstance(hInstance, nCmdShow)) return FALSE;
  314.     while (GetMessage(&msg, NULL, 0, 0)) {
  315.         TranslateMessage(&msg);
  316.         DispatchMessage(&msg);
  317.     }
  318.     return (int) msg.wParam;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement