2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-01-23 02:02:45 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkCamera.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/DecodeFile.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/effects/SkEmbossMaskFilter.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
|
|
|
#include "tools/timer/AnimTimer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class CameraView : public Sample {
|
2016-03-25 16:08:00 +00:00
|
|
|
SkTArray<sk_sp<SkShader>> fShaders;
|
2010-12-20 18:26:13 +00:00
|
|
|
int fShaderIndex;
|
|
|
|
bool fFrontFace;
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
CameraView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
fRX = fRY = fRZ = 0;
|
2010-12-20 18:26:13 +00:00
|
|
|
fShaderIndex = 0;
|
|
|
|
fFrontFace = false;
|
|
|
|
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
SkString str;
|
|
|
|
str.printf("/skimages/elephant%d.jpeg", i);
|
|
|
|
SkBitmap bm;
|
2016-03-18 22:48:49 +00:00
|
|
|
if (decode_file(str.c_str(), &bm)) {
|
2011-04-14 17:53:24 +00:00
|
|
|
SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) };
|
2010-12-20 18:26:13 +00:00
|
|
|
SkRect dst = { -150, -150, 150, 150 };
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
|
2014-04-28 14:55:39 +00:00
|
|
|
|
2019-04-08 17:20:23 +00:00
|
|
|
fShaders.push_back(bm.makeShader(&matrix));
|
2010-12-20 18:26:13 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-05 01:59:48 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "Camera");
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2010-12-20 18:26:13 +00:00
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(this->width()/2, this->height()/2);
|
|
|
|
|
|
|
|
Sk3DView view;
|
2010-12-20 18:26:13 +00:00
|
|
|
view.rotateX(fRX);
|
|
|
|
view.rotateY(fRY);
|
2008-12-17 15:59:43 +00:00
|
|
|
view.applyToCanvas(canvas);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
2010-12-20 18:26:13 +00:00
|
|
|
if (fShaders.count() > 0) {
|
|
|
|
bool frontFace = view.dotWithNormal(0, 0, SK_Scalar1) < 0;
|
|
|
|
if (frontFace != fFrontFace) {
|
|
|
|
fFrontFace = frontFace;
|
|
|
|
fShaderIndex = (fShaderIndex + 1) % fShaders.count();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setShader(fShaders[fShaderIndex]);
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
2010-12-20 18:26:13 +00:00
|
|
|
SkRect r = { -150, -150, 150, 150 };
|
|
|
|
canvas->drawRoundRect(r, 30, 30, paint);
|
|
|
|
}
|
2015-02-02 03:01:04 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2015-02-02 20:55:02 +00:00
|
|
|
if (timer.isStopped()) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fRY = 0;
|
2015-02-02 20:55:02 +00:00
|
|
|
} else {
|
|
|
|
fRY = timer.scaled(90, 360);
|
2010-12-20 18:26:13 +00:00
|
|
|
}
|
2015-02-02 03:01:04 +00:00
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-12-20 18:26:13 +00:00
|
|
|
SkScalar fRX, fRY, fRZ;
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new CameraView(); )
|