Beispiel #1
0
void Renderer::updateLookPos()
{
	ALLEGRO_TRANSFORM look;
	al_copy_transform(&look, &camera_transform_);

	for(int i = 0; i < 4; i++)
	{
		al_translate_transform_3d(&look, 0.0, 0.0, 1.0);

		Vector3D pos;
		unProject(&look, pos);

		int x = (int)floor(pos.x+0.5), z = (int)floor(pos.z+0.5);
		int cx = x / 16, cz = z / 16;
		//int bx = x % 16, bz = z % 16;

		// getChunk will cause regions and chunks to be loaded, but they should already be loaded by now...

		auto it = chunkData_.find(getChunkKey(cx, cz));
		if(it != chunkData_.end() && it->second)
		{
			//RendererChunk *rc = it->second;
			// DO STUFF
		}
	}


}
Beispiel #2
0
	void position_transform(ALLEGRO_TRANSFORM *t)
	{
		al_identity_transform(t);

		al_translate_transform_3d(t, stepout.x, stepout.y, stepout.z);
		al_rotate_transform_3d(t, -1, 0, 0, tilt);
		al_rotate_transform_3d(t, 0, -1, 0, spin);
	}
Beispiel #3
0
/** Updates the camera. */
Camera * camera_update(Camera * self, double dt) {
   
   ALLEGRO_DISPLAY *display = al_get_current_display();
   double dw = al_get_display_width(display);
   double dh = al_get_display_height(display);

   
   double  f = tan(self->field_of_view / 2.0);
   
   /* Set up orthograĥic transform for UI, etc. */
   al_identity_transform(&self->orthographic_transform);
   al_orthographic_transform(&self->orthographic_transform, 0, 0, -1, dw, dh, 1000);
  
   /* Perspective transform for the main screen 3D view. */
   al_identity_transform(&self->perspective_transform);
   /* Back up camera a bit. */
   /* Set up a nice 3D view. */
   al_translate_transform_3d(&self->perspective_transform, 0, 0, -1);
   al_perspective_transform(&self->perspective_transform, 
      -1 * dw / dh * f, f, 1, f * dw / dh, -f, 1000);
 
  /*  al_perspective_transform(&projection, -1 * dw / dh * f, f,
      1,
      f * dw / dh, -f, 1000);
  */
  /* Set up the camera's position and view transform. */
  /*  al_build_camera_transform(&self->camera_transform, 
      self->position.x, self->position.y, self->position.z,
      self->look.x    , self->look.y    , self->look.z    ,
      self->up.x      , self->up.y      , self->up.z      );
     
  */  
  
  al_identity_transform(&self->camera_transform);

  al_translate_transform_3d(&self->camera_transform, 
      self->position.x, self->position.y, self->position.z);
  
  al_rotate_transform_3d(&self->camera_transform, 0, -1, 0, self->alpha); 
  al_rotate_transform_3d(&self->camera_transform, -1, 0, 0, self->theta); 

  
  /* Finally move at the set speed. */
  self->position = vec3d_add(self->position, vec3d_mul(self->speed, dt));
  return self;
 }
