void lndr_gen_mv_matrix(Lander *lander) { mat4x4 ident, temp, trans; mat4x4_identity(ident); /* float glx = 2 / glob_game.ppw; float gly = 2 / glob_game.pph; */ /* glx *= lander->x; gly *= lander->y; glx--; gly--; */ float glx = lander->x; float gly = lander->y; mat4x4_scale_aniso(temp, ident, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE); mat4x4_rotate_Z(temp, temp, lander->rotation); mat4x4_translate(trans, glx, gly, 0); mat4x4_mul(ident, trans, temp); mat4x4_transpose(lander->mvMatrix, ident); }
int main(void) { GLFWwindow* window; GLuint vertex_buffer, vertex_shader, fragment_shader, program; GLint mvp_location, vpos_location, vcol_location; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); // NOTE: OpenGL error checks have been omitted for brevity glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); glCompileShader(vertex_shader); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); glCompileShader(fragment_shader); program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); mvp_location = glGetUniformLocation(program, "MVP"); vpos_location = glGetAttribLocation(program, "vPos"); vcol_location = glGetAttribLocation(program, "vCol"); glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) 0); glEnableVertexAttribArray(vcol_location); glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) (sizeof(float) * 2)); while (!glfwWindowShouldClose(window)) { float ratio; int width, height; mat4x4 m, p, mvp; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float) height; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); mat4x4_identity(m); mat4x4_rotate_Z(m, m, (float) glfwGetTime()); mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); mat4x4_mul(mvp, p, m); glUseProgram(program); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }
int main(void) { // set error callback glfwSetErrorCallback(error_callback); // init environment glfwInit(); // check GLFW and GLES version // glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // create window context GLFWwindow *window = glfwCreateWindow(640, 480, "untitled_1", NULL, NULL); glfwSetFramebufferSizeCallback(window, reshape_callback); glfwMakeContextCurrent(window); glfwSwapInterval(1); // load dynamic gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); // show current OpenGL variables printf("GL_VERSION : %s\n", glGetString(GL_VERSION)); printf("GL_RENDERER : %s\n", glGetString(GL_RENDERER)); // shader GLuint vertex_shader = create_shader(GL_VERTEX_SHADER, vertex_shader_text); GLuint fragment_shader = create_shader(GL_FRAGMENT_SHADER, fragment_shader_text); GLuint program = link_program(vertex_shader, fragment_shader); // load data from application to gpu GLuint vertex_buffer; glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); GLint mvp_location = glGetUniformLocation(program, "MVP"); GLint vpos_location = glGetAttribLocation(program, "vPosition"); GLint vcol_location = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray((GLuint) vpos_location); glVertexAttribPointer((GLuint) vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) 0); glEnableVertexAttribArray((GLuint) vcol_location); glVertexAttribPointer((GLuint) vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) (sizeof(float) * 2)); // uint32_t counter = 0; float angle = 0; // main loop, keep running while (!glfwWindowShouldClose(window)) { int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); float ratio = (float) width / height; // mat4x4 m, p, mvp; mat4x4_identity(m); mat4x4_rotate_Z(m, m, angle); // 构建旋转矩阵 counter += 1; if (counter < 100) { angle += 0.01; } else if ((counter < 200)) { angle -= 0.02; } else { counter = 0; } // mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); // 处理缩放 mat4x4_mul(mvp, p, m); // 组合旋转+缩放 // apply shader glUseProgram(program); // Specify the value of a uniform variable for the current program object glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *) mvp); // draw glDrawArrays(GL_TRIANGLES, 0, 3); // window has two buffers, one for display, another for render // after render, we need to swap them to display glfwSwapBuffers(window); // process events glfwPollEvents(); } // destroy glfwDestroyWindow(window); glfwTerminate(); return 0; }
void crop_rotate::toolbar(PapayaMemory* mem) { ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3, 0)); if (ImGui::Button("-90")) { mem->crop_rotate.base_rotation--; } ImGui::SameLine(); if (ImGui::Button("+90")) { mem->crop_rotate.base_rotation++; } ImGui::SameLine(); ImGui::PopStyleVar(); ImGui::PushItemWidth(85); ImGui::SliderAngle("Rotate", &mem->crop_rotate.slider_angle, -45.0f, 45.0f); ImGui::PopItemWidth(); ImGui::SameLine(ImGui::GetWindowWidth() - 94); // TODO: Magic number alert ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 0)); if (ImGui::Button("Apply")) { bool size_changed = (mem->crop_rotate.base_rotation % 2 != 0); // Swap render texture and document texture handles if (size_changed) { int32 temp = mem->doc.width; mem->doc.width = mem->doc.height; mem->doc.height = temp; mat4x4_ortho(mem->doc.proj_mtx, 0.f, (float)mem->doc.width, 0.f, (float)mem->doc.height, -1.f, 1.f); mem->doc.inverse_aspect = (float)mem->doc.height / (float)mem->doc.width; GLCHK( glDeleteTextures(1, &mem->misc.fbo_sample_tex) ); mem->misc.fbo_sample_tex = gl::allocate_tex(mem->doc.width, mem->doc.height); } GLCHK( glDisable(GL_BLEND) ); GLCHK( glViewport(0, 0, mem->doc.width, mem->doc.height) ); // Bind and clear the frame buffer GLCHK( glBindFramebuffer(GL_FRAMEBUFFER, mem->misc.fbo) ); GLCHK( glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mem->misc.fbo_sample_tex, 0) ); GLCHK( glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ); GLCHK( glClear(GL_COLOR_BUFFER_BIT) ); mat4x4 m, r; // Rotate around center { Vec2 offset = Vec2(mem->doc.width * 0.5f, mem->doc.height * 0.5f); mat4x4_dup(m, mem->doc.proj_mtx); mat4x4_translate_in_place(m, offset.x, offset.y, 0.f); // mat4x4_rotate_Z(r, m, math::to_radians(-90)); mat4x4_rotate_Z(r, m, mem->crop_rotate.slider_angle + math::to_radians(90.0f * mem->crop_rotate.base_rotation)); if (size_changed) { mat4x4_translate_in_place(r, -offset.y, -offset.x, 0.f); } else { mat4x4_translate_in_place(r, -offset.x, -offset.y, 0.f); } } // Draw the image onto the frame buffer GLCHK( glBindBuffer(GL_ARRAY_BUFFER, mem->meshes[PapayaMesh_RTTAdd].vbo_handle) ); GLCHK( glUseProgram(mem->shaders[PapayaShader_DeMultiplyAlpha].handle) ); GLCHK( glUniformMatrix4fv(mem->shaders[PapayaShader_ImGui].uniforms[0], 1, GL_FALSE, (GLfloat*)r) ); gl::set_vertex_attribs(mem->shaders[PapayaShader_DeMultiplyAlpha]); GLCHK( glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)mem->doc.texture_id) ); GLCHK( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ); GLCHK( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ); GLCHK( glDrawArrays (GL_TRIANGLES, 0, 6) ); uint32 temp = mem->misc.fbo_sample_tex; mem->misc.fbo_sample_tex = mem->doc.texture_id; mem->doc.texture_id = temp; if (size_changed) { core::resize_doc(mem, mem->doc.width, mem->doc.height); // Reposition canvas to maintain apparent position int32 delta = math::round_to_int((mem->doc.height - mem->doc.width) * 0.5f * mem->doc.canvas_zoom); mem->doc.canvas_pos.x += delta; mem->doc.canvas_pos.y -= delta; } // Reset stuff GLCHK( glBindFramebuffer(GL_FRAMEBUFFER, 0) ); GLCHK( glViewport(0, 0, (int32)ImGui::GetIO().DisplaySize.x, (int32)ImGui::GetIO().DisplaySize.y) ); mem->crop_rotate.slider_angle = 0.f; mem->crop_rotate.base_rotation = 0; } ImGui::SameLine(); if (ImGui::Button("Cancel")) { mem->crop_rotate.slider_angle = 0.f; mem->crop_rotate.base_rotation = 0; } ImGui::PopStyleVar(); }
int main(int argc, char** argv) { int ch, samples = 4; GLFWwindow* window; GLuint vertex_buffer, vertex_shader, fragment_shader, program; GLint mvp_location, vpos_location; while ((ch = getopt(argc, argv, "hs:")) != -1) { switch (ch) { case 'h': usage(); exit(EXIT_SUCCESS); case 's': samples = atoi(optarg); break; default: usage(); exit(EXIT_FAILURE); } } glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); if (samples) printf("Requesting MSAA with %i samples\n", samples); else printf("Requesting that MSAA not be available\n"); glfwWindowHint(GLFW_SAMPLES, samples); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(800, 400, "Aliasing Detector", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); glGetIntegerv(GL_SAMPLES, &samples); if (samples) printf("Context reports MSAA is available with %i samples\n", samples); else printf("Context reports MSAA is unavailable\n"); glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); glCompileShader(vertex_shader); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); glCompileShader(fragment_shader); program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); mvp_location = glGetUniformLocation(program, "MVP"); vpos_location = glGetAttribLocation(program, "vPos"); glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (void*) 0); while (!glfwWindowShouldClose(window)) { float ratio; int width, height; mat4x4 m, p, mvp; const double angle = glfwGetTime() * M_PI / 180.0; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float) height; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(program); mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f); mat4x4_translate(m, -1.f, 0.f, 0.f); mat4x4_rotate_Z(m, m, (float) angle); mat4x4_mul(mvp, p, m); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); glDisable(GL_MULTISAMPLE); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); mat4x4_translate(m, 1.f, 0.f, 0.f); mat4x4_rotate_Z(m, m, (float) angle); mat4x4_mul(mvp, p, m); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); glEnable(GL_MULTISAMPLE); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }
int main(int argc, char** argv) { int count = 0; double base; GLFWwindow* window; srand((unsigned int) time(NULL)); glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); for (;;) { int width, height; GLFWmonitor* monitor = NULL; GLuint vertex_shader, fragment_shader, program, vertex_buffer; GLint mvp_location, vpos_location; if (count & 1) { int monitorCount; GLFWmonitor** monitors = glfwGetMonitors(&monitorCount); monitor = monitors[rand() % monitorCount]; } if (monitor) { const GLFWvidmode* mode = glfwGetVideoMode(monitor); width = mode->width; height = mode->height; } else { width = 640; height = 480; } base = glfwGetTime(); window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } if (monitor) { printf("Opening full screen window on monitor %s took %0.3f seconds\n", glfwGetMonitorName(monitor), glfwGetTime() - base); } else { printf("Opening regular window took %0.3f seconds\n", glfwGetTime() - base); } glfwSetWindowCloseCallback(window, window_close_callback); glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); glCompileShader(vertex_shader); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); glCompileShader(fragment_shader); program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); mvp_location = glGetUniformLocation(program, "MVP"); vpos_location = glGetAttribLocation(program, "vPos"); glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (void*) 0); glfwSetTime(0.0); while (glfwGetTime() < 5.0) { float ratio; int width, height; mat4x4 m, p, mvp; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float) height; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f); mat4x4_identity(m); mat4x4_rotate_Z(m, m, (float) glfwGetTime()); mat4x4_mul(mvp, p, m); glUseProgram(program); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glfwSwapBuffers(window); glfwPollEvents(); if (glfwWindowShouldClose(window)) { close_window(window); printf("User closed window\n"); glfwTerminate(); exit(EXIT_SUCCESS); } } printf("Closing window\n"); close_window(window); count++; } glfwTerminate(); }