How to display surface with transparent enabled texture mapping?

5 次查看(过去 30 天)
Greetings community,
I have a question regarding a GUI that I am building...
I need to display two images, really two sprites; I managed to write some code in order to display a surface with a texturemap over it, and this appears like a sprite in a 2D graphical scenerio, but the transparent layer appears as a solid color...
How can I diplay the graphic map (X2) as a typical 2D game sprite (the code needs to run over Matlab 2011) with non drawing parts/colors?
This is the code I wrote:
function Image_Rotating()
X1 = imread('Sprite_fixed.png');
X2 = imread('Sprite_mobile.png');%This contains transparent layer, also %set a color as transparent can be useful, as it is done in 2D games
C1 = X1;
C2 = X2;
MainFigureHdl = [];
MainAxesHdl = [];
SpriteHdl = [];
FixedSpriteHdl = [];
Sprite.XGRID = [];
Sprite.YGRID = [];
sprite_size = [255 240];
[Sprite.XGRID, Sprite.YGRID] = meshgrid([-ceil(sprite_size(2)/2):floor(sprite_size(2)/2)], ...
[ceil(sprite_size(1)/2):-1:-floor(sprite_size(1)/2)]);
fig = figure;
MainAxesHdl = axes('Parent',fig,'XLim',[-300 300],'YLim',[-300 300],'ZLim',[-300 300]);
FixedSpriteHdl=surface(Sprite.XGRID,Sprite.YGRID,zeros(size(Sprite.XGRID)),C1,...
'FaceColor','texturemap',...
'EdgeColor','none',...
'Visible','on',...
'CDataMapping','direct',...
'Parent', MainAxesHdl);
SpriteHdl=surface(Sprite.XGRID,Sprite.YGRID,zeros(size(Sprite.XGRID)),C2,...
'FaceColor','texturemap',...
'FaceAlpha',1,...
'AlphaDataMapping','direct',...
'EdgeColor','none',...
'Visible','on',...
'CDataMapping','direct',...
'Parent', MainAxesHdl);
Angle = 0;
view(2)
while(Angle<361)
Sprite_Rotate(deg2rad(Angle),Sprite.XGRID,Sprite.YGRID,SpriteHdl,C2);
Angle = Angle + 1;
drawnow;%Later, drawnow will be time-managed in order to obtain desired fps
end
end
function Sprite_Rotate(Angle,XGRID,YGRID,Sprite_ID,Sprite_GFX)
% move bird to pos [X Y],
% and rotate the srite surface by X degrees, anticlockwise = +
cosa = cos(Angle);
sina = sin(Angle);
xrotgrid = cosa .* XGRID + sina .* YGRID;
yrotgrid = sina .* XGRID - cosa .* YGRID;
xtransgrid = xrotgrid;
ytransgrid = yrotgrid;
set(Sprite_ID, 'XData', xtransgrid, ...
'YData', ytransgrid, ...
'CData',Sprite_GFX);
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by