// Include Config header generated by GNU autotools #include // Make sure Glew is loaded first #include #include #include #include // Not used yet #include #include // File reader #include #include #include // Shader #include "ShaderLoader.h" // Camera #include "PlayerCamera.h" #include "helpers/RootDir.h" // Objects #include "Mesh.h" #include "Model.h" #include // Include error class #include "Error.h" Error error("main"); int main(int argc, char **argv) { // Initialise SDL2 if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { error.crash("Unable to initialise SDL: ", SDL_GetError()); return 1; } // Make OpenGL use double buffering (Render game first then shove to output) SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // TODO: Understand what a depth buffer is SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); // TODO: Discover if this is necessary for linux and what values they need be // SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); // SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); // SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); // SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); // Create Window SDL_Window *window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); if (window == NULL) { error.crash("Unable to initialise SDL Window: ", SDL_GetError()); return 1; } // Create glContext SDL_GLContext glContext = SDL_GL_CreateContext(window); // TODO: Test that glContext was created successfully // Tell us the number of vertex attributes allowed in bug reports // int nrAttributes; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes); error.log("Maximum num of vertex attributes supported: " + std::to_string(nrAttributes)); // Initialise Glew if (glewInit() != GLEW_OK) { error.crash("Unable to initialise GLEW (OpenGL Extention Wrangler)"); } // Create event handling struct SDL_Event input; ShaderLoader shader(ROOT_DIR "data/shaders/vertex.glsl", ROOT_DIR "data/shaders/fragment.glsl"); // Load texture image // SDL_Surface *image = IMG_Load(ROOT_DIR "data/container.jpg"); // if (image == nullptr) { // error.crash("SDL2_image was unable to load a texture", IMG_GetError()); //} // create and bind texture object // unsigned int texture; // glGenTextures(1, &texture); //// glBindTexture(GL_TEXTURE_2D, texture); //// Handle different SDL Surface data types // int mode = GL_RGB; // if (image->format->BytesPerPixel == 4) { // mode = GL_RGBA; //} //// Generate texture and mipmap from image // glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode, // GL_UNSIGNED_BYTE, image->pixels); // glGenerateMipmap(GL_TEXTURE_2D); // remove image surface now it's no longer needed to create texture // SDL_FreeSurface(image); // image = nullptr; // Generate Mesh // Define some useful structures to be used in the mesh object // struct Vertex { // glm::vec3 Position; // glm::vec3 Normal; // glm::vec2 TexCoords; // }; // struct Texture { // unsigned int id; // std::string type; // }; float vertices[] = { // positions // texture Co-ords 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left }; // unsigned int indices[] = { // 0, 1, 3, // First triangle // 1, 2, 3 // Second triangle //}; // std::vector mvertices; // Vertex vertex; // vertex.Position = glm::vec3(0.5f, 0.5f, 0.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(1.0f, 1.0f); // mvertices.push_back(vertex); // vertex.Position = glm::vec3(0.5f, -0.5f, 0.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(1.0f, 0.0f); // mvertices.push_back(vertex); // vertex.Position = glm::vec3(-0.5f, -0.5f, 0.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(0.0f, 0.0f); // mvertices.push_back(vertex); // vertex.Position = glm::vec3(-0.5f, 0.5f, 0.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(0.0f, 1.0f); // mvertices.push_back(vertex); // std::vector mindices{ // 0, 3, 1, // First triangle // 1, 3, 2 // Second triangle //}; // std::vector mtextures; // Texture mtexture; // mtexture.id = texture; // mtexture.type = "texture_diffuse"; // Mesh mesh(mvertices, mindices, mtextures); // std::vector mvertices2; // vertex.Position = glm::vec3(0.5f, 0.5f, 1.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(1.0f, 1.0f); // mvertices2.push_back(vertex); // vertex.Position = glm::vec3(0.5f, -0.5f, 1.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(1.0f, 0.0f); // mvertices2.push_back(vertex); // vertex.Position = glm::vec3(-0.5f, -0.5f, 1.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(0.0f, 0.0f); // mvertices2.push_back(vertex); // vertex.Position = glm::vec3(-0.5f, 0.5f, 1.0f); // vertex.Normal = glm::vec3(); // vertex.TexCoords = glm::vec2(0.0f, 1.0f); // mvertices2.push_back(vertex); // Mesh mesh2(mvertices2, mindices, mtextures); // std::vector modelMeshes; // modelMeshes.push_back(mesh); // modelMeshes.push_back(mesh2); // Model model(modelMeshes); // Model model2(mesh); // model.translate(glm::vec3(1.0f, 0.0f, 0.0f)); Model backpack(ROOT_DIR "data/models/backpack/backpack.obj"); // Model cube(ROOT_DIR "data/models/cube/cube.obj"); // Mess with perspective // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 // unit <-> 100 units float width = 800; float height = 600; glm::mat4 Projection = glm::perspective( glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f); // Create player camera object PlayerCamera camera; // Enable Render order things don't render in front of the wrong objects glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); // glCullFace(GL_FRONT); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); // Game loop bool running = true; while (running) { // SDL Event handling loop while (SDL_PollEvent(&input) > 0) { // Handle SDL quit event if (input.type == SDL_QUIT) { running = false; } // TODO: Do something with keys lol }; camera.tick(); // Clear screen ready for next loop glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Make every shader/rendering call from this point on use our shader // glUseProgram(shaderProgram); // Send our glsl shader our mvp shader.setMat4("MVP", camera.getMVP()); // Draw Meshes // model2.draw(shader); // model.draw(shader); // cube.draw(shader); backpack.draw(shader); // Finally render everything shader.use(); // I think this is meant to be here but it breaks... // shove vertex array into buffer // glBindVertexArray(VAO); // TODO: Run game here lol SDL_GL_SwapWindow(window); }; // Escaped Game loop // On close also destroy window SDL_DestroyWindow(window); // Close all leftover SDL systems SDL_Quit(); return 0; }