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

Started by
1 comment, last by nickme 7 years, 9 months ago

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

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

hi Nanoha

thanks for your reply.

I will take time to study your suggestions.

This topic is closed to new replies.

Advertisement