OpenGlController.cpp 8.05 KB
Newer Older
1
2
3
4
5
6
#include <gl\glew.h>
#include <gl\glfw3.h>
#include <Windows.h>
#include "OpenGLController.h"
#include "callback.h"

Anakin's avatar
Anakin committed
7
8
9
10
#include <glm\gtc\matrix_transform.hpp>
#include "shader.hpp"
#include "import.h"
#include "Texture.h"
11
#include "Object.h"
Anakin's avatar
Anakin committed
12
13
14

#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
15
//#define TEXTURE_NAME "Textures/texture32R.tga"
16
17
18
19

/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

Anakin's avatar
Anakin committed
20
OpenGLController* OpenGLController::getInstance(int oglMajor, int oglMinor)
21
{
Anakin's avatar
Anakin committed
22
	static OpenGLController *instace = new OpenGLController(oglMajor, oglMinor);
Anakin's avatar
Anakin committed
23
24
25
26
27
	return instace;
}

OpenGLController::~OpenGLController()
{
Anakin's avatar
Anakin committed
28
29
30
31
32
	glDeleteBuffers(1, &gluiUVBufferID);
	glDeleteBuffers(1, &gluiVertexBufferID);
	glDeleteVertexArrays(1, &gluiVertexArrayID);
	glDeleteProgram(gluiShaderPrgmID);

Anakin's avatar
Anakin committed
33
34
	glDeleteTextures(1, &gluiSamplerID);
	glfwTerminate();
35
36
}

Anakin's avatar
Anakin committed
37
38
39
40

/////////////////////////////////////////////////////////////////////////
// private constructor

Anakin's avatar
Anakin committed
41
OpenGLController::OpenGLController(int oglMajor, int oglMinor)
42
{
Anakin's avatar
Anakin committed
43
	// init variables
44
45
46
	initDefault();
	iOglMajorVersion = oglMajor;
	iOglMinorVersion = oglMinor;
Anakin's avatar
Anakin committed
47
48

	// run OGL
49
50
51
52
53
54
55
56
	processInit();
}

/////////////////////////////////////////////////////////////////////////
// private functions

void OpenGLController::initDefault()
{
Anakin's avatar
Anakin committed
57
	pWindow = NULL;
58
	sWindowName = "MeshViewer 2.0 pre-alpha";
Anakin's avatar
Anakin committed
59
60
61
62
63
64
65
66
67
	iWidth = 640;
	iHeight = 480;

	iAntiAliasingLevel = 4;

	gluiUVBufferID = 0;
	gluiTextureID = 0;
	gluiShaderPrgmID = 0;
	gluiSamplerID = 0;
68

Anakin's avatar
Anakin committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	gluiMatrixID = 0;

	fRotationX = 0;
	fRotationY = 0;
	fRotationZ = 0;
	dTranslationX = 0;
	dTranslationY = 0;
	dTranslationZ = 5;

	m4x4Model = glm::mat4(1.0f);
	m4x4View = glm::mat4(1.0f);
	m4x4Projection = glm::mat4(1.f);

	fFOV = 45.0f;
	fMinView = 0.1f;
	fMaxView = 100.0f;
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
}

void OpenGLController::processInit()
{
	startGLFW();
	createWindow();
	startGLEW();
	setCallbackFunctions();
	
	// set background color
	glClearColor(0.5000f, 0.8000f, 1.0000f, 0.0000f);

	// enable z-order
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

	// draw vertics only from one side
	glEnable(GL_CULL_FACE);
}

void OpenGLController::startGLFW()
{
	if (!glfwInit())
	{
		MessageBox(NULL, "Failed to initialize GLFW", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
		exit(0);
	}

	glfwWindowHint(GLFW_SAMPLES, iAntiAliasingLevel);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, iOglMajorVersion);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, iOglMinorVersion);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}

void OpenGLController::createWindow()
{
	pWindow = glfwCreateWindow(iWidth, iHeight, sWindowName.c_str(), NULL, NULL);

	if (pWindow == NULL)
	{
		std::string message = "Your GPU does not support OpenGL ";
		message += iOglMajorVersion;
		message += ".";
		message += iOglMinorVersion;
		message += "\nTry to use older version";

		MessageBox(NULL, message.c_str(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);

		glfwTerminate();
		exit(0);
	}

	glfwSetWindowUserPointer(pWindow, this);
	glfwMakeContextCurrent(pWindow);
}

Anakin's avatar
Anakin committed
141
142
143
144
145
146
147
148
149
150
151
152
void OpenGLController::startGLEW()
{
	glewExperimental = true;

	if (glewInit() != GLEW_OK)
	{
		MessageBox(NULL, "Failed to initialize GLEW", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
		glfwTerminate();
		exit(0);
	}
}

153
154
155
156
157
158
159
160
161
162
163
void OpenGLController::setCallbackFunctions()
{
	glfwSetMouseButtonCallback(pWindow, mouseButton);
	glfwSetCursorPosCallback(pWindow, mouseMove);
	glfwSetWindowSizeCallback(pWindow, windowResize);
	glfwSetScrollCallback(pWindow, mouseWheel);
	glfwSetKeyCallback(pWindow, keyPress);
}

glm::mat4 OpenGLController::getMVPMatrix()
{
Anakin's avatar
Anakin committed
164
165
166
167
168
169
170
171
172
173
174
175
	m4x4Projection = glm::perspective(fFOV, float(iWidth) / float(iHeight), fMinView, fMaxView);
	m4x4View = glm::lookAt(
		glm::vec3(dTranslationX, dTranslationY, dTranslationZ),
		glm::vec3(dTranslationX, dTranslationY, dTranslationZ - 1),
		glm::vec3(0, 1, 0)
	);
	m4x4Model = glm::mat4(1.0f);
	m4x4Model = glm::rotate(m4x4Model, fRotationX, glm::vec3(1, 0, 0));
	m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0));
	m4x4Model = glm::rotate(m4x4Model, fRotationZ, glm::vec3(0, 0, 1));

	return m4x4Projection * m4x4View * m4x4Model;
176
177
}

