offscreen.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //========================================================================
  2. // Offscreen rendering example
  3. // Copyright (c) Camilla Löwy <[email protected]>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. #define GLAD_GL_IMPLEMENTATION
  26. #include <glad/gl.h>
  27. #define GLFW_INCLUDE_NONE
  28. #include <GLFW/glfw3.h>
  29. #include "linmath.h"
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #define STB_IMAGE_WRITE_IMPLEMENTATION
  33. #include <stb_image_write.h>
  34. static const struct
  35. {
  36. float x, y;
  37. float r, g, b;
  38. } vertices[3] =
  39. {
  40. { -0.6f, -0.4f, 1.f, 0.f, 0.f },
  41. { 0.6f, -0.4f, 0.f, 1.f, 0.f },
  42. { 0.f, 0.6f, 0.f, 0.f, 1.f }
  43. };
  44. static const char* vertex_shader_text =
  45. "#version 110\n"
  46. "uniform mat4 MVP;\n"
  47. "attribute vec3 vCol;\n"
  48. "attribute vec2 vPos;\n"
  49. "varying vec3 color;\n"
  50. "void main()\n"
  51. "{\n"
  52. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  53. " color = vCol;\n"
  54. "}\n";
  55. static const char* fragment_shader_text =
  56. "#version 110\n"
  57. "varying vec3 color;\n"
  58. "void main()\n"
  59. "{\n"
  60. " gl_FragColor = vec4(color, 1.0);\n"
  61. "}\n";
  62. static void error_callback(int error, const char* description)
  63. {
  64. fprintf(stderr, "Error: %s\n", description);
  65. }
  66. int main(void)
  67. {
  68. GLFWwindow* window;
  69. GLuint vertex_buffer, vertex_shader, fragment_shader, program;
  70. GLint mvp_location, vpos_location, vcol_location;
  71. float ratio;
  72. int width, height;
  73. mat4x4 mvp;
  74. char* buffer;
  75. glfwSetErrorCallback(error_callback);
  76. glfwInitHint(GLFW_COCOA_MENUBAR, GLFW_FALSE);
  77. if (!glfwInit())
  78. exit(EXIT_FAILURE);
  79. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  80. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  81. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  82. window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  83. if (!window)
  84. {
  85. glfwTerminate();
  86. exit(EXIT_FAILURE);
  87. }
  88. glfwMakeContextCurrent(window);
  89. gladLoadGL(glfwGetProcAddress);
  90. // NOTE: OpenGL error checks have been omitted for brevity
  91. glGenBuffers(1, &vertex_buffer);
  92. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  93. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  94. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  95. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  96. glCompileShader(vertex_shader);
  97. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  98. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  99. glCompileShader(fragment_shader);
  100. program = glCreateProgram();
  101. glAttachShader(program, vertex_shader);
  102. glAttachShader(program, fragment_shader);
  103. glLinkProgram(program);
  104. mvp_location = glGetUniformLocation(program, "MVP");
  105. vpos_location = glGetAttribLocation(program, "vPos");
  106. vcol_location = glGetAttribLocation(program, "vCol");
  107. glEnableVertexAttribArray(vpos_location);
  108. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  109. sizeof(vertices[0]), (void*) 0);
  110. glEnableVertexAttribArray(vcol_location);
  111. glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
  112. sizeof(vertices[0]), (void*) (sizeof(float) * 2));
  113. glfwGetFramebufferSize(window, &width, &height);
  114. ratio = width / (float) height;
  115. glViewport(0, 0, width, height);
  116. glClear(GL_COLOR_BUFFER_BIT);
  117. mat4x4_ortho(mvp, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  118. glUseProgram(program);
  119. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  120. glDrawArrays(GL_TRIANGLES, 0, 3);
  121. glFinish();
  122. buffer = calloc(4, width * height);
  123. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  124. // Write image Y-flipped because OpenGL
  125. stbi_write_png("offscreen.png",
  126. width, height, 4,
  127. buffer + (width * 4 * (height - 1)),
  128. -width * 4);
  129. free(buffer);
  130. glfwDestroyWindow(window);
  131. glfwTerminate();
  132. exit(EXIT_SUCCESS);
  133. }