Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Rendering two textures with different dimensions ?

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    3

    Rendering two textures with different dimensions ?

    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:

    Code :
    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.

    Code :
    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_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    	}
    }


    Second, drawtexture():

    Code :
    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.

    Code :
    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.
    Last edited by _Shepherd; 11-02-2012 at 01:52 AM.

  2. #2
    Intern Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    97
    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 ??

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

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

    cu
    uwi
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    Free Programming Helpline: www.programming-hotline.com
    ---------------------------------------------------------
    A Friday Night Programmer: www.uwiworld.net

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    3
    Quote Originally Posted by uwi2k2 View Post
    but why are you using the texture dimensions as a vertax information ??

    Code :
    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
    Code :
    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
    Code :
    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 !!!

  4. #4
    Intern Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    97
    Quote Originally Posted by _Shepherd View Post
    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
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    Free Programming Helpline: www.programming-hotline.com
    ---------------------------------------------------------
    A Friday Night Programmer: www.uwiworld.net

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    3
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •