Alireza
Member
Hello guys
I had decided to use primitives to draw a sprite, to be precise I wanted to use pr_trianglelist. But then I realized that primitives can have a limited number of vertexes and if there are more than a 1000 of them the rest of them wont get drawn (look at picture 1), To fix this problem the sprite can be controlled by several objects (this is not very logical) or vertex buffer can be used.
After I used the vertex buffer I encounter a problem with calculating the correct UV of the sprite (picture 2), however this gets even stranger...if the size of the sprite is one of the powers of 2 and its located in a seperate texture page the problem with calculating the UV is solved (picture 3).
When the sprite is not one of the powers of 2 how do I calculate UV?
The code of the object that draws the sprite:
Create event :
Step event :
Draw event :
I had decided to use primitives to draw a sprite, to be precise I wanted to use pr_trianglelist. But then I realized that primitives can have a limited number of vertexes and if there are more than a 1000 of them the rest of them wont get drawn (look at picture 1), To fix this problem the sprite can be controlled by several objects (this is not very logical) or vertex buffer can be used.
After I used the vertex buffer I encounter a problem with calculating the correct UV of the sprite (picture 2), however this gets even stranger...if the size of the sprite is one of the powers of 2 and its located in a seperate texture page the problem with calculating the UV is solved (picture 3).
When the sprite is not one of the powers of 2 how do I calculate UV?
The code of the object that draws the sprite:
Create event :
Code:
sprite = sTwo;
spriteWidth = sprite_get_width(sprite);
spriteHeight = sprite_get_height(sprite);
texture = sprite_get_texture(sprite, image_index);
uvs = texture_get_uvs(texture);
uvLeft = uvs[0];
uvTop = uvs[1];
uvRight = uvs[2];
uvBottom = uvs[3];
xStart = xstart;
yStart = ystart;
pointsNumberWidth = 10;
pointsNumberHeight = 8;
segmentWidth = spriteWidth / (pointsNumberWidth - 1);
segmentHeight = spriteHeight / (pointsNumberHeight - 1);
widthTex = 1 / (pointsNumberWidth - 1);
heightTex = 1 / (pointsNumberHeight - 1);
// Create vertex format
vertex_format_begin();
vertex_format_add_position();
vertex_format_add_texcoord();
vertex_format_add_color();
vertexFormat = vertex_format_end();
Code:
// Move object
if(mouse_check_button(mb_left))
{
xStart = mouse_x;
yStart = mouse_y;
}
Code:
// Debug points
draw_circle_color(xStart, yStart, 8, c_red, c_red, false);
draw_circle_color(xStart + spriteWidth, yStart + spriteHeight, 8, c_red, c_red, false);
var _vertexBuffer = vertex_create_buffer();
vertex_begin(_vertexBuffer, vertexFormat);
for(var _i = 0; _i < (pointsNumberHeight - 1); ++_i)
{
for(var _j = 0; _j < (pointsNumberWidth - 1); ++_j)
{
var _pointOneX = xStart + _j * segmentWidth;
var _pointOneY = yStart + _i * segmentHeight;
var _pointOneXTex = uvLeft + (_j * widthTex);
var _pointOneYTex = uvTop + (_i * heightTex);
var _pointTwoX = xStart + (_j + 1) * segmentWidth;
var _pointTwoY = yStart + _i * segmentHeight;
var _pointTwoXTex = uvLeft + ((_j + 1) * widthTex);
var _pointTwoYTex = uvTop + (_i * heightTex);
var _pointThreeX = xStart + (_j + 1) * segmentWidth;
var _pointThreeY = yStart + (_i + 1) * segmentHeight;
var _pointThreeXTex = uvLeft + ((_j + 1) * widthTex);
var _pointThreeYTex = uvTop + ((_i + 1) * heightTex);
// Triangle one
vertex_position(_vertexBuffer, _pointOneX, _pointOneY);
vertex_texcoord(_vertexBuffer, _pointOneXTex, _pointOneYTex);
vertex_color(_vertexBuffer, c_white, 1);
vertex_position(_vertexBuffer, _pointTwoX, _pointTwoY);
vertex_texcoord(_vertexBuffer, _pointTwoXTex, _pointTwoYTex);
vertex_color(_vertexBuffer, c_white, 1);
vertex_position(_vertexBuffer, _pointThreeX, _pointThreeY);
vertex_texcoord(_vertexBuffer, _pointThreeXTex, _pointThreeYTex);
vertex_color(_vertexBuffer, c_white, 1);
}
}
vertex_end(_vertexBuffer);
vertex_submit(_vertexBuffer, pr_trianglelist, texture);
vertex_delete_buffer(_vertexBuffer);
Attachments
-
412.6 KB Views: 23