9 constexpr GLfloat vtx_coords[] = {
19 Application::Application()
24 , ctx(window.CreateContext())
27 , pitch_sensitivity(-0.0025f)
28 , yaw_sensitivity(-0.001f)
34 GLContext::EnableVSync();
38 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
41 "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
42 "gl_Position = MVP * v;\n"
50 "color = vec3(1, 1, 1);\n"
54 if (!program.Linked()) {
55 program.Log(std::cerr);
56 throw std::runtime_error("link program");
60 glGenVertexArrays(1, &VertexArrayID);
61 glBindVertexArray(VertexArrayID);
63 glGenBuffers(1, &vtx_buf);
64 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
65 glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
67 model.Position(glm::vec3(0, 0, -4));
68 cam.Position(glm::vec3(0, 0, 4));
70 mvp_handle = program.UniformLocation("MVP");
72 glClearColor(0.0, 0.0, 0.0, 1.0);
75 Application::~Application() {
80 void Application::Run() {
82 Uint32 last = SDL_GetTicks();
85 Uint32 now = SDL_GetTicks();
86 int delta = now - last;
92 void Application::Loop(int dt) {
98 void Application::HandleEvents() {
100 while (SDL_PollEvent(&event)) {
101 switch (event.type) {
102 case SDL_MOUSEMOTION:
103 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
104 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
105 std::cout << "x: " << event.motion.xrel << ", y: " << event.motion.yrel
106 << ", pitch: " << cam.Pitch() << ", yaw: " << cam.Yaw() << std::endl;
111 case SDL_WINDOWEVENT:
112 switch (event.window.event) {
113 case SDL_WINDOWEVENT_RESIZED:
114 cam.Viewport(event.window.data1, event.window.data2);
126 void Application::Render() {
127 glClear(GL_COLOR_BUFFER_BIT);
131 glm::mat4 mvp(cam.MakeMVP(model.Transform()));
132 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
134 glEnableVertexAttribArray(0);
135 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
136 glVertexAttribPointer(
137 0, // attribute 0 (for shader)
140 GL_FALSE, // normalized
149 glDisableVertexAttribArray(0);