-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
178 lines (145 loc) · 4.27 KB
/
Copy pathmain.cpp
File metadata and controls
178 lines (145 loc) · 4.27 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
#include <iostream>
#include <thread>
#include <chrono>
#include <cmath>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "World.hpp"
#include "Model.hpp"
#include "Light.hpp"
#include "Shader.hpp"
#include "Particle.hpp"
#define N 8 //MAA 64 in 1050
//#include "./CUDA/kernel.hpp"
const uint32_t WINDOW_HEIGHT = 1080; //1080
const uint32_t WINDOW_WIDTH = 1920; //1920
// const uint32_t WINDOW_HEIGHT = 600;
// const uint32_t WINDOW_WIDTH = 800;
const float WINDOW_FPS = 60;
const char *WINDOW_TITLE = "OpenGL Renderer";
World *world;
static float gCurerntTime = 0;
static float gDeltaTime = 0;
static float gLastTime = 0;
static float gCurrentFPS = 0;
using namespace std;
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
world->cam->resize(width, height);
}
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
world->cam->updateDolly(yoffset);
}
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
if (action == GLFW_PRESS)
{
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
world->cam->startRotation(xpos, ypos);
}
else if (action == GLFW_RELEASE)
{
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
world->cam->endRotation(xpos, ypos);
}
}
}
void cursor_position_callback(GLFWwindow *window, double xpos, double ypos)
{
world->cam->updateMousePos(xpos, ypos);
}
int main()
{
//cudaFunc();
// GLEW, GLFW init...
if (!glfwInit())
{
cerr << "Failed to initialize GLFW" << endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);
if (window == NULL)
{
cerr << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetCursorPos(window, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// callback here
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
// glfwSetKeyCallback(window, key_callback);
glewExperimental = true; // Needed in core profile
if (glewInit() != GLEW_OK)
{
cerr << "Failed to initialize GLEW" << endl;
return -1;
}
cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
glClearColor(156 / 255.0f, 167 / 255.0f, 186 / 255.0f, 1);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_LESS);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
//glDisable(GL_COLOR_MATERIAL);
glLoadIdentity();
// Init end.
ParticleSystem* ps = new ParticleSystem{};
vector<Particle> particles;
float distanceFromOrigin = 10;
for (int i = distanceFromOrigin; i< N+distanceFromOrigin; i++) {
for (int j = distanceFromOrigin; j<N+distanceFromOrigin; j++) {
for (int k = distanceFromOrigin ; k < N+distanceFromOrigin; k++) {
particles.push_back(Particle{glm::vec3(i,j,k)});
}
}
}
ps->initParticles(particles);
Shader *shader = new Shader{"vert.glsl", "frag.glsl"};
world = new World{};
world->worldShader.push_back(shader);
world->particleSystem = ps;
world->initPhysics();
gCurerntTime = glfwGetTime();
gDeltaTime = gCurerntTime - gLastTime;
gLastTime = gCurerntTime;
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
world->updatePyhsics(gDeltaTime);
world->drawModel();
world->drawParticle();
glfwSwapBuffers(window);
glfwPollEvents();
// frame control
gCurerntTime = glfwGetTime();
gDeltaTime = gCurerntTime - gLastTime;
gLastTime = gCurerntTime;
if (gDeltaTime <= (1 / WINDOW_FPS))
{
this_thread::sleep_for(chrono::duration<float>(1 / WINDOW_FPS));
}
gCurrentFPS = 1 / gDeltaTime;
//cout << "Current FPS : " << gCurrentFPS << endl;
}
glfwTerminate();
return 0;
}