// GLFW3 sample. Hello GLFW // // request SOIL (Simple OpenGL Image Library) // by mieki256 // Last updated: <2024/02/28 03:32:29 +0900> #include #include #include #include void error_callback(int error, const char *description) { fprintf(stderr, "Error: %s\n", description); } static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { glfwSetWindowShouldClose(window, GLFW_TRUE); } } int main(void) { GLFWwindow *window; GLuint tex_id; glfwSetErrorCallback(error_callback); if (!glfwInit()) { // Initialization failed exit(EXIT_FAILURE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1); // set OpenGL 1.1 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // create window window = glfwCreateWindow(1280, 720, "Hello GLFW", NULL, NULL); if (!window) { // Window or OpenGL context creation failed glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); glfwSwapInterval(1); // load texture image file. use SOIL tex_id = SOIL_load_OGL_texture("logo.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_POWER_OF_TWO); if (tex_id > 0) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, tex_id); } else { glDisable(GL_TEXTURE_2D); } glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); int scrw, scrh; glfwGetFramebufferSize(window, &scrw, &scrh); glViewport(0, 0, scrw, scrh); glMatrixMode(GL_PROJECTION); glLoadIdentity(); double a = (double)scrw / (double)scrh; glOrtho(-a, a, -1.0, 1.0, 1.0, -1.0); double angle = 0.0; // main loop while (!glfwWindowShouldClose(window)) { angle += (45.0 / 60.0); glClearColor(0.1, 0.2, 0.4, 1.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(angle, 0, 0, 1); float w = 0.8; float h = 0.8; glColor4f(1.0, 1.0, 1.0, 1.0); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-w, +h, -0.5); glTexCoord2f(1.0, 0.0); glVertex3f(+w, +h, -0.5); glTexCoord2f(1.0, 1.0); glVertex3f(+w, -h, -0.5); glTexCoord2f(0.0, 1.0); glVertex3f(-w, -h, -0.5); glEnd(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }