Implement submerging in SDL_gpu builds.

This commit is contained in:
Boldizsár Lipka 2014-08-05 10:39:38 +02:00
parent 55e6ebc954
commit a2da478de3
5 changed files with 41 additions and 30 deletions

View file

@ -8,6 +8,13 @@ uniform sampler2D tex;
void main()
{
float submerge_alpha = 0;
if (frag_texture_pos.y > 1.0 - frag_submerge) {
submerge_alpha = 0.7 + (frag_texture_pos.y - (1 - frag_submerge))
/ frag_submerge * 0.3;
}
vec4 submerge_mod = vec4(0, 0, 0, submerge_alpha);
gl_FragColor = texture2D(tex, frag_texture_pos)
+ frag_draw_color + frag_color_mod;
+ frag_draw_color + frag_color_mod - submerge_mod;
}

View file

@ -1696,34 +1696,9 @@ void display::render_image(int x, int y, const display::tdrawing_layer drawing_l
}
#ifdef SDL_GPU
sdl::timage img(surf);
img.set_submerge(submerged);
if(submerged > 0.0) {
// divide the surface into 2 parts
const int submerge_height = std::max<int>(0, surf->h*(1.0-submerged));
//const int depth = surf->h - submerge_height;
SDL_Rect srcrect = sdl::create_rect(0, 0, surf->w, submerge_height);
img.set_clip(srcrect);
drawing_buffer_add(drawing_layer, loc, x, y, img);
if(submerge_height != surf->h) {
//the lower part will be transparent
//float alpha_base = 0.3f; // 30% alpha at surface of water
float alpha_delta = 0.015f; // lose 1.5% per pixel depth
alpha_delta *= zoom_ / DefaultZoom; // adjust with zoom
//TODO: submerging
//surf = submerge_alpha(surf, depth, alpha_base, alpha_delta, false);
srcrect.y = submerge_height;
srcrect.h = surf->h-submerge_height;
y += submerge_height;
img.set_clip(srcrect);
drawing_buffer_add(drawing_layer, loc, x, y, img);
}
} else {
// simple blit
drawing_buffer_add(drawing_layer, loc, x, y, img);
}
drawing_buffer_add(drawing_layer, loc, x, y, img);
#else
if(submerged > 0.0) {
// divide the surface into 2 parts

View file

@ -87,9 +87,12 @@ timage::timage(const surface &source)
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
, smooth_(false)
, submerge_(0)
{
if (image_ == NULL) {
throw tgpu_exception("Failed to construct timage object.", true);
if (!source.null()) {
throw tgpu_exception("Failed to construct timage object.", true);
}
} else {
clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
image_->refcount = 1;
@ -111,9 +114,12 @@ timage::timage(SDL_Surface *source)
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
, smooth_(false)
, submerge_(0)
{
if (image_ == NULL) {
throw tgpu_exception("Failed to construct timage object.", true);
if (source != NULL) {
throw tgpu_exception("Failed to construct timage object.", true);
}
} else {
clip_ = create_gpu_rect(0, 0, image_->w, image_->h);
image_->refcount = 1;
@ -135,6 +141,7 @@ timage::timage()
, hwrap_(GPU_WRAP_NONE)
, vwrap_(GPU_WRAP_NONE)
, smooth_(false)
, submerge_(0)
{
}
@ -161,6 +168,7 @@ timage::timage(const timage &texture)
, hwrap_(texture.hwrap_)
, vwrap_(texture.vwrap_)
, smooth_(texture.smooth_)
, submerge_(texture.submerge_)
{
if (image_ != NULL) {
image_->refcount += 1;
@ -182,6 +190,7 @@ void timage::draw(CVideo &video, const int x, const int y)
GPU_SetImageFilter(image_, smooth_ ? GPU_FILTER_LINEAR : GPU_FILTER_NEAREST);
GPU_SetWrapMode(image_, hwrap_, vwrap_);
video.set_texture_color_modulation(red_mod_, green_mod_, blue_mod_, alpha_mod_);
video.set_texture_submerge(float(submerge_));
GPU_BlitTransform(image_, &clip_, video.render_target(), x + width()/2, y + height()/2,
rotation_, hscale_, vscale_);
}
@ -329,6 +338,16 @@ GPU_WrapEnum timage::vwrap() const
return vwrap_;
}
void timage::set_submerge(double val)
{
submerge_ = val;
}
double timage::submerge() const
{
return submerge_;
}
bool timage::null() const
{
return image_ == NULL;
@ -345,6 +364,7 @@ timage timage::clone() const
res.set_rotation(rotation());
res.set_scale(hscale(), vscale());
res.set_smooth_scaling(smooth_scaling());
res.set_submerge(submerge_);
return res;
}

View file

@ -239,6 +239,10 @@ public:
*/
GPU_WrapEnum vwrap( )const;
void set_submerge(double val);
double submerge() const;
/**
* Returns true if the managed texture is NULL.
*/
@ -270,6 +274,9 @@ private:
/** Smooth scaling. */
bool smooth_;
/** Submerge. */
double submerge_;
};
}
#endif

View file

@ -44,6 +44,8 @@ shader_program::shader_program(const std::string &vsrc, const std::string &fsrc)
attr_color_mod_ = GPU_GetAttributeLocation(program_object_,
"vert_color_mod");
attr_submerge_ = GPU_GetAttributeLocation(program_object_,
"vert_submerge");
set_color_mod(0, 0, 0, 0);
set_submerge(0);
}