/* Our program's entry point */
int mainHW2() {
    SDL_Window *mainwindow; /* Our window handle */
    SDL_GLContext maincontext; /* Our opengl context handle */

    if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */
        sdldie("Unable to initialize SDL"); /* Or die on error */

    /* Request opengl 3.2 context.
     * SDL doesn't have the ability to choose which profile at this time of writing,
     * but it should default to the core profile */
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    /* Turn on double buffering with a 24bit Z buffer.
     * You may need to change this to 16 or 32 for your system */
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);

    /* Create our window centered at 512x512 resolution */
    mainwindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED, 512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (!mainwindow) /* Die if creation failed */
        sdldie("Unable to create window");

    checkSDLError(__LINE__);

    /* Create our opengl context and attach it to our window */
    maincontext = SDL_GL_CreateContext(mainwindow);
    checkSDLError(__LINE__);

    /* This makes our buffer swap syncronized with the monitor's vertical refresh */
    SDL_GL_SetSwapInterval(1);

    /* Clear our buffer with a red background */
    glClearColor(1.0, 0.0, 0.0, 1.0);
    glClear( GL_COLOR_BUFFER_BIT);
    /* Swap our back buffer to the front */
    SDL_GL_SwapWindow(mainwindow);
    /* Wait 2 seconds */
    SDL_Delay(2000);

    /* Same as above, but green */
    glClearColor(0.0, 1.0, 0.0, 1.0);
    glClear( GL_COLOR_BUFFER_BIT);
    SDL_GL_SwapWindow(mainwindow);
    SDL_Delay(2000);

    /* Same as above, but blue */
    glClearColor(0.0, 0.0, 1.0, 1.0);
    glClear( GL_COLOR_BUFFER_BIT);
    SDL_GL_SwapWindow(mainwindow);
    SDL_Delay(2000);

    /* Delete our opengl context, destroy our window, and shutdown SDL */
    SDL_GL_DeleteContext(maincontext);
    SDL_DestroyWindow(mainwindow);
    SDL_Quit();

    return 0;
}
Exemple #2
0
bool render::setupSDL() {
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
		return false;
 
    /* Turn on double buffering with a 24bit Z buffer.
     * You may need to change this to 16 or 32 for your system */
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

	    /* Create our window centered at 512x512 resolution */
    mainwindow = SDL_CreateWindow("Ping Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (!mainwindow) /* Die if creation failed */
        return false;
	checkSDLError(__LINE__);
 
    /* Create our opengl context and attach it to our window */
    maincontext = SDL_GL_CreateContext(mainwindow);
    checkSDLError(__LINE__);
 
 
    /* This makes our buffer swap syncronized with the monitor's vertical refresh */
    SDL_GL_SetSwapInterval(1);

	return true;
}
Exemple #3
0
bool PGE_Window::init(QString WindowTitle)
{
    // Enabling double buffer, setting up colors...
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,            8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,          8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,           8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,          8);
//  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,          16);
//  SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,         32);
//  SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,      0);
//  SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,    0);
//  SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,     0);
//  SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,    0);
//  SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,  1);
//  SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,  2);
//  SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0);
    //SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);

    SDL_GL_SetSwapInterval(0);
    checkSDLError();

    GlRenderer::setViewportSize(Width, Height);

    window = SDL_CreateWindow(WindowTitle.toStdString().c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                              Width, Height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
    checkSDLError();

    SDL_SetWindowMinimumSize(window, Width, Height);
    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");

    if(window == NULL)
    {
        // If failed to create window - exiting
        QMessageBox::critical(NULL, "SDL Error",
            QString("Unable to create window!\n%1")
            .arg( SDL_GetError() ), QMessageBox::Ok);
        return false;
    }

#ifdef Q_OS_MACX
    QImage icon(":/icon/cat_256.png");
#else
    QImage icon(":/icon/cat_16.png");
#endif
    SDL_SetWindowIcon(window, GraphicsHelps::QImage_toSDLSurface(icon));

    glcontext = SDL_GL_CreateContext(window); // Creating of the OpenGL Context
    checkSDLError();

    SDL_ShowWindow(window);

    IsInit=true;
    return true;
}
    void asdf_multiplat_t::init_SDL() {
        //init video subsystem
        if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
            EXPLODE("Could not initialize SDL: %s.", SDL_GetError());
            running = false;
            return;
        }

        //set GL version to 3.3
        //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);

        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); //set 24 bit depth buffer
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //turn on double buffering

        //SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
        //SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);


        //create window
        uint32_t flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;

        if (settings.fullscreen)
            flags |= SDL_WINDOW_FULLSCREEN;
        if (settings.borderless)
            flags |= SDL_WINDOW_BORDERLESS;

        main_window = SDL_CreateWindow(WINDOW_TITLE.c_str(),
                                        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                        settings.resolution_width, settings.resolution_height,
                                        flags);
        checkSDLError(__LINE__);
        ASSERT(main_window != 0, "Unable to create window");

        //create OpenGL context
        gl_context = SDL_GL_CreateContext(main_window);
        checkSDLError(__LINE__);

        ASSERT(gl_context != nullptr, "Unable to create OpenGL Context");

        prev_ticks = SDL_GetTicks();
        //SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);

        SDL_GL_SetSwapInterval(1);
        LOG("SDL Initialized");
    }
