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