Beispiel #4
0
void Renderer::draw()
{
	//int dw = al_get_display_width(dpy_);
   //int dh = al_get_display_height(dpy_);

	ALLEGRO_TRANSFORM trans;
	al_identity_transform(&trans);

	al_compose_transform(&trans, &camera_transform_);
	al_rotate_transform_3d(&trans, 1.0, 0.0, 0.0, rx_look);

	setupProjection(&trans);
	al_identity_transform(&trans);


	al_use_transform(&trans);

	getWorldPos(camera_pos_);

	al_copy_transform(&cur3d_transform_, al_get_current_transform());

	glEnable(GL_DEPTH_TEST);
	// Accept fragment if it closer to the camera than the former one
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);
	glEnable(GL_ALPHA_TEST);

	if(!setShader(SHADER_DEFAULT))
	{
		NBT_Debug("failed to set default shader");
	}

	glBindVertexArray(vao_);

	resManager_->setAtlasUniforms();

	for(auto &it: chunkData_)
	{
		ChunkData *cd = it.second;

		ALLEGRO_TRANSFORM ctrans;
		al_identity_transform(&ctrans);
		al_translate_transform_3d(&ctrans, cd->x()*15.0, 0.0, cd->z()*15.0);
		al_use_transform(&ctrans);

		cd->draw(&ctrans);
	}

	glBindVertexArray(0);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	//drawSelection();

	resManager_->unsetAtlasUniforms();
}
Beispiel #5
0
	void reverse_position_transform(ALLEGRO_TRANSFORM *t)
	{
		// note: this is EXACTLY the same as position transform, except the
		// order of transformations is reversed, and the values are negated
		al_identity_transform(t);

		al_rotate_transform_3d(t, 0, 1, 0, spin);
		al_rotate_transform_3d(t, 1, 0, 0, tilt);
		al_translate_transform_3d(t, -stepout.x, -stepout.y, -stepout.z);
	}
