Advertisement
BlueBear

demoengine.cpp

Oct 2nd, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.41 KB | None | 0 0
  1. /* An example of the minimal Win32 & OpenGL program.  It only works in
  2. 16 bit color modes or higher (since it doesn't create a
  3. palette). */
  4. #pragma comment(lib, "opengl32.lib")
  5. #pragma comment(lib, "glew32.lib")
  6.  
  7. #include <windows.h>            /* must include this before GL/gl.h */
  8. #include <gl/glew.h>
  9. #include <GL/gl.h>          /* OpenGL header file */
  10. #include <GL/glu.h>         /* OpenGL utilities header file */
  11. #include <stdio.h>
  12.  
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16.  
  17. using namespace std;
  18.  
  19. HDC hDC;
  20.  
  21. // Globals
  22. // Real programs don't use globals :-D
  23. // Data would normally be read from files
  24. GLfloat vertices[] = { -1.0f, 0.0f, 0.0f,
  25. 0.0f, 1.0f, 0.0f,
  26. 0.0f, 0.0f, 0.0f };
  27. GLfloat colours[] = { 1.0f, 0.0f, 0.0f,
  28. 0.0f, 1.0f, 0.0f,
  29. 0.0f, 0.0f, 1.0f };
  30. GLfloat vertices2[] = { 0.0f, 0.0f, 0.0f,
  31. 0.0f, -1.0f, 0.0f,
  32. 1.0f, 0.0f, 0.0f };
  33.  
  34. // two vertex array objects, one for each object drawn
  35. unsigned int vertexArrayObjID[2];
  36. // three vertex buffer objects in this example
  37. unsigned int vertexBufferObjID[3];
  38.  
  39. void
  40. display()
  41. {
  42.     // Clear color and depth buffer
  43.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  44.  
  45.  
  46.     glBindVertexArray(vertexArrayObjID[0]); // First VAO
  47.     glDrawArrays(GL_TRIANGLES, 0, 3);   // draw first object
  48.  
  49.     glBindVertexArray(vertexArrayObjID[1]);     // select second VAO
  50.     glVertexAttrib3f((GLuint)1, 1.0, 0.0, 0.0); // set constant color attribute
  51.     glDrawArrays(GL_TRIANGLES, 0, 3);   // draw second object
  52.  
  53.     glBindVertexArray(0);
  54.     SwapBuffers(hDC);
  55. }
  56.  
  57.  
  58. LONG WINAPI
  59. WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  60. {
  61.     static PAINTSTRUCT ps;
  62.  
  63.     switch (uMsg) {
  64.     case WM_PAINT:
  65.         display();
  66.         BeginPaint(hWnd, &ps);
  67.         EndPaint(hWnd, &ps);
  68.         return 0;
  69.  
  70.     case WM_SIZE:
  71.         glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
  72.         PostMessage(hWnd, WM_PAINT, 0, 0);
  73.         return 0;
  74.  
  75.     case WM_CHAR:
  76.         switch (wParam) {
  77.         case 27:            /* ESC key */
  78.             PostQuitMessage(0);
  79.             break;
  80.         }
  81.         return 0;
  82.  
  83.     case WM_CLOSE:
  84.         PostQuitMessage(0);
  85.         return 0;
  86.     }
  87.  
  88.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  89. }
  90.  
  91.  
  92. char* loadFile(char *fname, GLint &fSize)
  93. {
  94.     ifstream::pos_type size;
  95.     char * memblock;
  96.     string text;
  97.  
  98.     // file read based on example in cplusplus.com tutorial
  99.     ifstream file(fname, ios::in | ios::binary | ios::ate);
  100.     if (file.is_open())
  101.     {
  102.         size = file.tellg();
  103.         fSize = (GLuint)size;
  104.         memblock = new char[size];
  105.         file.seekg(0, ios::beg);
  106.         file.read(memblock, size);
  107.         file.close();
  108.         cout << "file " << fname << " loaded" << endl;
  109.         text.assign(memblock);
  110.     }
  111.     else
  112.     {
  113.         cout << "Unable to open file " << fname << endl;
  114.         exit(1);
  115.     }
  116.     return memblock;
  117. }
  118.  
  119. HWND
  120. CreateOpenGLWindow(char* title, int x, int y, int width, int height,
  121. BYTE type, DWORD flags)
  122. {
  123.     int         pf;
  124.     HWND        hWnd;
  125.     WNDCLASS    wc;
  126.     PIXELFORMATDESCRIPTOR pfd;
  127.     static HINSTANCE hInstance = 0;
  128.  
  129.     /* only register the window class once - use hInstance as a flag. */
  130.     if (!hInstance) {
  131.         hInstance = GetModuleHandle(NULL);
  132.         wc.style = CS_OWNDC;
  133.         wc.lpfnWndProc = (WNDPROC)WindowProc;
  134.         wc.cbClsExtra = 0;
  135.         wc.cbWndExtra = 0;
  136.         wc.hInstance = hInstance;
  137.         wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  138.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  139.         wc.hbrBackground = NULL;
  140.         wc.lpszMenuName = NULL;
  141.         wc.lpszClassName = "OpenGL";
  142.  
  143.         if (!RegisterClass(&wc)) {
  144.             MessageBox(NULL, "RegisterClass() failed:  "
  145.                 "Cannot register window class.", "Error", MB_OK);
  146.             return NULL;
  147.         }
  148.     }
  149.  
  150.     hWnd = CreateWindow("OpenGL", title, WS_OVERLAPPEDWINDOW |
  151.         WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  152.         x, y, width, height, NULL, NULL, hInstance, NULL);
  153.  
  154.     if (hWnd == NULL) {
  155.         MessageBox(NULL, "CreateWindow() failed:  Cannot create a window.",
  156.             "Error", MB_OK);
  157.         return NULL;
  158.     }
  159.  
  160.     hDC = GetDC(hWnd);
  161.  
  162.     /* there is no guarantee that the contents of the stack that become
  163.     the pfd are zeroed, therefore _make sure_ to clear these bits. */
  164.     memset(&pfd, 0, sizeof(pfd));
  165.     pfd.nSize = sizeof(pfd);
  166.     pfd.nVersion = 1;
  167.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
  168.     pfd.iPixelType = type;
  169.     pfd.cColorBits = 32;
  170.  
  171.     pf = ChoosePixelFormat(hDC, &pfd);
  172.     if (pf == 0) {
  173.         MessageBox(NULL, "ChoosePixelFormat() failed:  "
  174.             "Cannot find a suitable pixel format.", "Error", MB_OK);
  175.         return 0;
  176.     }
  177.  
  178.     if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
  179.         MessageBox(NULL, "SetPixelFormat() failed:  "
  180.             "Cannot set format specified.", "Error", MB_OK);
  181.         return 0;
  182.     }
  183.  
  184.     DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  185.  
  186.     ReleaseDC(hWnd, hDC);
  187.  
  188.     return hWnd;
  189. }
  190.  
  191. int APIENTRY
  192. WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst,
  193. LPSTR lpszCmdLine, int nCmdShow)
  194. {
  195.     HDC hDC;                /* device context */
  196.     HGLRC hRC;              /* opengl context */
  197.     HWND  hWnd;             /* window */
  198.     MSG   msg;              /* message */
  199.  
  200.     hWnd = CreateOpenGLWindow("minimal", 0, 0, 800, 600, PFD_TYPE_RGBA, 0);
  201.     if (hWnd == NULL)
  202.         exit(1);
  203.     glEnable(GL_DEPTH_TEST);
  204.     glEnable(GL_CULL_FACE);
  205.     hDC = GetDC(hWnd);
  206.     hRC = wglCreateContext(hDC);
  207.     wglMakeCurrent(hDC, hRC);
  208.  
  209.     ShowWindow(hWnd, nCmdShow);
  210.     glewInit();
  211.  
  212.     // Allocate Vertex Array Objects
  213.     glGenVertexArrays(2, &vertexArrayObjID[0]);
  214.     // Setup first Vertex Array Object
  215.     glBindVertexArray(vertexArrayObjID[0]);
  216.     glGenBuffers(2, vertexBufferObjID);
  217.  
  218.     // VBO for vertex data
  219.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[0]);
  220.     glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
  221.     glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  222.     glEnableVertexAttribArray(0);
  223.  
  224.     // VBO for colour data
  225.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[1]);
  226.     glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), colours, GL_STATIC_DRAW);
  227.     glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
  228.     glEnableVertexAttribArray(1);
  229.  
  230.     // Setup second Vertex Array Object
  231.     glBindVertexArray(vertexArrayObjID[1]);
  232.     glGenBuffers(1, &vertexBufferObjID[2]);
  233.  
  234.     // VBO for vertex data
  235.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[2]);
  236.     glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), vertices2, GL_STATIC_DRAW);
  237.     glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  238.     glEnableVertexAttribArray(0);
  239.  
  240.     glBindVertexArray(0);
  241.  
  242.     GLuint p, f, v;
  243.  
  244.     char *vs, *fs;
  245.  
  246.     v = glCreateShader(GL_VERTEX_SHADER);
  247.     f = glCreateShader(GL_FRAGMENT_SHADER);
  248.  
  249.     // load shaders & get length of each
  250.     GLint vlen;
  251.     GLint flen;
  252.     vs = loadFile("shaders/shader.vert", vlen);
  253.     fs = loadFile("shaders/shader.frag", flen);
  254.  
  255.     const char * vv = vs;
  256.     const char * ff = fs;
  257.  
  258.     glShaderSource(v, 1, &vv, &vlen);
  259.     glShaderSource(f, 1, &ff, &flen);
  260.  
  261.     GLint compiled;
  262.  
  263.     glCompileShader(v);
  264.     glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
  265.     if (!compiled)
  266.     {
  267.         cout << "Vertex shader not compiled." << endl;
  268.     }
  269.  
  270.     glCompileShader(f);
  271.     glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
  272.     if (!compiled)
  273.     {
  274.         cout << "Fragment shader not compiled." << endl;
  275.     }
  276.  
  277.     p = glCreateProgram();
  278.  
  279.     glBindAttribLocation(p, 0, "in_Position");
  280.     glBindAttribLocation(p, 1, "in_Color");
  281.  
  282.     glAttachShader(p, v);
  283.     glAttachShader(p, f);
  284.  
  285.     glLinkProgram(p);
  286.     glUseProgram(p);
  287.  
  288.     delete[] vs; // dont forget to free allocated memory
  289.     delete[] fs; // we allocated this in the loadFile function...
  290.  
  291.     while (GetMessage(&msg, hWnd, 0, 0)) {
  292.         TranslateMessage(&msg);
  293.         DispatchMessage(&msg);
  294.     }
  295.  
  296.     wglMakeCurrent(NULL, NULL);
  297.     ReleaseDC(hWnd, hDC);
  298.     wglDeleteContext(hRC);
  299.     DestroyWindow(hWnd);
  300.  
  301.     return msg.wParam;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement