-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.cpp
157 lines (137 loc) · 4.19 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
#define GLEW_STATIC
#include <GL/glew.h>
#include "GLFW/glfw3.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Graphics.h"
#include "ShaderProgram.h"
#include "Model.h"
#include <iostream>
using namespace std;
using glm::vec3;
using glm::vec4;
void Graphics::DirtyInitialize()
{
}
void Graphics::DrawScene()
{
Camera* cam = scene->GetSceneCamera();
Light* mainLight = scene->GetSceneLight();
vector<Drawable*> sceneActors = scene->GetSceneActors();
float aspectratio = (float)frameBufferSize.x / frameBufferSize.y;
mat4 view = cam->GetViewMatrix();
vec4 lightPosition = mainLight->GetLightPosition();
vec3 lightIntensity = vec3(2.0f);
vec3 cameraPosition = cam->Position();
mat4 projection = cam->GetProjectionMatrix(aspectratio);
sceneInfo.view = view;
sceneInfo.projection = projection;
sceneInfo.lightPosition = lightPosition;
sceneInfo.lightIntensity = vec3(1.0f);
sceneInfo.cameraPosition = cameraPosition;
for (vector<Drawable*>::iterator it = sceneActors.begin(); it != sceneActors.end(); it++)
{
Drawable* currentActor = *it;
currentActor->Draw(sceneInfo);
}
}
void Graphics::DirtyRender()
{
DrawScene();
//glUseProgram(0);
CheckForErrors();
}
Graphics::Graphics()
{
}
Graphics::~Graphics()
{
}
void Graphics::Render()
{
glClearColor(0, 0, 0, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DirtyRender();
}
void Graphics::Initialize(unsigned int width, unsigned int height, Scene* scene)
{
PrintContextInfo();
Resize(width, height);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glfwWindowHint(GLFW_ALPHA_BITS, GL_TRUE);
if (scene == nullptr)
{
cout << "Error in Graphics::Initialize(int, int, Scene*): can't initialize with NULL scene!\n";
return;
}
this->scene = scene;
glEnable(GL_MULTISAMPLE);
DirtyInitialize();
CheckForErrors();
}
void Graphics::Resize(unsigned int width, unsigned int height)
{
frameBufferSize = glm::uvec2(width, height);
glViewport(0, 0, width, height);
}
void Graphics::CheckForErrors()
{
for (GLenum error; (error = glGetError()) != GL_NO_ERROR; )
{
cout << "OpenGL Error: \t";
if (error == GL_INVALID_ENUM)
cout << "GL_INVALID_ENUM";
if (error == GL_INVALID_VALUE)
cout << "GL_INVALID_VALUE";
if (error == GL_INVALID_OPERATION)
cout << "GL_INVALID_OPERATION";
if (error == GL_STACK_OVERFLOW)
cout << "GL_STACK_OVERFLOW";
if (error == GL_STACK_UNDERFLOW)
cout << "GL_STACK_UNDERFLOW";
if (error == GL_OUT_OF_MEMORY)
cout << "GL_OUT_OF_MEMORY";
if (error == GL_INVALID_FRAMEBUFFER_OPERATION)
cout << "GL_INVALID_FRAMEBUFFER_OPERATION";
if (error == GL_CONTEXT_LOST)
cout << "GL_CONTEXT_LOST";
cout << "\n";
std::cin.get();
}
}
GLContextInfo Graphics::GetContextInfo()
{
GLContextInfo infos;
glGetIntegerv(GL_MAJOR_VERSION, &infos.Version.Major);
glGetIntegerv(GL_MINOR_VERSION, &infos.Version.Minor);
infos.Version.Driver = (const char*)glGetString(GL_VERSION);
infos.Version.ShadingLanguage = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
infos.Vendor = (const char*)glGetString(GL_VENDOR);
infos.Renderer = (const char*)glGetString(GL_RENDERER);
GLint extensionCount = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &extensionCount);
for (int i = 0; i < extensionCount; i++)
{
infos.SupportedExtensions.push_back((const char*)glGetStringi(GL_EXTENSIONS, i));
}
GLint supportedGLSLCount = 0;
glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &supportedGLSLCount);
for (int i = 0; i < supportedGLSLCount; i++)
{
infos.SupportedGLSLVersions.push_back((const char*)glGetStringi(GL_SHADING_LANGUAGE_VERSION, i));
}
return infos;
}
void Graphics::PrintContextInfo()
{
GLContextInfo infos = Graphics::GetContextInfo();
cout << "OpenGL version: " << infos.Version.Major << "." << infos.Version.Minor << "\n";
cout << "GLSL version: " << infos.Version.ShadingLanguage << "\n";
cout << "Renderer: " << infos.Renderer << "\n";
cout << "Vendor: " << infos.Vendor << "\n";
cout << "Driver version: " << infos.Version.Driver << "\n";
}