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 9 of 9

Thread: Fit texture to window

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    5

    Fit texture to window

    Hi,
    i need to fit my texture to all window (displaying video stream). My display function is

    Code :
    void simpleUdpVisual::paintGL()
     {
    	if (data!=NULL)
    	{
    		glEnable (GL_TEXTURE_2D);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
    		glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
     
    		glBegin(GL_QUADS);
            		glTexCoord2f(0.0f, 0.0f); glVertex2f( -1.0,  1.0);
            		glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0,  1.0);
            		glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0,  -1.0);
            		glTexCoord2f(0.0f, 1.0f); glVertex2f( -1.0,  -1.0);
        		glEnd();
     
        		glFlush();
    	}
     
     }

    and ogl init function

    Code :
     void simpleUdpVisual::initializeGL()
     {
    	qglClearColor(Qt::red);
     
    	glEnable (GL_TEXTURE_2D);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     }

    If i run that on my PC it works fine and all video is fited into window.
    On laptop: top right corner of video is out of window screen.

    Pls, help.

  2. #2
    Member Regular Contributor strattonbrazil's Avatar
    Join Date
    Jun 2007
    Location
    Los Angeles, CA
    Posts
    306

    Re: Fit texture to window

    Here's a few ideas. First, you don't need to call glTexParameter over and over. It only needs to be called once. Just call it right after you bind the texture (where you load in the image). Your bind call should be in the loop. Not glTexParameter.

    It looks like you're not setting up your projection matrices. In the OpenGL specification the default matrices are the identity matrix for the projection and modelview. However, a driver might for whatever reason not do this so you can can set them as the identity to make sure. When you say the upper right corner, do you mean just the upper right and the upper left and lower right are fine? Or do you mean just the lower left is correct?

    Also, is this really all your code? Where do you create the texture? Where do you bind it?

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2009
    Location
    Princeton, NJ
    Posts
    19

    Re: Fit texture to window

    Are you using gluPerspective? Try this before your paintGL function:

    int viewport[4];
    viewport=GetViewport();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,viewport[2],viewport[3],0,0,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glDisable(GL_DEPTH_TEST);

  4. #4
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    5

    Re: Fit texture to window

    At first, thanks for helping me.

    A was trying something. This code works fine with initial size of window. After resizing window texture has always same size and not fill all window. (texture fill correctly all window only at initial size of window)

    screen:

    I am not OGL programmer but I need to show my stream video.

    All my .cpp code is here. header is not important. "data" is "unsigned char *"

    Code :
    #include <QtGui>
    #include "simpleudpvisual.h"
     
    #include <iostream>
    using namespace std;
     
    simpleUdpVisual::simpleUdpVisual(int w, int h, int bs)
    {
    	data = NULL;
    	height = h;
    	width = w;
    	buff_size = bs;
     
    	socket.connectToHost(QHostAddress("127.0.0.1"),41414);
     
    	if (socket.waitForConnected(1000)) cout << "Connected" << endl;
    	else cout << "Cant connect" << endl;
     
    	data = (unsigned char*) malloc(w*h*sizeof(unsigned char)*3);
     
    	socket.setReadBufferSize(buff_size);
    	connect(&amp;socket,SIGNAL(readyRead()),this,SLOT(readData()));
     
    	//setFixedSize(w,h);
    }
     
    void simpleUdpVisual::readData()
    {
    	static int writed = 0;
    	static int frm = 0;
    	int size = width*height*3;
     
    	int recv;
     
    	while(socket.bytesAvailable()>0)
    	{
    		if (writed+socket.bytesAvailable()>size)
    			{
    			writed = 0;
    			repaint();
    			}
     
    		recv = socket.read( (char*) data+writed ,buff_size );
     
    		writed+= recv;
    	}
    }
     
    void simpleUdpVisual::newData(unsigned char * d)
    {
    	data = d;
    	repaint();
    }
     
     void simpleUdpVisual::initializeGL()
    {
    	qglClearColor(Qt::red);
     
    	glEnable (GL_TEXTURE_2D);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    }
     
    void simpleUdpVisual::resizeGL(int width, int height)
    {
    }
     
    void simpleUdpVisual::paintGL()
    {
    	if (data!=NULL)
    	{
    		glMatrixMode (GL_MODELVIEW);
    		glPushMatrix ();
    		glLoadIdentity ();
    		glMatrixMode (GL_PROJECTION);
    		glPushMatrix ();
    		glLoadIdentity ();
     
    		glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
     
    		glBegin(GL_QUADS);
            		glTexCoord2f(0.0f, 0.0f); glVertex2f( -1.0,  1.0);
            		glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0,  1.0);
            		glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0,  -1.0);
            		glTexCoord2f(0.0f, 1.0f); glVertex2f( -1.0,  -1.0);
        		glEnd();
     
        		glFlush();
    	}
     
    }
     
    simpleUdpVisual::~simpleUdpVisual()
    {
    }

  5. #5
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Fit texture to window

    What about glViewport ? Needed to enlarge the part on window that GL can render to.

  6. #6
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    5

    Re: Fit texture to window

    Quote Originally Posted by ZbuffeR
    What about glViewport ? Needed to enlarge the part on window that GL can render to.
    It works. Thanks a lot.

    Do you think that code is clear?

    I am going to transform my code to use Pixel Buffer Object. I hope that drawing was faster and CPU usage lower.

  7. #7
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Fit texture to window

    Unless you are doing HD video, I am not sure PBO will have a big performance impact.

    Do you find your current implementation too slow ?

  8. #8
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    5

    Re: Fit texture to window

    I am working on on-line video tracking system and it needs to compute with CPU.

    Video is not slow, but i need to reduce CPU as much as possible.

    If you have any ideas please post.
    Thx

  9. #9
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Fit texture to window

    Yeah sure PBO may help, just benchmark it on your app.

Posting Permissions

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