//Program entry point. //argc is a count of the number of arguments (including the filename of the program). //argv is a pointer to each c-style string. int main(int argc, char **argv) { atexit(cleanup); cout << "Computer Graphics Assignment 1 Demo Program" << endl; if (argc > 1) { geom.LoadFile(argv[1]); } else { cerr << "Usage:" << endl; cerr << argv[0] << " <filename> " << endl; exit(1); } geom.CalculateNormals(); //initialise OpenGL GLFWwindow* window = initialiseGL(); //create framebuffers glGenFramebuffers(1, &frameBuffer); glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); renderTarget = setupTextureGL(NULL, 800, 600, GL_RGBA); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTarget, 0); //depth/stencil buffer glGenTextures(1, &depthStencil); glBindTexture(GL_TEXTURE_2D, depthStencil); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 800, 600, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthStencil, 0); GLenum err = GL_NO_ERROR; while ((err = glGetError()) != GL_NO_ERROR) { printf("%d\n", err); } //load and compile the shaders GLuint objectVertexShader = loadVertexShaderGL("vertex.glsl"); GLuint objectFragmentShader = loadFragmentShaderGL("fragment.glsl"); objectShaderProgram = makeShaderProgramGL(objectVertexShader, objectFragmentShader); GLuint screenVertexShader = loadVertexShaderGL("postvertex.glsl"); GLuint screenFragmentShader = loadFragmentShaderGL("postfragment.glsl"); screenShaderProgram = makeShaderProgramGL(screenVertexShader, screenFragmentShader); //transfer the geometry to the GPU glGenVertexArrays(1, &objectVertexArray); glBindVertexArray(objectVertexArray); //create buffers we will use glGenBuffers(1, &objectVertexBuffer); glGenBuffers(1, &objectElementBuffer); glGenBuffers(1, &objectNormBuffer); //get geometry vertices, normals and edge lists vector <glm::vec3> vert = geom.Vertices(); vector <glm::vec3> norm = geom.Norms(); //vector <glm::vec2> uv=geom.UVs(); vector <Triangle> tri = geom.Triangles(); //transfer to GPU //data for each vertex glBindBuffer(GL_ARRAY_BUFFER, objectVertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * vert.size(), &vert[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, objectNormBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * norm.size(), &norm[0], GL_STATIC_DRAW); //element array (triangles) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, objectElementBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, tri.size() * 3 * sizeof(unsigned int), &tri[0], GL_STATIC_DRAW); //setup for postprocessing glGenVertexArrays(1, &screenVertexArray); glBindVertexArray(screenVertexArray); //create buffers we will use glGenBuffers(1, &screenVertexBuffer); glGenBuffers(1, &screenElementBuffer); //data for each vertex glBindBuffer(GL_ARRAY_BUFFER, screenVertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 4, &screenVertex[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screenElementBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2 * 3 * sizeof(GLuint), &screenTri[0], GL_STATIC_DRAW); //load the texture readTexture(); texture = setupTextureGL(tex, 256, 256, GL_RGB); //main loop while (!glfwWindowShouldClose(window)) { DemoDisplay(); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); }
int main(int argc, char **argv) { atexit(cleanup); // parse arguments char *model_path = NULL; char *vertexshader_path = NULL; char *fragmentshader_path = NULL; char *texture_path = NULL; bool use_smoothed_normals; if (argc >= 6) { texture_path = argv[5]; useTexture = 1; } if (argc >= 5) { model_path = argv[1]; vertexshader_path = argv[2]; fragmentshader_path = argv[3]; use_smoothed_normals = *argv[4] != '0'; } else { std::cerr << "Usage:" << std::endl; std::cerr << argv[0] << " <model::path> " << " <vertex-shader::path> " << " <fragment-shader::path> " << " <smooth-normals::{0,1}>" << " (<texture::path>)" << std::endl; exit(1); } // initialise OpenGL glutInit(&argc, argv); glutInitWindowSize(windowX, windowY); glutCreateWindow("CG-CW1"); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glEnable(GL_DEPTH_TEST); // set display and keyboard callbacks glutDisplayFunc(display_handler); glutKeyboardFunc(keyboard_handler); // initialise the OpenGL Extension Wrangler library for VBOs GLenum err = glewInit(); if (err != GLEW_OK){ std::cerr << "Error!" << std::endl; exit(1); } if (!GLEW_VERSION_2_1) { std::cerr << "Error 2.1!" << std::endl; exit(1); } // create shader, prepare data for OpenGL trig.LoadFile(model_path); shader.Init(vertexshader_path, fragmentshader_path); setup_texture(texture_path, &textureID); setup_vertex_position_buffer_object(); setup_vertex_uv_buffer_object(); setup_vertex_normal_buffer_object(use_smoothed_normals); // set up camera and object transformation matrices projectionMatrix = get_default_projectionMatrix(); viewMatrix = get_default_viewMatrix(); modelMatrix = get_default_modelMatrix(); normalMatrix = get_default_normalMatrix(); glutMainLoop(); }