Exemple #5
0
// -----------------------------------------------------------------------------
void Window::initSDLWindow() {
  sdlWindow = SDL_CreateWindow(
      "Domino", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0,
      SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN |
          SDL_WINDOW_INPUT_GRABBED);
  checkSDLError("Error creating sdlWindow");
  assert(sdlWindow != nullptr && "Error creating sdlWindow");

  context = SDL_GL_CreateContext(sdlWindow);
  checkSDLError("Error creating GL context");
  assert(context != nullptr && "Error GL creating context");

  SDL_GL_MakeCurrent(sdlWindow, context);
  SDL_SetRelativeMouseMode(SDL_TRUE);
}
bool ElysiumEngine::SDLWindow::create()
{   
	if (!SDL_INIT)
		return false;

	window = SDL_CreateWindow(title.c_str(),0,0,resX,resY,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    checkSDLError();
    
    if(!window)
        return false;
    
    context = SDL_GL_CreateContext(window);
    checkSDLError();
    
    SDL_GL_SetSwapInterval(1);
    checkSDLError();
    
	return window != nullptr;
}
Exemple #7
0
bool Drawer::init()
{

	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		std::cout << "Failed to init SDL\n";
		return false;
	}

	// Create a Window that is usable with OpenGL context
	mWindow = SDL_CreateWindow(
		GAME_NAME,
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		SCREEN_WIDTH,
		SCREEN_HEIGHT,
		SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS
		);

	if (!mWindow) {
		std::cout << "Unable to create window\n";
		checkSDLError(__LINE__);
		return false;
	}

	// Connect the rendering context to the SDL Window
	mGLContext = SDL_GL_CreateContext(mWindow);

	setOpenGLAttributes();

	//Use Vsync
	if (SDL_GL_SetSwapInterval(1) < 0)
	{
		std::cout << "Warning: Unable to set VSync!" << std::endl;
		checkSDLError();
	}

	// Initialize GLEW
	glewExperimental = GL_TRUE;
	glewInit();

	return true;
}
Exemple #8
0
	void SDLFilm::beforeSet() {
		if (!texture)
			return;
		int w, h;
		Uint32 format;
		int pitch;
		SDL_QueryTexture(texture, &format, nullptr, &w, &h);
		if (SDL_LockTexture(texture, &lockRect, (void**)&pixels, &pitch)) {
			checkSDLError(__LINE__);
			return;
		}
		pixelFormat = SDL_AllocFormat(format);
	}
