]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/screenshot.c
Rework structure
[dragonblocks_alpha.git] / src / client / screenshot.c
1 #define STB_IMAGE_WRITE_IMPLEMENTATION
2 #include <GL/glew.h>
3 #include <GL/gl.h>
4 #include <stb_image_write.h>
5 #include <string.h>
6 #include <time.h>
7 #include "client/game.h"
8 #include "client/gl_debug.h"
9 #include "client/window.h"
10
11 char *screenshot()
12 {
13         // renderbuffer for depth & stencil buffer
14         GLuint rbo;
15         glGenRenderbuffers(1, &rbo); GL_DEBUG
16         glBindRenderbuffer(GL_RENDERBUFFER, rbo); GL_DEBUG
17         glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_DEPTH24_STENCIL8, window.width, window.height); GL_DEBUG
18
19         // 2 textures, one with AA, one without
20
21         GLuint txos[2];
22         glGenTextures(2, txos); GL_DEBUG
23
24         glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, txos[0]); GL_DEBUG
25         glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB, window.width, window.height, GL_TRUE); GL_DEBUG
26
27         glBindTexture(GL_TEXTURE_2D, txos[1]); GL_DEBUG
28         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window.width, window.height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); GL_DEBUG
29
30         // 2 framebuffers, one with AA, one without
31
32         GLuint fbos[2];
33         glGenFramebuffers(2, fbos); GL_DEBUG
34
35         glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]); GL_DEBUG
36         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, txos[0], 0); GL_DEBUG
37         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); GL_DEBUG
38
39         glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]); GL_DEBUG
40         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, txos[1], 0); GL_DEBUG
41
42         // render scene
43         glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]); GL_DEBUG
44         game_render(0.0);
45         glBindFramebuffer(GL_FRAMEBUFFER, 0); GL_DEBUG
46
47         // blit AA-buffer into no-AA buffer
48         glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]); GL_DEBUG
49         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]); GL_DEBUG
50         glBlitFramebuffer(0, 0, window.width, window.height, 0, 0, window.width, window.height, GL_COLOR_BUFFER_BIT, GL_NEAREST); GL_DEBUG
51
52         // read data
53         GLubyte data[window.width * window.height * 3];
54         glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]); GL_DEBUG
55         glPixelStorei(GL_PACK_ALIGNMENT, 1); GL_DEBUG
56         glReadPixels(0, 0, window.width, window.height, GL_RGB, GL_UNSIGNED_BYTE, data); GL_DEBUG
57
58         // create filename
59         char filename[BUFSIZ];
60         time_t timep = time(0);
61         strftime(filename, BUFSIZ, "screenshot-%Y-%m-%d-%H:%M:%S.png", localtime(&timep));
62
63         // save screenshot
64         stbi_flip_vertically_on_write(true);
65         stbi_write_png(filename, window.width, window.height, 3, data, window.width * 3);
66
67         // delete buffers
68         glDeleteRenderbuffers(1, &rbo); GL_DEBUG
69         glDeleteTextures(2, txos); GL_DEBUG
70         glDeleteFramebuffers(2, fbos); GL_DEBUG
71
72         return strdup(filename);
73 }