Beispiel #6
0
void ChunkData::draw(ALLEGRO_TRANSFORM *trans)
{
	for(int32_t i = 0; i < MAX_SLICES; i++)
	{
		auto &slice = slice_[i];
		if(!slice.vbo)
			break;
		
		// do translation here >:(
		
		ALLEGRO_TRANSFORM local_transform;
		al_copy_transform(&local_transform, trans);
		al_translate_transform_3d(&local_transform, 0.0, -slice.y, 0.0);
		al_use_transform(&local_transform);
		
		//al_draw_vertex_buffer(slice.vbo, 0, 0, slice.vtx_count-1, ALLEGRO_PRIM_TRIANGLE_LIST);
		al_draw_vertex_buffer(slice.vbo, 0, 0, slice.vtx_count, ALLEGRO_PRIM_TRIANGLE_LIST);
		//al_draw_vertex_buffer(vbo_, tex, 0, size_, ALLEGRO_PRIM_TRIANGLE_LIST);
	}
}
Beispiel #7
0
void Renderer::run()
{
	NBT_Debug("begin");

	al_hide_mouse_cursor(dpy_);

	al_identity_transform(&camera_transform_);

	float x = -camera_pos_.x, y = -camera_pos_.y, z = -camera_pos_.z;
	//x = -dim0_->spawnX();
	//z = -dim0_->spawnZ();

	al_translate_transform_3d(&camera_transform_, x, y, z);

	al_rotate_transform_3d(&camera_transform_, 0.0, 1.0, 0.0, DEG_TO_RAD(180));

	memset(key_state_, 0, sizeof(key_state_) * sizeof(key_state_[0]));

	al_start_timer(tmr_);

	NBT_Debug("run!");

	//al_use_shader(nullptr);

	/*ALLEGRO_TRANSFORM trans;
	al_identity_transform(&trans);
	al_orthographic_transform(&trans, 0, 0, -1, al_get_display_width(dpy_), al_get_display_height(dpy_), 1);
	al_set_projection_transform(dpy_, &trans);
	al_identity_transform(&trans);
	al_use_transform(&trans);

	if(!resManager_->getAtlas()->getSheet(0)->alBitmap())
		NBT_Debug("no sheet bitmap????");
	*/
	//al_draw_bitmap(resManager_->getAtlas()->getSheet(0)->alBitmap(), 0, 0, 0);

	//al_flip_display();
	//sleep(10);

	bool redraw = false;
	bool doUpdateLookPos = false;
	bool cleared = false;
	
	while(1)
	{
		ALLEGRO_EVENT ev;
      al_wait_for_event(queue_, &ev);

      if(ev.type == ALLEGRO_EVENT_TIMER)
		{
         redraw = true;
			//cam_.rx = 1.0;
			float x = 0.0, y = 0.0, z = 0.0;
			float translate_diff = 0.3;
			float ry = 0.0;
			float rotate_diff = 0.04;
			bool changeTranslation = false;
			bool changeRotation = false;

			if(key_state_[ALLEGRO_KEY_W])
			{
				z += translate_diff;
				changeTranslation = true;
			}

			if(key_state_[ALLEGRO_KEY_S])
			{
				z -= translate_diff;
				changeTranslation = true;
			}

			if(key_state_[ALLEGRO_KEY_A])
			{
				x += translate_diff;
				changeTranslation = true;
			}

			if(key_state_[ALLEGRO_KEY_D])
			{
				x -= translate_diff;
				changeTranslation = true;
			}

			if(key_state_[ALLEGRO_KEY_SPACE])
			{
				y -= translate_diff;
				changeTranslation = true;
			}

			if(key_state_[ALLEGRO_KEY_LSHIFT])
			{
				y += translate_diff;
				changeTranslation = true;
			}

			if(key_state_[ALLEGRO_KEY_LEFT])
			{
				ry += rotate_diff;
				changeRotation = true;
			}

			if(key_state_[ALLEGRO_KEY_RIGHT])
			{
				ry -= rotate_diff;
				changeRotation = true;
			}

			if(changeTranslation)
			{
				//camera_pos_.translate(x, y, z);
				al_translate_transform_3d(&camera_transform_, x, y, z);
				doUpdateLookPos = true;
			}

			if(changeRotation)
			{
				al_rotate_transform_3d(&camera_transform_, 0.0, 1.0, 0.0, ry);
				doUpdateLookPos = true;
			}

			if(doUpdateLookPos)
				updateLookPos();

      }
      else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			NBT_Debug("display close");
         break;
      }
      else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			//NBT_Debug("key down");
			//NBT_Debug("pos: %fx%f", -camera_transform_.m[2][0], -camera_transform_.m[2][2]);
			key_state_[ev.keyboard.keycode] = true;

			if (ev.keyboard.keycode == ALLEGRO_KEY_Q)
			{
				break;
			}
			else if(ev.keyboard.keycode == ALLEGRO_KEY_C)
			{
				NBT_Debug("CLEAR CHUNKS");
				glBindVertexArray(vao_);
				for(auto ch: chunkData_)
				{
					delete ch.second;
				}
				glBindVertexArray(0);
				chunkData_.clear();

				glDeleteBuffers(1, &vao_);
				
				cleared = true;
			}
			else if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
			{
				grab_mouse_ = !grab_mouse_;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			//NBT_Debug("pos: %fx%f", -camera_transform_.m[2][0], -camera_transform_.m[2][2]);
			key_state_[ev.keyboard.keycode] = false;
		}
		else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
		{
				grab_mouse_ = true;
		}
		else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES && grab_mouse_)
		{
			float dx = ev.mouse.dx, dy = ev.mouse.dy;

			if(dy > 0 && dy < 1.5)
				dy = 0.0;

			if(dy < 0 && dy > -1.5)
				dy = 0.0;

			if(dx > 0 && dx < 1.5)
				dy = 0.0;

			if(dx < 0 && dx > -1.5)
				dx = 0.0;

			float ry = dx / al_get_display_width(dpy_), rx = dy / al_get_display_height(dpy_);

			rx_look += rx;
			al_rotate_transform_3d(&camera_transform_, 0.0, 1.0, 0.0, ry);
//			al_rotate_transform_3d(&camera_transform_, 1.0, 0.0, 0.0, rx);

			//cam_.rx += dy / al_get_display_height(dpy_);

			al_set_mouse_xy(dpy_, al_get_display_width(dpy_)/2.0, al_get_display_height(dpy_)/2.0);

			doUpdateLookPos = true;
		}

      if(redraw && al_is_event_queue_empty(queue_))
		{
			if(!loadChunkQueue.empty())
			{
				NBT_Debug("%i chunks to load", loadChunkQueue.size());

				std::pair<int32_t, int32_t> pos = loadChunkQueue.front();
				loadChunkQueue.pop();

				processChunk(pos.first, pos.second);
			}
			else
			{
				if(!cleared)
				{
					//NBT_Debug("pos: %fx%fx%f", camera_pos_.getX(), camera_pos_.getZ(), camera_pos_.getY());
					autoLoadChunks(camera_pos_.getX() / 16.0, camera_pos_.getZ() / 16.0);
				}
			}

			ALLEGRO_STATE state;
			al_store_state(&state, ALLEGRO_STATE_ALL);
			al_set_projection_transform(dpy_, &al_proj_transform_);

			glClear(GL_DEPTH_BUFFER_BIT);

         redraw = false;
			al_clear_to_color(al_map_rgb(255,255,255));
         draw();

			al_restore_state(&state);
			al_set_projection_transform(dpy_, &al_proj_transform_);

			drawHud();

			al_restore_state(&state);
         al_flip_display();
      }


	}

	NBT_Debug("stop timer");
	al_stop_timer(tmr_);

	NBT_Debug("end");


	NBT_Debug("sizeof GL_FLOAT: %i", sizeof(GLfloat));
}
Beispiel #8
0
int main(void) {
  ALLEGRO_VERTEX        v[8];
  ALLEGRO_DISPLAY     * display;
  ALLEGRO_TRANSFORM     perst;
  ALLEGRO_TRANSFORM     camt;
  ALLEGRO_BITMAP      * texture, * texture2;
  ALLEGRO_EVENT_QUEUE * queue;
  ALLEGRO_EVENT         event;
  ALLEGRO_FONT        * font;
  int                   busy  = 1;
  float                 cx    = 0;
  float                 cy    = -2; // 128;
  float                 cz    = 0;
  int                   face  = 0; 
  int                   hori  = 0;
  float                 angle = 0;
  float                 theta = 0;  
  float                 near  = 2;
  float                 far   = 8192;
  float                 zoom  = 1;
  float                 scale = 1.0;
  float                 us    = 20; 
  float                 ratio = 480.0 / 640.0;
  al_init();  
  al_init_image_addon();
  al_init_font_addon();
  al_install_keyboard();
  font  = al_create_builtin_font();
  
  queue = al_create_event_queue();  
  al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 16, ALLEGRO_SUGGEST);
  
  display  = al_create_display(640, 480);
  al_register_event_source(queue, al_get_keyboard_event_source());  
  texture  = al_load_bitmap("tile_1.png");
  texture2 = al_load_bitmap("tile_2.png");
  
  /* Allegro coordinates: +Y is down, +X is to the right, 
   * and +Z comes "out of" the screen.
   * 
   */
  
  
  while (busy) {
    init_vertex(v+0,   0,   0,   0,   0,   0, 1, 0, 0, 1);
    init_vertex(v+1,   0,   0,  us,   0, 256, 0, 1, 0, 1);
    init_vertex(v+2,  us,   0,  us, 256, 256, 0, 0, 1, 1);
    init_vertex(v+3,  us,   0,   0, 256,   0, 1, 1, 0, 1);
    init_vertex(v+4,  us, -us,   0, 256, 256, 1, 0, 1, 1);
    init_vertex(v+5,   0, -us,   0, 256,   0, 0, 1, 1, 1); 
    init_vertex(v+6,   0, -us,  us, 256, 256, 0, 0, 0, 1); 
    init_vertex(v+7,   0,   0,  us,   0, 256, 1, 1, 1, 1); 
  
  
    al_identity_transform(&camt);
    al_scale_transform_3d(&camt, scale, scale, scale);
    al_translate_transform_3d(&camt, cx, cy, cz);
  
    angle = face * 0.125 * ALLEGRO_PI;
    al_rotate_transform_3d(&camt, 0, -1, 0, angle); 
    theta = hori * 0.125 * ALLEGRO_PI;
    al_rotate_transform_3d(&camt, -1, 0, 0, theta); 
    
    al_use_transform(&camt);
    // al_set_projection_transform(display, &perst);

  
    al_clear_to_color(al_map_rgb_f(0.75, 0.75, 0.95));
    al_set_render_state(ALLEGRO_DEPTH_TEST, 1);
    al_clear_depth_buffer(far);

    
    
    al_identity_transform(&perst);
    al_perspective_transform(&perst, -1, ratio, near, 
                                      1,  -ratio, far);
    al_use_projection_transform(&perst);

    al_draw_filled_rectangle(0, 0, 4, 5, al_map_rgb_f(0, 0.25, 0.25));
    
        
    al_draw_prim(v, NULL, texture, 0, 8, ALLEGRO_PRIM_TRIANGLE_FAN);
    
    draw_textured_colored_rectangle_3d(0 , -us, 0, us, us, 0, 
                                       1.0, 1.0, texture2, al_map_rgb_f(1,1,1));
    
    draw_textured_colored_rectangle_3d(0 , -us, 0, 0, us, us, 
                                       1.0, 1.0, texture2, al_map_rgb_f(1,1,1));

    draw_textured_colored_rectangle_3d(0 , 0, 0, us, 0, us,
                                       1.0, 1.0, texture2, al_map_rgb_f(1,1,1));
     
    
    al_identity_transform(&perst);
    al_orthographic_transform(&perst, 0, 0, -1.0, 640, 480, 1.0);
 
    al_identity_transform(&camt);
    al_use_projection_transform(&perst);
    al_use_transform(&camt);
    al_draw_filled_rectangle(111, 22, 133, 44, al_map_rgb_f(0.25, 0.25, 0));

    
    al_draw_multiline_textf(font, al_map_rgb_f(1,1,1), 10, 10, 620, 0, 0,
      "Coords: (%f %f %f)\nAngle: (%f %f)\nView: [%f %f %f %f]\nScale: %f", 
               cx, cy, cz,        angle, theta,   near, far, zoom, scale); 
    
    
    
    al_flip_display();  
    al_wait_for_event(queue, &event);
    
    if (event.type == ALLEGRO_EVENT_KEY_DOWN) { 
      switch (event.keyboard.keycode) {
        case ALLEGRO_KEY_RIGHT:
          cx += 8;
          break;

        case ALLEGRO_KEY_LEFT:
          cx -= 8;
          break;
          
        case ALLEGRO_KEY_UP:
          cy += 8;
          break;
          
        case ALLEGRO_KEY_DOWN:
          cy -= 8;
          break;  
        
        case ALLEGRO_KEY_HOME:
          cz += 8;
          break;
          
        case ALLEGRO_KEY_END:
          cz -= 8;
          break;  
          
        case ALLEGRO_KEY_R:
          face++;
          break;
      
        case ALLEGRO_KEY_L:
          face--;
          break;
          
        case ALLEGRO_KEY_H:
          hori++;
          break;
      
        case ALLEGRO_KEY_G:
          hori--;
          break;
  
          
        case ALLEGRO_KEY_N:
          near *= 2.0;
          break;
          
        case ALLEGRO_KEY_M:
          near /= 2.0;
          break;

        case ALLEGRO_KEY_F:
          far += 64;
          break;
          
        case ALLEGRO_KEY_V:
          far -= 64;
          break;
        
        case ALLEGRO_KEY_Z:
          zoom *= 2.0f;
          break;
          
        case ALLEGRO_KEY_S:
          zoom /= 2.0f;
          break;
        
        case ALLEGRO_KEY_A:
          scale *= 2.0f;
          break;
          
        case ALLEGRO_KEY_Q:
          scale /= 2.0f;
          break;
          
        case ALLEGRO_KEY_ESCAPE:
          busy = 0 ;
          break;
  
        default:
          break;
      }
    }
  }
  
  al_destroy_bitmap(texture);
  al_destroy_bitmap(texture2);
  
  
  return 0;  
}