Renderer::Renderer(Tracker &tracker)
	: m_window(NULL),
      m_tracker(tracker)
{
    SDL_Init(SDL_INIT_VIDEO);
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		sdlDie("Unable to initialize SDL");
	}

	m_window = SDL_CreateWindow("OpenGL Test1",
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		640, 480,
		SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
	if (m_window == NULL)
	{
		sdlDie("Unable to initialize SDL");
	}
	checkSDLError(__LINE__);

	m_glContext = SDL_GL_CreateContext(m_window);
	checkSDLError(__LINE__);
}
Exemple #10
0
void OMW::Engine::createWindow(Settings::Manager& settings)
{
    int screen = settings.getInt("screen", "Video");
    int width = settings.getInt("resolution x", "Video");
    int height = settings.getInt("resolution y", "Video");
    bool fullscreen = settings.getBool("fullscreen", "Video");
    bool windowBorder = settings.getBool("window border", "Video");
    bool vsync = settings.getBool("vsync", "Video");
    int antialiasing = settings.getInt("antialiasing", "Video");

    int pos_x = SDL_WINDOWPOS_CENTERED_DISPLAY(screen),
        pos_y = SDL_WINDOWPOS_CENTERED_DISPLAY(screen);

    if(fullscreen)
    {
        pos_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(screen);
        pos_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(screen);
    }

    Uint32 flags = SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE;
    if(fullscreen)
        flags |= SDL_WINDOW_FULLSCREEN;

    if (!windowBorder)
        flags |= SDL_WINDOW_BORDERLESS;

    SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS,
                settings.getBool("minimize on focus loss", "Video") ? "1" : "0");

    checkSDLError(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24));

    if (antialiasing > 0)
    {
        checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1));
        checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, antialiasing));
    }

    while (!mWindow)
    {
        mWindow = SDL_CreateWindow("OpenMW", pos_x, pos_y, width, height, flags);
        if (!mWindow)
        {
            // Try with a lower AA
            if (antialiasing > 0)
            {
                Log(Debug::Warning) << "Warning: " << antialiasing << "x antialiasing not supported, trying " << antialiasing/2;
                antialiasing /= 2;
                Settings::Manager::setInt("antialiasing", "Video", antialiasing);
                checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, antialiasing));
                continue;
            }
            else
            {
                std::stringstream error;
                error << "Failed to create SDL window: " << SDL_GetError();
                throw std::runtime_error(error.str());
            }
        }
    }

    setWindowIcon();

    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    SDL_GetWindowPosition(mWindow, &traits->x, &traits->y);
    SDL_GetWindowSize(mWindow, &traits->width, &traits->height);
    traits->windowName = SDL_GetWindowTitle(mWindow);
    traits->windowDecoration = !(SDL_GetWindowFlags(mWindow)&SDL_WINDOW_BORDERLESS);
    traits->screenNum = SDL_GetWindowDisplayIndex(mWindow);
    // We tried to get rid of the hardcoding but failed: https://github.com/OpenMW/openmw/pull/1771
    // Here goes kcat's quote:
    // It's ultimately a chicken and egg problem, and the reason why the code is like it was in the first place.
    // It needs a context to get the current attributes, but it needs the attributes to set up the context.
    // So it just specifies the same values that were given to SDL in the hopes that it's good enough to what the window eventually gets.
    traits->red = 8;
    traits->green = 8;
    traits->blue = 8;
    traits->alpha = 0; // set to 0 to stop ScreenCaptureHandler reading the alpha channel
    traits->depth = 24;
    traits->stencil = 8;
    traits->vsync = vsync;
    traits->doubleBuffer = true;
    traits->inheritedWindowData = new SDLUtil::GraphicsWindowSDL2::WindowData(mWindow);

    osg::ref_ptr<SDLUtil::GraphicsWindowSDL2> graphicsWindow = new SDLUtil::GraphicsWindowSDL2(traits);
    if(!graphicsWindow->valid()) throw std::runtime_error("Failed to create GraphicsContext");

    osg::ref_ptr<osg::Camera> camera = mViewer->getCamera();
    camera->setGraphicsContext(graphicsWindow);
    camera->setViewport(0, 0, width, height);

    mViewer->realize();

    mViewer->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height);
}
Exemple #11
0
void OMW::Engine::createWindow(Settings::Manager& settings)
{
    int screen = settings.getInt("screen", "Video");
    int width = settings.getInt("resolution x", "Video");
    int height = settings.getInt("resolution y", "Video");
    bool fullscreen = settings.getBool("fullscreen", "Video");
    bool windowBorder = settings.getBool("window border", "Video");
    bool vsync = settings.getBool("vsync", "Video");
    int antialiasing = settings.getInt("antialiasing", "Video");

    int pos_x = SDL_WINDOWPOS_CENTERED_DISPLAY(screen),
        pos_y = SDL_WINDOWPOS_CENTERED_DISPLAY(screen);

    if(fullscreen)
    {
        pos_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(screen);
        pos_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(screen);
    }

    Uint32 flags = SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE;
    if(fullscreen)
        flags |= SDL_WINDOW_FULLSCREEN;

    if (!windowBorder)
        flags |= SDL_WINDOW_BORDERLESS;

    SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS,
                settings.getBool("minimize on focus loss", "Video") ? "1" : "0");

    checkSDLError(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0));
    checkSDLError(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24));

    if (antialiasing > 0)
    {
        checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1));
        checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, antialiasing));
    }

    while (!mWindow)
    {
        mWindow = SDL_CreateWindow("OpenMW", pos_x, pos_y, width, height, flags);
        if (!mWindow)
        {
            // Try with a lower AA
            if (antialiasing > 0)
            {
                std::cout << "Note: " << antialiasing << "x antialiasing not supported, trying " << antialiasing/2 << std::endl;
                antialiasing /= 2;
                Settings::Manager::setInt("antialiasing", "Video", antialiasing);
                checkSDLError(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, antialiasing));
                continue;
            }
            else
            {
                std::stringstream error;
                error << "Failed to create SDL window: " << SDL_GetError() << std::endl;
                throw std::runtime_error(error.str());
            }
        }
    }

    setWindowIcon();

    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    SDL_GetWindowPosition(mWindow, &traits->x, &traits->y);
    SDL_GetWindowSize(mWindow, &traits->width, &traits->height);
    traits->windowName = SDL_GetWindowTitle(mWindow);
    traits->windowDecoration = !(SDL_GetWindowFlags(mWindow)&SDL_WINDOW_BORDERLESS);
    traits->screenNum = SDL_GetWindowDisplayIndex(mWindow);
    // FIXME: Some way to get these settings back from the SDL window?
    traits->red = 8;
    traits->green = 8;
    traits->blue = 8;
    traits->alpha = 0; // set to 0 to stop ScreenCaptureHandler reading the alpha channel
    traits->depth = 24;
    traits->stencil = 8;
    traits->vsync = vsync;
    traits->doubleBuffer = true;
    traits->inheritedWindowData = new SDLUtil::GraphicsWindowSDL2::WindowData(mWindow);

    osg::ref_ptr<SDLUtil::GraphicsWindowSDL2> graphicsWindow = new SDLUtil::GraphicsWindowSDL2(traits);
    if(!graphicsWindow->valid()) throw std::runtime_error("Failed to create GraphicsContext");

    osg::ref_ptr<osg::Camera> camera = mViewer->getCamera();
    camera->setGraphicsContext(graphicsWindow);
    camera->setViewport(0, 0, width, height);

    mViewer->realize();
}
Exemple #12
0
Core::Core(int screenWidth, int screenHeight) {
    this->screenWidth = screenWidth;
    this->screenHeight = screenHeight;

    // Request opengl 3.2 context.
    // SDL doesn't have the ability to choose which profile at this time of writing,
    // but it should default to the core profile
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    // Turn on double buffering with a 24bit Z buffer.
    // You may need to change this to 16 or 32 for your system
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    if (SDL_Init(SDL_INIT_VIDEO) < 0) // Initialize SDL's Video subsystem
        sdldie("Unable to initialize SDL"); // Or die on error

    // Create our window centered at 512x512 resolution
    mainwindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        screenWidth, screenHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (!mainwindow) // Die if creation failed
        sdldie("Unable to create window");

    checkSDLError(__LINE__);

    // Create our opengl context and attach it to our window
    maincontext = SDL_GL_CreateContext(mainwindow);
    checkSDLError(__LINE__);


    // This makes our buffer swap syncronized with the monitor's vertical refresh
    SDL_GL_SetSwapInterval(1);

    //printf("Initializing glew\n");
    GLenum status = glewInit();
    if(status != GLEW_OK) {
        fprintf(stderr, "INFO: glew couldn't be initialized. Exit\nGLEW Error: %s", glewGetErrorString(status));
        close();
    }

    resourceManager = new ResourceManager();
    renderManager = new RenderManager(resourceManager);

    mapEditor = new MapEditor(resourceManager, renderManager, screenWidth, screenHeight);
    characterEditor = new CharacterEditor(resourceManager, renderManager, screenWidth, screenHeight);

    currentContext = mapEditor;

    glPointSize(3.0f);

    glViewport(0, 0, screenWidth, screenHeight);

    // Set the color and depth clear values
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    //glDepthMask(GL_TRUE); // enabled by default
    glDepthFunc(GL_LEQUAL);
}
SDLContext::SDLContext()
{
	SDL_Init(SDL_INIT_EVERYTHING);
	checkSDLError(__LINE__);

	int numDisplays = SDL_GetNumVideoDisplays();
	std::cout << "Number of displays detected: " << numDisplays << std::endl;

	// Ugly hack to see if we have a projector to display the shadow on our setup with 5 displays
	if (numDisplays > 4) {
		std::cout << "Enabling shadow rendering on display 1" << std::endl;
		shadowRendering = true;
	}

	// Main Window
#ifdef STEREO
	// Your stereo display size
	// If set to half of your display each eye will take half of the screen
	SCREEN_WIDTH = 4096;
	SCREEN_HEIGHT = 2400;
#else
	// Comment screen width/height settings to get fullscreen
	SCREEN_WIDTH = 1920;
	SCREEN_HEIGHT = 1080;
#endif
	window = SDL_CreateWindow("Virtual Sculpting", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS
#ifndef STEREO
		| SDL_WINDOW_FULLSCREEN_DESKTOP
#endif
		);
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	int w, h;
	SDL_GetWindowSize(window, &w, &h);
	std::cout << "Main Window created, size: " << w << " x " << h << std::endl;
	checkSDLError(__LINE__);
	
	SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 0);
	context = SDL_GL_CreateContext(window);
	checkSDLError(__LINE__);

	GLenum error = glewInit();
	if (error != GLEW_OK) {
		std::cout << "FATAL: Could not init GLEW" << std::endl;
		std::cout << "Error: " << glewGetErrorString(error) << std::endl;
	}

