Advertisement
Guest User

Untitled

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