-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInitialiseOpenGL.cpp
More file actions
179 lines (148 loc) · 4.13 KB
/
InitialiseOpenGL.cpp
File metadata and controls
179 lines (148 loc) · 4.13 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
#include "stdafx.h"
#include "OpenGLClass.h"
#include "System.h"
#include "gbuffer.h"
#define GL_DEBUG_MODE
void _stdcall debugCallbackARB(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar*, GLvoid*);
OpenGLClass::OpenGLClass()
{
m_hdc = 0;
m_hrc = 0;
m_pixelFormat = 0;
}
OpenGLClass::OpenGLClass(const OpenGLClass&) {}
int OpenGLClass::reloadPixelFormat(PIXELFORMATDESCRIPTOR& pfd)
{
using namespace std;
//shutdown(); //What?
m_pixelFormat = ChoosePixelFormat(m_hdc, &pfd);
if (!m_pixelFormat)
{
Error("Could not create pixel format.");
return -1;
}
fstream formatfile(PIXELFORMATPATH, ios::out | ios::binary | ios::trunc);
if (formatfile.fail())
{
Warning(string("Could not open ") + PIXELFORMATPATH + "\nFormat not saved.");
return 1;
}
formatfile.write((char*)&m_pixelFormat, sizeof(m_pixelFormat));
formatfile.close();
return 0;
}
bool OpenGLClass::loadPixelFormat()
{
using namespace std;
fstream formatfile(PIXELFORMATPATH, ios::in | ios::binary);
if (formatfile.fail())
{
Warning(string("Could not open ") + PIXELFORMATPATH + "\nFormat regenerated.");
return false;
}
formatfile.read((char*)&m_pixelFormat, sizeof(m_pixelFormat));
formatfile.close();
return true;
}
bool OpenGLClass::initialiseContext(bool vsync, bool reset)
{
//Create device context
m_hdc = GetDC(systemptr->hwnd());
PIXELFORMATDESCRIPTOR pfd = { 0 };
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | (vsync ? PFD_DOUBLEBUFFER : 0);
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
if (reset)
{
if (reloadPixelFormat( pfd) == -1)
return false;
}
else if (!loadPixelFormat())
{
if (reloadPixelFormat( pfd) == -1)
return false;
}
if (!SetPixelFormat(m_hdc, m_pixelFormat, &pfd))
return false;
m_hrc = wglCreateContext(m_hdc);
if (m_hrc == NULL)
{
Error(std::string("Error creating OpenGL Context.\nError code: ") + toString(GetLastError()));
return false;
}
if (!wglMakeCurrent(m_hdc, m_hrc))
{
Error(std::string("Error setting OpenGL Context.\nError code: ") + toString(GetLastError()));
return false;
}
return true;
}
bool OpenGLClass::initialiseFunctionality()
{
//init glew
{
glewExperimental = TRUE;
GLenum err = glewInit();
if (err != GLEW_OK)
{
Error(std::string("Could not initialise GLEW.\n") + (char*)glewGetErrorString(err));
return false;
}
}
return true;
}
void OpenGLClass::initialiseContextSettings()
{
glClearColor(0.f, 0.f, 0.0f, 1.f);
glEnable(GL_TEXTURE_2D);
glEnable(GL_FRAMEBUFFER_SRGB);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDepthFunc(GL_LESS);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#ifdef GL_DEBUG_MODE
glDebugMessageCallback(debugCallbackARB, stderr);
glEnable(GL_DEBUG_OUTPUT);
#endif
glClearDepth(1.f);
}
bool OpenGLClass::initialiseSceneAssets()
{
//if (!m_sky.load("Game/Models/SkyDome.obj", Enum::GPU))
// return false;
return true;
}
bool OpenGLClass::initialise(bool reset)
{
if (!initialiseContext(systemptr->displaySet.vsync, reset))
return false;
if (!initialiseFunctionality())
return false;
if (!gbuffer.initialise(systemptr->displaySet.screenWidth, systemptr->displaySet.screenHeight))
return false;
if (!shadowBuffer.initialise())
return false;
initialiseContextSettings();
initialiseSceneAssets();
glViewport(0, 0, systemptr->displaySet.screenWidth, systemptr->displaySet.screenHeight);
if (!Shaders.directionalShader.load("Shaders/DirectionalLight.vert", "Shaders/DirectionalLight.frag")) return false;
if (!Shaders.skyShader.load("Shaders/sky.vert", "Shaders/sky.frag")) return false;
unsigned err = glGetError();
if (err != 0) Error(std::string("Unable to initialise GL. Error code " + toString(err)));
return true;
}
void OpenGLClass::initialiseScreenQuad(Vertex2D* quad, GLuint& VBO, GLuint& VAO)
{
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex2D) * 6, quad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), NULL);
glBindVertexArray(0);
}