forked from robertg71/3DGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·271 lines (224 loc) · 8.46 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Copyright (C) 2011 MoSync AB
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <mavsprintf.h>
#include <MAUtil/GLMoblet.h>
#include <GLES2/gl2.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <stdlib.h>
#include <hash_map>
#include "graph.h"
#include "GFont.h"
#include "Shaders.h"
#include "RenderText.h"
#include "MAHeaders.h"
#include "DTime.h"
#include "Touch.h"
using namespace MAUtil;
// TODO HANDLE RENDERTEXT in Y aswell we might need to get hold of min/max Y...
class MyGLMoblet: public GLMoblet
{
private:
// Bars location parameters
int mWidth;
int mHeight; // Screen resolution in ABS form e.g. 640,480
MoGraph::IGraph *mGraph; // interface/Base class to MoGraph
IFont *mFont; // interface/Base class to Font
glm::vec4 *mColors; // array pointing towards a color table
float *mTables; // array pointing towards a float table
DTime mDTime; // delta time between each tick! (update)
Time mTime; // elapsed time since creation of this class,
RenderText mText;
public:
/**
* Constructor for the main app class
*/
MyGLMoblet() :
GLMoblet(GLMoblet::GL2) , mGraph(0), mFont(0), mColors(0), mTables(0)
{
}
/**
* Destructor
*/
virtual ~MyGLMoblet()
{
delete [] mTables;
delete [] mColors;
delete mGraph; // Delete Graph
delete mFont; // Deleta Font
}
/**
* This method is called when a key is pressed.
*/
void keyPressEvent(int keyCode, int nativeCode)
{
switch (keyCode)
{
// Close the application if the back key, key 0,
// or the left "softkey" is pressed (the left
// softkey is usually located at the top left of
// the keypad of phones that have a keypad, and
// is often used as a back navigation key).
case MAK_BACK:
case MAK_0:
case MAK_SOFTLEFT:
// Exit the application.
maExit(0);
break;
}
}
/**
* This function is called with a coordinate when a pointer is pressed.
*/
void multitouchPressEvent(MAPoint2d p, int touchId)
{
mGraph->getTouch().multitouchPressEvent(p,touchId); // just forward function to the Graphs own touch handler
}
/**
* This function is called with a coordinate when a pointer is moved.
*/
void multitouchMoveEvent(MAPoint2d p, int touchId)
{
mGraph->getTouch().multitouchMoveEvent(p,touchId); // just forward function to the Graphs own touch handler
}
/**
* This function is called with a coordinate when a pointer is released.
*/
void multitouchReleaseEvent(MAPoint2d p, int touchId)
{
mGraph->getTouch().multitouchReleaseEvent(p,touchId); // just forward function to the Graphs own touch handler
}
/**
* init call backed from GLMoblet
*/
void init()
{
/*
* *Graph object needs to be allocated and then initiated,
* *Font is a separate system but is required in the graph for rendering text in 3D
* *RenterText is an independent text renderer flat on screen. but very same class is being used in Graph aswell
* can handle both orthogonal projection see drawText and drawText3D
*/
lprintfln("Init APP");
mWidth = (int)(1.0f*EXTENT_X(maGetScrSize()));
mHeight = (int)(1.0f*EXTENT_Y(maGetScrSize()));
mGraph = new MoGraph::Graph(); // Create MoGraph::Graph class
mFont = new BMFont(); // Create Font class
int grid = 33; // set up a desired grid for the graph in X & Z.
lprintfln("mGrid: %i", grid);
std::vector<MAHandle> fontTexArray;
fontTexArray.push_back(R_BOX_TEXTURE);
mFont->Init(R_BOX_FNT, fontTexArray); // Initiate font where to get its resources (.fnt) file generated from BMFont and the bitmap texture that contains the aphabet
lprintfln("Init RenderText w=%d h=%d\n",mWidth,mHeight);
mText.init(mWidth,mHeight,mFont); // initiate the text system by setting a Font & Screen dimensions
float gridStepY = 0.5f;
int gridLines = 5;
glm::vec4 bkcolor(0.0f, 0.0f, 0.0f, 1.0f);
mDTime.setDesiredFps(50.0f); // set up the DTime used for calculating FPS
setPreferredFramesPerSecond(50); // set preferred fps for the Moblet
// initiate Graph by setting up a grid sz in X & Z , also grid in Y with grid step, additional info like fit to screen, screen resolutions.
if (!mGraph->init(grid,grid,gridLines,gridStepY,true,mFont,mWidth,mHeight)) // initiate Graph with parameters as GridX, GridY, amount of gridLines in Y, stepping for each grid line in Y, fit to screen, Font class, screen width and screen height
maPanic(1,"Failed to initiate Graph");
mGraph->setBKColor(bkcolor); // additional set background color
// TEXT MANIPULATION IN GRAPH
// Text strings in the graph has a default setup. everything is built upon the Text structs that are pushed to an std::vector<Text> Text array
// We can change the existing setup by changeing the strings...
std::vector<MoGraph::Text> &textArray = mGraph->getScene().getTextMgr().getTextArray();
std::vector<std::string> strArray;
strArray.push_back("Sinus table ala Carte");
strArray.push_back("Y-Axel");
strArray.push_back("X-Axel");
strArray.push_back("Z-Axel");
for (size_t i = 0; i<textArray.size(); i++)
{
textArray[i].mText = strArray[i];
}
// and we can also push new ones to the table for more text in the graph... you can add as many as you please... fps issue in the end.
float scale = mGraph->getScene().getGridX()/500.0f;
// create additional example text
MoGraph::Text text;
text.mColor = glm::vec4(1.0f,0.0f,0.0f,1.0f);
text.mPos = glm::vec3(0.0f,5.0f,0.0f);
text.mRotate = glm::vec3(0.0f,0.0f,0.0f); // Rotation in degrees
text.mScale = glm::vec2(scale,scale);
text.mTextFlag = MoGraph::Text::CENTER;
text.mText = "MoGraph DEMO Beta";
textArray.push_back(text);
// just add more. here
}
/**
* \brief Draw, callebacked from the GLMoblet.
*/
void draw()
{
mDTime.tick();
MoGraph::Scene &scene = mGraph->getScene(); // get scene information
const int iGridZ = scene.getGridZ(); // need to be able to read the grid size
const int iGridX = scene.getGridX();
const int sz = iGridX * iGridZ;
const float tick = static_cast<float>(mTime.update())*0.001f;
int k = 0;
if (mTables == 0) // check if array already is allocated
mTables = new float[sz]; // allocate an array for set data to the Bars.
if (mColors == 0) // check if array already is allocated
mColors = new glm::vec4[sz]; // allocate an array for color table
for(int j=0; j<iGridZ; j++) // Build the data
{
k += 1-(iGridX&1); // if grid is even then extra add would be required
for(int i=0; i<iGridX; i++)
{
const int id = j*iGridX+i;
mTables[id] = 1.1f+1.0f*(sin(j*0.3f+ 1.3f*tick)+cos(i*0.3f+1.3f*tick));
// float c = 0.5f+0.5f*(float)(k&1);
// glm::vec4 col(1.0f-c,0.75f,c,1.0f);
glm::vec4 col(1.0f/iGridX*i,0.0f,1.0f/iGridZ*j,1.0f);
mColors[id] = col;
k++;
}
}
glEnable(GL_DEPTH_TEST);
// Update data to graph
mGraph->setValues(mTables,sz); // set the value array to the Graph to read from
mGraph->setColors(mColors,sz); // set the color array to the Graph to read from
mGraph->draw(); // Draw the whole graph system
glm::vec3 pos(100.0f,100.0f,100.0f);
glm::vec4 col(1.0f,1.0f,1.0f,1.0f);
mText.setScale(1.0f,1.0f);
mText.drawText("Testing 123!",pos,col);
pos.x = 0.0f;
pos.y = 0.0f;
pos.z = 10.0f;
for(int i=0; i<10; i+=1)
{
const float scale = 0.6f;
const float sy = scale*(1.0f+0.5f*sin(tick+(i*0.03f)));
mText.setScale(sy,sy); // note text needs to be written up side down due to orthogonal matrix 0,0 upper corner.. 640,480 lower right corner.
pos.x = static_cast<float>(50.0f+50.0f*sy);
pos.y = static_cast<float>(i*20*sy);
char buf[64];
sprintf ( buf, "%d FPS=%.2f ms=%d",i,mDTime.currentFps(),mDTime.getElapsed());
mText.drawText(buf,pos,col);
}
}
};
/**
* Entry point of the program. The MAMain function
* needs to be declared as extern "C".
*/
extern "C" int MAMain() {
Moblet::run(new MyGLMoblet());
return 0;
}