Esempio n. 1
0
int main(int argc, char **argv) {

    init_glew();

    GLFWwindow *window = initialize_window(g_windowWidth, g_windowHeight, g_gameTitle);

    bool running = true;
    /* Loop until the user closes the window */
    while (running) {

        /* Render here */
        setColor(0.5f, 0.69f, 1.0f, 1.0f);

        /*Swap front and back buffers */
        glfwSwapBuffers(window);

        /*poll for and process events*/
        glfwPollEvents();

        running = toggleExit(window, running);

        window = toggleFullScreenWindow(window, GLFW_KEY_F11);
    }

    glfwTerminate();
    return EXIT_SUCCESS;
}
Esempio n. 2
0
GLFWwindow* init_glfw(int window_width, int window_height, const char* title)
{
    GLFWwindow* window;
    glfwSetErrorCallback(_glfw_error_cb);

    if (!glfwInit()) {
        LOGP("glfwInit failed.");
        exit(-1);
    }

    //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    window = glfwCreateWindow(window_width, window_height, title, NULL, NULL);
    if (!window) {
        LOGP("Error, glfwCreateWindow failed.");
        glfwTerminate();
        exit(-1);
    }

    glfwSetMouseButtonCallback(window, glfw_mouse_button_callback);
    glfwSetCursorPosCallback(window, glfw_mouse_pos_callback);

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    init_glew();

    return window;
}
Esempio n. 3
0
OpenVrRenderer::OpenVrRenderer(const trance_pb::System& system)
: _initialised{false}
, _success{false}
, _width{0}
, _height{0}
, _system{nullptr}
, _compositor{nullptr}
{
  vr::HmdError error;
  _system = vr::VR_Init(&error, vr::VRApplication_Scene);
  if (!_system || error != vr::VRInitError_None) {
    std::cerr << "OpenVR initialization failed" << std::endl;
    std::cerr << vr::VR_GetVRInitErrorAsEnglishDescription(error) << std::endl;
    return;
  }
  _initialised = true;

  _window.reset(new sf::RenderWindow);
  _window->create({}, "trance", sf::Style::None);
  _window->setVerticalSyncEnabled(system.enable_vsync());
  _window->setFramerateLimit(0);
  _window->setVisible(false);
  _window->setActive(true);

  init_glew();

  _compositor = vr::VRCompositor();
  if (!_compositor) {
    std::cerr << "OpenVR compositor failed" << std::endl;
    return;
  }

  _system->GetRecommendedRenderTargetSize(&_width, &_height);
  for (int i = 0; i < 2; ++i) {
    GLuint fbo;
    GLuint fb_tex;

    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

    glGenTextures(1, &fb_tex);
    glBindTexture(GL_TEXTURE_2D, fb_tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
                 nullptr);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
      std::cerr << "framebuffer failed" << std::endl;
      return;
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    _fbo.push_back(fbo);
    _fb_tex.push_back(fb_tex);
  }
  _success = true;
}
Esempio n. 4
0
int init_engine(SDL_GLContext *context, SDL_Window *window)
{
	int img_flags = IMG_INIT_PNG;
	if (!(IMG_Init(img_flags) & img_flags)) {
		printf("SD_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
		return -1;
	}

	if (init_gl(context, window))
		return -1;

	if (init_glew())
		return -1;

	// if (init_effects(shader_programs, shader_infos, LENGTH(shader_programs))) {
	// 	printf("Something went wrong with shader program initialization!\n");
	// 	return -1;
	// }

	load_effects(
		effects.all,       LENGTH(effects.all),
		shader_file_paths, LENGTH(shader_file_paths),
		attribute_strings, LENGTH(attribute_strings),
		uniform_strings,   LENGTH(uniform_strings));

	open_simplex_noise(open_simplex_noise_seed, &osnctx);

	SDL_GameControllerEventState(SDL_ENABLE);
	/* Open the first available controller. */
	SDL_GameController *controller = NULL;
	for (int i = 0; i < SDL_NumJoysticks(); ++i) {
		printf("Testing controller %i\n", i);
		if (SDL_IsGameController(i)) {
			controller = SDL_GameControllerOpen(i);
			if (controller) {
				printf("Successfully opened controller %i\n", i);
				break;
			} else {
				printf("Could not open gamecontroller %i: %s\n", i, SDL_GetError());
			}
		} else {
			printf("Controller %i is not a controller?\n", i);
		}
	}

	init_keyboard();
	init_render(); //Located in render.c

	if (signal(SIGUSR1, reload_signal_handler) == SIG_ERR) {
		printf("An error occurred while setting a signal handler.\n");
	}

	return 0;
}
Esempio n. 5
0
gl_context::gl_context(std::string const& name, int width, int height) {
   init_glfw();
   create_glfw_window(name, width, height);
   glfwMakeContextCurrent(_main_window);
   TwInit(TW_OPENGL_CORE, nullptr);
   TwWindowSize(width, height);
   _tweak_bar = TwNewBar("MainBar");
   init_glew();
   glGenVertexArrays(1, &_vertex_array_id);
   glBindVertexArray(_vertex_array_id);
}
Esempio n. 6
0
        void init( float normsize, bool fullscreen )
        {
            static struct on {
                 on() { glfwInit();      }
                ~on() { glfwTerminate(); }
            } _;

            auto rect = get_screen_rect();
            screen_w = rect.first;
            screen_h = rect.second;

            {
                // reinit :P

                normsize = ( normsize <= 0 ? 1 : normsize );

                w = screen_w * normsize;
                h = screen_h * normsize;
            }

#if 0
            //wglSwapIntervalEXT(1);
            glfwOpenWindowHint( GLFW_ACCUM_RED_BITS, 16 );
            glfwOpenWindowHint( GLFW_ACCUM_GREEN_BITS, 16 );
            glfwOpenWindowHint( GLFW_ACCUM_BLUE_BITS, 16 );
            glfwOpenWindowHint( GLFW_ACCUM_ALPHA_BITS, 16 );
            if( glfwOpenWindow( w, h, 0,0,0,0,0,0, fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW ) != GL_TRUE )
            {
                std::cerr << "<gate/gate.hpp> says: Cant create " << w << 'x' << h << " window!" << std::endl;
                exit(1);
            }
#endif

            window = glfwCreateWindow(w, h, title.c_str(), NULL, NULL);
            if (!window)
            {
                glfwTerminate();
                std::cerr << "<gate/gate.hpp> says: Cant create " << w << 'x' << h << " window!" << std::endl;
                exit(1);
            }

            glfwMakeContextCurrent(window);

			glfwSetWindowTitle( window, title.c_str() );

            resize( w, h );

            init_glew();

            glInit();
        }
Esempio n. 7
0
gl_window init_gl_context(int w, int h, const char* name){
  if(!glfwInit()){
    fprintf(stderr, "Error, failed to initialize glfw\n");
    exit(EXIT_FAILURE);
  }
  atexit(glfwTerminate);
  glfwSetErrorCallback(handle_error);
  glfwWindowHint(GLFW_SAMPLES, 4);//4xAA
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1);
  GLFWwindow *win = glfwCreateWindow(w, h, name, NULL, NULL);
  if(!win){
    fprintf(stderr, "Error creating window\n");
    exit(EXIT_FAILURE);
  }
  glfwMakeContextCurrent(win);
  init_glew();
  glfwSwapInterval(1);
  return win;
}
Esempio n. 8
0
void			THIS::init(parent_t * const & p)
{
	printv(DEBUG, "%s\n", __PRETTY_FUNCTION__);

	if(p == NULL) return;

	setParent(p);
	
	auto app = get_fnd_app();

	if(!app->_M_flag.any(neb::fnd::app::util::flag::INIT_GLFW))
	{
		printv(WARNING, "glfw not initializaed\n");
		return;
	}
	
	auto g = std::dynamic_pointer_cast<neb::gfx::app::Base>(app->G::get_object());

	//auto self = std::dynamic_pointer_cast<THIS>(shared_from_this());
	
	if(window_) return;

	// create window
	
	glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	//glfwWindowHint(GLFW_VISIBLE, GL_FALSE);

	window_ = glfwCreateWindow(
			w_,
			h_,
			title_.c_str(),
			NULL,
			first_window_
			);

	if(first_window_ == NULL) {
		first_window_ = window_;
	}

	if(window_ == NULL) {
		glfwTerminate();
		printv(DEBUG, "glfwCreateWindow failed\n");
		exit(EXIT_FAILURE);
	}

	glfwSetWindowPos(window_, x_, y_);

	glfwMakeContextCurrent(window_);

	glfwSetWindowPosCallback(
			window_,
			neb::gfx::app::glfw::static_window_pos_fun);
	glfwSetWindowSizeCallback(
			window_,
			neb::gfx::app::glfw::static_window_size_fun);
	glfwSetWindowCloseCallback(
			window_,
			neb::gfx::app::glfw::static_window_close_fun);
	glfwSetWindowRefreshCallback(
			window_,
			neb::gfx::app::glfw::static_window_refresh_fun);
	glfwSetKeyCallback(
			window_,
			neb::gfx::app::glfw::static_key_fun);
	glfwSetMouseButtonCallback(
			window_,
			neb::gfx::app::glfw::static_mouse_button_fun);
	glfwSetCharCallback(
			window_,
			neb::gfx::app::glfw::staticCharFun);

	// add window to app's window map
	g->reg(shared_from_this());

	//if(all(neb::app::base::option::SHADERS)) create_programs();

	g->init_glew();

	g->create_programs();

	//printf("%s\n",glGetString(GL_SHADING_LANGUAGE_VERSION));

	//Check for necessary extensions
	if(!GL_ARB_depth_texture || !GL_ARB_shadow)
	{
		printv(CRITICAL, "I require ARB_depth_texture and ARB_shadow extensionsn\n");
		exit(0);//return false;
	}

	//Shading states
	glShadeModel(GL_SMOOTH);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	//Depth states

	glClearDepth(1.0f);
	glDepthFunc(GL_LEQUAL);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glEnable(GL_NORMALIZE);

	checkerror("unknown");
}
Esempio n. 9
0
int main() {
    init_glfw();
    GLFWwindow* window = create_window(800, 600, "OpenGL");
    init_glew();

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    GLuint ebo;
    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements,
                 GL_STATIC_DRAW);

    GLuint vertexShader = compile_shader(GL_VERTEX_SHADER, vertexSource);
    GLuint fragmentShader = compile_shader(GL_FRAGMENT_SHADER, fragSource);

    GLuint program = link_program(vertexShader, fragmentShader);
    glUseProgram(program);

    GLint posAttrib = glGetAttribLocation(program, "position");
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE,
                          7 * sizeof(float), 0);
    glEnableVertexAttribArray(posAttrib);

    GLint colorAttrib = glGetAttribLocation(program, "color");
    glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE,
                          7 * sizeof(float), (void*)(2 * sizeof(float)));
    glEnableVertexAttribArray(colorAttrib);

    GLint texAttrib = glGetAttribLocation(program, "texcoord");
    glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE,
                          7 * sizeof(float), (void*)(5 * sizeof(float)));
    glEnableVertexAttribArray(texAttrib);

    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);

    int width, height;
    unsigned char* image = SOIL_load_image("../sample.png", &width, &height,
                                           0, SOIL_LOAD_RGB);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
                 GL_UNSIGNED_BYTE, image);
    SOIL_free_image_data(image);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    while(!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);
        glfwPollEvents();

        if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
            glfwSetWindowShouldClose(window, GL_TRUE);
        }

        // Clear the screen to black
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUniform1f(glGetUniformLocation(program, "time"), glfwGetTime());
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    }

    glDeleteTextures(1, &tex);

    glDeleteProgram(program);

    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);

    glDeleteBuffers(1, &vbo);
    glDeleteBuffers(1, &ebo);

    glDeleteVertexArrays(1, &vao);

    glfwTerminate();
}
Esempio n. 10
0
int
main (int   argc,
      char *argv[])
{
  char *frag_code = NULL;
  glutInit (&argc, argv);

  static struct option long_options[] = {
      { "texture",  required_argument, NULL,  't' },
      { "geometry", required_argument, NULL,  'g' },
      { "help",     no_argument, NULL,        'h' },
      { 0,          0,                 NULL,  0   }
  };

  glutInitWindowSize (800, 600);
  glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
  glutCreateWindow ("Shadertoy");

  init_glew ();

  /* option parsing */
  while (1)
    {
      int c, slot, i;
      char nearest, repeat;

      c = getopt_long (argc, argv, ":t:g:?", long_options, NULL);
      if (c == -1)
        break;

      switch (c)
        {
          case 'g':
            for (i = 0; i < 4; i++)
              {
                char *token = strsep (&optarg, "x+");
                if (!token)
                  break;
                geometry[i] = atof (token);
              }

            fprintf (stderr, "geometry: %.0fx%.0f+%.0f+%.0f\n",
                     geometry[0], geometry[1], geometry[2], geometry[3]);
            break;

          case 't':
            if (optarg[0] <  '0' || optarg[0] >  '3' || strchr (optarg, ':') == NULL)
              {
                fprintf (stderr, "Argument for texture file needs a slot from 0 to 3\n");
                exit (1);
              }

            slot = optarg[0] - '0';

            repeat = 1;
            nearest = 0;

            for (c = 1; optarg[c] != ':' && optarg[c] != '\0'; c++)
              {
                switch (optarg[c])
                  {
                    case 'r':
                      repeat = 1;
                      break;
                    case 'o':
                      repeat = 0;
                      break;
                    case 'i':
                      nearest = 0;
                      break;
                    case 'n':
                      nearest = 1;
                      break;
                    default:
                      break;
                  }
              }

            if (optarg[c] != ':' ||
                !load_texture (optarg + c + 1, GL_TEXTURE_2D, &tex[slot], nearest, repeat))
              {
                fprintf (stderr, "Failed to load texture. Aborting.\n");
                exit (1);
              }
            break;

          case 'h':
          case ':':
          default:
            fprintf (stderr, "Usage:\n  %s [options] <shaderfile>\n", argv[0]);
            fprintf (stderr, "Options:    --help\n");
            fprintf (stderr, "            --texture [0-3]:<textureimage>\n");
            exit (c == ':' ? 1 : 0);
            break;
        }
    }

  if (optind != argc - 1)
    {
      fprintf (stderr, "No shaderfile specified. Aborting.\n");
      exit (-1);
    }

  frag_code = load_file (argv[optind]);
  if (!frag_code)
    {
      fprintf (stderr, "Failed to load Shaderfile. Aborting.\n");
      exit (-1);
    }

  prog = link_program (frag_code);
  if (prog < 0)
    {
      fprintf (stderr, "Failed to link shader program. Aborting\n");
      exit (-1);
    }

  ipc_socket_open (IPC_PORT);

  glutDisplayFunc  (display);
  glutMouseFunc    (mouse_press_handler);
  glutMotionFunc   (mouse_move_handler);
  glutKeyboardFunc (keyboard_handler);

  redisplay (1000/60);

  glutMainLoop ();

  return 0;
}