////////////////////////////// //Main Libary ///////////////////////////// #define GLEW_STATIC #include #include "SDL/SDL_image.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "resource.h" using namespace std; //Screen Sizes #define SCREEN_WIDTH 1280 #define SCREEN_HEIGHT 1024 //Buffers GLuint matrixuniform; GLuint Translationuniform; GLuint vertexbuffer; GLuint vao; GLuint textureID[0]; struct Point { float x, y, z; }; vectorfaces; unsigned int face[3]; size_t filesize; vectorpoints; //float* point=reinterpret_cast(&points[0]); //int num_bytes = points.size() * sizeof(points[0]); static const GLfloat myMatrix[] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; //Device Context HDC hDC; //OpenGL HGLRC hRC; //Window Class WNDCLASSEX wc; //Window Handle HWND hwnd; //Messages MSG Msg; //LOAD SHADER FUNCTION GLuint LoadProgram(const char * const vertex_file_path, const char * Fragment_file_path) { GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); //READ THE VERTEX SHADER CODE string VertexShaderCode; ifstream VertexShaderStream(vertex_file_path, std::ios::in); if(VertexShaderStream.is_open()) { string Line = ""; while(getline(VertexShaderStream, Line)) VertexShaderCode += "\n" + Line; VertexShaderStream.close(); } //READ THE FRAGMENT SHADER CODE string FragmentShaderCode; ifstream FragmentShaderStream(Fragment_file_path, std::ios::in); if(FragmentShaderStream.is_open()) { string Line = ""; while(getline(FragmentShaderStream, Line)) FragmentShaderCode += "\n" + Line; FragmentShaderStream.close(); } GLint Result = GL_FALSE; int InfoLogLength; //Compile Vertex Shader printf("Compiling Shader : %s\n", vertex_file_path); char const * VertexSourcePointer = VertexShaderCode.c_str(); glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL); glCompileShader(VertexShaderID); //Check Vertex Shader glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); vector VertexShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); //Compile Fragment Shader printf("Compiling Shader : %s\n", Fragment_file_path); char const * FragmentSourcePointer = FragmentShaderCode.c_str(); glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL); glCompileShader(FragmentShaderID); //Check Fragment Shader glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); vector FragmentShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]); fprintf(stdout, "Linking Program\n"); GLuint ProgramID = glCreateProgram(); //Bind Attribute glBindAttribLocation(ProgramID, 0, "position"); // glBindAttribLocation(ProgramID, 1, "Texcoord0"); //Link The Program glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); glLinkProgram(ProgramID); GLuint texture1; // texture1 = glGetUniformLocation(ProgramID, "myTextureSampler"); matrixuniform = glGetUniformLocation(ProgramID, "myMatrix"); // Translationuniform = glGetUniformLocation(ProgramID, "TranslationMatrix"); //Check The Program glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); vector ProgramErrorMessage( max(InfoLogLength, int(1)) ); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); //Delete Shader glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); glUseProgram(ProgramID); //Return ProgramID return ProgramID; } static const GLfloat test[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, }; static const GLfloat texture[] = { -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f }; // number of available formats int indexPixelFormat = 0; GLuint PixelFormat; int DescribePixelFormat ( HDC hdc, // device context of interest int iPixelFormat, // pixel format selector UINT nBytes, // size of buffer pointed to by ppfd LPPIXELFORMATDESCRIPTOR ppfd // pointer to structure to receive pixel // format data ); const char g_szClassName[] = "myWindowClass"; HINSTANCE hInst; LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInst; wc.hIcon = static_cast(LoadImage(hInstance,MAKEINTRESOURCE(IDI_MYICON),IMAGE_ICON,32, 32, LR_DEFAULTSIZE)); wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU); wc.lpszClassName = g_szClassName; wc.hIconSm = static_cast(LoadImage(hInstance,MAKEINTRESOURCE(IDI_MYICON),IMAGE_ICON,16,16,LR_DEFAULTSIZE)); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( 3, g_szClassName, "program", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } hDC = GetDC(hwnd); //get current windows device context static PIXELFORMATDESCRIPTOR pfd= { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number (?) PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 16, // Select A 16Bit Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored (?) 0, // No Alpha Buffer 0, // Shift Bit Ignored (?) 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored (?) 16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer (?) PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved (?) 0, 0, 0 // Layer Masks Ignored (?) }; //Choose a Pixel Format int ChoosenPixelFormat = ChoosePixelFormat(hDC, &pfd); //Set Pixel Format Close to Screen Format SetPixelFormat(hDC, ChoosenPixelFormat, &pfd); //Make OpenGL Context hRC = wglCreateContext(hDC); //Make Opengl Context Current wglMakeCurrent(hDC, hRC); glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); glGetError(); glewInit(); LoadProgram("graphicaluserinterface.v", "graphicaluserinterface.f"); glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glUniformMatrix4fv(matrixuniform, 1, TRUE, myMatrix); glBufferData(GL_ARRAY_BUFFER, points.size(), &points[0], GL_STATIC_DRAW); glEnable(GL_DEPTH_TEST); glGetError(); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; switch(msg) { case WM_CREATE: //window being created ValidateRect(hwnd, NULL); return 0; break; case WM_ERASEBKGND: return true; break; case WM_CLOSE: //window is closing /* Deselect rendering context and delete it*/ wglMakeCurrent(hDC, NULL); wglDeleteContext(hRC); /* Send quit message to queue*/ PostQuitMessage(0); return 0; break; case WM_COMMAND: switch(LOWORD(wParam)) case ID_BOX: { OPENFILENAME ofn; char szFileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW ofn.hwndOwner = hwnd; ofn.lpstrFilter = ".OBJ (*.obj)\0*.obj\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrDefExt = "obj"; if(GetOpenFileName(&ofn)) { // Do something usefull with the filename stored in szFileName <-- // cout << "File Path = "<< szFileName << endl; //make a fileobject and store list and the index of that list in a c string ifstream file (szFileName); //While FileObject not equal end of file while (!file.eof() ) { char modelbuffer[20000]; file.getline(modelbuffer, 20000); switch(modelbuffer[0]) { cout << " " << endl; case 'v' : Point p; sscanf(modelbuffer, "v %f %f %f", &p.x, &p.y, &p.z); points.push_back(p); cout << " p.x = " << p.x << " p.y = " << p.y << " p.z = " << p.x << endl; break; cout << " " << endl; case 'f': int read_count = sscanf(modelbuffer, "f %d %d %d %d", &face[0], &face[1], &face[2], &face[3]); cout << "face[0] = " << face[0] << " face[1] = " << face[1] << " face[2] = " << face[2] << " face[3] = " << face[3] << "\n"; if(read_count !=4) { cout << "bad/n"; throw std::exception(); } faces.push_back(face[0] - 1); faces.push_back(face[1] - 1); faces.push_back(face[2] - 1); faces.push_back(face[3] - 1); cout << face[0] - 1 << face[1] - 1 << face[2] - 1 << face[3] - 1 << endl; } } } } break; case WM_PAINT: //Draw hDC = BeginPaint(hwnd, &ps); // glEnableClientState(GL_VERTEX_ARRAY); glClearColor(1, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 *sizeof(points),points.data()); glDrawElements( GL_QUADS, faces.size(), GL_UNSIGNED_INT, faces.data()); SwapBuffers(hDC); glGetError(); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; case ID_FILE_EXIT: PostQuitMessage(0); return 0; break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }