bool ShaderRenderer::init()
{
	bool ok = true;

	glGenBuffers(1, &_position_buffer_object);

	glBindBuffer(GL_ARRAY_BUFFER, _position_buffer_object);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glGenVertexArrays(1, &_vao);

	_shader_vertex.openFile("shaders/vertex.glsl");
	_shader_fragment.openFile("shaders/fragment.glsl");
	
	_program.addShader(&_shader_vertex);
	_program.addShader(&_shader_fragment);
	if (!_program.link())
		ok = false;

	// Setup perspective
	if (ok)
	{
		GLint uniform_perspective = glGetUniformLocation(_program.getId(), "mat_perspective");
		glUseProgram(_program.getId());
		glUniformMatrix4fv(uniform_perspective, 1, GL_FALSE, perspectiveMatrix(1.0f, 0.1f, 100.0f).data());
		glUseProgram(0);
	}

	return ok;
}
void LightNode::lightPass(std::stack<QMatrix4x4> &MVStack, SceneGraph *root) {
    QOpenGLShaderProgram *shader = Shaders::storeDepthProgram;
    Shaders::bind(shader);

    glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO());

    GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT1};
    glDrawBuffers(1, DrawBuffers);

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0,0, shadowWidth(), shadowHeight());

    MVStack.push(MVStack.top());
    MVStack.top() *= lightView();

    root->drawGeometry(MVStack
                       , lightView()
                       , perspectiveMatrix()
                       , shader);

    MVStack.pop();

    //Recreate the mipmaps
    glBindTexture(GL_TEXTURE_2D, shadowMoments());
    glGenerateMipmap(GL_TEXTURE_2D);

    //Release and relax, brah
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    Shaders::release(shader);
}
Exemple #3
0
static void mainLoop(GLFWwindow *window, scene_t *scene)
{
	glfwPollEvents();
	if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
		bake(scene);

	int w, h;
	glfwGetFramebufferSize(window, &w, &h);
	glViewport(0, 0, w, h);

	// camera for glfw window
	float view[16], projection[16];
	fpsCameraViewMatrix(window, view);
	perspectiveMatrix(projection, 45.0f, (float)w / (float)h, 0.01f, 100.0f);

	// draw to screen with a blueish sky
	glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	drawScene(scene, view, projection);

	glfwSwapBuffers(window);
}
Exemple #4
0
float4x4 Camera::getProjectionMatrix() {
    return perspectiveMatrix(m_fFov, m_fRatio, m_fNearPlane, m_fFarPlane);
}
Exemple #5
0
int main( int argc, char *argv[] )
{
	unsigned int frames = 0;
	int   res;
	Mat4	projection_matrix;

	Mat4  viewMat;
	Mat4  rotMat;
	Mat4  modelMat;
	float aspect;
	float	dw, dh;
	
	int	debug_flag = 0;
	
	bcm_host_init();
	res = WinCreate( &g_sc );
	if( !res ) 
	{
		printf( "Create window error!\n" );
		return 0;
	}
	res = SurfaceCreate(&g_sc);
	if( !res ) 
	{
		printf( "Create surface error!\n" );
		return 0;
	}
	
	makeUnit(&viewMat);

	aspect = (float)g_sc.width / (float)g_sc.height;
	
	//makeProjectionMatrix(&g_sp.VpMatrix, 1, 65536, 53, aspect);
	//setPosition(&viewMat, 0, -4, -24 );

	dw = 0.5f * aspect;
	dh = 0.5f;

	perspectiveMatrix( -dw, +dw, -dh, +dh, 1, 160, &g_sp.VpMatrix );
	setPosition(&viewMat, 0, -12, -32 );

	//print_mat4( &g_sp.VpMatrix );

	mulMatrix(&g_sp.VpMatrix, &g_sp.VpMatrix, &viewMat);
	
	makeUnit(&modelMat);
	
	mulMatrix( &projection_matrix, &modelMat, &g_sp.VpMatrix );

	makeUnit(&rotMat);
	setRotationY(&rotMat, 0.5); /* 30 degree/sec */

	pmd_file = NULL;
	vmd_file = NULL;
	if( argc < 2 )
	{
		printf( "File Error!\n" );
		printf( "Argment1 : Pmd or Pmx file.\n" );
		printf( "Argment2 : Vmd file.\n" );
		return 0;
	}
	if( argc > 1 )
		pmd_file = argv[ 1 ];
	
	if( argc > 2 )
		vmd_file = argv[ 2 ];

	init();

	if( argc > 3 && argv[ 3 ] )
	{
		if( strncmp( argv[ 3 ], "-D", 2 ) == 0 )
			debug_flag = 1;
	}
	
	//print_mat4( &projection_matrix );

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

	Mat4 delta_mat;
	makeUnit( &delta_mat );
	
	if( argc > 4 )
	{
		int	i, j;
		char	cmd_string[ 255 ] = { 0 };
		for( i = 3; i < argc; i ++ )
		{
			int	length = 0;
			strcpy( cmd_string, argv[ i ] );
			puts( cmd_string );
			length = strlen( cmd_string );
			cmd_string[ length ] = ' ';
			cmd_string[ length + 1 ] = '&';
			cmd_string[ length + 2 ] = '\0';
			
			system( cmd_string );
		}
	}
	
	fps = new Fps();
	fps->set_fps( _fps_ );
	
	/* 1200frame / 60fps = 20sec */
	//while( frames < 1200 ) 
	while( !p->get_vmd( 0 )->is_end() || ( !vmd_file && frames < 160 ) )
	//while( 1 )
	{
		Mat4 pl_matrix;
		glViewport(0, 0, g_sc.width, g_sc.height);
		
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		/* X Rotation */
		mulMatrix( &pl_matrix, &projection_matrix, &delta_mat );
		//glUniformMatrix4fv(g_sp.uModelMatrix, 1, GL_FALSE, modelMat.m);

		p->set_projection_matrix( pl_matrix.m );
		draw();
		/*
		char c = get_keyboard();
		
		makeUnit( &delta_mat );
		switch( c )
		{
		case 'w':	setPosition( &delta_mat, 0, 0, +4 );	break;
		case 's':	setPosition( &delta_mat, 0, 0, -4 );	break;
		case 'a':	setRotationY( &delta_mat, +0.5 );	break;
		case 'd':	setRotationY( &delta_mat, -0.5 );	break;
		}
		*/

		eglSwapBuffers(g_sc.display, g_sc.surface);
		frames ++;		

		//glutTimerFunc( fps->get_wait_time() * 1000.0f, timer, 0 );
		usleep( 1600 );
		if( debug_flag )
			fps->draw();
	}
  
	printf( "Ending process!\n" );
  
	end();
  
	return 0;
}
Exemple #6
0
int main(int argc, char* argv[])
{
	glfwSetErrorCallback(error_callback);
	if (!glfwInit()) return 1;
	glfwWindowHint(GLFW_RED_BITS, 8);
	glfwWindowHint(GLFW_GREEN_BITS, 8);
	glfwWindowHint(GLFW_BLUE_BITS, 8);
	glfwWindowHint(GLFW_ALPHA_BITS, 8);
	glfwWindowHint(GLFW_DEPTH_BITS, 32);
	glfwWindowHint(GLFW_STENCIL_BITS, GLFW_DONT_CARE);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
	glfwWindowHint(GLFW_SAMPLES, 4);
	GLFWwindow *window = glfwCreateWindow(1024, 768, "Lightmapping Example", NULL, NULL);
	if (!window) return 1;
	glfwMakeContextCurrent(window);
	gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
	glfwSwapInterval(1);

	scene_t scene = {0};
	if (!initScene(&scene))
	{
		fprintf(stderr, "Could not initialize scene.\n");
		return 1;
	}

	printf("Ambient Occlusion Baking Example.\n");
	printf("Use your mouse and the W, A, S, D, E, Q keys to navigate.\n");
	printf("Press SPACE to start baking one light bounce!\n");
	printf("This will take a few seconds and bake a lightmap illuminated by:\n");
	printf("1. The mesh itself (initially black)\n");
	printf("2. A white sky (1.0f, 1.0f, 1.0f)\n");

	while (!glfwWindowShouldClose(window))
	{
		glfwPollEvents();
		if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
			bake(&scene);

		int w, h;
		glfwGetFramebufferSize(window, &w, &h);
		glViewport(0, 0, w, h);

		// camera for glfw window
		float view[16], projection[16];
		fpsCameraViewMatrix(window, view);
		perspectiveMatrix(projection, 45.0f, (float)w / (float)h, 0.01f, 100.0f);
		
		// draw to screen with a blueish sky
		glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		drawScene(&scene, view, projection);

		glfwSwapBuffers(window);
	}

	destroyScene(&scene);
	glfwDestroyWindow(window);
	glfwTerminate();
	return 0;
}
Exemple #7
0
void Viewer::paintGL() {
  draw_init();

  auto viewM = viewPoint.getViewMatrix();
  auto perspectiveM = perspectiveMatrix();

  // Norms of clipping planes
  Vector3D nearNorm = {0, 0, 1};
  Vector3D farNorm = {0, 0, -1};
  Vector3D leftNorm = {1, 0, 0};
  Vector3D rightNorm = {-1, 0, 0};
  Vector3D topNorm = {0, -1, 0};
  Vector3D bottomNorm = {0, 1, 0};

  // Points on clipping planes
  Point3D nearPoint = {0, 0, near};
  Point3D farPoint = {0, 0, far};
  Point3D leftPoint = {-1, 0, 0};
  Point3D rightPoint = {1, 0, 0};
  Point3D topPoint = {0, 1, 0};
  Point3D bottomPoint = {0, -1, 0};

  // Convert vp1 and vp2 into points in range [-1, 1]
  Point2D vp1, vp2;
  getAdjustedViewportBounds(&vp1, &vp2);

  int idx = 0;
  for (const auto& model : {boxModel, boxGnomon, worldGnomon}) {
    idx += 1;
    auto v = model.getLines();

    int jdx = -1;
    for (auto& line : v) {
      jdx += 1;
      // Last minute colour additions lol
      if (idx == 1) {
        if (jdx < 4) {
          set_colour(QColor(0.0, 1.0, 1.0));
        }
        else {
          set_colour(QColor(1.0, 1.0, 1.0));
        }
      }
      else if (idx == 2) {
        // Draw box gnomon as some kind of red
        set_colour(QColor(1.0, 1.0, 0.0));
      }
      else {
        // Draw world gnomon in fancy colours
        set_colour(QColor(jdx == 0 ? 1.0 : 0.0,
                          jdx == 1 ? 1.0 : 0.0,
                          jdx == 2 ? 1.0 : 0.0));
      }
      // Now we have the view coordinates
      line.start = viewM * line.start;
      line.end = viewM * line.end;

      // Now we want to clip the line to the near and far planes first, to
      // avoid ambiguity. (Actually, we only need to do the near one, but
      // far is the same so why not do that too) :)
      if (!clipLine(&line, nearNorm, nearPoint) ||
          !clipLine(&line, farNorm, farPoint)) {
        continue;
      }

      // We need these because our vectors are 3D not 4D...
      auto startZ = line.start[2];
      auto endZ = line.end[2];

      // Now multiply to project it onto the near plane
      line.start = perspectiveM * line.start;
      line.end = perspectiveM * line.end;

      // We no longer care about depth
      // TODO: Convert to 2D
      for (int i = 0; i < 3; ++i) {
        line.start[i] /= startZ;
        line.end[i] /= endZ;
      }

      // We can clip these afterwards (in 2D, since we don't care about depth)
      // TODO: 2D
      if (!clipLine(&line, rightNorm, rightPoint) ||
          !clipLine(&line, leftNorm, leftPoint) ||
          !clipLine(&line, topNorm, topPoint) ||
          !clipLine(&line, bottomNorm, bottomPoint)) {
        continue;
      }

      const Line2D windowLine = {
        {line.start[0], line.start[1]},
        {line.end[0], line.end[1]}
      };

      const Line2D viewportLine = {
        adjustForViewport(windowLine.start, vp1, vp2),
        adjustForViewport(windowLine.end, vp1, vp2)
      };

      draw_line(viewportLine.start, viewportLine.end);
    }
  }

  // Finally, draw viewport lines
  set_colour(QColor(0.0, 0.0, 0.0));
  draw_line(vp1, {vp1[0], vp2[1]});
  draw_line(vp1, {vp2[0], vp1[1]});
  draw_line({vp1[0], vp2[1]}, vp2);
  draw_line({vp2[0], vp1[1]}, vp2);
}
Exemple #8
0
Mat4 Camera::totalMatrix() const
{
	return perspectiveMatrix() * viewMatrix();
}
Exemple #9
0
void InitLivin(int width, int height){
   int i;
   char letterpath[] = "data/x.obj"; /* character mesh mask */
   char letterlist[LETTERNUM] = "bceginrsty"; /* Cybernetic genetics characters */
   char greetingspath[21];

   /* OpenGL related initialization */
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   
   perspectiveMatrix(pmatrix, 35.0, (float)width/(float)height, 0.1, 1500.0);

   /* Demo related initialization */
   head      = openDepthVideo("data/head.dv");
   matat     = openDepthVideo("data/matat.dv");
   face      = initFace("shaders/face.vs", "shaders/face.fs");
   armface   = initFace("shaders/face.vs", "shaders/face.fs");
   knock     = openOutline("data/knock.pyd",           "shaders/knock.vs",   "shaders/knock.fs");
   jump      = openOutline("data/jump.pyd",            "shaders/jump.vs",    "shaders/outline.fs");
   hang      = openOutline("data/hanging.pyd",         "shaders/hang.vs",    "shaders/outline.fs");
   run       = openOutline("data/run.pyd",             "shaders/run.vs",     "shaders/outline.fs");
   falling   = openOutline("data/falling_letters.pyd", "shaders/run.vs",     "shaders/outline.fs");
   door      = openOutline("data/door.pyd",            "shaders/door.vs",    "shaders/door.fs");
   trap      = openOutline("data/trap.pyd",            "shaders/trap.vs",    "shaders/outline.fs");
   fractalme = openOutline("data/fractalme.pyd",       "shaders/frme.vs",    "shaders/frme.fs");
   /* Acrobatic outlines */
   tiger     = openOutline("data/tiger.pyd",           "shaders/acrobat.vs", "shaders/outline.fs");
   hop       = openOutline("data/hop.pyd",             "shaders/acrobat.vs", "shaders/outline.fs");
   katf      = openOutline("data/kezenatfordulas.pyd", "shaders/acrobat.vs", "shaders/outline.fs");
   flip      = openOutline("data/flip.pyd",            "shaders/acrobat.vs", "shaders/outline.fs");
   run2      = openOutline("data/run2.pyd",            "shaders/acrobat.vs", "shaders/outline.fs");
   catwheel  = openOutline("data/catwheel.pyd",        "shaders/acrobat.vs", "shaders/outline.fs");
   tbill     = openOutline("data/tarkobillenes.pyd",   "shaders/acrobat.vs", "shaders/outline.fs");

   createBox();
   createBackground();
   initGreetings();
   createDistBack();
   bigcube = createBigCube(600);

   titletex   = loadPNGTexture("data/title.png");
   greentex   = loadPNGTexture("data/green.png");
   creditstex = loadPNGTexture("data/credits.png");
   bgalphatex = loadPNGTexture("data/bg_alpha.png");
   whitetex   = loadPNGTexture("data/white.png");
   icubetex   = loadPNGTexture("data/innercube.png");
   headouttex = loadPNGTexture("data/headout.png");
   chesstex   = loadPNGTexture("data/chessboard.png");
   atpartytex = loadPNGTexture("data/at_party.png");

   for(i = 0; i < 12; i++){
      sprintf(greetingspath, "data/greetings%d.png", i+1);
      greetingstex[i] = loadPNGTexture(greetingspath);
   }

   for(i = 0; i < MAXKNOCKINGMAN; i++){
      knockingmans[i * 3 + 0] = drand48() * 4.0 + 8.0;
      knockingmans[i * 3 + 1] = drand48() * 6.0 + 1.0;
      knockingmans[i * 3 + 2] = drand48();
   }

   /* Shaders */
   background_src[0] = loadShader(GL_VERTEX_SHADER,          "shaders/background.vs");
   background_src[1] = loadShader(GL_FRAGMENT_SHADER,        "shaders/background.fs");
   background_src[2] = loadShader(GL_TESS_EVALUATION_SHADER, "shaders/background.te");
   background_src[3] = loadShader(GL_TESS_CONTROL_SHADER,    "shaders/background.tc");
   background_src[4] = loadShader(GL_GEOMETRY_SHADER,        "shaders/background.gs");
   background_shader = createProgram(5, background_src);

   title_src[0]      = background_src[0];
   title_src[1]      = loadShader(GL_FRAGMENT_SHADER, "shaders/title.fs");
   title_src[2]      = background_src[2];
   title_src[3]      = background_src[3];
   title_src[4]      = background_src[4];
   title_shader      = createProgram(5, title_src);

   scroll_src[0]     = background_src[0];
   scroll_src[1]     = loadShader(GL_FRAGMENT_SHADER, "shaders/scroll.fs");
   scroll_src[2]     = background_src[2];
   scroll_src[3]     = background_src[3];
   scroll_src[4]     = background_src[4];
   scroll_shader     = createProgram(5, scroll_src);

   credits_src[0]    = background_src[0];
   credits_src[1]    = loadShader(GL_FRAGMENT_SHADER, "shaders/credits.fs");
   credits_src[2]    = background_src[2];
   credits_src[3]    = background_src[3];
   credits_src[4]    = loadShader(GL_GEOMETRY_SHADER, "shaders/credits.gs");
   credits_shader    = createProgram(5, credits_src);

   letter_src[0]     = loadShader(GL_VERTEX_SHADER,   "shaders/letter.vs");
   letter_src[1]     = loadShader(GL_FRAGMENT_SHADER, "shaders/letter.fs");
   letter_shader     = createProgram(2, letter_src);

   cube_src[0]       = loadShader(GL_VERTEX_SHADER,   "shaders/cube.vs");
   cube_src[1]       = loadShader(GL_FRAGMENT_SHADER, "shaders/cube.fs");
   cube_src[2]       = loadShader(GL_GEOMETRY_SHADER, "shaders/cube.gs");
   cube_shader       = createProgram(3, cube_src);

   gr_src[0]         = loadShader(GL_VERTEX_SHADER,   "shaders/greetings.vs");
   gr_src[1]         = loadShader(GL_FRAGMENT_SHADER, "shaders/greetings.fs");
   gr_shader         = createProgram(2, gr_src);

   dbgr_src[0]       = loadShader(GL_VERTEX_SHADER,   "shaders/dbgr.vs");
   dbgr_src[1]       = loadShader(GL_FRAGMENT_SHADER, "shaders/dbgr.fs");
   dbgr_shader       = createProgram(2, dbgr_src);

   bigcube_src[0]    = loadShader(GL_VERTEX_SHADER,   "shaders/bigcube.vs");
   bigcube_src[1]    = loadShader(GL_FRAGMENT_SHADER, "shaders/bigcube.fs");
   bigcube_shader    = createProgram(2, bigcube_src);

   /* Letters */
   for(i = 0; i < LETTERNUM; i++){
      letterpath[5] = letterlist[i];
      letters[i] = loadOBJ(letterpath);

      glBindVertexArray(letters[i]->vao);
      bindVarToBuff(letter_shader, "vertex", letters[i]->vbo[MESHVERTEXINDEX], 3);
      bindVarToBuff(letter_shader, "normal", letters[i]->vbo[MESHNORMALINDEX], 3);
      bindVarToBuff(letter_shader, "tcoord", letters[i]->vbo[MESHTEXINDEX],    2);
   }

   cube = loadOBJ("data/cube.obj");
   glBindVertexArray(cube->vao);
   bindVarToBuff(cube_shader, "vertex", cube->vbo[MESHVERTEXINDEX], 3);
   bindVarToBuff(cube_shader, "normal", cube->vbo[MESHNORMALINDEX], 3);
   bindVarToBuff(cube_shader, "tcoord", cube->vbo[MESHTEXINDEX],    2);

   initExplosion();

   startTime();
}