-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
252 lines (193 loc) · 6.33 KB
/
Graphics.cpp
File metadata and controls
252 lines (193 loc) · 6.33 KB
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
#include "Graphics.h"
#include "GlutImport.h"
#include <algorithm>
extern V2 Wsize;
V2 Graphics::getWindowSize()
{
return Wsize;
}
/* Copyright (c) 2024 Lilian Buzer - All rights reserved - */
/////////////////////////////////////////////////////////////
//
// RectWithTexture
//
/////////////////////////////////////////////////////////////
int GetTextureIdFromPNG(std::string PNGFileName);
int GetTextureIdFromJPG(std::string JPGFileName);
string GetExtension(string filename)
{
return filename.substr(filename.size() - 4);
}
#include <cctype>
static std::string ToLower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) { return (char)std::tolower(c); });
return s;
}
static std::string GetExtSafe(const std::string& f) {
auto s = ToLower(f);
if (s.size() >= 5 && s.rfind(".jpeg") == s.size() - 5) return ".jpeg";
if (s.size() >= 4) return s.substr(s.size() - 4); // .jpg / .png
return "";
}
void Graphics::drawRectWithTexture(std::string JPGPNGFileName, V2 pos, V2 size, float angleDeg)
{
// --- choix texture
int idTexture = 0;
auto ext = GetExtSafe(JPGPNGFileName);
if (ext == ".jpg" || ext == ".jpeg") idTexture = GetTextureIdFromJPG(JPGPNGFileName);
else if (ext == ".png") idTexture = GetTextureIdFromPNG(JPGPNGFileName);
else idTexture = GetTextureIdFromPNG("error.png");
// --- état rendu
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#define GL_CLAMP_TO_EDGE 0x812F
if (idTexture > 0)
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, (GLuint)idTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Facultatif mais utile contre les franges :
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(255, 255, 255, 255); // ne pas assombrir la texture
}
else
{
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glColor4ub(255, 0, 255, 255); // magenta “error”
}
float w = size.x, h = size.y;
const double MPI = 3.14159265358979323846;
// centre du quad
float cx = pos.x + w * 0.5f;
float cy = pos.y + h * 0.5f;
glPushMatrix();
glTranslatef(cx, cy, 0.0f);
glRotatef(angleDeg, 0.0f, 0.0f, 1.0f);
// dessiner autour de l'origine (plus simple pour la rotation)
float x0 = -w * 0.5f, y0 = -h * 0.5f;
float x1 = w * 0.5f, y1 = h * 0.5f;
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x0, y0, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x0, y1, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x1, y1, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x1, y0, 0.0f);
glEnd();
glPopMatrix();
// Nettoyage léger (optionnel)
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glColor4ub(255, 255, 255, 255);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
/////////////////////////////////////////////////////////////
//
// Geometry
//
/////////////////////////////////////////////////////////////
void Graphics::clearWindow(Color c)
{
glClearColor(c.R, c.G, c.B, c.A);
glClear(GL_COLOR_BUFFER_BIT);
}
void Graphics::setPixel(V2 P, Color c)
{
glColor4d(c.R, c.G, c.B, c.A);
glBegin(GL_POINTS);
glVertex2i(P.x, P.y); //Set pixel coordinates
glEnd();
//glFlush(); //Render pixel
}
void Graphics::drawRectangle(V2 P1, V2 Size, Color c, bool fill, int thickness)
{
glDisable(GL_TEXTURE_2D); // pas de texture
glLineWidth(thickness);
glColor4d(c.R, c.G, c.B, c.A);
glDisable(GL_TEXTURE_2D);
if (fill) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glRecti((int)P1.x, (int)P1.y, (int)(P1.x + Size.x), (int)(P1.y + Size.y));
}
void Graphics::drawCircle(V2 C, float r, Color c, bool fill, int thickness)
{
glLineWidth(thickness);
vector<V2> LPoints;
int lineAmount = r / 4; // nb of triangles used to draw circle
if (lineAmount < 20) lineAmount = 20;
const double PI = 3.14159265358;
//GLfloat radius = 0.8f; //radius
double step = 2 * PI / lineAmount;
for (int i = 0; i <= lineAmount; i++)
LPoints.push_back(V2(C.x + r * cos(i * step), C.y + r * sin(i * step)));
Graphics::drawPolygon(LPoints, c, fill);
}
void Graphics::drawLine(V2 P1, V2 P2, Color c, int thickness)
{
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth((GLfloat)thickness);
glColor4d(c.R, c.G, c.B, c.A);
glBegin(GL_LINES);
glVertex2f(P1.x, P1.y);
glVertex2f(P2.x, P2.y);
glEnd();
glLineWidth(1.0f);
glDisable(GL_BLEND);
}
void Graphics::drawPolygon(vector<V2>& PointList, Color c, bool fill, int thickness)
{
glDisable(GL_TEXTURE_2D);
glColor4d(c.R, c.G, c.B, c.A);
glLineWidth(thickness);
if (fill) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_POLYGON);
for (V2 P : PointList)
glVertex2f(P.x, P.y);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // reset d’hygiène
}
/////////////////////////////////////////////////////////////
//
// Font
//
/////////////////////////////////////////////////////////////
void DrawString(V2 pos, string text, float fontSize, float thickness, Color c, bool FontMono)
{
glColor4f(c.R, c.G, c.B, c.A);
// EPAISSEUR de la font
glLineWidth(thickness);
glPushMatrix();
glTranslatef(pos.x, pos.y, 0);
glScalef(1 / 152.38, 1 / 152.38, 1 / 152.38);
glScalef(fontSize, fontSize, fontSize);
const char* cc = text.c_str();
for (char* p = (char*)cc; *p; p++)
{
if (FontMono) glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *p);
else glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
}
glPopMatrix();
glLineWidth(1);
}
void Graphics::drawStringFontMono(V2 pos, string text, float fontSize, float thickness, Color c)
{
glDisable(GL_TEXTURE_2D);
DrawString(pos, text, fontSize, thickness, c, true);
}
void Graphics::drawStringFontRoman(V2 pos, string text, float fontSize, float thickness, Color c)
{
glDisable(GL_TEXTURE_2D);
DrawString(pos, text, fontSize, thickness, c, false);
}
//////////////////////////////////////////////
void MainWindowInit(string name, V2 WindowSize, V2 WindowStartPos);
void Graphics::initMainWindow(string name, V2 WindowSize, V2 WindowStartPos)
{
MainWindowInit(name, WindowSize, WindowStartPos);
}