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