示例#1
0
// -----------------------------------------------------------------------------
// Draw the canvas contents
// -----------------------------------------------------------------------------
void CropCanvas::draw()
{
	// Setup for 2d
	setup2D();

	// Draw background
	drawCheckeredBackground();

	// Determine graphic position & scale
	double width  = GetSize().x;
	double height = GetSize().y;

	// Get image dimensions
	auto&  tex_info = OpenGL::Texture::info(texture_);
	double x_dim    = (double)tex_info.size.x;
	double y_dim    = (double)tex_info.size.y;

	// Get max scale for x and y (including padding)
	double x_scale = ((double)width - UI::scalePx(24)) / x_dim;
	double y_scale = ((double)height - UI::scalePx(24)) / y_dim;

	// Set scale to smallest of the 2 (so that none of the texture will be clipped)
	double scale = std::min<double>(x_scale, y_scale);

	glPushMatrix();
	glTranslated(width * 0.5, height * 0.5, 0); // Translate to middle of area
	glScaled(scale, scale, scale);              // Scale to fit within area

	// Draw graphic
	double hw = 0;
	double hh = 0;
	if (texture_)
	{
		glEnable(GL_TEXTURE_2D);
		hw = x_dim * -0.5;
		hh = y_dim * -0.5;
		Drawing::drawTexture(texture_, hw, hh);
	}

	// Draw cropping rectangle
	OpenGL::setColour(0, 0, 0, 255, OpenGL::Blend::Normal);
	glDisable(GL_TEXTURE_2D);
	glTranslated(hw, hh, 0);                                          // Translate to top-left of graphic
	Drawing::drawLine(crop_rect_.tl.x, -1000, crop_rect_.tl.x, 1000); // Left
	Drawing::drawLine(-1000, crop_rect_.tl.y, 1000, crop_rect_.tl.y); // Top
	Drawing::drawLine(crop_rect_.br.x, -1000, crop_rect_.br.x, 1000); // Right
	Drawing::drawLine(-1000, crop_rect_.br.y, 1000, crop_rect_.br.y); // Bottom

	// Shade cropped-out area
	OpenGL::setColour(0, 0, 0, 100, OpenGL::Blend::Normal);
	Drawing::drawFilledRect(-1000, -1000, crop_rect_.tl.x, 1000);                      // Left
	Drawing::drawFilledRect(crop_rect_.br.x, -1000, 1000, 1000);                       // Right
	Drawing::drawFilledRect(crop_rect_.tl.x, -1000, crop_rect_.br.x, crop_rect_.tl.y); // Top
	Drawing::drawFilledRect(crop_rect_.tl.x, crop_rect_.br.y, crop_rect_.br.x, 1000);  // Bottom

	glPopMatrix();

	SwapBuffers();
}
示例#2
0
文件: game.c 项目: nixpulvis/maze_gl
// Display 2D overlay on top of 3D game
void display2D(void) {
  // Set up OpenGL for 2D overlay
  setup2D();

  // Get width and height of screen
  int width = glutGet(GLUT_WINDOW_WIDTH);
  int height = glutGet(GLUT_WINDOW_HEIGHT);

  // Radius of compass
  int compassRadius = 100;

  // Offset from edge of screen
  int compassOffset = 50;

  // Width of compass's hand (technically radius, but not a circle)
  int compassHandWidth = 20;

  // Center of compass radius (the small circle in the center)
  int compassCenterRadius = 5;

  // Number of triangles to generate the pseudo circle
  // Incease for more accuracy
  int compassNumOfTriangles = 64;

  // Center of compass location on screen
  int compassCenterX = width-(compassRadius+compassOffset);
  int compassCenterY = height-(compassRadius+compassOffset);

  // Color of compass, off-white
  glColor3f(0.8f, 0.8f, 0.8f);

  // Draw the Basic circle of the compass
  glBegin(GL_POLYGON);
  for(int i = 0; i < compassNumOfTriangles; i++) {
    glVertex2f(
      compassCenterX+(compassRadius*cos((float)i/((float)compassNumOfTriangles/2)*M_PI)), 
      compassCenterY+(compassRadius*sin((float)i/((float)compassNumOfTriangles/2)*M_PI)));
  }
  glEnd();

  // Calculate the angle that would be North (+x)
  float cardinalAngle = g_state.player.look.yaw - M_PI_2;

  // Color of compass hand, pure red
  glColor3f(1.0f, 0.0f, 0.0f);

  // Draw the Compass Hand
  glBegin(GL_POLYGON);
  glVertex2f(
    compassCenterX-compassRadius*cos(cardinalAngle),
    compassCenterY+compassRadius*sin(cardinalAngle));
  glVertex2f(
    compassCenterX-compassHandWidth*cos(cardinalAngle+M_PI_2),
    compassCenterY+compassHandWidth*sin(cardinalAngle+M_PI_2));
  glVertex2f(
    compassCenterX+compassHandWidth*cos(cardinalAngle),
    compassCenterY-compassHandWidth*sin(cardinalAngle));
  glVertex2f(
    compassCenterX+compassHandWidth*cos(cardinalAngle+M_PI_2),
    compassCenterY-compassHandWidth*sin(cardinalAngle+M_PI_2));
  glEnd();

  // Color of compass center, pure black
  glColor3f(0.0f, 0.0f, 0.0f);

  // Draw the basic circle for the center of the compass
  glBegin(GL_POLYGON);
  for(int i = 0; i < compassNumOfTriangles; i++) {
    glVertex2f(
      compassCenterX+(compassCenterRadius*cos((float)i/16*M_PI)),
      compassCenterY+compassCenterRadius*sin((float)i/16*M_PI));
  }
  glEnd();

  // Color of crosshair, pure black
  glColor3f(0.0f, 0.0f, 0.0f);

  // Draw crosshair 2 pixels thick in center of screen
  glBegin(GL_QUADS);
  glVertex2f(width/2-1, height/2-10);
  glVertex2f(width/2+1, height/2-10);
  glVertex2f(width/2+1, height/2+10);
  glVertex2f(width/2-1, height/2+10);

  glVertex2f(width/2-10, height/2-1);
  glVertex2f(width/2+10, height/2-1);
  glVertex2f(width/2+10, height/2+1);
  glVertex2f(width/2-10, height/2+1);

  glEnd();
}