#include "TestTriangle.h" TestTriangle::TestTriangle(Archimedes::App* a, void* h) : Archimedes::Module(a, h) { name = "TestTriangle"; } TestTriangle::~TestTriangle() { if(app) { app->unregisterEvent(Archimedes::ResizeWindowEvent()); app->unregisterEvent(Archimedes::CloseWindowEvent()); app->unregisterEvent(Archimedes::KeyPressedWindowEvent()); app->unregisterEvent(Archimedes::KeyReleasedWindowEvent()); app->unregisterEvent(Archimedes::MouseButtonPressedWindowEvent()); app->unregisterEvent(Archimedes::MouseButtonReleasedWindowEvent()); app->unregisterEvent(Archimedes::ScrollWindowEvent()); app->unregisterEvent(Archimedes::MouseMovedWindowEvent()); app->unregisterEvent(Archimedes::FocusedWindowEvent()); app->unregisterEvent(Archimedes::FocusLostWindowEvent()); app->unregisterEvent(Archimedes::MovedWindowEvent()); glfwTerminate(); } } void TestTriangle::onLoad() { ////////////////////////////register events app->registerEvent(Archimedes::ResizeWindowEvent()); app->registerEvent(Archimedes::CloseWindowEvent()); app->registerEvent(Archimedes::KeyPressedWindowEvent()); app->registerEvent(Archimedes::KeyReleasedWindowEvent()); app->registerEvent(Archimedes::MouseButtonPressedWindowEvent()); app->registerEvent(Archimedes::MouseButtonReleasedWindowEvent()); app->registerEvent(Archimedes::ScrollWindowEvent()); app->registerEvent(Archimedes::MouseMovedWindowEvent()); app->registerEvent(Archimedes::FocusedWindowEvent()); app->registerEvent(Archimedes::FocusLostWindowEvent()); app->registerEvent(Archimedes::MovedWindowEvent()); //////////////////////glfw std::cout << "events registered" << std::endl; data.window = nullptr; data.sendEvent = [this](Archimedes::Event* e) { app->emitEvent(e); }; glfwSetErrorCallback([](int e, const char* m){ std::cout << "GLFW Error " << e << ": " << m << std::endl; }); if(!glfwInit()) { std::cout << "glfwInit failed!\n"; std::abort(); } #if RENDRER == 1 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif w = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL); if(!w) { std::cout << "glfwCreateWindow failed!\n"; glfwTerminate(); std::abort(); } glfwMakeContextCurrent(w); glfwSwapInterval(1); glfwSetWindowUserPointer(w, &data); glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){ Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new Archimedes::ResizeWindowEvent(w, h)); }); glfwSetWindowCloseCallback(w, [](GLFWwindow* window){ Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new Archimedes::CloseWindowEvent(d.window)); }); glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){ Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window); switch(action) { case GLFW_PRESS: d.sendEvent(new Archimedes::KeyPressedWindowEvent(key, 0)); break; case GLFW_RELEASE: d.sendEvent(new Archimedes::KeyReleasedWindowEvent(key)); break; case GLFW_REPEAT: d.sendEvent(new Archimedes::KeyPressedWindowEvent(key, 1)); break; } }); glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){ Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window); switch(action) { case GLFW_PRESS: d.sendEvent(new Archimedes::MouseButtonPressedWindowEvent(button)); break; case GLFW_RELEASE: d.sendEvent(new Archimedes::MouseButtonReleasedWindowEvent(button)); break; } }); glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){ Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new Archimedes::ScrollWindowEvent(dx, dy)); }); glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){ Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window); d.sendEvent(new Archimedes::MouseMovedWindowEvent(dx, dy)); }); //////////////////////gl std::cout << "glfw setup" << std::endl; if(glewInit() != GLEW_OK) { std::cout << "glew is not ok!" << std::endl; std::abort(); } ///////////////////////////////NUCLEAR // build and compile our shader program // ------------------------------------ // vertex shader unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); // check for shader compile errors int success; char infoLog[512]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; } // fragment shader unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); // check for shader compile errors glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; } // link shaders shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); // check for linking errors glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glBindBuffer(GL_ARRAY_BUFFER, 0); // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary. glBindVertexArray(0); // uncomment this call to draw in wireframe polygons. //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } void TestTriangle::run() { // render // ------ glClearColor(0.2f, 0.2f, 0.4f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // draw our first triangle glUseProgram(shaderProgram); glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); // no need to unbind it every time // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // ------------------------------------------------------------------------------- glfwSwapBuffers(w); glfwPollEvents(); } bool TestTriangle::onEvent(const Archimedes::Event& e) { unsigned int type = app->getEventType(e); if(type == app->getEventType(Archimedes::ResizeWindowEvent())) { Archimedes::ResizeWindowEvent& event = (Archimedes::ResizeWindowEvent&) e; glViewport(0, 0, event.width, event.height); return true; } else if(type == app->getEventType(Archimedes::CloseWindowEvent())) { app->emitEvent(new Archimedes::DoUnloadModuleEvent(name)); return true; } else if(type == app->getEventType(Archimedes::KeyPressedWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::KeyReleasedWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::MouseButtonPressedWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::MouseButtonReleasedWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::ScrollWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::MouseMovedWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::FocusedWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::FocusLostWindowEvent())) { return true; } else if(type == app->getEventType(Archimedes::MovedWindowEvent())) { return true; } return false; }