#ifdef STEREO
	{
		SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
		windowRight = SDL_CreateWindow("Virtual Sculpting 2", SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
		rendererRight = SDL_CreateRenderer(windowRight, -1, SDL_RENDERER_ACCELERATED);
		SDL_GetWindowSize(windowRight, &w, &h);
		std::cout << "Right Window created, size: " << w << " x " << h << std::endl;
		checkSDLError(__LINE__);
		contextRight = SDL_GL_CreateContext(windowRight);
		checkSDLError(__LINE__);

		if (shadowRendering) { // Shadow window
			SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
			windowShadow = SDL_CreateWindow("Virtual Sculpting 3", SCREEN_WIDTH*2, 0, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
			rendererShadow = SDL_CreateRenderer(windowShadow, -1, SDL_RENDERER_ACCELERATED);
			SDL_GetWindowSize(windowShadow, &w, &h);
			std::cout << "Shadow Window created, size: " << w << " x " << h << std::endl;
			checkSDLError(__LINE__);
			contextShadow = SDL_GL_CreateContext(windowShadow);
			checkSDLError(__LINE__);

			// Post-context creation flags
			//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
			//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
			SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
			SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
			SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
			SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
			checkSDLError(__LINE__);

			GLenum error = glewInit();
			if (error != GLEW_OK) {
				std::cout << "FATAL: Could not init GLEW" << std::endl;
				std::cout << "Error: " << glewGetErrorString(error) << std::endl;
			}

			wglShareLists((HGLRC)context, (HGLRC)contextShadow);

		}

		// Post-context creation flags
		//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
		//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
		SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
		SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
		SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
		checkSDLError(__LINE__);

		GLenum error = glewInit();
		if (error != GLEW_OK) {
			std::cout << "FATAL: Could not init GLEW" << std::endl;
			std::cout << "Error: " << glewGetErrorString(error) << std::endl;
		}

		wglShareLists((HGLRC)context, (HGLRC)contextRight);
	}
#endif

	SDL_GL_MakeCurrent(window, context);

	
	// Post-context creation flags
	//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
	//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
	SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
	checkSDLError(__LINE__);

	running = true;
	render = NULL;

	int glVersion[2] = {-1, -1}; 
	glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]);
	glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]);

	std::cout << "Using OpenGL: " << glVersion[0] << "." << glVersion[1] << std::endl; // Output which version of OpenGL we are using

	// Scene rendering

	render = new StereoRender();
	render->Init();
	render->Resize( SCREEN_WIDTH, SCREEN_HEIGHT);

	// FPS count
	FPS = 1.0f;
	lastTick = SDL_GetTicks();
	lastFPSTick = lastTick;

	speed = 1.0f;
	depth = 0.0f; 
	wantedDepth = 0.0f;

	// SCENE VIEW
	showScene= false;
	sceneRotSpeed = 5.0f;
	SCENE_PREVIW_SIZE = SCREEN_HEIGHT/4;

	sceneProj = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);
	
	// PROJECTOR SHADOW 0.92, 1.42
	float projW = 1.39f;
	float projH = 0.925f;
	float projDepth = 5.0f;
	//Relative to the center of the screen, in meters
	float projPosX = 0.1225f;
	float projPosY = 3.0f;
	float projPosZ =  0.855 + projW/2.0f;

	shadowProj = glm::ortho(-projW/2.0f, projW/2.0f, -projH/2.0f, projH/2.0f, 0.1f, projDepth);
	shadowView = glm::lookAt(glm::vec3(projPosX, projPosY,projPosZ), glm::vec3(projPosX, projPosY-projDepth, projPosZ), glm::vec3(1.0f, 0.0f, 0.0f));

	std::cout << "SDLContext initialized" << std::endl;
}
Exemple #14
0
void initwindow(SDL_Window ** window, SDL_GLContext * context){
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0 ){
        std::cerr << SDL_GetError() << std::endl;
        exit(1);
    }

    atexit(SDL_Quit);

    int flags = IMG_INIT_JPG | IMG_INIT_PNG;
    if ( (IMG_Init(flags) & flags) != flags ){
        std::cerr << "IMG_Init failed : \n" << IMG_GetError() << std::endl;
        exit(1);
    }

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);

    *window = SDL_CreateWindow(
            WINDOW_NAME,
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            SCREEN_WIDTH,
            SCREEN_HEIGHT,
            SDL_WINDOW_OPENGL
    );
    if (*window == nullptr) sdlDie("Unable to create window");

    *context = SDL_GL_CreateContext(*window);
    checkSDLError();

    std::cout << "------SDL Test------\n";
    SDL_version sdlversion;
    SDL_GetVersion(&sdlversion);
    std::cout << "SDL Version " << (int)sdlversion.major << "." << (int)sdlversion.minor << "." << (int)sdlversion.patch << std::endl;
    std::cout << "OpenGL Version " << glGetString(GL_VERSION) << std::endl;

    SDL_GL_SetSwapInterval(1);
    /*renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == nullptr) sdlDie("Unable to create renderer");

    SDL_Texture *texture = LoadImage("hello.bmp");
    if (texture == nullptr){
        std::cerr << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    SDL_Delay(1000);

    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);*/
}
Exemple #15
0
SDL_Window *GLFuncs::Initialize(int screenWidth, int screenHeight, GLboolean fullscreen, const char* name)
{
	Uint32 flags;

	if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
		Log::Out << "Unable to initialize SDL: " << SDL_GetError() << endl;
		return NULL;
	}
	atexit(SDL_Quit);

	flags = SDL_WINDOW_OPENGL;

	_screenWidth = screenWidth;
	_screenHeight = screenHeight;

	_windowWidth = _screenWidth;
	_windowHeight = _screenHeight;

	if (fullscreen)
	{
		flags |= SDL_WINDOW_FULLSCREEN;

		SDL_DisplayMode currentDisplay;
		if(SDL_GetCurrentDisplayMode(0, &currentDisplay)) {
			Log::Out << "Couldn't get current display mode: " << SDL_GetError() << endl;
		} else {
			Log::Out << "Current display mode: " << currentDisplay.w << "x" << currentDisplay.h << endl;
			_windowWidth = currentDisplay.w;
			_windowHeight = currentDisplay.h;
		}
	}

	Log::Out << "Window size: " << _windowWidth << "x" << _windowHeight << endl;

	int realW, realH;
	GetRealSize(&realW, &realH);
	Log::Out << "Real size: " << realW << "x" << realH << endl;
	this->StaticProjection = glm::ortho(0.0f, (float)_windowWidth, (float)_windowHeight, 0.0f, -1.0f, 1.0f);

	setGLAttributes();

	_window = SDL_CreateWindow(name,
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
		_windowWidth, _windowHeight,
		flags);

	if (!_window) {
		Log::Out << "Unable to create window." << endl;
		checkSDLError(__LINE__);
	}

	SDL_Surface *icon = Pack::GetInstance()->GetImg("data/UWOLIcon.png");
	SDL_SetWindowIcon(_window, icon);

	_mainContext = SDL_GL_CreateContext(_window);
	SDL_GL_MakeCurrent(_window, _mainContext);

