2008-12-17 18:59:43 +03:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCamera.h"
|
|
|
|
#include "SkEmbossMaskFilter.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkRandom.h"
|
2010-12-20 21:26:13 +03:00
|
|
|
#include "SkImageDecoder.h"
|
2008-12-17 18:59:43 +03:00
|
|
|
|
|
|
|
class CameraView : public SkView {
|
2010-12-20 21:26:13 +03:00
|
|
|
SkTDArray<SkShader*> fShaders;
|
|
|
|
int fShaderIndex;
|
|
|
|
bool fFrontFace;
|
2008-12-17 18:59:43 +03:00
|
|
|
public:
|
2010-12-20 21:26:13 +03:00
|
|
|
CameraView() {
|
2008-12-17 18:59:43 +03:00
|
|
|
fRX = fRY = fRZ = 0;
|
2010-12-20 21:26:13 +03:00
|
|
|
fShaderIndex = 0;
|
|
|
|
fFrontFace = false;
|
|
|
|
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
SkString str;
|
|
|
|
str.printf("/skimages/elephant%d.jpeg", i);
|
|
|
|
SkBitmap bm;
|
|
|
|
if (SkImageDecoder::DecodeFile(str.c_str(), &bm)) {
|
|
|
|
SkShader* s = SkShader::CreateBitmapShader(bm,
|
|
|
|
SkShader::kClamp_TileMode,
|
|
|
|
SkShader::kClamp_TileMode);
|
|
|
|
|
2011-04-14 21:53:24 +04:00
|
|
|
SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
|
2010-12-20 21:26:13 +03:00
|
|
|
SkRect dst = { -150, -150, 150, 150 };
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
|
|
|
|
s->setLocalMatrix(matrix);
|
|
|
|
*fShaders.append() = s;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
2010-12-20 21:26:13 +03:00
|
|
|
virtual ~CameraView() {
|
|
|
|
fShaders.unrefAll();
|
2008-12-17 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2010-12-20 21:26:13 +03:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
2008-12-17 18:59:43 +03:00
|
|
|
if (SampleCode::TitleQ(*evt))
|
|
|
|
{
|
|
|
|
SampleCode::TitleR(evt, "Camera");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2010-12-20 21:26:13 +03:00
|
|
|
|
|
|
|
void drawBG(SkCanvas* canvas) {
|
2008-12-17 18:59:43 +03:00
|
|
|
canvas->drawColor(0xFFDDDDDD);
|
|
|
|
}
|
|
|
|
|
2010-12-20 21:26:13 +03:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
2008-12-17 18:59:43 +03:00
|
|
|
this->drawBG(canvas);
|
|
|
|
|
|
|
|
canvas->translate(this->width()/2, this->height()/2);
|
|
|
|
|
|
|
|
Sk3DView view;
|
2010-12-20 21:26:13 +03:00
|
|
|
view.rotateX(fRX);
|
|
|
|
view.rotateY(fRY);
|
2008-12-17 18:59:43 +03:00
|
|
|
view.applyToCanvas(canvas);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
SkScalar rad = SkIntToScalar(50);
|
|
|
|
SkScalar dim = rad*2;
|
2010-12-20 21:26:13 +03:00
|
|
|
if (fShaders.count() > 0) {
|
|
|
|
bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
|
|
|
|
if (frontFace != fFrontFace) {
|
|
|
|
fFrontFace = frontFace;
|
|
|
|
fShaderIndex = (fShaderIndex + 1) % fShaders.count();
|
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
|
2010-12-20 21:26:13 +03:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setShader(fShaders[fShaderIndex]);
|
2008-12-17 18:59:43 +03:00
|
|
|
#if 0
|
2010-12-20 21:26:13 +03:00
|
|
|
canvas->drawCircle(0, 0, rad, paint);
|
|
|
|
canvas->drawCircle(-dim, -dim, rad, paint);
|
|
|
|
canvas->drawCircle(-dim, dim, rad, paint);
|
|
|
|
canvas->drawCircle( dim, -dim, rad, paint);
|
|
|
|
canvas->drawCircle( dim, dim, rad, paint);
|
|
|
|
#else
|
|
|
|
SkRect r = { -150, -150, 150, 150 };
|
|
|
|
canvas->drawRoundRect(r, 30, 30, paint);
|
2008-12-17 18:59:43 +03:00
|
|
|
#endif
|
2010-12-20 21:26:13 +03:00
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
|
2010-12-20 21:26:13 +03:00
|
|
|
fRY += SampleCode::GetAnimSecondsDelta() * 90;
|
|
|
|
if (fRY >= SkIntToScalar(360)) {
|
2008-12-17 18:59:43 +03:00
|
|
|
fRY = 0;
|
2010-12-20 21:26:13 +03:00
|
|
|
}
|
2008-12-17 18:59:43 +03:00
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-12-20 21:26:13 +03:00
|
|
|
SkScalar fRX, fRY, fRZ;
|
2008-12-17 18:59:43 +03:00
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new CameraView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
|