PDA

View Full Version : Fit texture to window



wydesenej
02-23-2010, 04:05 AM
Hi,
i need to fit my texture to all window (displaying video stream). My display function is



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



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.

strattonbrazil
02-23-2010, 10:36 AM
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?

Pziko
02-23-2010, 10:37 AM
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);

wydesenej
02-24-2010, 03:00 AM
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: http://s2.imgupload.cz/img/203095/A92Xs/tracker.png

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 *"



#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(read Data()));

//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()
{
}

ZbuffeR
02-24-2010, 03:15 AM
What about glViewport ? Needed to enlarge the part on window that GL can render to.

wydesenej
02-24-2010, 03:23 AM
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 (http://www.songho.ca/opengl/gl_pbo.html). I hope that drawing was faster and CPU usage lower.

ZbuffeR
02-24-2010, 03:34 AM
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 ?

wydesenej
02-24-2010, 04:13 PM
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

ZbuffeR
02-24-2010, 04:22 PM
Yeah sure PBO may help, just benchmark it on your app.