-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
158 lines (121 loc) · 4.45 KB
/
main.cpp
File metadata and controls
158 lines (121 loc) · 4.45 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
//womble is a faggot
#include "ImRenderer.h"
#include <IMGUI\imgui.h>
#include <GLFW\glfw3.h>
#include <iostream>
static float mouseX = 0.0;
static float mouseY = 0.0;
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
mouseX = static_cast<float>(xpos);
mouseY = static_cast<float>(ypos);
}
GLFWwindow* createWindow(int width, int height, const char* title)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(width, height, "Immediate Renderer Test", nullptr, nullptr);
if (window == nullptr)
{
glfwTerminate();
}
glfwMakeContextCurrent(window);
if (!gladLoadGL())
{
glfwTerminate();
}
return window;
}
float lerp(float v0, float v1, float t) {
return (1 - t)*v0 + t*v1;
}
glm::vec2 ScreenToWorld(const glm::vec2& point, float width, float height, const glm::vec3& camera, const glm::mat4& projection, const glm::mat4& view)
{
// normalize mouse position from window pixel space to between -1, 1
GLfloat normMouse_x = (2.0f * point.x) / width - 1.0f;
GLfloat normMouse_y = 1.0f - (2.0f * point.y) / height;
glm::vec4 ray_clip = glm::vec4(glm::vec2(normMouse_x, normMouse_y), -1.0, 1.0);
glm::vec4 ray_eye = glm::inverse(projection) * ray_clip;
ray_eye = glm::vec4(ray_eye.x, ray_eye.y, -1.0, 0.0);
glm::vec4 ray_world = glm::inverse(view) * ray_eye;
ray_world = glm::normalize(ray_world);
float l = -(camera.z / ray_world.z);
return glm::vec2(camera.x + l * ray_world.x, camera.y + l * ray_world.y);
}
int main()
{
int shouldClose = 0;
int width = 800;
int height = 600;
float aspect = (float)width / (float)height;
GLFWwindow* window = createWindow(width, height, "Immediate Renderer Test");
glfwSwapInterval(1);
ImRenderer IR = ImRenderer(width, height, glm::perspective(glm::radians(90.0f), aspect, 0.1f, 100.0f));
glfwSetCursorPosCallback(window, cursor_position_callback);
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, -20.0f);
#define QUALITY 16
float vertices[12 * QUALITY];
unsigned int triangles[6 * QUALITY];
float angleStart = -45.0f;
float angleEnd = -angleStart;
float angleDelta = (angleEnd - angleStart) / QUALITY;
float angleCurrent = angleStart;
float angleNext = angleStart + angleDelta;
glm::vec3 pos_curr_min = glm::vec3();
glm::vec3 pos_curr_max = glm::vec3();
glm::vec3 pos_next_min = glm::vec3();
glm::vec3 pos_next_max = glm::vec3();
for (int i = 0; i < QUALITY; i++)
{
glm::vec3 sphere_current = glm::vec3(std::sin(glm::radians(angleCurrent)),
std::cos(glm::radians(angleCurrent)), 0);
glm::vec3 sphere_next = glm::vec3(std::sin(glm::radians(angleNext)),
std::cos(glm::radians(angleNext)), 0);
pos_curr_min = sphere_current * 4.0f;
pos_curr_max = sphere_current * 8.0f;
pos_next_min = sphere_next * 4.0f;
pos_next_max = sphere_next * 8.0f;
int a = 4 * i;
int b = 4 * i + 1;
int c = 4 * i + 2;
int d = 4 * i + 3;
vertices[12 * i + 0] = pos_curr_min.x;
vertices[12 * i + 1] = pos_curr_min.y;
vertices[12 * i + 2] = pos_curr_min.z;
vertices[12 * i + 3] = pos_curr_max.x;
vertices[12 * i + 4] = pos_curr_max.y;
vertices[12 * i + 5] = pos_curr_max.z;
vertices[12 * i + 6] = pos_next_max.x;
vertices[12 * i + 7] = pos_next_max.y;
vertices[12 * i + 8] = pos_next_max.z;
vertices[12 * i + 9] = pos_next_min.x;
vertices[12 * i + 10] = pos_next_min.y;
vertices[12 * i + 11] = pos_next_min.z;
triangles[6 * i] = a;
triangles[6 * i + 1] = b;
triangles[6 * i + 2] = c;
triangles[6 * i + 3] = c;
triangles[6 * i + 4] = d;
triangles[6 * i + 5] = a;
angleCurrent += angleDelta;
angleNext += angleDelta;
}
int shapeID = IR.addShape(vertices, triangles, 12 * QUALITY, 6 * QUALITY);
while (!shouldClose)
{
shouldClose = glfwWindowShouldClose(window);
glfwPollEvents();
IR.clear(GL_COLOR_BUFFER_BIT);
IR.background(0);
IR.setCamera(cameraPosition, glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
IR.drawShapeElements(shapeID, 6 * QUALITY, ImRenderer::DrawType::TRIANGLES);
IR.quad();
glfwSwapBuffers(window);
}
IR.cleanUp();
glfwTerminate();
return 0;
}