Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 17th, 2012  |  syntax: C++  |  size: 11.98 KB  |  views: 51  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //////////////////////////////
  2. //Main Libary
  3. /////////////////////////////
  4. #define GLEW_STATIC
  5. #include <windows.h>
  6. #include "SDL/SDL_image.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <glew.h>
  10. #include <iostream>
  11. #include <cassert>
  12. #include <string>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <fstream>
  16. #include <glext.h>
  17. #include <gl/gl.h>
  18. #include <gl/glu.h>
  19. #include <Commdlg.h>
  20. #include <iterator>
  21. #include<IL/il.h>
  22. #include "resource.h"
  23. using namespace std;
  24. //Screen Sizes
  25. #define SCREEN_WIDTH 1280
  26. #define SCREEN_HEIGHT 1024
  27.  
  28.  
  29. //Buffers
  30. GLuint matrixuniform;
  31. GLuint Translationuniform;
  32. GLuint vertexbuffer;
  33. GLuint vao;
  34. GLuint textureID[0];
  35.  
  36. struct Point
  37. {
  38.  
  39. float x,  y, z;
  40.  
  41. };
  42.  
  43. vector<unsigned int>faces;
  44. unsigned int face[3];
  45.  
  46. size_t filesize;
  47. vector<Point>points;
  48. //float* point=reinterpret_cast<float*>(&points[0]);
  49. //int num_bytes = points.size() * sizeof(points[0]);
  50.  
  51.  
  52.  
  53.  
  54. static const GLfloat myMatrix[] =
  55. {
  56.  
  57.   1.0, 0.0, 0.0, 0.0,
  58.   0.0, 1.0, 0.0, 0.0,
  59.   0.0, 0.0, 1.0, 0.0,
  60.   0.0, 0.0, 0.0, 1.0
  61.  
  62.  
  63. };
  64.  
  65.  
  66.  
  67.  
  68.  
  69. //Device Context
  70. HDC hDC;
  71.  
  72. //OpenGL
  73. HGLRC hRC;
  74.  
  75. //Window Class
  76. WNDCLASSEX wc;
  77.  
  78. //Window Handle
  79. HWND hwnd;
  80.  
  81. //Messages
  82. MSG Msg;
  83.  
  84.  
  85. //LOAD SHADER FUNCTION
  86. GLuint LoadProgram(const char * const vertex_file_path, const char * Fragment_file_path)
  87. {
  88.  
  89.   GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  90.   GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  91.  
  92.   //READ THE VERTEX SHADER CODE
  93.   string VertexShaderCode;
  94.   ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  95.   if(VertexShaderStream.is_open())
  96.   {
  97.     string Line = "";
  98.     while(getline(VertexShaderStream, Line))
  99.       VertexShaderCode += "\n" + Line;
  100.     VertexShaderStream.close();
  101.   }
  102.  
  103.   //READ THE FRAGMENT SHADER CODE
  104.   string FragmentShaderCode;
  105.   ifstream FragmentShaderStream(Fragment_file_path, std::ios::in);
  106.   if(FragmentShaderStream.is_open())
  107.   {
  108.  
  109.     string Line = "";
  110.     while(getline(FragmentShaderStream, Line))
  111.       FragmentShaderCode += "\n" + Line;
  112.     FragmentShaderStream.close();
  113.  
  114.   }
  115.  
  116.   GLint Result = GL_FALSE;
  117.   int InfoLogLength;
  118.  
  119.   //Compile Vertex Shader
  120.   printf("Compiling Shader : %s\n", vertex_file_path);
  121.   char const * VertexSourcePointer = VertexShaderCode.c_str();
  122.   glShaderSource(VertexShaderID,  1,  &VertexSourcePointer,  NULL);
  123.   glCompileShader(VertexShaderID);
  124.  
  125.   //Check Vertex Shader
  126.   glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS,  &Result);
  127.   glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  128.   vector<char> VertexShaderErrorMessage(InfoLogLength);
  129.   glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  130.   fprintf(stdout, "%s\n",  &VertexShaderErrorMessage[0]);
  131.  
  132.   //Compile Fragment Shader
  133.   printf("Compiling Shader : %s\n",  Fragment_file_path);
  134.   char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  135.   glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
  136.   glCompileShader(FragmentShaderID);
  137.  
  138.   //Check Fragment Shader
  139.   glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  140.   glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  141.   vector<char> FragmentShaderErrorMessage(InfoLogLength);
  142.   glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  143.   fprintf(stdout,  "%s\n",  &FragmentShaderErrorMessage[0]);
  144.  
  145.   fprintf(stdout,  "Linking Program\n");
  146.   GLuint ProgramID = glCreateProgram();
  147.  
  148.   //Bind Attribute
  149.   glBindAttribLocation(ProgramID, 0, "position");
  150.  // glBindAttribLocation(ProgramID, 1, "Texcoord0");
  151.  
  152.   //Link The Program
  153.   glAttachShader(ProgramID, VertexShaderID);
  154.   glAttachShader(ProgramID, FragmentShaderID);
  155.   glLinkProgram(ProgramID);
  156.  
  157.   GLuint texture1;
  158.  
  159. //  texture1 = glGetUniformLocation(ProgramID, "myTextureSampler");
  160.   matrixuniform =  glGetUniformLocation(ProgramID, "myMatrix");
  161.  // Translationuniform = glGetUniformLocation(ProgramID, "TranslationMatrix");
  162.  
  163.  
  164.   //Check The Program
  165.   glGetProgramiv(ProgramID,  GL_LINK_STATUS, &Result);
  166.   glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  167.   vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
  168.   fprintf(stdout,  "%s\n", &ProgramErrorMessage[0]);
  169.  
  170.   //Delete Shader
  171.   glDeleteShader(VertexShaderID);
  172.   glDeleteShader(FragmentShaderID);
  173.  
  174.   glUseProgram(ProgramID);
  175.  
  176.   //Return ProgramID
  177.   return ProgramID;
  178. }
  179.  
  180.  
  181. static const GLfloat test[] = {
  182.   -1.0f, -1.0f, 0.0f,
  183.   1.0f, -1.0f, 0.0f,  
  184.   0.0f,  1.0f, 0.0f,  
  185. };
  186.  
  187. static const GLfloat texture[] = {
  188.  
  189.   -1.0f, 0.0f,
  190.   0.0f, 1.0f,
  191.   -1.0f, 1.0f
  192.  
  193. };
  194.  
  195.  
  196.  
  197. // number of available formats
  198. int indexPixelFormat = 0;
  199. GLuint  PixelFormat;
  200.  
  201. int DescribePixelFormat
  202. (
  203.  HDC  hdc,           // device context of interest
  204.  int  iPixelFormat,  // pixel format selector
  205.  UINT nBytes,        // size of buffer pointed to by ppfd
  206.  LPPIXELFORMATDESCRIPTOR  ppfd
  207.  // pointer to structure to receive pixel
  208.  // format data
  209.  );
  210.  
  211.  
  212. const char g_szClassName[] = "myWindowClass";
  213.  
  214. HINSTANCE hInst;
  215.  
  216. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  217.  
  218. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  219.     LPSTR lpCmdLine, int nCmdShow)
  220. {
  221.  
  222.  
  223.   //Step 1: Registering the Window Class
  224.   wc.cbSize        = sizeof(WNDCLASSEX);
  225.   wc.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;;
  226.   wc.lpfnWndProc   = WndProc;
  227.   wc.cbClsExtra    = 0;
  228.   wc.cbWndExtra    = 0;
  229.   wc.hInstance     = hInst;
  230.   wc.hIcon         = static_cast<HICON>(LoadImage(hInstance,MAKEINTRESOURCE(IDI_MYICON),IMAGE_ICON,32, 32,      LR_DEFAULTSIZE));
  231.   wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
  232.   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  233.   wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  234.   wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
  235.   wc.lpszClassName = g_szClassName;
  236.   wc.hIconSm       = static_cast<HICON>(LoadImage(hInstance,MAKEINTRESOURCE(IDI_MYICON),IMAGE_ICON,16,16,LR_DEFAULTSIZE));
  237.  
  238.   if(!RegisterClassEx(&wc))
  239.   {
  240.     MessageBox(NULL, "Window Registration Failed!", "Error!",
  241.         MB_ICONEXCLAMATION | MB_OK);
  242.     return 0;
  243.   }
  244.  
  245.   // Step 2: Creating the Window
  246.   hwnd = CreateWindowEx(
  247.       3,
  248.       g_szClassName,
  249.       "program",
  250.       WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  251.       CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT,
  252.       NULL, NULL, hInstance, NULL);
  253.  
  254.   if(hwnd == NULL)
  255.   {
  256.     MessageBox(NULL, "Window Creation Failed!", "Error!",
  257.         MB_ICONEXCLAMATION | MB_OK);
  258.     return 0;
  259.   }
  260.  
  261.   hDC = GetDC(hwnd);  //get current windows device context
  262.  
  263.  
  264.  
  265.   static        PIXELFORMATDESCRIPTOR pfd=
  266.   {
  267.     sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
  268.     1,                                  // Version Number (?)
  269.     PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
  270.       PFD_SUPPORT_OPENGL |                      // Format Must Support OpenGL
  271.       PFD_DOUBLEBUFFER,                 // Must Support Double Buffering
  272.     PFD_TYPE_RGBA,                              // Request An RGBA Format
  273.     16,                                 // Select A 16Bit Color Depth
  274.     0, 0, 0, 0, 0, 0,                   // Color Bits Ignored (?)
  275.     0,                                  // No Alpha Buffer
  276.     0,                                  // Shift Bit Ignored (?)
  277.     0,                                  // No Accumulation Buffer
  278.     0, 0, 0, 0,                         // Accumulation Bits Ignored (?)
  279.     16,                                 // 16Bit Z-Buffer (Depth Buffer)  
  280.     0,                                  // No Stencil Buffer
  281.     0,                                  // No Auxiliary Buffer (?)
  282.     PFD_MAIN_PLANE,                             // Main Drawing Layer
  283.     0,                                  // Reserved (?)
  284.     0, 0, 0                                     // Layer Masks Ignored (?)
  285.   };
  286.  
  287.   //Choose a Pixel Format
  288.   int ChoosenPixelFormat =  ChoosePixelFormat(hDC,  &pfd);
  289.  
  290.   //Set Pixel Format Close to Screen Format
  291.   SetPixelFormat(hDC, ChoosenPixelFormat,  &pfd);
  292.  
  293.   //Make OpenGL Context
  294.   hRC = wglCreateContext(hDC);
  295.  
  296.   //Make Opengl Context Current
  297.   wglMakeCurrent(hDC, hRC);
  298.  
  299.  
  300.   glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  301.   glGetError();
  302.  
  303.   glewInit();
  304.  
  305.   LoadProgram("graphicaluserinterface.v", "graphicaluserinterface.f");
  306.  
  307.   glGenVertexArrays(1, &vao);
  308.  
  309.   glBindVertexArray(vao);
  310.  
  311.   glGenBuffers(1, &vertexbuffer);  
  312.  
  313.   glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  314.  
  315.   glUniformMatrix4fv(matrixuniform, 1, TRUE, myMatrix);
  316.  
  317.   glBufferData(GL_ARRAY_BUFFER, points.size(), &points[0], GL_STATIC_DRAW);
  318.  
  319.    glEnable(GL_DEPTH_TEST);
  320.  
  321. glGetError();
  322.  
  323.   ShowWindow(hwnd, nCmdShow);
  324.   UpdateWindow(hwnd);
  325.  
  326.  
  327.  
  328.  
  329.   // Step 3: The Message Loop
  330.   while(GetMessage(&Msg, NULL, 0, 0) > 0)
  331.   {
  332.     TranslateMessage(&Msg);
  333.     DispatchMessage(&Msg);
  334.   }
  335.   return Msg.wParam;
  336. }
  337.  
  338.  
  339. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,WPARAM wParam, LPARAM lParam)
  340. {
  341.    PAINTSTRUCT ps;
  342.  
  343.   switch(msg)
  344.   {
  345.  
  346.     case WM_CREATE: //window being created
  347.  ValidateRect(hwnd, NULL);
  348.      
  349.       return 0;
  350.       break;
  351.  
  352.     case WM_ERASEBKGND:
  353.       return true;
  354.       break;
  355.  
  356.     case WM_CLOSE:  //window is closing
  357.  
  358.       /*      Deselect rendering context and delete it*/
  359.       wglMakeCurrent(hDC, NULL);
  360.       wglDeleteContext(hRC);
  361.  
  362.       /*      Send quit message to queue*/
  363.       PostQuitMessage(0);
  364.  
  365.       return 0;
  366.       break;
  367.     case WM_COMMAND:
  368.    
  369.       switch(LOWORD(wParam))
  370.    
  371.     case ID_BOX:
  372.     {
  373.      
  374.        OPENFILENAME ofn;
  375.       char szFileName[MAX_PATH] = "";
  376.  
  377.     ZeroMemory(&ofn, sizeof(ofn));
  378.  
  379.     ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
  380.     ofn.hwndOwner = hwnd;
  381.     ofn.lpstrFilter = ".OBJ (*.obj)\0*.obj\0All Files (*.*)\0*.*\0";
  382.     ofn.lpstrFile = szFileName;
  383.     ofn.nMaxFile = MAX_PATH;
  384.     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  385.     ofn.lpstrDefExt = "obj";
  386.  
  387.  if(GetOpenFileName(&ofn))
  388.     {
  389.         // Do something usefull with the filename stored in szFileName <--
  390.     //    cout << "File Path = "<<  szFileName << endl;
  391.  
  392.  
  393.  
  394.      
  395.  
  396. //make a fileobject and store list and the index of that list in a c string
  397.           ifstream file (szFileName);
  398.  
  399.  
  400.  
  401.           //While FileObject not equal end of file
  402.           while (!file.eof() )
  403.           {
  404.  
  405.             char modelbuffer[20000];
  406.  
  407.             file.getline(modelbuffer, 20000);
  408.  
  409.             switch(modelbuffer[0])
  410.             {
  411.  
  412.               cout << " " << endl;
  413.  
  414.               case 'v' :
  415.  
  416.               Point p;
  417.  
  418.               sscanf(modelbuffer, "v %f %f %f",  &p.x, &p.y, &p.z);
  419.  
  420.               points.push_back(p);
  421.  
  422.               cout << " p.x = " << p.x << " p.y = " << p.y << " p.z = " << p.x << endl;
  423.  
  424.               break;
  425.  
  426.               cout << " " << endl;
  427.  
  428.               case 'f':
  429.  
  430.               int read_count = sscanf(modelbuffer, "f %d %d %d %d", &face[0], &face[1], &face[2],  &face[3]);
  431.  
  432.               cout << "face[0] = " << face[0] << " face[1] = " << face[1] << " face[2] = " << face[2] << " face[3] = " << face[3] << "\n";
  433.  
  434.               if(read_count !=4)
  435.               {
  436.  
  437.                 cout << "bad/n";
  438.                 throw std::exception();
  439.  
  440.               }
  441.  
  442.               faces.push_back(face[0] - 1);
  443.               faces.push_back(face[1] - 1);
  444.               faces.push_back(face[2] - 1);
  445.               faces.push_back(face[3] - 1);
  446.  
  447.               cout << face[0] - 1 << face[1] - 1 << face[2] - 1 << face[3] - 1 << endl;
  448.  
  449.  
  450.             }
  451.    
  452. }
  453.    
  454. }
  455. }  
  456.  
  457.         break;
  458.  
  459.  
  460.     case WM_PAINT:
  461.      //Draw
  462.  
  463.   hDC = BeginPaint(hwnd, &ps);
  464.  
  465. // glEnableClientState(GL_VERTEX_ARRAY);
  466.      
  467.   glClearColor(1, 0, 0, 1);
  468.  
  469.  
  470.   glClear(GL_COLOR_BUFFER_BIT);
  471.  
  472.  
  473.   glEnableVertexAttribArray(0);
  474.  
  475.   glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  476.  
  477.  
  478.   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 *sizeof(points),points.data());
  479.  
  480.   glDrawElements( GL_QUADS, faces.size(), GL_UNSIGNED_INT,  faces.data());
  481.  
  482.   SwapBuffers(hDC);
  483.  
  484.  
  485.   glGetError();
  486.  
  487.  
  488.  
  489.  EndPaint(hwnd, &ps);  
  490.       break;
  491.  
  492.     case WM_DESTROY:
  493.       PostQuitMessage(0);
  494.       break;
  495.  
  496.     case ID_FILE_EXIT:
  497.       PostQuitMessage(0);      
  498.       return 0;
  499.     break;
  500.  
  501.  
  502.     default:
  503.       return DefWindowProc(hwnd, msg, wParam, lParam);
  504.   }
  505.  
  506.  
  507.  
  508.   return 0;
  509. }
clone this paste RAW Paste Data