mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 01:18:34 +00:00
model transformation works!
This commit is contained in:
1 parent
e17a902560
commit
b4fbf635d0
3 files changed
+69
-14
No files matched your search
@@ -15,6 +15,7 @@ pub struct PositionableObject {
|
||||
|
||||
pub rotation_typed: Rotation3<f32>,
|
||||
pub translation_typed: Translation3<f32>,
|
||||
pub is_transformed: bool,
|
||||
}
|
||||
|
||||
impl PositionableObject {
|
||||
@@ -27,6 +28,7 @@ impl PositionableObject {
|
||||
translation: Vector3::default(),
|
||||
rotation_typed: Rotation3::identity(),
|
||||
translation_typed: Translation3::identity(),
|
||||
is_transformed: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,32 +50,40 @@ impl PositionableObject {
|
||||
self.translation.z = z;
|
||||
}
|
||||
|
||||
pub fn get_transformation(&self) -> Matrix4<f32> {
|
||||
/*pub fn get_transformation(&self) -> Matrix4<f32> {
|
||||
let mut transformation = Matrix4::identity();
|
||||
//let transformation = transformation * &self.scale;
|
||||
//transformation.append_nonuniform_scaling(&self.scale);
|
||||
//transformation.append_translation(&self.translation);
|
||||
transformation
|
||||
}
|
||||
}*/
|
||||
|
||||
pub fn get_transformation_for_shader(&self) -> *const f32 {
|
||||
/*pub fn get_transformation_for_shader(&self) -> *const GLfloat {
|
||||
let transformation = self.get_transformation();
|
||||
transformation.as_ptr()
|
||||
}
|
||||
}*/
|
||||
|
||||
pub fn draw(&self, shader_id: GLuint) {
|
||||
pub fn draw(&self, model_view_uniform_id: GLint) {
|
||||
unsafe {
|
||||
let name : CString = CString::new("view").expect("string is correct");
|
||||
let name_ptr : *const c_char = name.as_ptr() as *const c_char;
|
||||
//let name : CString = CString::new("view").expect("string is correct");
|
||||
//let name_ptr : *const c_char = name.as_ptr() as *const c_char;
|
||||
|
||||
let loc = gl::GetUniformLocation(shader_id, name_ptr);
|
||||
println!("uniform location: {}", loc);
|
||||
//let loc = gl::GetUniformLocation(shader_id, name_ptr);
|
||||
//println!("uniform location: {}", loc);
|
||||
|
||||
println!("matrix: {:?}", self.get_transformation());
|
||||
//let mat_ptr = self.get_transformation_for_shader();
|
||||
|
||||
let mat_ptr = self.get_transformation_for_shader();
|
||||
//;gl::UniformMatrix4fv(loc, 1, gl::TRUE, mat_ptr);
|
||||
|
||||
//gl::UniformMatrix4fv(loc, 1, gl::TRUE, mat_ptr);
|
||||
let mut transformation = Matrix4::identity();
|
||||
transformation.append_translation_mut(&self.translation);
|
||||
|
||||
println!("matrix: {:?}", transformation);
|
||||
println!("translation: {:?}", &self.translation);
|
||||
|
||||
let mat_ptr = transformation.as_ptr();
|
||||
|
||||
gl::UniformMatrix4fv(model_view_uniform_id, 1, gl::FALSE, mat_ptr);
|
||||
}
|
||||
|
||||
self.renderable_object.draw();
|
||||
|
||||
@@ -33,6 +33,7 @@ use std::sync::{Arc, Mutex};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tobj::load_obj;
|
||||
use crate::assets::positionable_object::PositionableObject;
|
||||
use cgmath::{Matrix4, SquareMatrix, Matrix};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, PointCloudRendererError>;
|
||||
|
||||
@@ -88,6 +89,10 @@ pub struct PointCloudRenderer {
|
||||
view_matrix: [[f32; 4]; 4],
|
||||
projection_matrix: [[f32; 4]; 4],
|
||||
|
||||
/// Matrix applied to each model
|
||||
/// If a model doesn't set its own, it defaults to identity matrix
|
||||
default_model_view_matrix: [[f32; 4]; 4],
|
||||
|
||||
/// Renderer setting: size of the rendered points
|
||||
point_size: u8,
|
||||
|
||||
@@ -103,6 +108,9 @@ pub struct PointCloudRenderer {
|
||||
/// Uniform location in the shader program.
|
||||
projection_index: GLint,
|
||||
|
||||
/// Uniform location in the shader program.
|
||||
model_view_index: GLint,
|
||||
|
||||
/// Uniform location in the shader program.
|
||||
enable_shading_index: GLint,
|
||||
|
||||
@@ -192,6 +200,10 @@ impl PointCloudRenderer {
|
||||
let PROJECTION : CString = CString::new("projection").expect("string is correct");
|
||||
let PROJECTION_PTR : *const c_char = PROJECTION.as_ptr() as *const c_char;
|
||||
|
||||
/// Uniform variable name in OpenGL shader program
|
||||
let MODEL_VIEW : CString = CString::new("modelView").expect("string is correct");
|
||||
let MODEL_VIEW_PTR : *const c_char = MODEL_VIEW.as_ptr() as *const c_char;
|
||||
|
||||
/// Uniform variable name in OpenGL shader program
|
||||
let ENABLE_SHADING : CString = CString::new("enableShading").expect("string is correct");
|
||||
let ENABLE_SHADING_PTR : *const c_char = ENABLE_SHADING.as_ptr() as *const c_char;
|
||||
@@ -210,6 +222,7 @@ impl PointCloudRenderer {
|
||||
|
||||
let mut view_index = 0;
|
||||
let mut projection_index = 0;
|
||||
let mut model_view_index = 0;
|
||||
let mut enable_shading_index = 0;
|
||||
|
||||
unsafe {
|
||||
@@ -219,6 +232,7 @@ impl PointCloudRenderer {
|
||||
// FIXME: THe 'view' and 'projection' uniforms are not binding for some reason.
|
||||
view_index = gl::GetUniformLocation(program_id, VIEW_PTR);
|
||||
projection_index = gl::GetUniformLocation(program_id, PROJECTION_PTR);
|
||||
model_view_index = gl::GetUniformLocation(program_id, MODEL_VIEW_PTR);
|
||||
enable_shading_index = gl::GetUniformLocation(program_id, ENABLE_SHADING_PTR);
|
||||
// TODO: This is hardcoded to 2
|
||||
point_cloud_texture_indices.push(gl::GetUniformLocation(program_id, POINT_CLOUD_0_PTR));
|
||||
@@ -256,6 +270,13 @@ impl PointCloudRenderer {
|
||||
[0.0, 0.0, -0.2002, 0.0],
|
||||
];
|
||||
|
||||
let default_model_view_matrix= [
|
||||
[1.0, 0.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0, 0.0],
|
||||
[0.0, 0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 0.0, 1.0],
|
||||
];
|
||||
|
||||
Self {
|
||||
num_cameras,
|
||||
arcball_camera: arcball,
|
||||
@@ -263,6 +284,7 @@ impl PointCloudRenderer {
|
||||
projection: initial_projection_matrix_4x4(),
|
||||
view_matrix: initial_view,
|
||||
projection_matrix: initial_projection,
|
||||
default_model_view_matrix,
|
||||
shader_program_id: program_id,
|
||||
vertex_shader_id,
|
||||
fragment_shader_id,
|
||||
@@ -270,6 +292,7 @@ impl PointCloudRenderer {
|
||||
enable_shading: false,
|
||||
view_index,
|
||||
projection_index,
|
||||
model_view_index,
|
||||
enable_shading_index,
|
||||
point_cloud_texture_indices,
|
||||
vertex_array_objects: Vec::with_capacity(num_cameras),
|
||||
@@ -312,6 +335,9 @@ impl PointCloudRenderer {
|
||||
|
||||
let mut positionable_object = PositionableObject::new(renderable_object);
|
||||
|
||||
positionable_object.translate(0.0, 0.0, 50.0);
|
||||
positionable_object.rotate(180.0, 0.0, 50.0);
|
||||
|
||||
self.renderable_object = Some(positionable_object);
|
||||
|
||||
/*let filename = "/home/bt/dev/storyteller/assets/vr_staircase/scene.gltf";
|
||||
@@ -462,6 +488,24 @@ impl PointCloudRenderer {
|
||||
let typed_view = self.view_matrix.as_ptr() as *const GLfloat;
|
||||
gl::UniformMatrix4fv(self.view_index, 1, gl::FALSE, typed_view);
|
||||
|
||||
|
||||
let mut use_default_model_view = true;
|
||||
|
||||
if let Some(ref renderable) = self.renderable_object {
|
||||
if renderable.is_transformed {
|
||||
//let mut transformation : Matrix4<f32> = Matrix4::identity();
|
||||
//let typed_model_view = transformation.as_ptr();
|
||||
//gl::UniformMatrix4fv(self.model_view_index, 1, gl::FALSE, typed_model_view);
|
||||
|
||||
use_default_model_view = false;
|
||||
}
|
||||
}
|
||||
|
||||
if use_default_model_view {
|
||||
let typed_model_view = self.default_model_view_matrix.as_ptr() as *const GLfloat;
|
||||
gl::UniformMatrix4fv(self.model_view_index, 1, gl::FALSE, typed_model_view);
|
||||
}
|
||||
|
||||
// Update render settings in shader
|
||||
let _enable_shading = if self.enable_shading { 1 } else { 0 };
|
||||
let enable_shading = 0; // TODO FIXME FIXME FIXME
|
||||
@@ -480,7 +524,7 @@ impl PointCloudRenderer {
|
||||
gl::BindVertexArray(0);
|
||||
|
||||
if let Some(ref renderable) = self.renderable_object {
|
||||
renderable.draw(self.shader_program_id);
|
||||
renderable.draw(self.model_view_index);
|
||||
}
|
||||
|
||||
gl_get_error()
|
||||
|
||||
@@ -13,6 +13,7 @@ in vec3 normal;
|
||||
|
||||
out vec4 vertexColor;
|
||||
|
||||
uniform mat4 modelView;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
@@ -99,7 +100,7 @@ void main()
|
||||
vertexPosition.y -= 2.0;
|
||||
}
|
||||
|
||||
gl_Position = projection * view * vec4(vertexPosition, 1);
|
||||
gl_Position = projection * view * modelView * vec4(vertexPosition, 1);
|
||||
vertexColor = colorOut;
|
||||
|
||||
// Pass along the 'invalid pixel' flag as the alpha channel
|
||||
|
||||
Reference in new issue
Block a user