-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.cpp
72 lines (57 loc) · 1.57 KB
/
Triangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Triangle.cpp
// cpp-triangle-test
//
// Created by Stepan Kopylov on 15/04/2021.
//
#include "Triangle.hpp"
#include "nvg/nanovg_gl.h"
#include "nvg/nanovg_gl_utils.h"
#define TRIANGLE_HEIGHT 100
#define TRIANGLE_WIDTH 120
void Triangle::prepareToDraw()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
}
void Triangle::draw(float w, float h, float scale, float angle){
prepareToDraw();
if(!nanovgInitialized){
vg = nvgCreateGLES2(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
nanovgInitialized = true;
}
nvgBeginFrame(vg, w, h, 3);
nvgSave(vg);
float centerX, centerY;
centerX = w/2.f;
centerY = h/2.f;
nvgTranslate(vg, centerX, centerY);
nvgRotate(vg, degreesToRadians(angle));
nvgTranslate(vg, -centerX, -centerY);
nvgBeginPath(vg);
nvgMoveTo(vg, centerX, centerY-TRIANGLE_HEIGHT/2);
nvgLineTo(vg, centerX+TRIANGLE_WIDTH/2, centerY+TRIANGLE_HEIGHT/2);
nvgLineTo(vg, centerX-TRIANGLE_WIDTH/2, centerY+TRIANGLE_HEIGHT/2);
nvgLineTo(vg, centerX, centerY-TRIANGLE_HEIGHT/2);
nvgClosePath(vg);
NVGcolor color = nvgRGB(0.0, 255, 0.0);
nvgFillColor(vg, color);
nvgFill(vg);
nvgEndFrame(vg);
nvgRestore(vg);
if(radCallback){
radCallback(degreesToRadians(angle));
}
}
void Triangle::setRadCallback(RadCallback cRadCallback){
radCallback = cRadCallback;
}
double Triangle::degreesToRadians(double degree)
{
double pi = 3.14159265359;
return (degree * (pi / 180));
}