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