Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <gl/gl.h>
- #include <GL/glu.h>
- LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
- void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
- void DisableOpenGL(HWND, HDC, HGLRC);
- static void init();
- static void drawGLBagaca();
- #define W 256
- #define H 256
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- WNDCLASSEX wcex;
- HWND hwnd;
- HDC hDC;
- HGLRC hRC;
- MSG msg;
- BOOL bQuit = FALSE;
- /* register window class */
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_OWNDC;
- wcex.lpfnWndProc = WindowProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = "GLSample";
- wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
- if (!RegisterClassEx(&wcex))
- return 0;
- /* create main window */
- hwnd = CreateWindowEx(0,
- "GLSample",
- "Sample",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- W,
- H,
- NULL,
- NULL,
- hInstance,
- NULL);
- ShowWindow(hwnd, nCmdShow);
- /* enable OpenGL for the window */
- EnableOpenGL(hwnd, &hDC, &hRC);
- /* program main loop */
- while (!bQuit)
- {
- /* check for messages */
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- /* handle or dispatch messages */
- if (msg.message == WM_QUIT)
- {
- bQuit = TRUE;
- }
- else
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- else
- {
- drawGLBagaca();
- SwapBuffers(hDC);
- Sleep (1);
- }
- }
- /* shutdown OpenGL */
- DisableOpenGL(hwnd, hDC, hRC);
- /* destroy the window explicitly */
- DestroyWindow(hwnd);
- return msg.wParam;
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CLOSE:
- PostQuitMessage(0);
- break;
- case WM_DESTROY:
- return 0;
- case WM_SHOWWINDOW:
- /*init();*/
- break;
- case WM_KEYDOWN:
- {
- switch (wParam)
- {
- case VK_ESCAPE:
- PostQuitMessage(0);
- break;
- }
- }
- break;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
- {
- PIXELFORMATDESCRIPTOR pfd;
- int iFormat;
- /* get the device context (DC) */
- *hDC = GetDC(hwnd);
- /* set the pixel format for the DC */
- ZeroMemory(&pfd, sizeof(pfd));
- pfd.nSize = sizeof(pfd);
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW |
- PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 24;
- pfd.cDepthBits = 16;
- pfd.iLayerType = PFD_MAIN_PLANE;
- iFormat = ChoosePixelFormat(*hDC, &pfd);
- SetPixelFormat(*hDC, iFormat, &pfd);
- /* create and enable the render context (RC) */
- *hRC = wglCreateContext(*hDC);
- wglMakeCurrent(*hDC, *hRC);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- /*
- <= H
- glOrtho(-4.0, 4.0, -4.0*(GLfloat)H/(GLfloat)W,
- 4.0*(GLfloat)H/(GLfloat)W, -4.0, 4.0);
- */
- glOrtho(-4.0*(GLfloat)W/(GLfloat)H,
- 4.0*(GLfloat)W/(GLfloat)H, -4.0, 4.0, -4.0, 4.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(85.0, 1.0, 1.0, 1.0);
- init();
- }
- void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
- {
- wglMakeCurrent(NULL, NULL);
- wglDeleteContext(hRC);
- ReleaseDC(hwnd, hDC);
- }
- /* --------------------------------------------------------------- */
- #define imageWidth 64
- #define imageHeight 64
- GLubyte image[3*imageWidth*imageHeight];
- void makeImage(void)
- {
- int i, j;
- float ti, tj;
- for (i = 0; i < imageWidth; i++) {
- ti = 2.0*3.14159265*i/imageWidth;
- for (j = 0; j < imageHeight; j++) {
- tj = 2.0*3.14159265*j/imageHeight;
- image[3*(imageHeight*i+j)] =
- 4*i;/*(GLubyte) 127*(1.0+sin(ti));*/
- image[3*(imageHeight*i+j)+1] =
- 4*j;/*(GLubyte) 127*(1.0+cos(2*tj));*/
- image[3*(imageHeight*i+j)+2] =
- 0;/*(GLubyte) 127*(1.0+cos(ti+tj));*/
- if (image[3*(imageHeight*i+j)]>255) image[3*(imageHeight*i+j)] = 255;
- if (image[3*(imageHeight*i+j)+1]>255) image[3*(imageHeight*i+j)+1] = 255;
- }
- }
- }
- /*/ --------------------------------------------------------------
- // implementations
- based:
- http://glprogramming.com/red/chapter12.html
- http://linux.die.net/man/3/glmap2f
- // --------------------------------------------------------------*/
- GLfloat f0 = -2.0;
- GLfloat f1 = -1.5;
- GLfloat f2 = -1.0;
- GLfloat f3 = -0.5;
- GLfloat f4 = 0.5;
- GLfloat f5 = 1.0;
- GLfloat f6 = 1.5;
- GLfloat f7 = 2.0;
- GLfloat f8 = 3.0;
- GLfloat f9 = 4.0;
- GLfloat fa = 0.0;
- /*GLfloat ctrlpoints[4][4][3] = {
- {{ -1.5, -1.5, 4.0}, { -0.5, -1.5, 2.0}, {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}},
- {{ -1.5, -0.5, 1.0}, { -0.5, -0.5, 3.0}, {0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}},
- {{ -1.5, 0.5, 4.0}, { -0.5, 0.5, 0.0}, {0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}},
- {{ -1.5, 1.5, -2.0}, { -0.5, 1.5, -2.0}, {0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}}
- };*/
- GLfloat texpts[2][2][2] = {{{0.0, 0.0}, {0.0, 1.0}},
- {{1.0, 0.0}, {1.0, 1.0}}};
- static void init()
- {
- GLfloat ctrlpoints[4][4][3] =
- {
- { {f1,f1,f9}, {f3,f1,f7}, {f4,f1,f2}, {f6,f1,f7} },
- { {f1,f3,f5}, {f3,f3,f8}, {f4,f3,fa}, {f6,f3,f2} },
- { {f1,f4,f9}, {f3,f4,fa}, {f4,f4,f8}, {f6,f4,f9} },
- { {f1,f6,f0}, {f3,f6,f0}, {f4,f6,fa}, {f6,f6,f2} }
- };
- glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
- 0, 1, 12, 4, &ctrlpoints[0][0][0]);
- glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2,
- 0, 1, 4, 2, &texpts[0][0][0]);
- glEnable(GL_MAP2_TEXTURE_COORD_2);
- glEnable(GL_MAP2_VERTEX_3);
- glMapGrid2f(8, 0.0, 1.0, 1, 0.0, 1.0);
- makeImage();
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
- GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, 3, imageWidth, imageHeight, 0,
- GL_RGB, GL_UNSIGNED_BYTE, image);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_DEPTH_TEST);
- /*glShadeModel (GL_FLAT);*/
- }
- static void drawGLBagaca()
- {
- /* OpenGL animation code goes here */
- glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- /*glEnable(GL_DEPTH_TEST);*/
- /*glEnable(GL_DEPTH_WRITEMASK);*/
- GLfloat ctrlpoints[4][4][3] =
- {
- { {f1,f1,f9}, {f3,f1,f7}, {f4,f1,f2}, {f6,f1,f7} },
- { {f1,f3,f5}, {f3,f3,f8}, {f4,f3,fa}, {f6,f3,f2} },
- { {f1,f4,f9}, {f3,f4,fa}, {f4,f4,f8}, {f6,f4,f9} },
- { {f1,f6,f0}, {f3,f6,f0}, {f4,f6,fa}, {f6,f6,f2} }
- };
- glPushMatrix();
- static float th = 0;
- th += 0.1;
- glRotatef(th, 0.0f, 0.0f, 1.0f);
- /*ctrlpoints[0][0][0] = th;*/
- /*f1 = th;*/
- glMap2f(GL_MAP2_VERTEX_3,
- 0, 1,
- 3,
- 4,
- 0, 1,
- 12,
- 4,
- &ctrlpoints[0][0][0]);
- glMap2f(GL_MAP2_TEXTURE_COORD_2,
- 0, 1,
- 2,
- 2,
- 0, 1,
- 4,
- 2,
- &texpts[0][0][0]);
- glMapGrid2f(8, 0.0, 1.0, 1, 0.0, 1.0);
- /*glBegin(GL_LINE_STRIP);
- glColor4f(1.0f, 0.0f, 0.0f, 0.1f); glVertex2f(0.0f, 1.0f);
- glColor4f(0.0f, 1.0f, 0.0f, 0.1f); glVertex2f(0.87f, -0.5f);
- glColor4f(0.0f, 0.0f, 1.0f, 0.1f); glVertex2f(-0.87f, -0.5f);
- glEnd();*/
- glScalef(0.3f,0.3f,0.3f);
- glColor3f(1.0, 1.0, 1.0);
- glEvalMesh2(GL_FILL, 0, 8, 0, 1);
- glFlush();
- glPopMatrix();
- }
Advertisement
Add Comment
Please, Sign In to add comment