forked from mortennobel/SimpleRenderEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset-icon.cpp
More file actions
96 lines (79 loc) · 2.9 KB
/
Copy pathset-icon.cpp
File metadata and controls
96 lines (79 loc) · 2.9 KB
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
96
#include <iostream>
#include <vector>
#include <fstream>
#include "sre/Texture.hpp"
#include "sre/Renderer.hpp"
#include "sre/Material.hpp"
#include "sre/SDLRenderer.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <sre/Inspector.hpp>
using namespace sre;
class RenderToTextureExample {
public:
RenderToTextureExample(){
r.init();
camera.lookAt({0,0,3},{0,0,0},{0,1,0});
camera.setPerspectiveProjection(60,0.1,100);
texture = Texture::create().withRGBAData(nullptr, 64, 64).build();
framebuffer = Framebuffer::create().withColorTexture(texture).build();
materialOffscreen = Shader::getStandardBlinnPhong()->createMaterial();
materialOffscreen->setSpecularity({1,1,1,120});
material = Shader::getStandardBlinnPhong()->createMaterial();
material->setTexture(texture);
mesh = Mesh::create().withCube().build();
worldLights.addLight(Light::create().withPointLight({0,0,3}).withColor({1,1,1}).withRange(20).build());
r.frameRender = [&](){
render();
};
r.mouseEvent = [&](SDL_Event& event){
if (event.button.button==SDL_BUTTON_RIGHT){
showInspector = true;
}
};
r.startEventLoop();
}
void render(){
auto renderToTexturePass = RenderPass::create() // Create a renderpass which writes to the texture using a framebuffer
.withCamera(camera)
.withWorldLights(&worldLights)
.withFramebuffer(framebuffer)
.withClearColor(true, {0, 1, 1, 0})
.withGUI(false)
.build();
renderToTexturePass.draw(mesh, glm::eulerAngleY(glm::radians((float)i)), materialOffscreen);
renderToTexturePass.finish();
auto rawData = texture->getRawImage();
r.setWindowIcon(texture);
auto renderPass = RenderPass::create() // Create a renderpass which writes to the screen.
.withCamera(camera)
.withWorldLights(&worldLights)
.withClearColor(true, {1, 0, 0, 1})
.withGUI(true)
.build();
renderPass.draw(mesh, glm::eulerAngleY(glm::radians((float)i)), material);
// The offscreen texture is used in material
static Inspector prof;
prof.update();
if (showInspector){
prof.gui(true);
}
i++;
}
private:
SDLRenderer r;
Camera camera;
WorldLights worldLights;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<Material> materialOffscreen;
std::shared_ptr<Material> material;
std::shared_ptr<Texture> texture;
std::shared_ptr<Framebuffer> framebuffer;
int i=0;
bool showInspector = false;
};
int main() {
std::make_unique<RenderToTextureExample>();
return 0;
}