PDA

View Full Version : Rendering two textures with different dimensions ?



_Shepherd
11-02-2012, 01:25 AM
I have two textures that I want to render in Glut, one is 71x96 and the other one is 75x105 pixels.
I'm having troubles rendering the second texture, which renders 71x96 instead of 75x105.

Here is my code...

First, I have a drawengine class to do the rendering:


drawengine::drawengine(float w, float h)
{
float xratio = 2 / w;
float yratio = 2 / h;

width = 1.0f;
height = 1.0f;

texture_w[0] = 71 * xratio;
texture_h[0] = 96 * yratio;

texture_w[1] = 75 * xratio;
texture_h[1] = 105 * yratio;

// create textures.
createtexture();
}

and createtexture() function to take care of loading the textures using SOIL library.


void drawengine::createtexture()
{
int index;
char* filename[2] = {"texture1.png", "texture2.png"};

for(index = 0; index < 2; index++)
{
// load an image file directly as a new OpenGL texture
textures[index] = SOIL_load_OGL_texture
(
filename[index],
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS /*| SOIL_FLAG_INVERT_Y*/ | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);

// check for an error during the load process
if(textures[index] == 0)
std::cout << "file not found";

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, textures[index]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}
}


Second, drawtexture():


bool drawengine::drawtexture(int index, float x, float y)
{
if((-width <= x && x <= width) && (-height <= y && y <= height))
{
// draw texture
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[index]);

glLoadIdentity();
glTranslatef(x, y, 0.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f , 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(texture_w[index], 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(texture_w[index],-texture_h[index], 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f,-texture_h[index], 0.0f);
glEnd();

glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);

return true;
}
else
return false;
}


Then, render(), the function I pass to glutDisplayFunc() and glutIdleFunc(). de is a pointer of type drawengine.


void render()
{
// OpenGL...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.1f, 0.9f, 0.3f, 0.0f);

de->drawtexture(/*texture1*/ 0, -0.9f, 0.8f); // draws a 71x96 quad. ok.
de->drawtexture(/*texture2*/ 1, -0.9f, 0.8f); // draws a 71x96 quad. ????!!

// swaps the buffers of the current window if double buffered.
glutSwapBuffers();
}

Thanks in advance.

uwi2k2
11-02-2012, 02:06 AM
hi,

i am quite shure that its not the problem of rendering but loading the texture.
anyway the texture should best be a pow of 2 ( 2,4,8,16,32,.. ) in width and height...

but why are you using the texture dimensions as a vertax information ??



glVertex3f(texture_w[index], 0.0f, 0.0f);

can you post a img of the result and mark what is wrong ?

cu
uwi

_Shepherd
11-02-2012, 03:22 AM
but why are you using the texture dimensions as a vertax information ??



glVertex3f(texture_w[index], 0.0f, 0.0f);


For texture1, I want to draw a quad of 71x96. I should do the following calculation to convert pixels to vertex

texture_w[0] = 71 * xratio = 71 * 2.0f / w = 71 * 2.0f / 600 = 0.236666f
texture_h[0] = 96 * yratio = 96 * 2.0f / h = 96 * 2.0f / 420 = 0.45714f

So

glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f , 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(texture_w[0], 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(texture_w[0],-texture_h[0], 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f,-texture_h[0], 0.0f);
equals

glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f , 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.236666f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.236666f,-0.45714f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f,-0.45714f, 0.0f);

Now that I drew the 1st quad with texture1 0.236666f x 0.45714f, I try to do the same thing for the 2nd quad 0.25f x 0.5f (texture_w[1] x texture_h[1])...
It doesn't work. I get a quad of 0.236666f x 0.45714f !!!

uwi2k2
11-03-2012, 06:50 AM
Now that I drew the 1st quad with texture1 0.236666f x 0.45714f, I try to do the same thing for the 2nd quad 0.25f x 0.5f (texture_w[1] x texture_h[1])...
It doesn't work. I get a quad of 0.236666f x 0.45714f !!!

hi,

pls post a screenshot and a zip of you project / code
so i can run it on my pc ...
sometimes written words are not enough .. :-)

cu
uwi

_Shepherd
11-03-2012, 11:57 PM
Hi.
Don't trouble yourself, I got the problem fixed. I had a getter func that was only returning the width and height of texture1. Thanks.