9 constexpr GLfloat vtx_coords[] = {
19 Application::Application()
24 , ctx(window.CreateContext())
27 , move_velocity(0.001f)
28 , pitch_sensitivity(-0.0025f)
29 , yaw_sensitivity(-0.001f)
41 GLContext::EnableVSync();
45 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
48 "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
49 "gl_Position = MVP * v;\n"
57 "color = vec3(1, 1, 1);\n"
61 if (!program.Linked()) {
62 program.Log(std::cerr);
63 throw std::runtime_error("link program");
67 glGenVertexArrays(1, &VertexArrayID);
68 glBindVertexArray(VertexArrayID);
70 glGenBuffers(1, &vtx_buf);
71 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
72 glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
74 model.Position(glm::vec3(0, 0, -4));
75 cam.Position(glm::vec3(0, 0, 4));
77 mvp_handle = program.UniformLocation("MVP");
79 glClearColor(0.0, 0.0, 0.0, 1.0);
82 Application::~Application() {
87 void Application::Run() {
89 Uint32 last = SDL_GetTicks();
92 Uint32 now = SDL_GetTicks();
93 int delta = now - last;
99 void Application::Loop(int dt) {
106 void Application::HandleEvents() {
108 while (SDL_PollEvent(&event)) {
109 switch (event.type) {
112 switch (event.key.keysym.sym) {
114 front = event.key.state == SDL_PRESSED;
117 back = event.key.state == SDL_PRESSED;
120 left = event.key.state == SDL_PRESSED;
123 right = event.key.state == SDL_PRESSED;
126 up = event.key.state == SDL_PRESSED;
129 down = event.key.state == SDL_PRESSED;
133 case SDL_MOUSEMOTION:
134 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
135 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
140 case SDL_WINDOWEVENT:
141 switch (event.window.event) {
142 case SDL_WINDOWEVENT_RESIZED:
143 cam.Viewport(event.window.data1, event.window.data2);
155 void Application::Update(int dt) {
157 if (right && !left) {
158 vel.x = move_velocity;
159 } else if (left && !right) {
160 vel.x = -move_velocity;
163 vel.y = move_velocity;
164 } else if (down && !up) {
165 vel.y = -move_velocity;
167 if (back && !front) {
168 vel.z = move_velocity;
169 } else if (front && !back) {
170 vel.z = -move_velocity;
178 void Application::Render() {
179 glClear(GL_COLOR_BUFFER_BIT);
183 glm::mat4 mvp(cam.MakeMVP(model.Transform()));
184 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
186 glEnableVertexAttribArray(0);
187 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
188 glVertexAttribPointer(
189 0, // attribute 0 (for shader)
192 GL_FALSE, // normalized
201 glDisableVertexAttribArray(0);