int main(int argc, char **argv) { QGuiApplication app(argc, argv); // create an OpenGL format specifier QSurfaceFormat format; // set the number of samples for multisampling // will need to enable glEnable(GL_MULTISAMPLE); once we have a context format.setSamples(4); format.setMajorVersion(3); format.setMinorVersion(2); // now we are going to set to Compat Profile OpenGL so we can use and old Immediate mode GL format.setProfile(QSurfaceFormat::CompatibilityProfile); // now set the depth buffer to 24 bits format.setDepthBufferSize(24); QSurfaceFormat::setDefaultFormat(format); // now we are going to create our scene window OpenGLWindow window; // we can now query the version to see if it worked std::cout<<"Profile is "<<format.majorVersion()<<" "<<format.minorVersion()<<"\n"; // set the window size window.resize(1024, 720); // and finally show window.show(); return app.exec(); }
int main(int argc, char **argv) { QGuiApplication app(argc, argv); // create an OpenGL format specifier QSurfaceFormat format; // set the number of samples for multisampling // will need to enable glEnable(GL_MULTISAMPLE); once we have a context format.setSamples(4); #if defined( DARWIN) // at present mac osx Mountain Lion only supports GL3.2 // the new mavericks will have GL 4.x so can change format.setMajorVersion(4); format.setMinorVersion(1); #else // with luck we have the latest GL version so set to this format.setMajorVersion(4); format.setMinorVersion(3); #endif // now we are going to set to CoreProfile OpenGL so we can't use and old Immediate mode GL format.setProfile(QSurfaceFormat::CoreProfile); // now set the depth buffer to 24 bits format.setDepthBufferSize(24); QSurfaceFormat::setDefaultFormat(format); // now we are going to create our scene window OpenGLWindow window; // set the window size window.resize(1024, 720); // and finally show window.show(); return app.exec(); }
LRESULT CALLBACK OpenGLWindow::StaticWindowProc( HWND p_WindowHandle, UINT p_Message, WPARAM p_WordParam, LPARAM p_LongParam ) { LONG_PTR UserData = GetWindowLongPtr( p_WindowHandle, GWLP_USERDATA ); OpenGLWindow *pThis = reinterpret_cast< OpenGLWindow * >( UserData ); assert( pThis ); assert( p_WindowHandle == pThis->m_WindowHandle ); return pThis->WindowProc( p_Message, p_WordParam, p_LongParam ); }
/*Function that runs the program that takes inn 2 arguments. argc(argument count) and argv (argument vector) are how command line arguments are passed to main(). argc is an integer parameter and it is the number of arguments passed to the program. Program name is always the first argument, so there will be at least one argument to a program and minimum value of argc will be 1. But if a program has itself 2 arguments the value of argc will be 3. Parameter argv points to a string array and is called the "argument vector". It is a one dimensional string array of function arguments.*/ int main(int argc, char* argv[]){ /*Useing QApplication function and calls it app that thakes inn 2 arguments argc, argv*/ QApplication app(argc, argv); /*Useing OpenGLWindow Class and call it window*/ OpenGLWindow window; /*Useing .show to show openGLWindow*/ window.show(); /*returns app and .exec funtion execute a new executable*/ return app.exec(); }
/// callback routine for window specific window events LONG WINAPI OpenGLWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG lRet = 1; OpenGLWindow* glWnd = (OpenGLWindow*) GetWindowLongPtr(hWnd, GWL_USERDATA); if(glWnd) lRet = glWnd->WndProc(hWnd, uMsg, wParam, lParam); else { if(uMsg == WM_CREATE) { CREATESTRUCT* cs = (LPCREATESTRUCT) lParam;; glWnd = (OpenGLWindow*) cs->lpCreateParams; if(glWnd) lRet = glWnd->WndProc(hWnd, uMsg, wParam, lParam); } else lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); } return lRet; }
LRESULT CALLBACK OpenGLWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { OpenGLWindow* This = reinterpret_cast<OpenGLWindow*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA)); if (!This) { This = static_cast<OpenGLWindow*>(::TlsGetValue(s_tlsIndex)); ::TlsSetValue(s_tlsIndex, 0); if(!::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(This))) { } This->hWnd = hWnd; } const LRESULT ret = This->MainProc(hWnd, uMsg, wParam, lParam); if (uMsg == WM_NCDESTROY) { This->hWnd = 0; } return ret; }
LRESULT OpenGLWindow::eventHandler(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam) { if (Message == WM_NCCREATE) { //Get the creation parameters. CREATESTRUCT* pCreateStruct = reinterpret_cast<CREATESTRUCT*>(lParam); //Set as the "user data" parameter of the window SetWindowLongPtr(Handle, GWLP_USERDATA, reinterpret_cast<long>(pCreateStruct->lpCreateParams)); } //Get the CMainWindow instance corresponding to the window handle OpenGLWindow* openGLWindow = reinterpret_cast<OpenGLWindow*>(GetWindowLongPtr(Handle, GWLP_USERDATA)); if (openGLWindow != NULL) { openGLWindow->processEvent(Message, wParam, lParam); } return DefWindowProc(Handle, Message, wParam, lParam); }
TEST(TestOpenGLWindow, test_construction) { int width = 300; int height = 300; QString windowName = "OpenGL Window"; int x = 200; int y = 300; OpenGLWindow *window = new OpenGLWindow( width, height, windowName, x, y ); EXPECT_EQ(window->width(), width); EXPECT_EQ(window->height(), height); EXPECT_EQ(window->windowTitle(), windowName); EXPECT_TRUE(window->isVisible()); delete window; }
int SDL_main(int argc, char** argv) #endif { if(SDL_Init(SDL_INIT_VIDEO) != 0) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Error", "Unable to initialize SDL", 0); return 1; } OpenGLWindow window; window.initGL(); bool running = true; while(running) { // Check for a quit event before passing to the GLWindow SDL_Event e; while(SDL_PollEvent(&e)) { if(e.type == SDL_QUIT) { running = false; } else if(!window.handleEvent(e)) { running = false; } } //render window.render(); // We sleep for 10ms here so as to prevent excessive CPU usage SDL_Delay(10); } window.cleanup(); SDL_Quit(); return 0; }
LRESULT CALLBACK OpenGLWindow::InitialWindowProc( HWND p_WindowHandle, UINT p_Message, WPARAM p_WordParam, LPARAM p_LongParam ) { if( p_Message == WM_NCCREATE ) { LPCREATESTRUCT CreateStruct = reinterpret_cast< LPCREATESTRUCT >( p_LongParam ); void *pCreateParam = CreateStruct->lpCreateParams; OpenGLWindow *pThis = reinterpret_cast< OpenGLWindow * >( pCreateParam ); pThis->m_WindowHandle = p_WindowHandle; SetWindowLongPtr( pThis->m_WindowHandle, GWLP_USERDATA, reinterpret_cast< LONG_PTR >( pThis ) ); SetWindowLongPtr( pThis->m_WindowHandle, GWLP_WNDPROC, reinterpret_cast< LONG_PTR >( &OpenGLWindow::StaticWindowProc ) ); return pThis->WindowProc( p_Message, p_WordParam, p_LongParam ); } return DefWindowProc( p_WindowHandle, p_Message, p_WordParam, p_LongParam ); }
int main() { std::srand(std::time(0)); //Seed Random OpenGLWindow window; glfwSwapInterval(1); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //SGLShaderProgram shader("../Shaders/VertexShader.glsl", "../shaders/FragmentShader.glsl"); //shader.useProgram(); //Set Orthographic Projection uniform variable float degreeToRad = 3.1415/180.0f; //Create Camera and Projection Matrix SGLCamera camera; camera.setBoth(vec3(400, 300, 190), vec2(0, 180)); glm::mat4 projectionMatrix = glm::perspective(45.0f, (float)window.width/(float)window.height, 0.01f, 500.0f); SGLRenderer basicRenderer("../Shaders/VertexShader.glsl", "../shaders/FragmentShader.glsl"); basicRenderer.shaderProgram.setUniformMat4f("projectionMatrix", projectionMatrix); basicRenderer.shaderProgram.setUniformMat4f("viewMatrix", camera.viewMatrix); SGLCubeRenderable testCube(glm::vec3(400, 300, 50), glm::vec3(10, 10, 10), glm::vec3(1, 0, 0)); testCube.setOrigin((float)5, (float)5, (float)5); testCube.setColor(1, vec3(1, 1, 0)); testCube.setColor(2, vec3(1, 1, 1)); testCube.setColor(3, vec3(0, 1, 1)); testCube.setColor(4, vec3(0, 1, 1)); testCube.setColor(5, vec3(1, 1, 1)); testCube.setColor(6, vec3(1, 1, 0)); testCube.setColor(7, vec3(1, 0.5, 0)); testCube.resetVertexArray(); SGLBoxRenderable2D testBox(glm::vec3(400, 300, 1.0), glm::vec2(50,50), glm::vec3(1, 0, 0)); SGLBoxRenderable2D testBox2(glm::vec3(100, 300, 0.5), glm::vec2(50,50), glm::vec3(0, 0, 1)); SGLBoxRenderable2D testBox3(glm::vec3(300, 200, 1.0), glm::vec2(50,50), glm::vec3(1, 1, 0)); SGLBoxRenderable2D bigBox (glm::vec3(400, 300, 0), glm::vec2(600, 600), glm::vec3(1, 1, 1)); bigBox.vertexColors[0] = glm::vec3(1, 0, 0); bigBox.vertexColors[2] = glm::vec3(0, 1, 0); bigBox.vertexColors[3] = glm::vec3(0, 0, 1); bigBox.setOrigin(glm::vec3(300, 300, 0)); bigBox.resetVertexArray(); //load updated data into vertex array bigBox.setTexture("../images/Spaceman.png"); testBox.setTexture("../images/Spaceman.png"); testBox2.setTexture("../IMAGES/Spaceman.png"); double lastFrameTime = 0; double deltaFrameTime = 0; double previousMouseX = 0; double previousMouseY = 0; while(window.isOpen()) { double mouseX, mouseY; window.getMousePosition(mouseX, mouseY); //Clean the Screen and process events glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); deltaFrameTime = glfwGetTime() - lastFrameTime; lastFrameTime = glfwGetTime(); //Handle Window Resizing window.updateSize(); glViewport(0, 0, window.width, window.height); //rotate camera camera.rotate(range(mouseY - previousMouseY, -10.0, 10.0), -1 * range(mouseX - previousMouseX, -10.0, 10.0)); camera.setRotation(vec2(range(camera.getRotation().x, -90.0f, 90.0f), camera.getRotation().y)); //Don't let the camera go upside down basicRenderer.shaderProgram.setUniformMat4f("viewMatrix", camera.viewMatrix); //rotate bigBox.rotate(30 * deltaFrameTime); testCube.rotate(-30 * deltaFrameTime, glm::vec3(1, 1, 1)); //Draw stuff basicRenderer.draw(testCube); basicRenderer.draw(testBox2); basicRenderer.draw(testBox3); basicRenderer.draw(bigBox); basicRenderer.draw(testCube); //Display what we have drawn window.update(); //Check for OpenGL Error Codes GLenum error; error = glGetError(); if(error != GL_NO_ERROR) cout << "GL Error: " << error << endl; //Report FrameRate framesThisSecond++; if(glfwGetTime() - lastReportOfFramerate > 1) { cout << framesThisSecond / (glfwGetTime() - lastReportOfFramerate) << " Frames Per Second" << endl; framesThisSecond = 0; lastReportOfFramerate = (float)glfwGetTime(); } previousMouseX = mouseX; previousMouseY = mouseY; } return 0; }
int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; bool done = false; Draw* draw = new Draw(); //"Draw" class takes all 3D objects and draws them on screen Object* Box = new Object(GL_LINES); //The box object Object* Chessboard = new Object(GL_QUADS); //The chessboard object createBox(Box); //Create the box float a = 0.2f; //The widht/height of each square of the chessboard object createChessboard(Chessboard, a); //Create the chessboard object //Push both 3D objects into the draw class, so that the get drawn draw->pushObject(Box); draw->pushObject(Chessboard); //The size of the chessboard which should be searched during the camera calibration plus //the number of images which should be taken int board_h = 6, board_w = 8, nImages = 2; bool found = false; //Will be used to determine wether a chessboard has been //found in the camera frame or not cv::VideoCapture capture(0); //create an object with access to the camera myCV::CameraCalibration camCalib(capture, 8, 6, nImages); //create an object which handels the camera calibration cv::Mat image(480,640,CV_32FC3), grayImg; cv::Mat R(3,1,CV_64F), T(3,1,CV_64F); //Declaration of rotation and translation Vector std::vector<cv::Point2f> corners; //Vector which will contain the corner coordinates for each camera frame std::vector<cv::Point3f> _3DPoints; //Vector that contains the 3D coordinates for each chessboard corner //Creating the OpenGL window OpenGLWindow* window = new OpenGLWindow(640, 480, IPL_DEPTH_8U, charToWideChar("3D Window"), draw); //Initialising the 3D-Points for the chessboard float rot = 0.0f; cv::Point3f _3DPoint; float y = (((board_h-1.0f)/2.0f)*a)+(a/2.0f); float x = 0.0f; for(int h = 0; h < board_h; h++, y+=a){ x = (((board_w-2.0f)/2.0f)*(-a))-(a/2.0f); for(int w = 0; w < board_w; w++, x+=a){ _3DPoint.x = x; _3DPoint.y = y; _3DPoint.z = 0.0f; _3DPoints.push_back(_3DPoint); } } //Camera calibration try{ camCalib.Initialisation(); } catch(cv::Exception &e){ MessageBoxA(NULL, e.what(), "Cameracalibration", MB_ICONQUESTION | MB_YESNO); } capture >> image; corners.clear(); cv::Mat M = camCalib.getIntrinsicsMatrix(); cv::Mat D = camCalib.getDistortionCoeffs(); if (!window->CreateGLWindow()) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if ((!window->DrawGLScene()) || window->getKeyStatus(VK_ESCAPE)) // Active? Was There A Quit Received? { done=true; // ESC or DrawGLScene Signalled A Quit } else // Not Time To Quit, Update Screen { //Search for a chessboard in the camera frame and save his corners found = cv::findChessboardCorners(image, cv::Size(8,6), corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS); if(found){ //A chessboard has been found cv::cvtColor(image, grayImg, CV_BGR2GRAY); cv::cornerSubPix(grayImg, corners, cv::Size(11, 11), cv::Size(-1,-1), cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 )); cv::drawChessboardCorners(image, cv::Size(board_w, board_h), cv::Mat(corners), found); //Draw the chessboard corners on the camera frame cv::solvePnP(cv::Mat(_3DPoints), cv::Mat(corners), M, D, R, T); //Calculate the Rotation and Translation vector double theta = 0; //Calculate the angle of the rotation theta = sqrt((R.at<double>(0,0)*R.at<double>(0,0))+ (R.at<double>(1,0)*R.at<double>(1,0))+ (R.at<double>(2,0)*R.at<double>(2,0))); //Translate the 3D chessboard Chessboard->setXTrans((-T.at<double>(0,0))); Chessboard->setYTrans((-T.at<double>(1,0)-1.0)); Chessboard->setZTrans((-T.at<double>(2,0)+5.0)); //Rotate the 3D chessboard about the given vector for "theta" degrees. Chessboard->setRot((theta*180.0f)/3.14159f, R.at<double>(0,0), R.at<double>(1,0), R.at<double>(2,0)); } cv::imshow("Calibrate", image); corners.clear(); capture >> image; SwapBuffers(window->getHDC()); //Swap Buffers if(cv::waitKey(1) == 27){ return 0; } } } } window->KillGLWindow(); return (msg.wParam); // Exit The Program }
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { if (!lpCmdLine || strlen(lpCmdLine)<=0) { #ifdef UNICODE MessageBox(NULL, L"You have to specify a GameEngine file as argument", L"Info", MB_OK | MB_ICONINFORMATION); #else MessageBox(NULL, "You have to specify a GameEngine file as argument", "Info", MB_OK | MB_ICONINFORMATION); #endif return -1; } // Creates an OpenGL Context and initializes the game engine OpenGLWindow* glWindow = new OpenGLWindow("Alfred", width, height); if ( !glWindow->createWindow()) { #ifdef UNICODE MessageBox(NULL, L"Error creating OpenGLWindow", L"Error", MB_OK | MB_ICONWARNING); #else MessageBox(NULL, "Error creating OpenGLWindow", "Error", MB_OK | MB_ICONWARNING); #endif delete glWindow; return -1; } AlfredXpadApp* app = new AlfredXpadApp(); if (!app->init(lpCmdLine)) { #ifdef UNICODE MessageBox(NULL, L"Error initializing GameEngine", L"Info", MB_OK | MB_ICONINFORMATION); #else MessageBox(NULL, "Error initializing GameEngine", "Info", MB_OK | MB_ICONINFORMATION); #endif delete app; delete glWindow; return -1; } // Register Keyboard callback glWindow->setRenderCB(AlfredXpadApp::renderCb, app); glWindow->setKeyboardCB(AlfredXpadApp::keyboardCb); glWindow->setMouseCB(AlfredXpadApp::mouseCb); glWindow->setResizeCB(AlfredXpadApp::resizeCb); AlfredXpadApp::resizeCb(width, height, app); // Run Event Loop while( app->isRunning() && glWindow->isRunning() ) { // Update OpenGL Window glWindow->paintGL(); MSG msg; while( PeekMessage( &msg, 0x0, 0, 0, PM_NOREMOVE ) == TRUE) { if (GetMessage(&msg, NULL, 0,0)) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { break; } } } // Release ACOSAS <-- ???? delete app; // Delete OpenGL Window delete glWindow; return 0; }