#ifndef __APPLE__
	glewExperimental = GL_TRUE;
	glewInit();
#endif

	char* obtainedVersion = (char*)glGetString(GL_VERSION);
	string glVersion = getVersionFromStr(obtainedVersion);

	Log::Out << "OpenGL: " << obtainedVersion << endl;

	int version;
	stringstream ss(glVersion);
	ss >> version;

#ifdef __APPLE__
	this->_useShaders = true;
#else
	this->_useShaders = (glCreateProgram != NULL); //version >= 20;
#endif
	if (_useShaders) {
		this->_vtxAttribIdx = 0;
		this->_uvAttribIdx = 1;
		this->_colAttribIdx = 2;

		this->_glslVersion = getGLSLVersion();
		Log::Out << "GLSL: " << (this->_glslVersion == "" ? "Not available!" : this->_glslVersion) << endl;
	}
	else {
		Log::Out << "Shaders are not available" << endl;
	}

	this->_useVBO = (glGenVertexArrays!=NULL); //version >= 20;
	if (this->_useVBO) {
		this->_useFramebuffer = this->initFramebuffer();
	}

	glViewport(0, 0, _screenWidth, _screenHeight);

	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	glDisable(GL_DEPTH_TEST);
	glDepthMask(GL_FALSE);

	//	glShadeModel(GL_SMOOTH);

	glDisable(GL_CULL_FACE);

	//#if !ESSENTIAL_GL_PRACTICES_SUPPORT_GL3
	//	//glEnable(GL_ALPHA_TEST);
	//	//glAlphaFunc(GL_GREATER, 0.01f);
	//	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	//#endif

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glPointSize(2.0f);

	if (this->_useVBO) {
		glGenVertexArrays(1, &_vaoVertex);
		glBindVertexArray(_vaoVertex);

		glGenBuffers(1, &_vboVertex);
		glGenBuffers(1, &_vboUV);
		glGenBuffers(1, &_vboColor);
		glGenBuffers(1, &_vboLineVertex);
	}

	this->ResetMVP();

	GLint texture_units;

	glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &texture_units);
	_activeTextures.resize(texture_units, 0);

	return _window;
}
bool initGL() {
	#ifdef DEBUG_DRIVERS /************ DEBUG DRIVERS ********************************/
	// Identify video drivers (this is for debug)
    OUTSTREAM << "\nSDL found the following video drivers:\n";
    int numdrivers = SDL_GetNumVideoDrivers();
    const char* drivername;
    for (int i = 0; i < numdrivers; ++i) {
        drivername = SDL_GetVideoDriver(i);
        if (SDL_VideoInit(drivername) == 0) {
            SDL_VideoQuit();
            OUTSTREAM << "\t\t   Driver " << drivername << " works.\n";
        }
        else {
            OUTSTREAM << "\t<!>\tDriver " << drivername << " DOES NOT WORK!\n";
        }
    }
    // Identify render drivers (this is for debug)
    OUTSTREAM << "SDL found the following render drivers: ";
    numdrivers = SDL_GetNumRenderDrivers();
    for (int i = 0; i < numdrivers; ++i) {
        SDL_RendererInfo info;
        SDL_GetRenderDriverInfo(i, &info);
        OUTSTREAM << info.name << ((i < numdrivers - 1)? ", " : "\n");
    }
    #endif /************************** DEBUG DRIVERS ********************************/

    // Initialize SDL overall.
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        OUTSTREAM << "<!>    SDL did not initialize! SDL Error: " << SDL_GetError() << std::endl;
        return false;
    }

    // SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    // Specify OpenGL version
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, GLVERSION_MAJOR);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, GLVERSION_MINOR);

    // Turn on double buffering with a 24bit Z buffer.
    // You may need to change this to 16 or 32 for your system
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    // Create the SDL window
    pWindow = SDL_CreateWindow("OpenGL Window",	// name of window
                    SDL_WINDOWPOS_CENTERED,		// x position of window
                    SDL_WINDOWPOS_CENTERED,		// y position of window
                    RESOLUTION_X, RESOLUTION_Y,	// x and y width of window
                    SCREENOPTIONS);    			// options (fullscreen, etc)

    // If the window couldn't be created for whatever reason
    if (pWindow == NULL) {
        OUTSTREAM << "<!>    SDL window was not created! SDL Error: " << SDL_GetError() << std::endl;
        return false;
	}
	else {
		OUTSTREAM << "SDL window created.\n";
	}

    //Create context
    context = SDL_GL_CreateContext(pWindow);    //context is the place where openGL can draw
    if(context == NULL) {
        OUTSTREAM << "<!>    OpenGL context was not created! SDL Error: " << SDL_GetError() << std::endl;
        return false;
	}
	else {
		OUTSTREAM << "GL context created.\n";
	}

	checkGlError(__LINE__);

    //Initialize GLEW (openGL Extensions Wrangler)
    glewExperimental = GL_TRUE;     // Sometimes things wont work without this line
    GLenum glewError = glewInit();  // GL enumerator error is thrown here when using openGL versions 3.2+ It's fine.
                                    // see https://www.opengl.org/wiki/OpenGL_Loading_Library
    if(glewError != GLEW_OK) {
        OUTSTREAM << "<!>    Could not initialize GLEW! " << glewGetErrorString(glewError) << std::endl;
        return false;
	}
	else {
		OUTSTREAM << "GLEW initialized.\n";
	}

	checkGlError(__LINE__);


    #ifdef USE_VSYNC /************ USE VSYNC ********************************/
	if (SDL_GL_SetSwapInterval(1) < 0) {
		OUTSTREAM << "<!>    Warning: Unable to set VSync! SDL Error: " << SDL_GetError() << std::endl;
		// Do not return. This is not an essential functionality.
	}
	else {
		OUTSTREAM << "VSync enabled.\n";
	}
	#endif /********************** USE VSYNC ********************************/
    
    // set openGL clear color
    glClearColor(CLEARCOLOR);

    #ifdef USE_DEPTHBUFFER
    // enable the occlusion of objects hidden behind other objects (using the depth buffer)
    glEnable(GL_DEPTH_TEST);				
    // set the test for that occlusion to: "draw if distance of new fragment is less than or equal to old fragment"
    glDepthFunc(GL_LEQUAL);
    #endif

    #ifdef USE_BACKFACE_CULLING
    // enable the culling of back faces (back faces will not be drawn)
    glEnable(GL_CULL_FACE);
    #endif

    #ifdef USE_TRANSPARENCY
    // enable transparency
    glEnable (GL_BLEND);
    // set transparency function (this is standard transparency)
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    #endif

	checkGlError(__LINE__);
    checkSDLError(__LINE__);

    return true;
}