offscreen.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #if USE_NATIVE_OSMESA
  30. #define GLFW_EXPOSE_NATIVE_OSMESA
  31. #include <GLFW/glfw3native.h>
  32. #endif
  33. #include "linmath.h"
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #define STB_IMAGE_WRITE_IMPLEMENTATION
  37. #include <stb_image_write.h>
  38. static const struct
  39. {
  40. float x, y;
  41. float r, g, b;
  42. } vertices[3] =
  43. {
  44. { -0.6f, -0.4f, 1.f, 0.f, 0.f },
  45. { 0.6f, -0.4f, 0.f, 1.f, 0.f },
  46. { 0.f, 0.6f, 0.f, 0.f, 1.f }
  47. };
  48. static const char* vertex_shader_text =
  49. "#version 110\n"
  50. "uniform mat4 MVP;\n"
  51. "attribute vec3 vCol;\n"
  52. "attribute vec2 vPos;\n"
  53. "varying vec3 color;\n"
  54. "void main()\n"
  55. "{\n"
  56. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  57. " color = vCol;\n"
  58. "}\n";
  59. static const char* fragment_shader_text =
  60. "#version 110\n"
  61. "varying vec3 color;\n"
  62. "void main()\n"
  63. "{\n"
  64. " gl_FragColor = vec4(color, 1.0);\n"
  65. "}\n";
  66. static void error_callback(int error, const char* description)
  67. {
  68. fprintf(stderr, "Error: %s\n", description);
  69. }
  70. int main(void)
  71. {
  72. GLFWwindow* window;
  73. GLuint vertex_buffer, vertex_shader, fragment_shader, program;
  74. GLint mvp_location, vpos_location, vcol_location;
  75. float ratio;
  76. int width, height;
  77. mat4x4 mvp;
  78. char* buffer;
  79. glfwSetErrorCallback(error_callback);
  80. glfwInitHint(GLFW_COCOA_MENUBAR, GLFW_FALSE);
  81. if (!glfwInit())
  82. exit(EXIT_FAILURE);
  83. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  84. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  85. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  86. window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  87. if (!window)
  88. {
  89. glfwTerminate();
  90. exit(EXIT_FAILURE);
  91. }
  92. glfwMakeContextCurrent(window);
  93. gladLoadGL(glfwGetProcAddress);
  94. // NOTE: OpenGL error checks have been omitted for brevity
  95. glGenBuffers(1, &vertex_buffer);
  96. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  97. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  98. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  99. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  100. glCompileShader(vertex_shader);
  101. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  102. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  103. glCompileShader(fragment_shader);
  104. program = glCreateProgram();
  105. glAttachShader(program, vertex_shader);
  106. glAttachShader(program, fragment_shader);
  107. glLinkProgram(program);
  108. mvp_location = glGetUniformLocation(program, "MVP");
  109. vpos_location = glGetAttribLocation(program, "vPos");
  110. vcol_location = glGetAttribLocation(program, "vCol");
  111. glEnableVertexAttribArray(vpos_location);
  112. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  113. sizeof(vertices[0]), (void*) 0);
  114. glEnableVertexAttribArray(vcol_location);
  115. glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
  116. sizeof(vertices[0]), (void*) (sizeof(float) * 2));
  117. glfwGetFramebufferSize(window, &width, &height);
  118. ratio = width / (float) height;
  119. glViewport(0, 0, width, height);
  120. glClear(GL_COLOR_BUFFER_BIT);
  121. mat4x4_ortho(mvp, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  122. glUseProgram(program);
  123. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  124. glDrawArrays(GL_TRIANGLES, 0, 3);
  125. glFinish();
  126. #if USE_NATIVE_OSMESA
  127. glfwGetOSMesaColorBuffer(window, &width, &height, NULL, (void**) &buffer);
  128. #else
  129. buffer = calloc(4, width * height);
  130. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  131. #endif
  132. // Write image Y-flipped because OpenGL
  133. stbi_write_png("offscreen.png",
  134. width, height, 4,
  135. buffer + (width * 4 * (height - 1)),
  136. -width * 4);
  137. #if USE_NATIVE_OSMESA
  138. // Here is where there's nothing
  139. #else
  140. free(buffer);
  141. #endif
  142. glfwDestroyWindow(window);
  143. glfwTerminate();
  144. exit(EXIT_SUCCESS);
  145. }