Anakin's avatar
Anakin committed
178
179
180
181

/////////////////////////////////////////////////////////////////////////
// public getter

182
183
184
185
186
187
188
189
190
191
192
GLFWwindow * OpenGLController::getWindow() const
{
	return pWindow;
}


/////////////////////////////////////////////////////////////////////////
// public functions

void OpenGLController::resize(int width, int height)
{
Anakin's avatar
Anakin committed
193
194
	iWidth = width;
	iHeight = height;
195
196
197
198
}

void OpenGLController::addRotX(float value)
{
Anakin's avatar
Anakin committed
199
	fRotationX += value;
200
201
202
203
}

void OpenGLController::addRotY(float value)
{
Anakin's avatar
Anakin committed
204
	fRotationY += value;
205
206
207
208
}

void OpenGLController::addTransX(double value)
{
Anakin's avatar
Anakin committed
209
	dTranslationX += value;
210
211
212
213
}

void OpenGLController::addTransY(double value)
{
Anakin's avatar
Anakin committed
214
	dTranslationY += value;
215
216
217
218
}

void OpenGLController::addTransZ(double value)
{
Anakin's avatar
Anakin committed
219
	dTranslationZ += value;
220
221
222
223
224
225
226
227
}

void OpenGLController::updateScene()
{
	// get new matrices
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// use shader prgm
Anakin's avatar
Anakin committed
228
	glUseProgram(gluiShaderPrgmID);
229
230

	// tell shader transformation
Anakin's avatar
Anakin committed
231
	glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix()[0][0]);
232
233
234

	// bind texture in texture unit 0
	glActiveTexture(GL_TEXTURE0);
Anakin's avatar
Anakin committed
235
	glBindTexture(GL_TEXTURE_2D, gluiTextureID);
236
237
238
239
240
	// tell sampler to use texture unit 0
	glUniform1i(gluiSamplerID, 0);

	// open attribute position
	glEnableVertexAttribArray(0);
Anakin's avatar
Anakin committed
241
	glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
242
243
244
245
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	// open attribute uv
	glEnableVertexAttribArray(1);
Anakin's avatar
Anakin committed
246
	glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
247
248
249
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
	
	//draw objects
Anakin's avatar
Anakin committed
250
	glDrawArrays(GL_TRIANGLES, 0, ui32MeshSize);
251
252
253
254
255

	//close attributes
	glDisableVertexAttribArray(0);
	glDisableVertexAttribArray(1);

Anakin's avatar
Anakin committed
256
257
258
259
260
261
262
	glfwSwapBuffers(pWindow);
	glfwPollEvents();

}

void OpenGLController::loadMsh(const char * path)
{
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
	// generate stuff
	glGenVertexArrays(1, &gluiVertexArrayID);
	glBindVertexArray(gluiVertexArrayID);

	glGenBuffers(1, &gluiVertexBufferID);
	glGenBuffers(1, &gluiUVBufferID);

	try
	{
		gluiShaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER);
	}
	catch (std::invalid_argument e)
	{
		MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
		exit(1);
	}

	gluiMatrixID = glGetUniformLocation(gluiShaderPrgmID, "MVP");
	gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler");

	// get data
284
	std::list<std::string> listTextures;
285
286
287
288
	try
	{
		Object obj(path);
		vfVertices = obj.getVertex();
Anakin's avatar
Anakin committed
289
		vfUV = obj.getUV();
290
		listTextures = obj.getTexture();
291
		ui32MeshSize = obj.getSize().front();
292
293
294
295
296
297
298
	}
	catch (std::invalid_argument e)
	{
		MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
		exit(1);
	}

299
300
	glGenTextures(1, &gluiTextureID);
	glBindTexture(GL_TEXTURE_2D, gluiTextureID);
Anakin's avatar
Anakin committed
301

302
303
304
305
306
	try
	{
		if (listTextures.empty())
			throw std::invalid_argument("no texture names");

307
308
309
310
311
312
		std::string tempPath = path;

		while (tempPath.back() != '/' && tempPath.back() != '\\')
			tempPath.pop_back();

		TextureTGA tempTex(std::string(tempPath + listTextures.front()).c_str());
313
314
315
316
		glTexImage2D(GL_TEXTURE_2D, 0, tempTex.hasAlpha() ? GL_RGBA : GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, tempTex.hasAlpha() ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data());
	}
	catch (std::invalid_argument e)
	{
Anakin's avatar
Anakin committed
317
		GLubyte solidColor[4] = { 255, 0, 0, 255};
318
319
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)solidColor);
	}
Anakin's avatar
Anakin committed
320

321
322
323
324
325
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glGenerateMipmap(GL_TEXTURE_2D);
Anakin's avatar
Anakin committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

	glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
	glBufferData(
		GL_ARRAY_BUFFER,
		sizeof(vfVertices) * vfVertices.size(),
		vfVertices.data(),
		GL_STATIC_DRAW
	);

	glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
	glBufferData(
		GL_ARRAY_BUFFER,
		sizeof(vfUV) * vfUV.size(),
		vfUV.data(),
		GL_STATIC_DRAW
	);
Anakin's avatar
Anakin committed
342

343
}