-
Notifications
You must be signed in to change notification settings - Fork 1
/
Application.cpp
173 lines (129 loc) · 4.22 KB
/
Application.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
#include "vkpch.h"
#include "Application.h"
#include "Log.h"
#include "Render/Shader.h"
#include "Render/Mesh.h"
#include "Msc/Common.h"
#include "Math/Math.h"
#include "Input/Input.h"
#include "Window/Window.h"
#include "Render/VertexBuffer.h"
#include "Render/IndexBuffer.h"
#include "Render/VertexArray.h"
#include "EngineScript.h"
#include "Time.h"
#include "GameObject.h"
#include "Transform.h"
#include "Script.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
const GLubyte GLFW_VER_MAJOR = 4;
const GLubyte GLFW_VER_MINOR = 6;
namespace Valk
{
Application::Application()
{
}
void Application::Run()
{
Log::Init();
// Initialize GLFW
glfwInit();
// Set window hints
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFW_VER_MAJOR);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFW_VER_MINOR);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
glfwWindowHint(GLFW_FOCUSED, GLFW_TRUE); // Ignored for fullscreen
glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE); // Ignored for windowed
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
glfwWindowHint(GLFW_SAMPLES, 4);
// Create a window (window must be created before GLAD is initialized)
Window window("Engine", 800, 600);
// Initialize the OpenGL loading library GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
//return -1;
}
Input input(&window);
window.PrintGLContext();
window.UpdateViewport();
// Shaders
Shader shader("./res/Shaders/shader.vert", "./res/Shaders/shader.frag");
static const GLfloat vertex_positions[]
{
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 1.0f
};
static const GLfloat vertex_colors[]
{
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f
};
static const GLushort vertex_indices[]
{
0, 1, 2
};
GLuint ebo[1];
GLuint vao[1];
GLuint vbo[1];
// Setup element array buffer
glGenBuffers(1, ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW);
// Setup vertex attributes
glGenVertexArrays(1, vao);
glBindVertexArray(vao[0]);
glGenBuffers(1, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions) + sizeof(vertex_colors), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex_positions), vertex_positions);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertex_positions), sizeof(vertex_colors), vertex_colors);
// Wireframe Mode
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
window.SetClearColor(0, 0, 0, 1);
glm::mat4 trans = glm::mat4(1.0f);
//trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));
trans = glm::scale(trans, glm::vec3(0.5f, 0.5, 0.5));
//trans = glm::translate(trans, glm::vec3(-3.0f, 0.0f, -5.0f));
window.SetVSync(true);
double lastFrameTime = 0.0f;
while (!window.Closed())
{
double currentTime = glfwGetTime();
double deltaTime = currentTime - lastFrameTime;
lastFrameTime = currentTime;
Time::deltaTime = deltaTime;
//std::cout << (deltaTime * 1000) << " ms" << std::endl;
//window.displayFPS();
if (Input::GetKeyDown(GLFW_KEY_ESCAPE))
glfwSetWindowShouldClose(window.GLFWwindow, true);
if (Input::GetKeyUp(GLFW_KEY_F11))
window.ToggleFullscreen();
if (Input::GetMouseButtonUp(GLFW_MOUSE_BUTTON_LEFT))
std::cout << "(" << Input::m_MousePosition.x << ", " << Input::m_MousePosition.y << ")" << std::endl;
auto timeValue = static_cast<float>(glfwGetTime());
auto pulse = (sin(timeValue) / 2.0f) + 0.5f;
//trans = glm::rotate(trans, glm::radians<float>(100.0f * pulse * (float)deltaTime), glm::vec3(0.0, 0.0, 1.0));
window.Clear();
shader.Use();
shader.SetColor("color", 1, pulse, 0, 1);
shader.SetMat4("transform", glm::value_ptr(trans));
//
glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL);
window.Update();
}
shader.Clean();
glfwTerminate();
}
Application::~Application()
{
}
}