CSEngine
Loading...
Searching...
No Matches
CaptureDef.h
1//
2// Created by ounols on 19. 5. 5.
3//
4
5#pragma once
6
7#define STB_IMAGE_WRITE_IMPLEMENTATION
8
9#include "Loader/STB/stb_image_write.h"
10#include "../OGLDef.h"
11
12namespace CSE {
13
14 static int saveScreenshot(const char* filename) {
15 GLint viewport[4];
16 glGetIntegerv(GL_VIEWPORT, viewport);
17
18 int x = viewport[0];
19 int y = viewport[1];
20 int width = viewport[2];
21 int height = viewport[3];
22
23 char* data = new char[width * height * 4];
24
25
26// glPixelStorei(GL_PACK_ALIGNMENT, 1);
27 glReadBuffer(GL_COLOR_ATTACHMENT0);
28 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
29
30 int saved = stbi_write_bmp(filename, width, height, 4, data);
31
32 delete[] data;
33
34 return saved;
35 }
36
37 static int saveTexture(const char* filename) {
38 GLint viewport[4];
39 glGetIntegerv(GL_VIEWPORT, viewport);
40
41 int* x = &viewport[0];
42 int* y = &viewport[1];
43 int* width = &viewport[2];
44 int* height = &viewport[3];
45
46 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, viewport[0], viewport[1], viewport[2], viewport[3], 0);
47
48 char* data = new char[(*width) * (*height) * 4];
49
50
51 glPixelStorei(GL_PACK_ALIGNMENT, 1);
52 glReadPixels(*x, *y, *width, *height, GL_RGBA, GL_UNSIGNED_BYTE, data);
53
54 int saved = stbi_write_bmp(filename, *width, *height, 4, data);
55
56 delete[] data;
57
58 return saved;
59 }
60}