int main() { srand(time(NULL)); aspect::GLWindow window(XRES, YRES); glfwMakeContextCurrent(window.get_window()); glfwSetKeyCallback(window.get_window(), key_callback); glfwSetInputMode(window.get_window(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetCursorPos(window.get_window(), XRES/2, YRES/2); gs.camera.set_position(glm::vec3(+0.0f, +10.0f, +0.0f)); double currentframe = glfwGetTime(); double lastframe = currentframe; aspect::GLProgram monkey_program("shaders/vshader-nm.glsl", "shaders/fshader-nm.glsl"); aspect::Mesh monkey_mesh("models/monkey.dae"); aspect::ModelAsset monkey_asset(&monkey_mesh, &monkey_program); aspect::ModelInstance monkey(&monkey_asset); monkey.offset_position(glm::vec3(40, 6, 60)); monkey.update(); gs.model_instances.push_back(&monkey); aspect::GLProgram chunk_program("shaders/vshader-tex-light.glsl", "shaders/fshader-tex-light.glsl"); for(int x = 0; x < 16; x++) { for(int y = 0; y < 1; y++) { for(int z = 0; z < 16; z++) { aspect::CubeChunk *chunk = new aspect::CubeChunk(&chunk_program, x*CCOUNTX, y*CCOUNTY, z*CCOUNTZ); chunk->update(); gs.cube_chunks.push_back(chunk); } } } glClearColor(0.20, 0.6, 0.7, 0.00); while(!glfwWindowShouldClose(window.get_window())) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for_each(begin(gs.model_instances), end(gs.model_instances), [](aspect::ModelInstance *instance) { instance->draw(gs.camera.matrix()); walk(instance); }); for_each(begin(gs.cube_chunks), end(gs.cube_chunks), [](aspect::CubeChunk *chunk) { chunk->draw(gs.camera.matrix()); }); handle_mouse(window.get_window()); update_fps_counter(window.get_window()); window.update_fps_counter(); glfwSwapBuffers(window.get_window()); glfwPollEvents(); currentframe = glfwGetTime(); gs.delta_time = currentframe - lastframe; lastframe = currentframe; } return 0; }
void FPSCounter::draw() { update_fps_counter(); if (odd_frame) { Fonts::pingus_small.render(origin_center, Display::get_width()/2, 34, fps_string, Display::get_framebuffer()); odd_frame = false; } else { Fonts::pingus_small.render(origin_center, Display::get_width()/2, 34, "+ " + fps_string + " +", Display::get_framebuffer()); odd_frame = true; } }
void FPSCounter::on_event() { update_fps_counter(); if (odd_frame) { font.draw(origin_center, Display::get_width()/2, 34, fps_string); odd_frame = false; } else { font.draw(origin_center, Display::get_width()/2, 34, "+ " + std::string(fps_string) + " +"); odd_frame = true; } }
int main() { glfwSetErrorCallback(glfw_error_callback); // start gl context and os windows if (!glfwInit()) { fprintf(stderr, "Error: could not init glfw.\n"); return 0; } // renc: if these lines are enable, it change from 4.4 to 3.2 // renc: so i just disable these lines. // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow *window = glfwCreateWindow( g_iWindowWidth, g_iWindowHeight, "Hello Triangle", NULL, NULL); if (!window) { fprintf(stderr, "Error: could not open window with glfw.\n"); return 0; } glfwMakeContextCurrent(window); glfwSetWindowSizeCallback(window, glfw_window_size_callback); // glewExperimental = GL_TRUE; glewInit(); const GLubyte *renderer = glGetString(GL_RENDERER); const GLubyte *version = glGetString(GL_VERSION); printf("Renderer: %s.\n", renderer); printf("OpenGL version supproted %s.\n", version); // tell gl to only draw onto a pixel // if the shape is closer to the viewer glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as closer // geometry data unsigned int iPointCount = 3; unsigned int iVtxDim = 3; // vec3f float *points = NULL; points = build_triangle(); //unsigned int iPointDataSize = sizeof(points); iPointDataSize; //if (iPointDataSize != sizeof(float) * 9) //{ // fprintf(stderr, "Error: point data size.\n"); // return 1; //}; GLuint vbo = 0; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*iPointCount*iVtxDim, points, GL_STATIC_DRAW); // Most meshes will use a collection of one or more vertex buffer // objects to hold vertex points, tex-coordinates, vertex normals, // ectc. In older GL implementions we would have to bind each one, // and define their memory layout, EVERY TIME THAT WE DRAW THE MESH. // To simplify that, we have new thing called the vertex attribute // object (VAO), which remembers all of the vertex buffers that you // want to use, and memory layer of each one. We set up the vertex // array object once per mesh. When we want to draw, all we do then // is bind the VAO and draw. // // unsigned int iPosAttrLoc = 0; GLuint vao = 0; glGenVertexArrays(1, &vao); glBindVertexArray(vao); glEnableVertexAttribArray(iPosAttrLoc);// attr location 0 glBindBuffer(GL_ARRAY_BUFFER, vbo); // vbo is bind to attr loc 0. glVertexAttribPointer(iPosAttrLoc, iVtxDim, GL_FLOAT, GL_FALSE, 0, NULL); // bindBuffer must before vtxAttrPointer which describe the layout // of each buffer(vbo) to the vao. // renc: when we define vbo, we use bufferData to transfer data, // renc: here we need to tell data layout to vao. /// const char* vertex_shader = "#version 400\n" "in vec3 vp;" "void main() {" " gl_Position = vec4(vp, 1.0);" "}"; const char* fragment_shader = "#version 400\n" "out vec4 frag_colour;" "void main() {" " frag_colour = vec4(0.5, 0.0, 0.5, 1.0);" "}"; GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vertex_shader, NULL); glCompileShader(vs); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, &fragment_shader, NULL); glCompileShader(fs); GLuint shader_programme = glCreateProgram(); glAttachShader(shader_programme, fs); glAttachShader(shader_programme, vs); glBindAttribLocation(shader_programme, iPosAttrLoc, "vp"); glLinkProgram(shader_programme); // to draw everything with lines (wire-frame) //glPolygonMode(GL_FRONT, GL_LINE); // mainloop while (!glfwWindowShouldClose(window)) { update_fps_counter(window); // wipe the drawing surface clear glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // if window is resized, the gl part does not scale to fit, // to fix that we need this glViewport. // renc: this seems map the whole window to [-1,1]. glViewport(0, 0, g_iWindowWidth, g_iWindowHeight); // try to disale this use program, check the scene. glUseProgram(shader_programme); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, 3); // put the stuff we've been drawing onto the display glfwSwapBuffers(window); // update other events like input handling, // update non-graphical events like key-presses. glfwPollEvents(); if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, 1); } glfwTerminate(); return 1; }
void WindowGL::startFrame() { update_fps_counter(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.6f, 0.6f, 0.8f, 1.0f); }