-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
95 lines (74 loc) · 1.92 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <gpgpu/Context.hpp>
#include <gpgpu/Framebuffer.hpp>
#include <gpgpu/Program.hpp>
#include <OpenImageIO/imagebufalgo.h>
using namespace std;
int main() {
/*
* Context
*/
gpgpu::Context context;
cout << context << endl;
/*
* Shader
*/
gpgpu::Program fixed_color_shader;
auto vs = gpgpu::create_shader(gpgpu::Shader::Vertex, R"(
#version 130
uniform mat4 camera;
in vec4 vertex;
void main() {
gl_Position = camera * vertex;
})"
);
auto fs = gpgpu::create_shader(gpgpu::Shader::Fragment, R"(
#version 130
out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
})"
);
fixed_color_shader.append({vs, fs});
fixed_color_shader.link();
/*
* Framebuffer
*/
gpgpu::Framebuffer fb(800, 600, true);
auto canvas = make_shared<gpgpu::Texture2D>(800, 600);
fb.set_color_attachment(canvas, 0);
/*
* Geometry
*/
float vertices[] = {
-0.5, -0.5, 0.0,
0.0, 0.5, 0.0,
0.5, -0.5, 0.0
};
auto geometry = make_shared<gpgpu::ArrayBuffer>();
geometry->data(3, 3, vertices);
unsigned int face_vertices[] = {
0, 1, 2
};
gpgpu::ElementArrayBuffer faces;
faces.data(3, 3, face_vertices);
/*
* Camera
*/
Eigen::Matrix4f camera = Eigen::Matrix4f::Identity();
/*
* Render
*/
fb.bind();
fixed_color_shader.use();
fixed_color_shader.uniform("camera", camera);
fixed_color_shader.attribute("vertex", geometry);
fixed_color_shader.render(faces);
// Or without indirection.
//fixed_color_shader.render(geometry, "vertex", GL_TRIANGLES);
auto image = canvas->image();
OpenImageIO::ImageBuf output;
OpenImageIO::ImageBufAlgo::flip(output, *image);
output.write("canvas.png");
return 0;
}