Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Sub Image on Texture

  1. #1
    Senior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Sub Image on Texture

    Hello
    I'm trying to create a sub image on a texture at the point of my cursor, when my cursor is in a texture.
    So far, I've managed to know when I'm in a texture.
    I also know that i need to use glTexSubImage* to create a sub image.
    But i don't know how to calculate the position of my cursor in the texture.
    I've been trying to figure it out for hours but i still don't know how to do it.
    This is the information i have:
    the window x and y coordinates of my cursor
    The world x and y coordinates of my cursor
    the position of my texture
    the size of my texture(in world coordinates).
    So can somebody help me and tell me how to calculate it?
    Thanx Hylke

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Aug 2001
    Location
    Italy
    Posts
    625

    Re: Sub Image on Texture

    Leaving out the texture, do you need to have it working for arbitrary geometry or just for special cases?
    Can the texture-mapped geometry be transformed in arbitrary ways or it's guaranteed it'll always be in a certain position (say, facing parallel the near plane)?
    The first case is definetly more involved and I'm not sure it has real applications, at least at basic level.

  3. #3
    Senior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Re: Sub Image on Texture

    Originally posted by Obli:
    Leaving out the texture, do you need to have it working for arbitrary geometry or just for special cases?
    Can the texture-mapped geometry be transformed in arbitrary ways or it's guaranteed it'll always be in a certain position (say, facing parallel the near plane)?
    The first case is definetly more involved and I'm not sure it has real applications, at least at basic level.
    Does arbitrary mean random?
    But the texture doesn't always have the same place, but a random place.The random coordinates are saved in a x and y variable.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Sep 2003
    Location
    Ireland
    Posts
    135

    Re: Sub Image on Texture

    If you're looking at the texture square on, meaning that it's as if the texture is a piece of paper on a table, and you're looking at the table from above, then this is quite easy to calculate, even if the texture is rotated, because it's a 2 dimensional calculation.

    Regardless of the number of dimensions, you're just expressing the coordinates of the mouse point projected onto the texture's plane, in the texture's coordinates.

    You just take the dot product of the vector from the texture's origin to the projected mouse coordinate, with the texture's unit X and Y axes.

    C

  5. #5
    Senior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Re: Sub Image on Texture

    Originally posted by charliejay:
    If you're looking at the texture square on, meaning that it's as if the texture is a piece of paper on a table, and you're looking at the table from above, then this is quite easy to calculate, even if the texture is rotated, because it's a 2 dimensional calculation.
    Yup, that's the case, it's completly 2D
    Regardless of the number of dimensions, you're just expressing the coordinates of the mouse point projected onto the texture's plane, in the texture's coordinates.

    You just take the dot product of the vector from the texture's origin to the projected mouse coordinate, with the texture's unit X and Y axes.

    C
    That sounds kinda complicated.
    Can you give a small example?
    Thanx Hylke

  6. #6
    Senior Member Regular Contributor
    Join Date
    Sep 2003
    Location
    Ireland
    Posts
    135

    Re: Sub Image on Texture

    http://homepage.mac.com/cjj/index.html

    You can just go strarght to the dot product steps because the mousepoint is already projected on the texture's plane.

    All values, including those for the texture's axes and origin, should be expressed in the same coordinate system, probably window coordinates.

    CJ

  7. #7
    Senior Member Regular Contributor
    Join Date
    Sep 2003
    Location
    Ireland
    Posts
    135

    Re: Sub Image on Texture

    Look up the dot product on any maths site, mathworld.wolfram.com is good.

    Basically, you use the dot product anywhere you want to express one vector in term of some others (which is what you want to do), provided all these vectors are expressed in the same coordinate system.

    CJ

  8. #8
    Senior Member Regular Contributor
    Join Date
    May 2004
    Location
    Renkum
    Posts
    193

    Re: Sub Image on Texture

    Originally posted by charliejay:
    http://homepage.mac.com/cjj/index.html

    You can just go strarght to the dot product steps because the mousepoint is already projected on the texture's plane.

    All values, including those for the texture's axes and origin, should be expressed in the same coordinate system, probably window coordinates.

    CJ
    My English is not so good, so could you give a small code sample to demonstrate what i have to do?
    Thanx Hylke

  9. #9
    Senior Member Regular Contributor
    Join Date
    Sep 2003
    Location
    Ireland
    Posts
    135

    Re: Sub Image on Texture

    OK, it's pure math, so here are the calculations, can't do code, because I don't program where I have internet access.

    We have 3 vectors, each with an x and a y component, because we're in 2 dimensions.

    All values are defined in world coordinate space.

    The vectors are,

    TX, TY, and OM.
    Each of these is an (x,y) pair.

    Referring to my diagram, where O is the meeting point of TX and TY

    OM = M.x-O.x , M.y-O.y

    These are the two (2 dimensional) dot product calculations
    DX=TX.x*OM.x + TX.y*OM.y
    DY=TY.x*OM.x + TY.y*OM.y

    They describe the mouse point in terms of the coordinate system defined by the texture origin, and the texture axes.

    You can then test that point against the texture rectangle, convert it to integer coordinates, etc, and then use those coordinates for the Sub Image...

    CJ

  10. #10
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    2,833

    Re: Sub Image on Texture

    Give him the .h/.c files, that's all he wants. He wants to copy and paste, he doesn't want to know how it works.
    BTW, don't mind us - we're just reading this because we thought it was an advanced opengl topic....can't think why we'd think that.
    Knackered

Page 1 of 2 12 LastLast

Similar Threads

  1. YUV 10 bit texture image
    By akihiro in forum OpenGL ES
    Replies: 0
    Last Post: 08-22-2017, 03:46 AM
  2. Image not as texture
    By Tester358 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-16-2012, 01:32 PM
  3. problem in drawing a texture image(monochrome image)
    By philm in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-07-2003, 10:45 AM
  4. problem in drawing a texture image ( monochrome image)
    By philm in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 03-07-2003, 09:00 AM
  5. how to texture map with an image of 450*720?
    By yqs2002 in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 11-19-2002, 01:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean