Advertisement Jump to content
Sign in to follow this  

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

This topic is 929 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



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);


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);
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

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:

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);

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;
glTexCoord2d(texStartU, texEndV); glVertex2i(100, 100);
glTexCoord2d(texStartU, texStartV); glVertex2i(100, sizeY);
glTexCoord2d(texEndU, texStartV); glVertex2i(sizeX, sizeY);
glTexCoord2d(texEndU, texEndV); glVertex2i(sizeX, 100);
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!