forked from bwasty/learn-opengl-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_5_1_framebuffers.rs
319 lines (277 loc) · 13.6 KB
/
_5_1_framebuffers.rs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
extern crate glfw;
use self::glfw::Context;
extern crate gl;
use self::gl::types::*;
use std::ptr;
use std::mem;
use std::os::raw::c_void;
use std::ffi::CStr;
use common::{process_events, processInput, loadTexture};
use shader::Shader;
use camera::Camera;
use cgmath::{Matrix4, vec3, Deg, perspective, Point3};
use cgmath::prelude::*;
// settings
const SCR_WIDTH: u32 = 1280;
const SCR_HEIGHT: u32 = 720;
pub fn main_4_5_1() {
let mut camera = Camera {
Position: Point3::new(0.0, 0.0, 3.0),
..Camera::default()
};
let mut firstMouse = true;
let mut lastX: f32 = SCR_WIDTH as f32 / 2.0;
let mut lastY: f32 = SCR_HEIGHT as f32 / 2.0;
// timing
let mut deltaTime: f32; // time between current frame and last frame
let mut lastFrame: f32 = 0.0;
// glfw: initialize and configure
// ------------------------------
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core));
#[cfg(target_os = "macos")]
glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
// glfw window creation
// --------------------
let (mut window, events) = glfw.create_window(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window");
// query framebuffer size as it might be quite different from the requested size on Retina displays
let (scr_width, scr_height) = window.get_framebuffer_size();
window.make_current();
window.set_framebuffer_size_polling(true);
window.set_cursor_pos_polling(true);
window.set_scroll_polling(true);
// tell GLFW to capture our mouse
window.set_cursor_mode(glfw::CursorMode::Disabled);
// gl: load all OpenGL function pointers
// ---------------------------------------
gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
let (shader, screenShader, cubeVBO, cubeVAO, planeVBO, planeVAO, quadVBO, quadVAO, cubeTexture, floorTexture, framebuffer, textureColorbuffer) = unsafe {
// configure global opengl state
// -----------------------------
gl::Enable(gl::DEPTH_TEST);
// build and compile our shader program
// ------------------------------------
let shader = Shader::new(
"src/_4_advanced_opengl/shaders/5.1.framebuffers.vs",
"src/_4_advanced_opengl/shaders/5.1.framebuffers.fs");
let screenShader = Shader::new(
"src/_4_advanced_opengl/shaders/5.1.framebuffers_screen.vs",
"src/_4_advanced_opengl/shaders/5.1.framebuffers_screen.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
let cubeVertices: [f32; 180] = [
// positions // texture Coords
-0.5, -0.5, -0.5, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, 0.5, -0.5, 1.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, 0.5, -0.5, 1.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, 0.5, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
0.5, -0.5, -0.5, 1.0, 1.0,
0.5, -0.5, 0.5, 1.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 1.0,
-0.5, 0.5, -0.5, 0.0, 1.0,
0.5, 0.5, -0.5, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0
];
let planeVertices: [f32; 30] = [
// positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
5.0, -0.5, 5.0, 2.0, 0.0,
-5.0, -0.5, 5.0, 0.0, 0.0,
-5.0, -0.5, -5.0, 0.0, 2.0,
5.0, -0.5, 5.0, 2.0, 0.0,
-5.0, -0.5, -5.0, 0.0, 2.0,
5.0, -0.5, -5.0, 2.0, 2.0
];
let quadVertices: [f32; 24] = [ // vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates.
// positions // texCoords
-1.0, 1.0, 0.0, 1.0,
-1.0, -1.0, 0.0, 0.0,
1.0, -1.0, 1.0, 0.0,
-1.0, 1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0
];
// cube VAO
let (mut cubeVAO, mut cubeVBO) = (0, 0);
gl::GenVertexArrays(1, &mut cubeVAO);
gl::GenBuffers(1, &mut cubeVBO);
gl::BindVertexArray(cubeVAO);
gl::BindBuffer(gl::ARRAY_BUFFER, cubeVBO);
gl::BufferData(gl::ARRAY_BUFFER,
(cubeVertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
&cubeVertices[0] as *const f32 as *const c_void,
gl::STATIC_DRAW);
let stride = 5 * mem::size_of::<GLfloat>() as GLsizei;
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
gl::EnableVertexAttribArray(1);
gl::VertexAttribPointer(1, 2, gl::FLOAT, gl::FALSE, stride, (3 * mem::size_of::<GLfloat>()) as *const c_void);
gl::BindVertexArray(0);
// plane VAO
let (mut planeVAO, mut planeVBO) = (0, 0);
gl::GenVertexArrays(1, &mut planeVAO);
gl::GenBuffers(1, &mut planeVBO);
gl::BindVertexArray(planeVAO);
gl::BindBuffer(gl::ARRAY_BUFFER, planeVBO);
gl::BufferData(gl::ARRAY_BUFFER,
(planeVertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
&planeVertices[0] as *const f32 as *const c_void,
gl::STATIC_DRAW);
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
gl::EnableVertexAttribArray(1);
gl::VertexAttribPointer(1, 2, gl::FLOAT, gl::FALSE, stride, (3 * mem::size_of::<GLfloat>()) as *const c_void);
// screen quad VAO
let (mut quadVAO, mut quadVBO) = (0, 0);
gl::GenVertexArrays(1, &mut quadVAO);
gl::GenBuffers(1, &mut quadVBO);
gl::BindVertexArray(quadVBO);
gl::BindBuffer(gl::ARRAY_BUFFER, quadVBO);
gl::BufferData(gl::ARRAY_BUFFER,
(quadVertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
&quadVertices[0] as *const f32 as *const c_void,
gl::STATIC_DRAW);
gl::EnableVertexAttribArray(0);
let stride = 4 * mem::size_of::<GLfloat>() as GLsizei;
gl::VertexAttribPointer(0, 2, gl::FLOAT, gl::FALSE, stride, ptr::null());
gl::EnableVertexAttribArray(1);
gl::VertexAttribPointer(1, 2, gl::FLOAT, gl::FALSE, stride, (2 * mem::size_of::<GLfloat>()) as *const c_void);
// load textures
// -------------
let cubeTexture = loadTexture("resources/textures/container.jpg");
let floorTexture = loadTexture("resources/textures/metal.png");
// shader configuration
// --------------------
shader.useProgram();
shader.setInt(c_str!("texture1"), 0);
screenShader.useProgram();
screenShader.setInt(c_str!("screenTexture"), 0);
// framebuffer configuration
// -------------------------
let mut framebuffer = 0;
gl::GenFramebuffers(1, &mut framebuffer);
gl::BindFramebuffer(gl::FRAMEBUFFER, framebuffer);
// create a color attachment texture
let mut textureColorbuffer = 0;
gl::GenTextures(1, &mut textureColorbuffer);
gl::BindTexture(gl::TEXTURE_2D, textureColorbuffer);
gl::TexImage2D(gl::TEXTURE_2D, 0, gl::RGB as i32, scr_width, scr_height, 0, gl::RGB, gl::UNSIGNED_BYTE, ptr::null());
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
gl::FramebufferTexture2D(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, gl::TEXTURE_2D, textureColorbuffer, 0);
// create a renderbuffer object for depth and stencil attachment (we won't be sampling these)
let mut rbo = 0;
gl::GenRenderbuffers(1, &mut rbo);
gl::BindRenderbuffer(gl::RENDERBUFFER, rbo);
gl::RenderbufferStorage(gl::RENDERBUFFER, gl::DEPTH24_STENCIL8, scr_width, scr_height); // use a single renderbuffer object for both a depth AND stencil buffer.
gl::FramebufferRenderbuffer(gl::FRAMEBUFFER, gl::DEPTH_STENCIL_ATTACHMENT, gl::RENDERBUFFER, rbo); // now actually attach it
// now that we actually created the framebuffer and added all attachments we want to check if it is actually complete now
if gl::CheckFramebufferStatus(gl::FRAMEBUFFER) != gl::FRAMEBUFFER_COMPLETE {
println!("ERROR::FRAMEBUFFER:: Framebuffer is not complete!");
}
gl::BindFramebuffer(gl::FRAMEBUFFER, 0);
// draw as wireframe
// gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE);
(shader, screenShader, cubeVBO, cubeVAO, planeVBO, planeVAO, quadVBO, quadVAO, cubeTexture, floorTexture, framebuffer, textureColorbuffer)
};
// render loop
// -----------
while !window.should_close() {
// per-frame time logic
// --------------------
let currentFrame = glfw.get_time() as f32;
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// events
// -----
process_events(&events, &mut firstMouse, &mut lastX, &mut lastY, &mut camera);
// input
// -----
processInput(&mut window, deltaTime, &mut camera);
// render
// ------
unsafe {
// bind to framebuffer and draw scene as we normally would to color texture
gl::BindFramebuffer(gl::FRAMEBUFFER, framebuffer);
gl::Enable(gl::DEPTH_TEST); // enable depth testing (is disabled for rendering screen-space quad)
// make sure we clear the framebuffer's content
gl::ClearColor(0.1, 0.1, 0.1, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
shader.useProgram();
let mut model: Matrix4<f32>;
let view = camera.GetViewMatrix();
let projection: Matrix4<f32> = perspective(Deg(camera.Zoom), SCR_WIDTH as f32 / SCR_HEIGHT as f32 , 0.1, 100.0);
shader.setMat4(c_str!("view"), &view);
shader.setMat4(c_str!("projection"), &projection);
// cubes
gl::BindVertexArray(cubeVAO);
gl::ActiveTexture(gl::TEXTURE0);
gl::BindTexture(gl::TEXTURE_2D, cubeTexture);
model = Matrix4::from_translation(vec3(-1.0, 0.0, -1.0));
shader.setMat4(c_str!("model"), &model);
gl::DrawArrays(gl::TRIANGLES, 0, 36);
model = Matrix4::from_translation(vec3(2.0, 0.0, 0.0));
shader.setMat4(c_str!("model"), &model);
gl::DrawArrays(gl::TRIANGLES, 0, 36);
// floor
gl::BindVertexArray(planeVAO);
gl::BindTexture(gl::TEXTURE_2D, floorTexture);
shader.setMat4(c_str!("model"), &Matrix4::identity());
gl::DrawArrays(gl::TRIANGLES, 0, 6);
gl::BindVertexArray(0);
// now bind back to default framebuffer and draw a quad plane with the attached framebuffer color texture
gl::BindFramebuffer(gl::FRAMEBUFFER, 0);
gl::Disable(gl::DEPTH_TEST); // disable depth test so screen-space quad isn't discarded due to depth test.
// clear all relevant buffers
gl::ClearColor(1.0, 1.0, 1.0, 1.0); // set clear color to white (not really necessery actually, since we won't be able to see behind the quad anyways)
gl::Clear(gl::COLOR_BUFFER_BIT);
screenShader.useProgram();
gl::BindVertexArray(quadVAO);
gl::BindTexture(gl::TEXTURE_2D, textureColorbuffer); // use the color attachment texture as the texture of the quad plane
gl::DrawArrays(gl::TRIANGLES, 0, 6);
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
window.swap_buffers();
glfw.poll_events();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
unsafe {
gl::DeleteVertexArrays(1, &cubeVAO);
gl::DeleteVertexArrays(1, &planeVAO);
gl::DeleteVertexArrays(1, &quadVAO);
gl::DeleteBuffers(1, &cubeVBO);
gl::DeleteBuffers(1, &planeVBO);
gl::DeleteBuffers(1, &quadVBO);
}
}