How do I draw a sprite the cool way? (Ouch, my textures!)

Discussion in 'Programming' started by RujiK, Aug 16, 2019 at 4:13 AM.

  1. RujiK

    RujiK Member

    Jun 21, 2016
    Do I have mushy brain syndrome? I'm trying to draw a sprite from inside a texture page but the UV's seem to be way off. It draws a distorted image from a different part of the texture page.

    ww = sprite_get_width(_sprite);
    hh = sprite_get_height(_sprite);
    _texture = sprite_get_texture(_sprite,0);
    uvs = sprite_get_uvs(_sprite,0);
    _l = uvs[0]; //left
    _t = uvs[1]; //top
    _r = uvs[2]; //right
    _b = uvs[3]; //bottom
    The code is very simple so I'm really lost at what I'm doing wrong. Any help would be greatly appreciated. (Generous upvotes if that is any incentive to help.)
  2. Lonewolff

    Lonewolff Member

    Jan 8, 2018
    I tested this out and see what you mean.

    It seems strange, because if you do this it works fine, even with multiple different sprites on the texture page.

    draw_vertex_texture(xx,yy, 0.0, 0.0);
    draw_vertex_texture(xx+ww,yy, 1.0, 0.0);
    draw_vertex_texture(xx+ww,yy+hh, 1.0, 1.0);
    draw_vertex_texture(xx+ww,yy+hh, 1.0, 1.0);
    draw_vertex_texture(xx,yy+hh, 0.0, 1.0);
    draw_vertex_texture(xx,yy, 0.0, 0.0);
    Maybe sprite_get_texture(_sprite,0) make a copy of the texture, or perhaps even sorts out the UV's behind the scenes somewhere to make them normalised again? Dunno. :confused:
    RujiK likes this.
  3. CloseRange

    CloseRange Member

    Jul 2, 2016
    there isn't a good reason for this that I know of but it has to do with how game maker does the whole drawing uv's with textures.
    normally when you do draw_vertex_texture the point (1, 1) represents the bottom right of the whole image. But nope for texture pages like this it seems that (1, 1) represents the bottom right of the first image in the page.
    That's why what @Lonewolff worked, nothing was copied or normalized or sorted, just a dumb thing game maker does.
    anyway this is avery easy fix. change where you set _l _t _r and _b to this:
    _l = uvs[0]*(1/texture_get_texel_width(_texture))/ww; //left
    _t = uvs[1]*(1/texture_get_texel_width(_texture))/hh; //top
    _r = uvs[2]*(1/texture_get_texel_width(_texture))/ww; //right
    _b = uvs[3]*(1/texture_get_texel_width(_texture))/hh; //bottom
    texture_get_texel is a.... weird one at best. It's how texture pages are sized (instead of pixels for whatever reason) but if you want to convert from texel's to pixels then you do 1/texelSize
    and then we just take that number, devided by width and height respectivly (this will get us how many images, or cells... i guess?, are in the texture)
    take that and multiply it by the uv's to get the correct uv's
    RujiK likes this.
  4. NightFrost

    NightFrost Member

    Jun 24, 2016
    Texel, I recall, is the size of a single pixel on a texture page. The page size in pixels varies depending on how graphics are stacked there, but the GPU will always want its size as running from 0 to 1 so it divides 1.0 by page's pixel dimensions to arrive at texel size. So when you go in reverse, taking 1.0 and divide by texel width and height, you get the texture page width and height, respectively, in pixels. (A quick test appears to confirm this.)
    RujiK likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice