int main() { Window window; Camera camera(window.GetAspectRatio(), glm::vec3(2.0, 2.0, 1.0)); Mesh* sphereMesh = CreateMesh("sphere.obj", "sphere"); Shader defaultShader("Shaders/defaultShader.vert", "Shaders/defaultShader.frag", "defaultShader"); SetupMesh(sphereMesh, defaultShader); do { window.Clear(); DrawMesh(*sphereMesh, camera, defaultShader); camera.front = sphereMesh->position; sphereMesh->rotY -= 0.016; { if (glfwGetKey(window.GetWindowInstance(), GLFW_KEY_W) == GLFW_PRESS) { sphereMesh->position.x -= 0.16; } else if (glfwGetKey(window.GetWindowInstance(), GLFW_KEY_S) == GLFW_PRESS) { sphereMesh->position.x += 0.16; } if (glfwGetKey(window.GetWindowInstance(), GLFW_KEY_A) == GLFW_PRESS) { sphereMesh->position.z += 0.16; } else if (glfwGetKey(window.GetWindowInstance(), GLFW_KEY_D) == GLFW_PRESS) { sphereMesh->position.z -= 0.16; } } window.Update(); } while (!window.Closed() && glfwGetKey(window.GetWindowInstance(), GLFW_KEY_ESCAPE) != GLFW_PRESS); delete sphereMesh; }
void Renderer::initialize() { std::string shaderPath( "../Shaders" ); std::string defaultShader( "Default" ); m_shaderManager = ShaderProgramManager::createInstance( shaderPath, defaultShader ); m_textureManager = TextureManager::createInstance(); initShaders(); initBuffers(); Core::Vector3Array mesh; mesh.push_back( { Scalar( -1 ), Scalar( -1 ), Scalar( 0 ) } ); mesh.push_back( { Scalar( -1 ), Scalar( 1 ), Scalar( 0 ) } ); mesh.push_back( { Scalar( 1 ), Scalar( 1 ), Scalar( 0 ) } ); mesh.push_back( { Scalar( 1 ), Scalar( -1 ), Scalar( 0 ) } ); std::vector<uint> indices( { 0, 1, 2, 0, 3, 2 } ); m_quadMesh.reset( new Mesh( "quad" ) ); m_quadMesh->loadGeometry( mesh, indices ); m_quadMesh->updateGL(); }
/*! * @brief Initilize everything SDL needs at the start */ void GLGraphics::Init(void) //Initilize SDL { // Register the components needed for graphics. RegisterComponent(MC_Transform); RegisterComponent(MC_Sprite); CreateMesh(); // Create the default shader. ShaderPtr defaultShader(new GLShader()); // Load the shader files for the default shader. defaultShader->LoadShaderFile("dvert.glsl", "dfrag.glsl", 0); // Compile the shaders. defaultShader->Compile(); // Find the variables in the shaders that will be modified by the end-users. defaultShader->FindUniforms("model"); defaultShader->FindUniforms("view"); defaultShader->FindUniforms("proj"); defaultShader->FindUniforms("color"); // Add the shader to the ShaderMap. addShader("Box", defaultShader); }
void ShaderLibrary::loadPreferences() { QString defaultShader(Preferences::DefaultShader()); if (defaultShader.isEmpty()) defaultShader = NoShader; QVariantMap defaultShaderParameters(Preferences::DefaultShaderParameters()); setUniformVariables(defaultShader, defaultShaderParameters); bindShader(defaultShader); }
// The MAIN function, from here we start the application and run the game loop int main() { // Init GLFW glfwInit(); GLFWmonitor* primary = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(primary); // Set all the required options for GLFW 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); glfwWindowHint(GLFW_SAMPLES, 4); // Create a GLFWwindow object that we can use for GLFW's functions GLFWwindow* window = glfwCreateWindow(mode->width, mode->height, "LearnOpenGL", primary, nullptr); glfwMakeContextCurrent(window); // Set the required callback functions glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); // Options glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions glewExperimental = GL_TRUE; // Initialize GLEW to setup the OpenGL Function pointers glewInit(); // Define the viewport dimensions glViewport(0, 0, mode->width, mode->height); // Setup some OpenGL options glEnable(GL_DEPTH_TEST); Scene scene = Scene("default"); // Build and compile our shader program Shader defaultShader("default.vs", "default.frag", "default.gs"); Shader alternateShader("alternate.vs", "alternate.frag", "alternate.gs"); Shader* currentShader = &defaultShader; Shader skyboxShader("skybox.vs", "skybox.frag"); #pragma region "object_initialization" GLfloat skyboxVertices[] = { // Positions -10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, -10.0f, -10.0f, 10.0f, -10.0f, -10.0f, 10.0f, 10.0f, -10.0f, -10.0f, 10.0f, -10.0f, -10.0f, -10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, -10.0f, -10.0f, 10.0f, -10.0f, -10.0f, 10.0f, 10.0f, -10.0f, -10.0f, 10.0f, 10.0f, -10.0f, -10.0f, 10.0f, -10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, -10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, 10.0f, -10.0f, -10.0f, 10.0f, -10.0f, 10.0f, -10.0f, 10.0f, 10.0f, -10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, 10.0f, 10.0f, -10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, 10.0f, -10.0f, -10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, 10.0f, -10.0f, 10.0f }; //wVertex data GLfloat vertices[] = { //positions //colors -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Top-left 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Top-right 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // Bottom-right -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f // Bottom-left }; /*GLfloat* floatVertices = scene.getVertices(); GLfloat vertices[sizeof(floatVertices)/sizeof(GLfloat)]; for (int i = 0; i < 24 ; i++) { vertices[i] = floatVertices[i]; }*/ glGenBuffers(1, &VBO); glGenVertexArrays(1, &VAO); //// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s). glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); //// Position attribute //setAttributeData(0, 3, 6, 0); //// Color attribute //setAttributeData(1, 3, 6, 3); // Position attribute std::vector<int> va = scene.getVecAtrib(); setAttributeData(va[0], va[1], va[2], va[3]); // Color attribute std::vector<int> ca = scene.getColAtrib(); setAttributeData(ca[0], ca[1], ca[2], ca[3]); // Unbind VAO glBindVertexArray(0); // Setup skybox VAO GLuint skyboxVAO, skyboxVBO; glGenVertexArrays(1, &skyboxVAO); glGenBuffers(1, &skyboxVBO); glBindVertexArray(skyboxVAO); glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); glBindVertexArray(0); #pragma endregion // Cubemap (Skybox) std::vector<const GLchar*> faces = scene.getFaces(); /*faces.push_back("Resources/skybox/right.jpg"); faces.push_back("Resources/skybox/left.jpg"); faces.push_back("Resources/skybox/top.jpg"); faces.push_back("Resources/skybox/bottom.jpg"); faces.push_back("Resources/skybox/back.jpg"); faces.push_back("Resources/skybox/front.jpg");*/ GLuint cubemapTexture = loadCubemap(faces); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Game loop while (!glfwWindowShouldClose(window)) { // Set frame time GLfloat currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions glfwPollEvents(); Input_Switch_Shader(¤tShader, &defaultShader, &alternateShader); Do_Movement(); // Check camera position and see if it reaches the boundary of the room // reset the camera to the other side of the room when it crosses the boundary glm::vec3 camPos = camera.getPostion(); if (abs(camPos.x) > 5.0f) { camPos.x = -1 * camPos.x; camera.setPosition(camPos); Switch_Shader(¤tShader, &defaultShader, &alternateShader); } if (abs(camPos.y) > 5.0f) { camPos.y = -1 * camPos.y; camera.setPosition(camPos); Switch_Shader(¤tShader, &defaultShader, &alternateShader); } if (abs(camPos.z) > 5.0f) { camPos.z = -1 * camPos.z; camera.setPosition(camPos); Switch_Shader(¤tShader, &defaultShader, &alternateShader); } // Render // Clear the colorbuffer glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw the scene currentShader->Use(); // Create camera transformation glm::mat4 view = camera.GetViewMatrix(); GLint viewLoc = glGetUniformLocation(currentShader->Program, "view"); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); // Calculate the model matrix for each object and pass it to shader before drawing glm::mat4 model; GLint modelLoc = glGetUniformLocation(currentShader->Program, "model"); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glm::mat4 projection = glm::perspective(camera.Zoom, (float)mode->width / (float)mode->height, 0.1f, 1000.0f); //currentShader->Use(); GLint projLoc = glGetUniformLocation(currentShader->Program, "projection"); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection)); glBindVertexArray(VAO); glDrawArrays(GL_POINTS, 0, 4); glBindVertexArray(0); //Draw skybox as last glDepthFunc(GL_LEQUAL); // Change depth function so depth test passes when values are equal to depth buffer's content skyboxShader.Use(); view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // Remove any translation component of the view matrix glUniformMatrix4fv(glGetUniformLocation(skyboxShader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(glGetUniformLocation(skyboxShader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); // skybox cube glBindVertexArray(skyboxVAO); glActiveTexture(GL_TEXTURE0); glUniform1i(glGetUniformLocation(skyboxShader.Program, "skybox"), 0); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); glDepthFunc(GL_LESS); // Set depth function back to default // Swap the screen buffers glfwSwapBuffers(window); } // Properly de-allocate all resources once they've outlived their purpose glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); // Terminate GLFW, clearing any resources allocated by GLFW. glfwTerminate(); return 0; }