offscreen.c
Go to the documentation of this file.
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/glad.h>
27 #include <GLFW/glfw3.h>
28 
29 #if USE_NATIVE_OSMESA
30  #define GLFW_EXPOSE_NATIVE_OSMESA
31  #include <GLFW/glfw3native.h>
32 #endif
33 
34 #include "linmath.h"
35 
36 #include <stdlib.h>
37 #include <stdio.h>
38 
39 #define STB_IMAGE_WRITE_IMPLEMENTATION
40 #include <stb_image_write.h>
41 
42 static const struct
43 {
44  float x, y;
45  float r, g, b;
46 } vertices[3] =
47 {
48  { -0.6f, -0.4f, 1.f, 0.f, 0.f },
49  { 0.6f, -0.4f, 0.f, 1.f, 0.f },
50  { 0.f, 0.6f, 0.f, 0.f, 1.f }
51 };
52 
53 static const char* vertex_shader_text =
54 "#version 110\n"
55 "uniform mat4 MVP;\n"
56 "attribute vec3 vCol;\n"
57 "attribute vec2 vPos;\n"
58 "varying vec3 color;\n"
59 "void main()\n"
60 "{\n"
61 " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
62 " color = vCol;\n"
63 "}\n";
64 
65 static const char* fragment_shader_text =
66 "#version 110\n"
67 "varying vec3 color;\n"
68 "void main()\n"
69 "{\n"
70 " gl_FragColor = vec4(color, 1.0);\n"
71 "}\n";
72 
73 static void error_callback(int error, const char* description)
74 {
75  fprintf(stderr, "Error: %s\n", description);
76 }
77 
78 int main(void)
79 {
81  GLuint vertex_buffer, vertex_shader, fragment_shader, program;
82  GLint mvp_location, vpos_location, vcol_location;
83  float ratio;
84  int width, height;
85  mat4x4 mvp;
86  char* buffer;
87 
89 
91 
92  if (!glfwInit())
93  exit(EXIT_FAILURE);
94 
98 
99  window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
100  if (!window)
101  {
102  glfwTerminate();
103  exit(EXIT_FAILURE);
104  }
105 
106  glfwMakeContextCurrent(window);
108 
109  // NOTE: OpenGL error checks have been omitted for brevity
110 
111  glGenBuffers(1, &vertex_buffer);
112  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
114 
115  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
116  glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
117  glCompileShader(vertex_shader);
118 
119  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
120  glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
121  glCompileShader(fragment_shader);
122 
123  program = glCreateProgram();
124  glAttachShader(program, vertex_shader);
125  glAttachShader(program, fragment_shader);
126  glLinkProgram(program);
127 
128  mvp_location = glGetUniformLocation(program, "MVP");
129  vpos_location = glGetAttribLocation(program, "vPos");
130  vcol_location = glGetAttribLocation(program, "vCol");
131 
132  glEnableVertexAttribArray(vpos_location);
133  glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
134  sizeof(vertices[0]), (void*) 0);
135  glEnableVertexAttribArray(vcol_location);
136  glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
137  sizeof(vertices[0]), (void*) (sizeof(float) * 2));
138 
139  glfwGetFramebufferSize(window, &width, &height);
140  ratio = width / (float) height;
141 
142  glViewport(0, 0, width, height);
144 
145  mat4x4_ortho(mvp, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
146 
147  glUseProgram(program);
148  glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
149  glDrawArrays(GL_TRIANGLES, 0, 3);
150 
151 #if USE_NATIVE_OSMESA
152  glfwGetOSMesaColorBuffer(window, &width, &height, NULL, (void**) &buffer);
153 #else
154  buffer = calloc(4, width * height);
155  glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
156 #endif
157 
158  // Write image Y-flipped because OpenGL
159  stbi_write_png("offscreen.png",
160  width, height, 4,
161  buffer + (width * 4 * (height - 1)),
162  -width * 4);
163 
164 #if USE_NATIVE_OSMESA
165  // Here is where there's nothing
166 #else
167  free(buffer);
168 #endif
169 
170  glfwDestroyWindow(window);
171 
172  glfwTerminate();
173  exit(EXIT_SUCCESS);
174 }
175 
int main(void)
Definition: offscreen.c:78
#define glBufferData
#define glCreateProgram
#define GL_VERTEX_SHADER
float y
Definition: offscreen.c:44
vec4 mat4x4[4]
Definition: linmath.h:83
#define GL_STATIC_DRAW
khronos_float_t GLfloat
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
Definition: linmath.h:341
#define GL_RGBA
#define glGetAttribLocation
void *(* GLADloadproc)(const char *name)
static GLFWwindow * window
Definition: joysticks.c:55
#define GL_UNSIGNED_BYTE
#define GLFW_CONTEXT_VERSION_MAJOR
Context client API major version hint and attribute.
Definition: glfw3.h:921
#define glDrawArrays
float b
Definition: offscreen.c:45
#define GLFW_COCOA_MENUBAR
macOS specific init hint.
Definition: glfw3.h:1077
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
Definition: init.c:309
#define GLFW_FALSE
Zero.
Definition: glfw3.h:287
#define glUniformMatrix4fv
GLFWAPI int glfwGetOSMesaColorBuffer(GLFWwindow *handle, int *width, int *height, int *format, void **buffer)
#define glUseProgram
static void error_callback(int error, const char *description)
Definition: offscreen.c:73
GLenum GLfloat * buffer
float g
Definition: offscreen.c:45
float x
Definition: offscreen.c:44
#define glCreateShader
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define glVertexAttribPointer
#define GL_FLOAT
#define glEnableVertexAttribArray
#define GL_COLOR_BUFFER_BIT
GLbitfield GLuint program
Definition: glext.h:1889
int GLint
float r
Definition: offscreen.c:45
#define glBindBuffer
#define glClear
GLint GLsizei GLsizei height
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
#define glGetUniformLocation
#define GL_FRAGMENT_SHADER
#define glShaderSource
#define glViewport
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
static const textual_icon exit
Definition: model-views.h:254
#define glAttachShader
#define glCompileShader
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
The header of the native access functions.
#define GL_FALSE
static const struct @18 vertices[3]
unsigned int GLuint
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes)
#define glGenBuffers
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:444
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
static const char * vertex_shader_text
Definition: offscreen.c:53
#define glLinkProgram
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
#define GLFW_VISIBLE
Window visibility window hint and attribute.
Definition: glfw3.h:780
#define NULL
Definition: tinycthread.c:47
static const char * fragment_shader_text
Definition: offscreen.c:65
#define glReadPixels
#define GLFW_CONTEXT_VERSION_MINOR
Context client API minor version hint and attribute.
Definition: glfw3.h:927
#define GL_ARRAY_BUFFER
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
#define GL_TRIANGLES
GLFWAPI void glfwInitHint(int hint, int value)
Sets the specified init hint to the desired value.
Definition: init.c:251


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:38