forked from BernardCastello/Year-2
/
Graphics.cpp
180 lines (147 loc) · 4.62 KB
/
Graphics.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
#include "Graphics.h"
namespace Graphics
{
// A pointer to the array of Monitor's in the system
GLFWmonitor** Monitor;
int aiNumOfMonitors[3];
// The current Monitor's attributes get stored here
const GLFWvidmode* Mode;
// The OpenGL Window
GLFWwindow* Window;
glm::mat4 View;
glm::mat4 Projection;
glm::vec4 RED(1, 0, 0, 1);
glm::vec4 ORANGE(1, 0.5, 0, 1);
glm::vec4 YELLOW(1, 1, 0, 1);
glm::vec4 LIGHT_GREEN(0.5, 1, 0, 1);
glm::vec4 GREEN(0, 1, 0, 1);
glm::vec4 SEA_GREEN(0, 1, 0.5, 1);
glm::vec4 CYAN(0, 1, 1, 1);
glm::vec4 LIGHT_BLUE(0, 0.5, 1, 1);
glm::vec4 BLUE(0, 0, 1, 1);
glm::vec4 PURPLE(0.5, 0, 1, 1);
glm::vec4 PINK(1, 0, 1, 1);
glm::vec4 WHITE(1);
glm::vec4 BLACK(0, 0, 0, 1);
void DrawSphere(glm::vec3 a_vPos, float a_fRadius, float a_fQuality, glm::vec4 a_vColor)
{
Gizmos::addSphere(a_vPos, a_fRadius, a_fQuality, a_fQuality, a_vColor);
}
void DrawImage(GLuint glTexture)
{
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, Surface.Surface);
glBegin(GL_QUADS);
//Bottom-left vertex (corner)
glColor3b(127, 127, 127);
glTexCoord2i(0, 0); //Position on texture to begin interpolation
glVertex3f(x, y, 0.f); //Vertex Coords
//Bottom-right vertex (corner)
glTexCoord2i(1, 0);
glVertex3f(x + Surface.w, y, 0.f);
//Top-right vertex (corner)
glTexCoord2i(1, 1);
glVertex3f(x + Surface.w, y + Surface.h, 0.f);
//Top-left vertex (corner)
glTexCoord2i(0, 1);
glVertex3f(x, y + Surface.h, 0.f);
glEnd();
}
GLuint LoadTexture(char* szFileName)
{
GLuint glTexture;
FIBITMAP* pBitmap = FreeImage_Load(FreeImage_GetFileType("Images/Charmander.png", 0), "Images/Charmander.png");
FIBITMAP* pImage = FreeImage_ConvertTo32Bits(pBitmap);
int iWidth = FreeImage_GetWidth(pImage);
int iHeight = FreeImage_GetHeight(pImage);
glGenTextures(1, &glTexture);
glBindTexture(GL_TEXTURE_2D, glTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, iWidth, iHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(pImage));
FreeImage_Unload(pImage);
return glTexture;
}
void SetView(glm::vec3 a_vEye, glm::vec3 a_vCenter, glm::vec3 a_vUp)
{
View = glm::lookAt(a_vEye, a_vCenter, a_vUp);
}
GLFWwindow* GetWin()
{
return Window;
}
int GetWinWidth()
{
return Mode->width;
}
int GetWinHeight()
{
return Mode->height;
}
void Clear()
{
//GL_COLOR_BUFFER_BIT informs OpenGL to wipe the back-buffer colours clean.
//GL_DEPTH_BUFFER_BIT informs it to clear the distance to the closest pixels.
//If we didn’t do this then OpenGL may think the image of the last frame is still there
//and our new visuals may not display
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear the backbuffer
Gizmos::clear();
Gizmos::addTransform(glm::mat4(1));
}
void SwapBuffer()
{
Gizmos::draw(Projection * View);
glfwSwapBuffers(Window);
}
int Init(bool a_bSetFullScreen, unsigned int a_uiWidth, unsigned int a_uiHeight, int a_uiPosX, int a_uiPosY)
{
Monitor = glfwGetMonitors(aiNumOfMonitors);
Mode = glfwGetVideoMode(Monitor[0]);
glfwWindowHint(GLFW_RED_BITS, Mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, Mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, Mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, Mode->refreshRate);
if (a_bSetFullScreen)
Window = glfwCreateWindow(Mode->width, Mode->height, "The Solar System", Monitor[0], nullptr);
else
{
Window = glfwCreateWindow(a_uiWidth, a_uiHeight, "The Solar System", nullptr, nullptr);
if (a_uiPosX == -1)
a_uiPosX = (Mode->width - a_uiWidth) / 2;
if (a_uiPosY == -1)
a_uiPosY = (Mode->height - a_uiHeight) / 2;
glfwSetWindowPos(Window, a_uiPosX, a_uiPosY);
}
if (Window == nullptr)
{
glfwTerminate();
return -2;
}
//make the glfw window
glfwMakeContextCurrent(Window);
if (ogl_LoadFunctions() == ogl_LOAD_FAILED)
{
glfwDestroyWindow(Window);
glfwTerminate();
return -3;
}
// Parses the version of OpenGL and prints it
auto major = ogl_GetMajorVersion();
auto minor = ogl_GetMinorVersion();
printf_s("GL: %i.%i\n", major, minor);
//initialize all of our gizmos and set up the virtual camera
Gizmos::create();
SetView({ 10, 0, 0 }, { 0, 0, 0 }, { 0, 1, 0 });
Projection = glm::perspective(glm::pi<float>() * 0.25f, 16 / 9.f, 0.001f, 1000.f);
glClearColor(0.5f, 0.5f, 0.5f, 1);//set the clear color
glEnable(GL_DEPTH_TEST); // enables the depth buffer
return 0;
}
void Quit()
{
Gizmos::destroy();
// the rest of our code goes here!
glfwDestroyWindow(Window);
glfwTerminate();
}
}