Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdlib.h>
- #include <math.h>
- #include<GL/gl.h>
- #include<GL/glu.h>
- const double gamma = 2.0;
- GLfloat CorrectGamma(double c)
- {
- return pow(c, gamma);
- }
- void HSVToRGB(float h, float s, float v, float &r, float &g, float &b){
- int i;
- float f, q, p, t;
- h /= 60;
- i = floor(h);
- f = h - i;
- p = v*(1-s);
- q = v*(1-s*f);
- t = v*(1-s*(1-f));
- switch(i)
- {
- case 0:
- r = v;
- g = t;
- b = p;
- break;
- case 1:
- r = q;
- g = v;
- b = p;
- break;
- case 2:
- r = p;
- g = v;
- b = t;
- break;
- case 3:
- r = p;
- g = q;
- b = t;
- break;
- case 4:
- r = t;
- g = p;
- b = v;
- break;
- case 5:
- r = v;
- g = p;
- b = q;
- break;
- }
- }
- void DrawScene(GLfloat xRot, GLfloat yRot)
- {
- int i;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glRotatef(xRot, 1.0f, 0.0f, 0.0f);
- glRotatef(yRot, 0.0f, 1.0f, 0.0f);
- glBegin(GL_LINES);
- glColor3f(1,1,1);
- glVertex3f(10,0,0);
- glVertex3f(-10,0,0);
- glVertex3f(0,10,0);
- glVertex3f(0,-10,0);
- glVertex3f(0,0,10);
- glVertex3f(0,0,-10);
- glEnd();
- glBegin(GL_QUAD_STRIP); //ref bar
- glColor3f(0,0,0);
- glVertex3f(-5,-7,6);
- glVertex3f(-5,-4,6);
- for(i=1;i<=10;i++)
- {
- glColor3f(CorrectGamma(0),CorrectGamma(0.1*(GLfloat)i),CorrectGamma(0));
- glVertex3f(-5+(GLfloat)i,-7,6);
- glVertex3f(-5+(GLfloat)i,-4,6);
- }
- glEnd();
- glBegin(GL_QUAD_STRIP); //mod bar
- glColor3f(0,0,0);
- glVertex3f(-5,-10,6);
- glVertex3f(-5,-7,6);
- for(i=1;i<=10;i++)
- {
- glColor3f(0,0.1*(GLfloat)i,0);
- glVertex3f(-5+(GLfloat)i,-10,6);
- glVertex3f(-5+(GLfloat)i,-7,6);
- }
- glEnd();
- // Zadanie 3 - tęcza
- //glBegin(GL_QUAD_STRIP); //mod bar
- // float r, g, b;
- // for(i=0;i<=10;i++)
- // {
- // HSVToRGB(36*i, 1, 1, r, g, b);
- // //glColor3f(0,0.1*(GLfloat)i,0);
- // glColor3f(r, g, b);
- // glVertex3f(-5+(GLfloat)i,0,6);
- // glVertex3f(-5+(GLfloat)i,-3,6);
- // }
- //glEnd();
- // Zadanie 2 - Kostka
- //glBegin(GL_QUADS);
- // //left
- // glColor3f(0,0,0);
- // glVertex3f(-3,-3,-3);
- // glColor3f(0,1,0);
- // glVertex3f(-3,3,-3);
- // glColor3f(1,1,0);
- // glVertex3f(3,3,-3);
- // glColor3f(1,0,0);
- // glVertex3f(3,-3,-3);
- // //glColor4f(0,1.0,0,0.6);
- // //glVertex3f(-3,-3,3);
- // //glVertex3f(-3,3,3);
- // //glVertex3f(3,3,3);
- // //glVertex3f(3,-3,3);
- // //front
- // glColor3f(1,0,1);
- // glVertex3f(3,-3,3);
- // glColor3f(0,0,1);
- // glVertex3f(-3,-3,3);
- // glColor3f(0,0,0);
- // glVertex3f(-3,-3,-3);
- // glColor3f(1,0,0);
- // glVertex3f(3,-3,-3);
- // //right
- // glColor3f(1,0,1);
- // glVertex3f(3,-3,3);
- // glColor3f(0,0,1);
- // glVertex3f(-3,-3,3);
- // glColor3f(0,1,1);
- // glVertex3f(-3,3,3);
- // glColor3f(1,1,1);
- // glVertex3f(3,3,3);
- // //back
- // glColor3f(1,1,0);
- // glVertex3f(3,3,-3);
- // glColor3f(0,1,0);
- // glVertex3f(-3,3,-3);
- // glColor3f(0,1,1);
- // glVertex3f(-3,3,3);
- // glColor3f(1,1,1);
- // glVertex3f(3,3,3);
- // //bot
- // glColor3f(0,0,0);
- // glVertex3f(-3,-3,-3);
- // glColor3f(0,1,0);
- // glVertex3f(-3,3,-3);
- // glColor3f(0,1,1);
- // glVertex3f(-3,3,3);
- // glColor3f(0,0,1);
- // glVertex3f(-3,-3,3);
- // //top
- // glColor3f(1,1,0);
- // glVertex3f(3,3,-3);
- // glColor3f(1,0,0);
- // glVertex3f(3,-3,-3);
- // glColor3f(1,0,1);
- // glVertex3f(3,-3,3);
- // glColor3f(1,1,1);
- // glVertex3f(3,3,3);
- //glEnd();
- glEnd();
- glFinish();
- }
- void SetMyPixelFormat(HDC hdc)
- {
- PIXELFORMATDESCRIPTOR pfd;
- ZeroMemory(&pfd, sizeof(pfd));
- pfd.nSize = sizeof(pfd);
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 32;
- pfd.cDepthBits = 16;
- pfd.iLayerType = PFD_MAIN_PLANE;
- int nPixelFormat = ChoosePixelFormat(hdc, &pfd);
- SetPixelFormat(hdc, nPixelFormat, &pfd);
- }
- void ResizeWindow(int width, int height)
- {
- if (height*width==0) return;
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-10, 10, -10, 10, -10, 10);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glEnable(GL_DEPTH_TEST);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// set the blend mode
- glEnable(GL_BLEND); //obligatory for blending and transparencies
- glEnable(GL_LINE_SMOOTH);
- //glEnable(GL_CULL_FACE);
- glShadeModel(GL_FLAT);
- glShadeModel(GL_SMOOTH);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- static HGLRC hrc;
- static GLfloat xRot = 0.0f;
- static GLfloat yRot = 0.0f;
- switch (message)
- {
- case WM_SIZE:
- ResizeWindow(LOWORD(lParam),HIWORD(lParam));
- break;
- case WM_CREATE:
- hdc = GetDC(hWnd);
- SetMyPixelFormat(hdc);
- hrc = wglCreateContext(hdc);
- wglMakeCurrent(hdc, hrc);
- ReleaseDC(hWnd, hdc);
- break;
- case WM_KEYDOWN:
- if(wParam==VK_UP) xRot-= 5.0f;
- if(wParam==VK_DOWN) xRot += 5.0f;
- if(wParam==VK_LEFT) yRot -= 5.0f;
- if(wParam==VK_RIGHT) yRot += 5.0f;
- if(xRot > 356.0f) xRot = 0.0f;
- if(xRot < -1.0f) xRot = 355.0f;
- if(yRot > 356.0f) yRot = 0.0f;
- if(yRot < -1.0f) yRot = 355.0f;
- InvalidateRect(hWnd,NULL,FALSE);
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- DrawScene(xRot, yRot);
- SwapBuffers(hdc);
- EndPaint(hWnd, &ps);
- break;
- case WM_ERASEBKGND:
- return 1;
- break;
- case WM_DESTROY:
- wglMakeCurrent(NULL, NULL);
- wglDeleteContext(hrc);
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = NULL;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = "Primitives";
- wcex.hIconSm = NULL;
- return RegisterClassEx(&wcex);
- }
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hWnd = CreateWindow("Primitives", "OGL color lab", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
- if (!hWnd) return FALSE;
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- MyRegisterClass(hInstance);
- if (!InitInstance(hInstance, nCmdShow)) return FALSE;
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int) msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement