Jump to content
  • Advertisement
Sign in to follow this  
nickme

vs 2015 how to blit a subset of texture to a specific position(x,y)

This topic is 749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi

 

I can display all the image to the whole screen, but I dont know how to render a rectangular in a texture to a specific screen location.

 

gluOrtho2d(0, sizex, sizeY, 0);

...

glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex2i(0, 0);
glTexCoord2d(0.0, 1.0); glVertex2i(0, sizeY);
glTexCoord2d(1.0, 1.0); glVertex2i(sizeX, sizeY);
glTexCoord2d(1.0, 0.0); glVertex2i(sizeX, 0);
glEnd();
 
let's say if i want to render a image position of (40, 40) with w/h (277, 82) to position (100, 100)
do i change the value in glTexCoord2d or glVertex2i?
thank in advance for your help.
Edited by nickme

Share this post


Link to post
Share on other sites
Advertisement

You would need to change both.

 

Tex coords say what part of the image you wish to display and the vertex says where/how big the part you are displaying will be.

 

For example, if you wanted to draw the top left of the image in the bottom right of the screen you could do something like:

glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex2i(sizeX/2, sizeY/2);
glTexCoord2d(0.0, 0.5); glVertex2i(sizeX/2, sizeY);
glTexCoord2d(0.5, 0.5); glVertex2i(sizeX, sizeY);
glTexCoord2d(0.5, 0.0); glVertex2i(sizeX, sizeY/2);
glEnd();

I've used your tex coordinate layout but bare in mind that OpenGl will use the bottom left of a texture as 0/0 rather than the top right so it may be flipped. It depends how you have setup your projection (I see you have the projection but I get too confused trying to work out which way around it all is, I believe you have top/left being 0,0?). Bare than in mind with the example I've given as it could be flipped but hopefully it should give you an idea of where to start from.

 

When building it up yourself I would advise changing the texture coordinates first until you get the desired result and then move it around. 

 

This should do what you want but you will probably have to tweak it a bit (it might be flipped upside down).

gluOrtho2d(0, sizex, sizeY, 0);

float texStartU = 40/imageWidth;
float texStartV = 40/imageHeight;
float texEndU = (40+277)/imageWidth;
float texEndV = (40+82)/imageHeight;
glBegin(GL_QUADS);
glTexCoord2d(texStartU, texEndV); glVertex2i(100, 100);
glTexCoord2d(texStartU, texStartV); glVertex2i(100, sizeY);
glTexCoord2d(texEndU, texStartV); glVertex2i(sizeX, sizeY);
glTexCoord2d(texEndU, texEndV); glVertex2i(sizeX, 100);
glEnd();
Edited by Nanoha

Share this post


Link to post
Share on other sites

hi Nanoha

 

thanks for your reply.

 

I will take time to study your suggestions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!