PDA

View Full Version : Pixel precise rendering of textures



M/\dm/\n
01-21-2008, 02:28 PM
I have a problem and since I haven't been coding in GL for quite some time I could really use some help.

The problem is that I need to apply a GUI over rendered screen and this GUI is in texture. The second problem is that I can't rely on all new extensions, I can only guarantee it's GL 1.1.

I remember that pixel precise rendering was an issue before, but since it has been so long since I was doing something like that, I can't even remember where to look.

I tried rendering to screen via setting Ortho @ texture length/width and using tex coords of 0.f->1.f but the texture is stretched and looks ugly.

What am I doing wrong?

-NiCo-
01-21-2008, 02:39 PM
Try this:



glViewport(0,0,texwidth,texheight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glTexcoord2f(0.0f,0.0f);
glVertex2f(-1.0f,-1.0f);
glTexcoord2f(1.0f,0.0f);
glVertex2f( 1.0f,-1.0f);
glTexcoord2f(1.0f,1.0f);
glVertex2f( 1.0f, 1.0f);
glTexcoord2f(0.0f,1.0f);
glVertex2f(-1.0f, 1.0f);
glEnd();



or



glViewport(0,0,windowwidth,windowheight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,windowwidth,0,windowheight,-1.0,1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glTexcoord2f(0.0f,0.0f);
glVertex2f(0.0f,0.0f);
glTexcoord2f(1.0f,0.0f);
glVertex2f( texwidth,0.0f);
glTexcoord2f(1.0f,1.0f);
glVertex2f( texwidth, texheight);
glTexcoord2f(0.0f,1.0f);
glVertex2f(-1.0f, texheight);
glEnd();




N.

M/\dm/\n
01-21-2008, 02:55 PM
Thank you, but it seems to match the code I was using and I'm still receiving the same minifying problem and skipped lines in texture...

Here is the code I was using

glClearColor(1.f, 1.f, 1.f, 1.f);
glClearDepth(-100.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 640.0, 0.0, 480.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
GLuint tex = ilutGLBindTexImage();
glBindTexture(GL_TEXTURE_2D, tex);

glColor4f (1.f, 1.f, 1.f, 1.f);

glBegin (GL_QUADS);

glTexCoord2f(0.f, 0.f);
glVertex3f (offset, 0.f, -.8f);

glTexCoord2f(1.f, 0.f);
glVertex3f (640.f-offset, 0.f, -.8f);

glTexCoord2f(1.f, 1.f);
glVertex3f (640.f-offset, 480.f, -.8f);

glTexCoord2f(0.f, 1.f);
glVertex3f (offset, 480.f, -.8f);

glEnd ();
glDeleteTextures(1, &tex);
ilClearImage();

if(1)
{
ilBindImage(WatermarkHandle);
glBindTexture(GL_TEXTURE_2D, tex1);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glColor4f (1.f, 1.f, 1.f, 1.f);

glBegin (GL_QUADS);

glTexCoord2f(0.f, 0.f);
glVertex3f (1.f, 1.f, -.7f);

glTexCoord2f(1.f, 0.f);
glVertex3f (640.f, 1.f, -.7f);

glTexCoord2f(1.f, 1.f);
glVertex3f (640.f, 480.f, -.7f);

glTexCoord2f(0.f, 1.f);
glVertex3f (1.f, 480.f, -.7f);

glEnd ();
glDisable(GL_BLEND);
//glDeleteTextures(1, &tex1);
}

This is the setup I had... And it doesn't get better if I use 0.0f in glVertex2f... Viewport is set via glViewport(0, 0, 640, 480);

I tried everything...


glBegin (GL_QUADS);

glTexCoord2f(0.f, 0.f);
glVertex3f (0.f, 0.f, -.7f);

glTexCoord2f(1.f, 0.f);
glVertex3f (640.f, 0.f, -.7f);

glTexCoord2f(1.f, 1.f);
glVertex3f (640.f, 480.f, -.7f);

glTexCoord2f(0.f, 1.f);
glVertex3f (0.f, 480.f, -.7f);

glEnd ();

:(

and
glBegin (GL_QUADS);

glTexCoord2f(0.f, 0.f);
glVertex3f (-1.f, -1.f, -.7f);

glTexCoord2f(1.f, 0.f);
glVertex3f (640.f, -1.f, -.7f);

glTexCoord2f(1.f, 1.f);
glVertex3f (640.f, 480.f, -.7f);

glTexCoord2f(0.f, 1.f);
glVertex3f (-1.f, 480.f, -.7f);

glEnd ();

-NiCo-
01-21-2008, 03:28 PM
Well, the last piece of code doesn't make sense at all. The first piece of code only makes sense if offset is 0 or you use

640.f+offset

instead of:

640.f-offset

The second piece of code works if your viewport is WxH, your ortho call is WxH and your texture is 640x480 where W and H are arbitrary values.

N.

Ffelagund
01-22-2008, 01:18 AM
http://msdn2.microsoft.com/en-us/library/ms537007.aspx

zed
01-22-2008, 01:26 AM
opengl1.1 thus no rectangle tetxures

>>I tried rendering to screen via setting Ortho @ texture length/width and using tex coords of 0.f->1.f but the texture is stretched and looks ugly.

i assume thats cause youre rendering a texture eg 512x512 into a 640x480 area hence the stretching, a possible soltion is to use part of a larger texture eg 1024x512 + use only 640x480 of it

M/\dm/\n
01-22-2008, 02:08 AM
Well, the last piece of code doesn't make sense at all. The first piece of code only makes sense if offset is 0 or you use

640.f+offset

instead of:

640.f-offset

The second piece of code works if your viewport is WxH, your ortho call is WxH and your texture is 640x480 where W and H are arbitrary values.

The idea is that I render one texture which is centered, and might not match 640x480 view port, then 640x480 GUI over it. The rendering with offset part is for the first, centered, texture.

View port is 640x480, ortho is set up to 640x480 as well.

I guess it really might be the NPOT textures that are messing up as Zed said. But as far as I remember going to the nearest POT had it's share of issues as well. But I will definitely try to do that.

Thank you!

-NiCo-
01-22-2008, 03:17 AM
Right, I missed the part where you said there's only OpenGL 1.1 support. Don't tell me you're using gluBuildMipmaps to generate your textures? If not, you probably have OpenGL 1.4 or 2.0 support if you're able to load NPOT textures in a TEXTURE_2D target.
gluBuildMipmaps messes with your pixels, so unless the original size is POT you won't be able to do pixel-precise rendering.
N.

M/\dm/\n
01-22-2008, 04:18 AM
Oh, right, everything makes perfect sense now. Picture is built and bound via Devil library, I bet it uses some sort of gluMipMs behind the scenes, lines are getting ruined and after bunch of scaling everything is a total mess.

Gosh, after all this break from OpenGL I'm failing to understand even the simple things now.

Shame I'm bound to GL 1.1... Off to texture packing then :)

Thank you!