Camera Fixes

-=-=-=-=-=-=

-Object Picking and orthogonal camera related functions fixed (i hope)
-Going to preview mode in the camera shows a frame with the correct game aspect ratio
-Changed Camera API and properties a little t make it more straightforward
-Fixed bug in shader compiler.

-Fixed bug in ShaderGL
This commit is contained in:
Juan Linietsky 2014-09-15 20:06:37 -03:00
parent 8cab401d08
commit 642c63319e
6 changed files with 106 additions and 41 deletions

View File

@ -67,9 +67,10 @@ Plane CameraMatrix::xform4(const Plane& p_vec4) {
void CameraMatrix::set_perspective(float p_fovy_degrees, float p_aspect, float p_z_near, float p_z_far,bool p_flip_fov) {
if (p_flip_fov)
p_fovy_degrees=get_fovy(p_fovy_degrees,p_aspect);
if (p_flip_fov) {
p_fovy_degrees=get_fovy(p_fovy_degrees,1.0/p_aspect);
}
float sine, cotangent, deltaZ;
float radians = p_fovy_degrees / 2.0 * Math_PI / 180.0;
@ -110,7 +111,7 @@ void CameraMatrix::set_orthogonal(float p_left, float p_right, float p_bottom, f
void CameraMatrix::set_orthogonal(float p_size, float p_aspect, float p_znear, float p_zfar,bool p_flip_fov) {
if (p_flip_fov) {
if (!p_flip_fov) {
p_size*=p_aspect;
}

View File

@ -60,7 +60,7 @@ struct CameraMatrix {
static float get_fovy(float p_fovx,float p_aspect) {
return Math::atan(p_aspect * Math::tan(p_fovx * 0.5))*2.0;
return Math::rad2deg(Math::atan(p_aspect * Math::tan(Math::deg2rad(p_fovx) * 0.5))*2.0);
}
float get_z_far() const;

View File

@ -267,7 +267,9 @@ ShaderGLES2::Version* ShaderGLES2::get_current_version() {
/* SETUP CONDITIONALS */
Vector<const char*> strings;
//strings.push_back("#version 120\n"); //ATI requieres this before anything
#ifdef GLEW_ENABLED
strings.push_back("#version 120\n"); //ATI requieres this before anything
#endif
int define_line_ofs=1;
for(int j=0;j<conditional_count;j++) {

View File

@ -74,16 +74,18 @@ bool Camera::_set(const StringName& p_name, const Variant& p_value) {
mode=PROJECTION_ORTHOGONAL;
changed_all=true;
} else if (p_name=="fov")
} else if (p_name=="fov" || p_name=="fovy" || p_name=="fovx")
fov=p_value;
else if (p_name=="size")
else if (p_name=="size" || p_name=="sizex" || p_name=="sizey")
size=p_value;
else if (p_name=="near")
near=p_value;
else if (p_name=="far")
far=p_value;
else if (p_name=="keep_aspect")
set_keep_aspect_mode(KeepAspect(int(p_value)));
else if (p_name=="vaspect")
set_use_vertical_aspect(p_value);
set_keep_aspect_mode(p_value?KEEP_WIDTH:KEEP_HEIGHT);
else if (p_name=="current") {
if (p_value.operator bool()) {
make_current();
@ -107,16 +109,16 @@ bool Camera::_get(const StringName& p_name,Variant &r_ret) const {
if (p_name=="projection") {
r_ret= mode;
} else if (p_name=="fov")
} else if (p_name=="fov" || p_name=="fovy" || p_name=="fovx")
r_ret= fov;
else if (p_name=="size")
else if (p_name=="size" || p_name=="sizex" || p_name=="sizey")
r_ret= size;
else if (p_name=="near")
r_ret= near;
else if (p_name=="far")
r_ret= far;
else if (p_name=="vaspect")
r_ret= vaspect;
else if (p_name=="keep_aspect")
r_ret= int(keep_aspect);
else if (p_name=="current") {
if (is_inside_scene() && get_scene()->is_editor_hint()) {
@ -142,19 +144,29 @@ void Camera::_get_property_list( List<PropertyInfo> *p_list) const {
case PROJECTION_PERSPECTIVE: {
p_list->push_back( PropertyInfo( Variant::REAL, "fov" , PROPERTY_HINT_RANGE, "1,89,0.1") );
p_list->push_back( PropertyInfo( Variant::REAL, "fov" , PROPERTY_HINT_RANGE, "1,89,0.1",PROPERTY_USAGE_NOEDITOR) );
if (keep_aspect==KEEP_WIDTH)
p_list->push_back( PropertyInfo( Variant::REAL, "fovx" , PROPERTY_HINT_RANGE, "1,89,0.1",PROPERTY_USAGE_EDITOR) );
else
p_list->push_back( PropertyInfo( Variant::REAL, "fovy" , PROPERTY_HINT_RANGE, "1,89,0.1",PROPERTY_USAGE_EDITOR) );
} break;
case PROJECTION_ORTHOGONAL: {
p_list->push_back( PropertyInfo( Variant::REAL, "size" , PROPERTY_HINT_RANGE, "1,16384,0.01" ) );
p_list->push_back( PropertyInfo( Variant::REAL, "size" , PROPERTY_HINT_RANGE, "1,16384,0.01",PROPERTY_USAGE_NOEDITOR ) );
if (keep_aspect==KEEP_WIDTH)
p_list->push_back( PropertyInfo( Variant::REAL, "sizex" , PROPERTY_HINT_RANGE, "0.1,16384,0.01",PROPERTY_USAGE_EDITOR) );
else
p_list->push_back( PropertyInfo( Variant::REAL, "sizey" , PROPERTY_HINT_RANGE, "0.1,16384,0.01",PROPERTY_USAGE_EDITOR) );
} break;
}
p_list->push_back( PropertyInfo( Variant::REAL, "near" , PROPERTY_HINT_EXP_RANGE, "0.01,4096.0,0.01") );
p_list->push_back( PropertyInfo( Variant::REAL, "far" , PROPERTY_HINT_EXP_RANGE, "0.01,4096.0,0.01") );
p_list->push_back( PropertyInfo( Variant::BOOL, "vaspect") );
p_list->push_back( PropertyInfo( Variant::INT, "keep_aspect",PROPERTY_HINT_ENUM,"Keep Width,Keep Height") );
p_list->push_back( PropertyInfo( Variant::BOOL, "current" ) );
p_list->push_back( PropertyInfo( Variant::INT, "visible_layers",PROPERTY_HINT_ALL_FLAGS ) );
p_list->push_back( PropertyInfo( Variant::OBJECT, "environment",PROPERTY_HINT_RESOURCE_TYPE,"Environment" ) );
@ -441,7 +453,7 @@ Vector3 Camera::project_local_ray_normal(const Point2& p_pos) const {
ray=Vector3(0,0,-1);
} else {
CameraMatrix cm;
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,vaspect);
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
float screen_w,screen_h;
cm.get_viewport_size(screen_w,screen_h);
ray=Vector3( ((p_pos.x/viewport_size.width)*2.0-1.0)*screen_w, ((1.0-(p_pos.y/viewport_size.height))*2.0-1.0)*screen_h,-near).normalized();
@ -472,7 +484,7 @@ Vector3 Camera::project_ray_origin(const Point2& p_pos) const {
Vector2 pos = p_pos / viewport_size;
float vsize,hsize;
if (vaspect) {
if (keep_aspect==KEEP_WIDTH) {
vsize = size/viewport_size.get_aspect();
hsize = size;
} else {
@ -503,9 +515,9 @@ Point2 Camera::unproject_position(const Vector3& p_pos) const {
if (mode==PROJECTION_ORTHOGONAL)
cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,vaspect);
cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
else
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,vaspect);
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
Plane p(get_camera_transform().xform_inv(p_pos),1.0);
@ -533,9 +545,9 @@ Vector3 Camera::project_position(const Point2& p_point) const {
CameraMatrix cm;
if (mode==PROJECTION_ORTHOGONAL)
cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,vaspect);
cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
else
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,vaspect);
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
Size2 vp_size;
cm.get_viewport_size(vp_size.x,vp_size.y);
@ -583,6 +595,20 @@ Ref<Environment> Camera::get_environment() const {
}
void Camera::set_keep_aspect_mode(KeepAspect p_aspect) {
keep_aspect=p_aspect;
VisualServer::get_singleton()->camera_set_use_vertical_aspect(camera,p_aspect==KEEP_WIDTH);
_change_notify();
}
Camera::KeepAspect Camera::get_keep_aspect_mode() const{
return keep_aspect;
}
void Camera::_bind_methods() {
@ -608,12 +634,16 @@ void Camera::_bind_methods() {
ObjectTypeDB::bind_method( _MD("look_at_from_pos","pos","target","up"),&Camera::look_at_from_pos );
ObjectTypeDB::bind_method(_MD("set_environment","env:Environment"),&Camera::set_environment);
ObjectTypeDB::bind_method(_MD("get_environment:Environment"),&Camera::get_environment);
ObjectTypeDB::bind_method(_MD("set_use_vertical_aspect","enable"),&Camera::set_use_vertical_aspect);
ObjectTypeDB::bind_method(_MD("is_using_vertical_aspect"),&Camera::is_using_vertical_aspect);
ObjectTypeDB::bind_method(_MD("set_keep_aspect_mode","mode"),&Camera::set_keep_aspect_mode);
ObjectTypeDB::bind_method(_MD("get_keep_aspect_mode"),&Camera::get_keep_aspect_mode);
//ObjectTypeDB::bind_method( _MD("_camera_make_current"),&Camera::_camera_make_current );
BIND_CONSTANT( PROJECTION_PERSPECTIVE );
BIND_CONSTANT( PROJECTION_ORTHOGONAL );
BIND_CONSTANT( KEEP_WIDTH );
BIND_CONSTANT( KEEP_HEIGHT );
}
float Camera::get_fov() const {
@ -661,26 +691,15 @@ Vector<Plane> Camera::get_frustum() const {
Size2 viewport_size = viewport_ptr->get_visible_rect().size;
CameraMatrix cm;
if (mode==PROJECTION_PERSPECTIVE)
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,vaspect);
cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
else
cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,vaspect);
cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
return cm.get_projection_planes(get_global_transform());
}
void Camera::set_use_vertical_aspect(bool p_enable) {
vaspect=p_enable;
VisualServer::get_singleton()->camera_set_use_vertical_aspect(camera,p_enable);
}
bool Camera::is_using_vertical_aspect() const{
return vaspect;
}
void Camera::look_at(const Vector3& p_target, const Vector3& p_up_normal) {
@ -712,7 +731,7 @@ Camera::Camera() {
force_change=false;
mode=PROJECTION_PERSPECTIVE;
set_perspective(60.0,0.1,100.0);
vaspect=false;
keep_aspect=KEEP_HEIGHT;
layers=0xFFFFFFFF;
//active=false;
}

View File

@ -46,6 +46,11 @@ public:
PROJECTION_ORTHOGONAL
};
enum KeepAspect {
KEEP_WIDTH,
KEEP_HEIGHT
};
private:
bool force_change;
@ -56,7 +61,7 @@ private:
float fov;
float size;
float near,far;
bool vaspect;
KeepAspect keep_aspect;
RID camera;
RID scenario_id;
@ -126,8 +131,8 @@ public:
void set_environment(const Ref<Environment>& p_environment);
Ref<Environment> get_environment() const;
void set_use_vertical_aspect(bool p_enable);
bool is_using_vertical_aspect() const;
void set_keep_aspect_mode(KeepAspect p_aspect);
KeepAspect get_keep_aspect_mode() const;
void look_at(const Vector3& p_target, const Vector3& p_up_normal);
void look_at_from_pos(const Vector3& p_pos,const Vector3& p_target, const Vector3& p_up_normal);
@ -140,5 +145,6 @@ public:
VARIANT_ENUM_CAST( Camera::Projection );
VARIANT_ENUM_CAST( Camera::KeepAspect );
#endif

View File

@ -38,7 +38,7 @@
#include "tools/editor/editor_settings.h"
#include "scene/resources/surface_tool.h"
#include "tools/editor/spatial_editor_gizmos.h"
#include "globals.h"
#define DISTANCE_DEFAULT 4
@ -1753,6 +1753,41 @@ void SpatialEditorViewport::_draw() {
}
if (previewing) {
Size2 ss = Size2( Globals::get_singleton()->get("display/width"), Globals::get_singleton()->get("display/height") );
float aspect = ss.get_aspect();
Size2 s = get_size();
Rect2 draw_rect;
switch(previewing->get_keep_aspect_mode()) {
case Camera::KEEP_WIDTH: {
draw_rect.size = Size2(s.width,s.width/aspect);
draw_rect.pos.x=0;
draw_rect.pos.y=(s.height-draw_rect.size.y)*0.5;
} break;
case Camera::KEEP_HEIGHT: {
draw_rect.size = Size2(s.height*aspect,s.height);
draw_rect.pos.y=0;
draw_rect.pos.x=(s.width-draw_rect.size.x)*0.5;
} break;
}
draw_rect = Rect2(Vector2(),s).clip(draw_rect);
surface->draw_line(draw_rect.pos,draw_rect.pos+Vector2(draw_rect.size.x,0),Color(0.6,0.6,0.1,0.5),2.0);
surface->draw_line(draw_rect.pos+Vector2(draw_rect.size.x,0),draw_rect.pos+draw_rect.size,Color(0.6,0.6,0.1,0.5),2.0);
surface->draw_line(draw_rect.pos+draw_rect.size,draw_rect.pos+Vector2(0,draw_rect.size.y),Color(0.6,0.6,0.1,0.5),2.0);
surface->draw_line(draw_rect.pos,draw_rect.pos+Vector2(0,draw_rect.size.y),Color(0.6,0.6,0.1,0.5),2.0);
}
}
@ -1936,6 +1971,7 @@ void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
if (!preview)
preview_camera->hide();
view_menu->show();
surface->update();
} else {
@ -1943,6 +1979,7 @@ void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
previewing->connect("exit_scene",this,"_preview_exited_scene");
VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), preview->get_camera() ); //replace
view_menu->hide();
surface->update();
}
}