Skip to main content
gamedev.net
🔒 Locked

Stretch a texture.

Started by bako Nov 29, 2008 at 7:46 AM 4 replies 4.8k views
Original Post
bako
bako
hi all. I've some issues to do with the texture, I've a texture and i want to apply that on a quad. I've look at the examples done in the tutorials but i want to do something else. i want to stretch the texture on the y axis of the quad and repeat the texture on the x axis of the quad. it's possible? tnxs
bako
bako
i found a solution, but i don't know if it's a good way.
so:
first save the width of the texture when the texture is loaded.
then, when I've to display the texture, i compute the value of the point like this:
float value = (float)glc.getWidth()/w; 

where w is the width of texture.
and draw the quad with texture:

         gl.glBegin(GL.GL_QUADS);  // Start Drawing A Textured Quad        gl.glTexCoord2f(-1.0f, -1.0f);        gl.glVertex2f(-2.0f, -2.0f);  // Bottom Left        gl.glTexCoord2f(value, -1.0f);        gl.glVertex2f(2.0f, -2.0f);  // Bottom Right        gl.glTexCoord2f(value, 1.0f);        gl.glVertex2f(2.0f, 2.0f);  // Top Right        gl.glTexCoord2f(-1.0f, 1.0f );        gl.glVertex2f(-2.0f, 2.0f);  // Top Left        gl.glEnd();  // Done Drawing The Quad


it's a good way to proceed??
Funkyjive
Funkyjive
One thing you can avoid is doing the division at the top. If you are doing what I think you are... you are dividing the textures width by its width which would always result in 1. This still works and gives correct coords that can be mapped, however it can be avoided.

Here is one way to think about it when just learning. Get a sheet of paper and set it in front of you. Let the bottom-left corner have coordinates of (0,0). Now you want to stretch an image across the paper to the bottom-right corner. This corner would have texture coordinates of (x,0) where x is how many times it repeats. This means the coordinate (2,0) would repeat your image twice and coordinates of (1,0) for this corner would be stretching the image to this corner with no repeating. The top-left corner would be similar with coordinates of (0,y) where y is how many times it repeats vertically. Finally, the top-right corner would be (x,y) with x and y having the same meaning as before.
With this same example how many full images would be displayed with the top-right coordinates of (2,3)? (Assuming (0,0) (2,0) (0,3) as the other coordinates) The answer would be 6 because it repeats twice horizontally and 3 times vertically.

Also, negative and fractal quantities for texture coords are legal. For example .5 would only map half of your image and -1 would mirror it (Assuming correct order of winding).

Hope this quick rundown of texture coordinates helps. My best advice would be to hard code different numbers in for the texture coordinates and see if it matches what you think it should.
"I would rather be in the boat with a drink on the rocks, than in the drink with a boat on the rocks" -Unknown
mako_5
mako_5
Try:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Repeat on X axisglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);  // Stretch on Y axis

Then texture coordinates are clamped between 0 and 1 on the x axis, and for example, going from 0 to 2 on the y axis will repeat the texture 2 times in the y direction.
bako
bako
Quote:
Original post by mako_5
Try:

*** Source Snippet Removed ***
Then texture coordinates are clamped between 0 and 1 on the x axis, and for example, going from 0 to 2 on the y axis will repeat the texture 2 times in the y direction.


i try, but maybe i miss something. so, i've done like this:

load the texutre
   public void loadGLTextures(GL gl) throws IOException {        String tileNames[] = {"demos/data/images/logo.png"};        gl.glGenTextures(1, textures, 0);        TextureReader.Texture texture = TextureReader.readTexture(tileNames[0]);        //Create Nearest Filtered Texture        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT); // Repeat on X axis        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);  // Stretch on Y axis        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0,                3,                texture.getWidth(),                texture.getHeight(),                0,                GL.GL_RGB,                GL.GL_UNSIGNED_BYTE,                texture.getPixels());    }


display

 public void display(GLAutoDrawable gld) {        GL gl = glc.getGL();        // Clear The Screen And The Depth Buffer        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);        gl.glLoadIdentity();       // Reset The View        gl.glTranslatef(0.0f, 0.0f, -2.0f);  // Move Into The Screen 5 Units        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]); // Select Our Logo Texture        gl.glBegin(GL.GL_QUADS);  // Start Drawing A Textured Quad        gl.glTexCoord2f(-2.0f, -2.0f);        gl.glVertex2f(-2.0f, -2.0f);  // Bottom Left        gl.glTexCoord2f(2.0f, -2.0f);        gl.glVertex2f(2.0f, -2.0f);  // Bottom Right        gl.glTexCoord2f(2.0f, 2.0f);        gl.glVertex2f(2.0f, 2.0f);  // Top Right        gl.glTexCoord2f(-2.0f, 2.0f);        gl.glVertex2f(-2.0f, 2.0f);  // Top Left        gl.glEnd();  // Done Drawing The Quad        gl.glEnable(GL.GL_BLEND);  // Enable Blending        gl.glDisable(GL.GL_DEPTH_TEST);  // Disable Depth Testing    }


but this is the result http://bako.interfree.it/result.jpg
bako
bako
emm, other question: there's no possibility to rotate a texture? i've to rotate the matrix?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.