Esempio n. 1
0
void GLRenderer::blitTexture(const GPUTexture *tex, bool flipVertically,
		bool centerHoriz, bool centerVert, const Vector2i &offset) {
	tex->bind();
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);
	Vector2i scrSize = Vector2i(viewport[2], viewport[3]);
	Vector2i texSize = Vector2i(tex->getSize().x, tex->getSize().y);
	if (scrSize.x == 0 || scrSize.y == 0) {
		tex->unbind();
		return;
	}

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, scrSize.x, scrSize.y, 0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(0.375f, 0.375f, 0.0f);
	glBegin(GL_QUADS);

	Vector2i upperLeft(0), lowerRight(0);
	if (centerHoriz)
		upperLeft.x = (scrSize.x - texSize.x)/2;
	if (centerVert)
		upperLeft.y = (scrSize.y - texSize.y)/2;
	upperLeft += offset;
	lowerRight = upperLeft + texSize;

	if (flipVertically)
		std::swap(upperLeft.y, lowerRight.y);

	const float zDepth = -1.0f; // just before the far plane
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f((float) upperLeft.x, (float) upperLeft.y, zDepth);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f((float) lowerRight.x, (float) upperLeft.y, zDepth);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f((float) lowerRight.x, (float) lowerRight.y, zDepth);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f((float) upperLeft.x, (float) lowerRight.y, zDepth);
	glEnd();

	tex->unbind();
}
Esempio n. 2
0
bool LineIntersectsRect( const DVec2 &v1, const DVec2 &v2, float x, float y, float width, float height )
{
        FVec2 lowerLeft( x, y+height );
        FVec2 upperRight( x+width, y );
        FVec2 upperLeft( x, y );
        FVec2 lowerRight( x+width, y+height);
        // check if it is inside
        if (v1.x > lowerLeft.x && v1.x < upperRight.x && v1.y < lowerLeft.y && v1.y > upperRight.y &&
            v2.x > lowerLeft.x && v2.x < upperRight.x && v2.y < lowerLeft.y && v2.y > upperRight.y )
        {   
            return true;
        }
        // check each line for intersection
        if (LineIntersectLine(v1,v2, upperLeft, lowerLeft ) ) return true;
        if (LineIntersectLine(v1,v2, lowerLeft, lowerRight) ) return true;
        if (LineIntersectLine(v1,v2, upperLeft, upperRight) ) return true;
        if (LineIntersectLine(v1,v2, upperRight, lowerRight) ) return true;
        return false;
}
Esempio n. 3
0
/**
 * Main program
 * @param  argc Number of command line arguments, inlucing the program itself.
 * @param  argv Vector of command line arguments.
 * @return      EXIT_SUCCESS if program exits normally, EXIT_ERROR otherwise.
 */
