bitetti

bezier gl study

Nov 22nd, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.73 KB | None | 0 0
  1. #include <windows.h>
  2. #include <gl/gl.h>
  3. #include <GL/glu.h>
  4.  
  5. LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  6. void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
  7. void DisableOpenGL(HWND, HDC, HGLRC);
  8.  
  9. static void init();
  10. static void drawGLBagaca();
  11.  
  12.  
  13. #define W 256
  14. #define H 256
  15.  
  16. int WINAPI WinMain(HINSTANCE hInstance,
  17.                    HINSTANCE hPrevInstance,
  18.                    LPSTR lpCmdLine,
  19.                    int nCmdShow)
  20. {
  21.     WNDCLASSEX wcex;
  22.     HWND hwnd;
  23.     HDC hDC;
  24.     HGLRC hRC;
  25.     MSG msg;
  26.     BOOL bQuit = FALSE;
  27.  
  28.     /* register window class */
  29.     wcex.cbSize = sizeof(WNDCLASSEX);
  30.     wcex.style = CS_OWNDC;
  31.     wcex.lpfnWndProc = WindowProc;
  32.     wcex.cbClsExtra = 0;
  33.     wcex.cbWndExtra = 0;
  34.     wcex.hInstance = hInstance;
  35.     wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  36.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  37.     wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  38.     wcex.lpszMenuName = NULL;
  39.     wcex.lpszClassName = "GLSample";
  40.     wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
  41.  
  42.  
  43.     if (!RegisterClassEx(&wcex))
  44.         return 0;
  45.  
  46.     /* create main window */
  47.     hwnd = CreateWindowEx(0,
  48.                           "GLSample",
  49.                           "Sample",
  50.                           WS_OVERLAPPEDWINDOW,
  51.                           CW_USEDEFAULT,
  52.                           CW_USEDEFAULT,
  53.                           W,
  54.                           H,
  55.                           NULL,
  56.                           NULL,
  57.                           hInstance,
  58.                           NULL);
  59.  
  60.     ShowWindow(hwnd, nCmdShow);
  61.  
  62.     /* enable OpenGL for the window */
  63.     EnableOpenGL(hwnd, &hDC, &hRC);
  64.  
  65.     /* program main loop */
  66.     while (!bQuit)
  67.     {
  68.         /* check for messages */
  69.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  70.         {
  71.             /* handle or dispatch messages */
  72.             if (msg.message == WM_QUIT)
  73.             {
  74.                 bQuit = TRUE;
  75.             }
  76.             else
  77.             {
  78.                 TranslateMessage(&msg);
  79.                 DispatchMessage(&msg);
  80.             }
  81.         }
  82.         else
  83.         {
  84.             drawGLBagaca();
  85.  
  86.             SwapBuffers(hDC);
  87.             Sleep (1);
  88.         }
  89.     }
  90.  
  91.     /* shutdown OpenGL */
  92.     DisableOpenGL(hwnd, hDC, hRC);
  93.  
  94.     /* destroy the window explicitly */
  95.     DestroyWindow(hwnd);
  96.  
  97.     return msg.wParam;
  98. }
  99.  
  100. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  101. {
  102.     switch (uMsg)
  103.     {
  104.         case WM_CLOSE:
  105.             PostQuitMessage(0);
  106.         break;
  107.  
  108.         case WM_DESTROY:
  109.             return 0;
  110.  
  111.         case WM_SHOWWINDOW:
  112.             /*init();*/
  113.             break;
  114.  
  115.         case WM_KEYDOWN:
  116.         {
  117.             switch (wParam)
  118.             {
  119.                 case VK_ESCAPE:
  120.                     PostQuitMessage(0);
  121.                 break;
  122.             }
  123.         }
  124.         break;
  125.  
  126.         default:
  127.             return DefWindowProc(hwnd, uMsg, wParam, lParam);
  128.     }
  129.  
  130.     return 0;
  131. }
  132.  
  133. void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
  134. {
  135.     PIXELFORMATDESCRIPTOR pfd;
  136.  
  137.     int iFormat;
  138.  
  139.     /* get the device context (DC) */
  140.     *hDC = GetDC(hwnd);
  141.  
  142.     /* set the pixel format for the DC */
  143.     ZeroMemory(&pfd, sizeof(pfd));
  144.  
  145.     pfd.nSize = sizeof(pfd);
  146.     pfd.nVersion = 1;
  147.     pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  148.                   PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  149.     pfd.iPixelType = PFD_TYPE_RGBA;
  150.     pfd.cColorBits = 24;
  151.     pfd.cDepthBits = 16;
  152.     pfd.iLayerType = PFD_MAIN_PLANE;
  153.  
  154.     iFormat = ChoosePixelFormat(*hDC, &pfd);
  155.  
  156.     SetPixelFormat(*hDC, iFormat, &pfd);
  157.  
  158.     /* create and enable the render context (RC) */
  159.     *hRC = wglCreateContext(*hDC);
  160.  
  161.     wglMakeCurrent(*hDC, *hRC);
  162.  
  163.     glMatrixMode(GL_PROJECTION);
  164.     glLoadIdentity();
  165.     /*
  166.     <= H
  167.     glOrtho(-4.0, 4.0, -4.0*(GLfloat)H/(GLfloat)W,
  168.               4.0*(GLfloat)H/(GLfloat)W, -4.0, 4.0);
  169.     */
  170.     glOrtho(-4.0*(GLfloat)W/(GLfloat)H,
  171.               4.0*(GLfloat)W/(GLfloat)H, -4.0, 4.0, -4.0, 4.0);
  172.     glMatrixMode(GL_MODELVIEW);
  173.     glLoadIdentity();
  174.     glRotatef(85.0, 1.0, 1.0, 1.0);
  175.  
  176.     init();
  177.  
  178. }
  179.  
  180. void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
  181. {
  182.     wglMakeCurrent(NULL, NULL);
  183.     wglDeleteContext(hRC);
  184.     ReleaseDC(hwnd, hDC);
  185. }
  186.  
  187.  
  188. /* --------------------------------------------------------------- */
  189. #define imageWidth 64
  190. #define imageHeight 64
  191. GLubyte image[3*imageWidth*imageHeight];
  192.  
  193. void makeImage(void)
  194. {
  195.    int i, j;
  196.    float ti, tj;
  197.  
  198.    for (i = 0; i < imageWidth; i++) {
  199.       ti = 2.0*3.14159265*i/imageWidth;
  200.       for (j = 0; j < imageHeight; j++) {
  201.          tj = 2.0*3.14159265*j/imageHeight;
  202.          image[3*(imageHeight*i+j)] =
  203.               4*i;/*(GLubyte) 127*(1.0+sin(ti));*/
  204.          image[3*(imageHeight*i+j)+1] =
  205.               4*j;/*(GLubyte) 127*(1.0+cos(2*tj));*/
  206.          image[3*(imageHeight*i+j)+2] =
  207.               0;/*(GLubyte) 127*(1.0+cos(ti+tj));*/
  208.          if (image[3*(imageHeight*i+j)]>255) image[3*(imageHeight*i+j)] = 255;
  209.          if (image[3*(imageHeight*i+j)+1]>255) image[3*(imageHeight*i+j)+1] = 255;
  210.       }
  211.    }
  212. }
  213.  
  214.  
  215.  
  216.  
  217. /*/ --------------------------------------------------------------
  218. //               implementations
  219.  based:
  220.     http://glprogramming.com/red/chapter12.html
  221.     http://linux.die.net/man/3/glmap2f
  222. // --------------------------------------------------------------*/
  223.  
  224. GLfloat f0 = -2.0;
  225. GLfloat f1 = -1.5;
  226. GLfloat f2 = -1.0;
  227. GLfloat f3 = -0.5;
  228. GLfloat f4 =  0.5;
  229. GLfloat f5 =  1.0;
  230. GLfloat f6 =  1.5;
  231. GLfloat f7 =  2.0;
  232. GLfloat f8 =  3.0;
  233. GLfloat f9 =  4.0;
  234. GLfloat fa =  0.0;
  235.  
  236.  
  237. /*GLfloat ctrlpoints[4][4][3] = {
  238.    {{ -1.5, -1.5, 4.0}, { -0.5, -1.5, 2.0},    {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}},
  239.    {{ -1.5, -0.5, 1.0}, { -0.5, -0.5, 3.0},    {0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}},
  240.    {{ -1.5, 0.5, 4.0}, { -0.5, 0.5, 0.0},    {0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}},
  241.    {{ -1.5, 1.5, -2.0}, { -0.5, 1.5, -2.0},    {0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}}
  242. };*/
  243. GLfloat texpts[2][2][2] = {{{0.0, 0.0}, {0.0, 1.0}},
  244.                         {{1.0, 0.0}, {1.0, 1.0}}};
  245.  
  246.  
  247.  
  248.  
  249. static void init()
  250. {
  251.     GLfloat ctrlpoints[4][4][3] =
  252.     {
  253.       { {f1,f1,f9}, {f3,f1,f7}, {f4,f1,f2}, {f6,f1,f7} },
  254.       { {f1,f3,f5}, {f3,f3,f8}, {f4,f3,fa}, {f6,f3,f2} },
  255.       { {f1,f4,f9}, {f3,f4,fa}, {f4,f4,f8}, {f6,f4,f9} },
  256.       { {f1,f6,f0}, {f3,f6,f0}, {f4,f6,fa}, {f6,f6,f2} }
  257.     };
  258.  
  259.    glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
  260.            0, 1, 12, 4, &ctrlpoints[0][0][0]);
  261.    glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2,
  262.            0, 1, 4, 2, &texpts[0][0][0]);
  263.    glEnable(GL_MAP2_TEXTURE_COORD_2);
  264.    glEnable(GL_MAP2_VERTEX_3);
  265.    glMapGrid2f(8, 0.0, 1.0, 1, 0.0, 1.0);
  266.    makeImage();
  267.    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  268.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  269.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  270.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  271.                    GL_NEAREST);
  272.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  273.                    GL_NEAREST);
  274.    glTexImage2D(GL_TEXTURE_2D, 0, 3, imageWidth, imageHeight, 0,
  275.                 GL_RGB, GL_UNSIGNED_BYTE, image);
  276.    glEnable(GL_TEXTURE_2D);
  277.    glEnable(GL_DEPTH_TEST);
  278.    /*glShadeModel (GL_FLAT);*/
  279. }
  280.  
  281.  
  282. static void drawGLBagaca()
  283. {
  284.     /* OpenGL animation code goes here */
  285.     glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
  286.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  287.  
  288.     /*glEnable(GL_DEPTH_TEST);*/
  289.     /*glEnable(GL_DEPTH_WRITEMASK);*/
  290.  
  291.     GLfloat ctrlpoints[4][4][3] =
  292.     {
  293.       { {f1,f1,f9}, {f3,f1,f7}, {f4,f1,f2}, {f6,f1,f7} },
  294.       { {f1,f3,f5}, {f3,f3,f8}, {f4,f3,fa}, {f6,f3,f2} },
  295.       { {f1,f4,f9}, {f3,f4,fa}, {f4,f4,f8}, {f6,f4,f9} },
  296.       { {f1,f6,f0}, {f3,f6,f0}, {f4,f6,fa}, {f6,f6,f2} }
  297.     };
  298.  
  299.     glPushMatrix();
  300.     static float th = 0;
  301.     th += 0.1;
  302.     glRotatef(th, 0.0f, 0.0f, 1.0f);
  303.  
  304.     /*ctrlpoints[0][0][0] = th;*/
  305.     /*f1 = th;*/
  306.     glMap2f(GL_MAP2_VERTEX_3,
  307.             0, 1,
  308.             3,
  309.             4,
  310.            0, 1,
  311.            12,
  312.            4,
  313.            &ctrlpoints[0][0][0]);
  314.     glMap2f(GL_MAP2_TEXTURE_COORD_2,
  315.             0, 1,
  316.             2,
  317.             2,
  318.            0, 1,
  319.            4,
  320.            2,
  321.            &texpts[0][0][0]);
  322.     glMapGrid2f(8, 0.0, 1.0, 1, 0.0, 1.0);
  323.  
  324.     /*glBegin(GL_LINE_STRIP);
  325.  
  326.     glColor4f(1.0f, 0.0f, 0.0f, 0.1f);   glVertex2f(0.0f,   1.0f);
  327.     glColor4f(0.0f, 1.0f, 0.0f, 0.1f);   glVertex2f(0.87f,  -0.5f);
  328.     glColor4f(0.0f, 0.0f, 1.0f, 0.1f);   glVertex2f(-0.87f, -0.5f);
  329.  
  330.     glEnd();*/
  331.  
  332.     glScalef(0.3f,0.3f,0.3f);
  333.     glColor3f(1.0, 1.0, 1.0);
  334.     glEvalMesh2(GL_FILL, 0, 8, 0, 1);
  335.     glFlush();
  336.  
  337.     glPopMatrix();
  338. }
  339.  
  340.  
Advertisement
Add Comment
Please, Sign In to add comment