int main(int argc, char** argv) {
  if(argc < 2 || argc > 3) {
    std::cout << "Usage: warper input_image [ouput_image]" << std::endl;
    return EXIT_FAILURE;
  }

  readImage(argv[1]);

  Matrix3x3 M(1.0, 0.0, 0.0,
              0.0, 1.0, 0.0,
              0.0, 0.0, 1.0);
  process_input(M, originalWidth, originalHeight);

  Vector3d upperRight(originalWidth-1,originalHeight-1, 1);
  Vector3d lowerRight(originalWidth-1,0, 1);
  Vector3d upperLeft(0,originalHeight-1, 1);
  Vector3d lowerLeft(0,0, 1);

  upperRight = (M * upperRight)/upperRight[2];
  lowerRight = (M * lowerRight)/lowerRight[2];
  upperLeft = (M * upperLeft)/upperLeft[2];
  lowerLeft = (M * lowerLeft)/lowerLeft[2];

  newWidth = max(max(lowerLeft[0], lowerRight[0]), max(upperLeft[0], upperRight[0]));
  newHeight = max(max(lowerLeft[1], lowerRight[1]), max(upperLeft[1], upperRight[1]));

  int originX = min(min(lowerLeft[0], lowerRight[0]), min(upperLeft[0], upperRight[0]));
  int originY = min(min(lowerLeft[1], upperLeft[1]), min(lowerRight[1], upperRight[1]));

  newHeight = newHeight - originY;
  newWidth = newWidth - originX;

  Vector3d newOrigin(originX, originY, 0);

  // Initalize 2d array
  warppedPixels = new rgba_pixel*[newHeight];
  warppedPixels[0] = new rgba_pixel[newWidth*newHeight];

  for (int i=1; i < newHeight; i++) {
    warppedPixels[i] = warppedPixels[i-1] + newWidth;
  }

  Matrix3x3 invM = M.inv();

  for(int row = 0; row < newHeight; row++)
    for(int col = 0; col < newWidth; col++) {

      Vector3d pixel_out(col, row, 1);
      pixel_out = pixel_out + newOrigin;
      Vector3d pixel_in = invM * pixel_out;

      float u = pixel_in[0] / pixel_in[2];
      float v = pixel_in[1] / pixel_in[2];

      int roundedU = round(u);
      int roundedV = round(v);

      if((0 <= roundedU && roundedU < originalWidth) && (0 <= roundedV && roundedV < originalHeight)) {
          warppedPixels[row][col] = pixels[roundedV][roundedU];
        }
      else {
        rgba_pixel p;
        p.r = 0;
        p.g = 0;
        p.b = 0;
        p.a = 1;
        warppedPixels[row][col] = p;
      }
    }

    // Flip for openGL
    openGLFlip();

    // Init OpenGL
    glutInit(&argc, argv);
    openGLSetup(newWidth, newHeight);

    if(argc == 3) {
      outImage = argv[2];
      writeImage();
    }

    // Start running display window
    glutMainLoop();

  return EXIT_SUCCESS;
}
Esempio n. 4
0
wxPoint MutIconShapeClass<T>::GetPerimeterPoint(const wxPoint &i,const wxPoint &o, wxWindow * paintingWindow) const {
//	wxPoint inner = ScreenToClient(i); unused
	wxPoint myoffset = GetPositionInWindow(paintingWindow);
	wxPoint outer = o - myoffset;
	DEBUGLOG(routinggui,_T("outer (%d,%d) is in device (%d,%d)"),o.x,o.y,outer.x,outer.y);
	wxRect iconRect = this->GetIconRect();
	DEBUGLOG(routinggui,_T("Icon rect: x=%d, y=%d, w=%d, h=%d"),iconRect.x,iconRect.y,iconRect.width,iconRect.height);
	wxPoint iconCenter(iconRect.x+iconRect.width/2,
			   iconRect.y+iconRect.height/2);
	DEBUGLOG(routinggui,_T("center = (%d,%d)"), iconCenter.x, iconCenter.y);
/*
	wxRect screenRect = this->GetScreenRect();
	// transform Screen rect to upper left and 
	wxPoint upperLeft(this->ScreenToClient(wxPoint(screenRect.x,screenRect.y)));
	wxPoint lowerRight(this->ScreenToClient(wxPoint(screenRect.x+screenRect.width,screenRect.y+screenRect.height)));
*/
	wxRect screenRect = this->GetClientRect();
	wxPoint upperLeft(screenRect.x,screenRect.y);
	wxPoint lowerRight(screenRect.x+screenRect.width,screenRect.y+screenRect.height);
	DEBUGLOG(routinggui,_T("Rectangle: (%d,%d) -- (%d,%d)"),upperLeft.x, upperLeft.y, lowerRight.x, lowerRight.y);

	wxPoint perimeterpoint(0,0);
	if (lowerRight.x <= outer.x) {
		perimeterpoint.x = lowerRight.x;
		perimeterpoint.y = iconCenter.y;
	} else if (outer.x <= upperLeft.x) {
		perimeterpoint.x = upperLeft.x;
		perimeterpoint.y = iconCenter.y;
	} else if (outer.y <= upperLeft.y) {
		perimeterpoint.x = iconCenter.x;
		perimeterpoint.y = upperLeft.y;
	} else if (lowerRight.y <= outer.y) {
		perimeterpoint.x = iconCenter.x;
		perimeterpoint.y = lowerRight.y;
	} else {
		perimeterpoint = iconCenter;
	}

	mutpointlist::iterator pos = std::find(usedperimeterpoints.begin(),
					       usedperimeterpoints.end(),
					       perimeterpoint);
	if (pos == usedperimeterpoints.end())
		usedperimeterpoints.push_back(perimeterpoint);

	wxPoint retval = perimeterpoint + myoffset;

	DEBUGLOG(routinggui,_T("perimeter point (%d,%d), returning (%d,%d)"), 
		 perimeterpoint.x,perimeterpoint.y,retval.x,retval.y);

	return retval;


#if 0
	wxRect r = this->GetRect();
	DEBUGLOG (other, _T("Rect: (%d,%d) -- (%d,%d)"),r.x,r.y,r.x+r.width,r.y+r.height);
	DEBUGLOG (other, _T("Points: i = (%d,%d), o = (%d, %d)"),i.x,i.y,o.x,o.y);
	wxRect ir = this->GetIconRect();
#ifdef DEBUG
	std::cerr.flush();
	mutASSERT(r.Contains(i));
#endif

	wxPoint p;
	int xoffset = 0;
#if __WXMAC__ && 0
	xoffset = this->GetWindowBorderSize().x/2;
#endif

	r.y += borderOffset.y;
#if __WXMAC__
	r.y += maxBorderSize.y - borderOffset.y;
#endif
	if (r.x+r.width <= o.x) {
		p.x = r.x + r.width + xoffset;
		p.y = ir.y + ir.height/2;
	} else if (r.x >= o.x) {
		p.x = r.x - xoffset;
		p.y = ir.y + ir.height/2;
	} else if (r.y <= o.y) {
		p.x = r.x + r.width/2;
		p.y = r.y;
	} else if (r.y + r.height >= o.y) {
		p.x = r.x + r.width/2;
		p.y = r.y + r.height;
	} else p = o;


	mutpointlist::iterator pos = std::find(usedperimeterpoints.begin(),
					       usedperimeterpoints.end(),
					       p);
	if (pos == usedperimeterpoints.end())
		usedperimeterpoints.push_back(p);

#if __WXGTK__ && 0
	p.y += maxBorderSize.y - this->GetWindowBorderSize().y / 2;
#endif
	return p;
#endif
}