PDA

View Full Version : Textures in ortho mode



CocoaSpud
08-15-2000, 11:36 AM
I am trying to auto-generate a series of polygons in orthographic mode with the same BMP texture mapped to each one. On some of polygons, the image isn't drawn like I am expecting. I used the NeHe Lesson 6 as a basis.

Some portions of the code:

#define IMAGE_COUNT 10
struct FPOINT
{
float x;
float y;
};
struct POLY_DATA
{
FPOINT ul;
FPOINT ur;
FPOINT ll;
FPOINT lr;
};
POLY_DATA TextureCoords =
{
{ 0.0, 1.0 }, { 1.0, 1.0 },
{ 0.0, 0.0 }, { 1.0, 0.0 }
};
POLY_DATA VertexList[IMAGE_COUNT] =
{
{ { 2.0,2.0 }, { 18.0,2.0 },
{ 2.0,21.0 }, { 18.0,21.0 } },
{ { 132.0,2.0 }, { 148.0,2.0 },
{ 132.0,21.0 }, { 148.0,21.0 } },
{ { 262.0,2.0 }, { 278.0,19.0 },
{ 262.0,60.0 }, { 278.0,38.0 } },
{ { 392.0,19.0 }, { 408.0,2.0 },
{ 392.0,38.0 }, { 408.0,60.0 } },
{ { 522.0,2.0 }, { 570.0,2.0 },
{ 522.0,60.0 }, { 570.0,60.0 } },
{ { 2.0,146.0 }, { 34.0,183.0 },
{ 2.0,279.0 }, { 34.0,242.0 } },
{ { 132.0,183.0 }, { 164.0,146.0 },
{ 132.0,242.0 }, { 164.0,279.0 } },
{ { 262.0,146.0 }, { 374.0,146.0 },
{ 262.0,280.0 }, { 374.0,280.0 } },
{ { 392.0,147.0 }, { 424.0,184.0 },
{ 392.0,357.0 }, { 424.0,320.0 } },
{ { 522.0,184.0 }, { 554.0,147.0 },
{ 522.0,320.0 }, { 554.0,357.0 } }
};

AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if (!Filename)
return NULL;
File=fopen(Filename,"r");
if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}

int LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if (TextureImage[0]=LoadBMP("Data/wall.bmp"))
{
Status=TRUE;
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}
if (TextureImage[0])
{
if (TextureImage[0]->data)
free(TextureImage[0]->data);
free(TextureImage[0]);
}
return Status;
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int InitGL(GLvoid)
{
if (!LoadGLTextures())
return FALSE;
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
return TRUE;
}

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texture[0]);
for (int i=0;i<IMAGE_COUNT; i++)
{
glBegin(GL_QUADS);
glTexCoord2f(TextureCoords.ll.x, TextureCoords.ll.y);
glVertex2f(VertexList[i].ll.x, VertexList[i].ll.y);
glTexCoord2f(TextureCoords.lr.x, TextureCoords.lr.y);
glVertex2f(VertexList[i].lr.x, VertexList[i].lr.y);
glTexCoord2f(TextureCoords.ur.x, TextureCoords.ur.y);
glVertex2f(VertexList[i].ur.x, VertexList[i].ur.y);
glTexCoord2f(TextureCoords.ul.x, TextureCoords.ul.y);
glVertex2f(VertexList[i].ul.x, VertexList[i].ul.y);
glEnd();
}
return TRUE;
}

And a picture of the problem, notice the bend in some of the images.

http://uaforever.rpgmaker.net/texprob.jpg

Any ideas on what would fix this?

Richard

08-17-2000, 02:43 AM
it looks to me that you haven't enabled perspective correction.
see glHint(...) and glEnable(...) on how to enable that.

Relic
08-17-2000, 05:41 AM
Once again...
http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/000971.html
http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/000685.html

The quads are just plain flat and the rasterizer has no idea how to do perspective correction without a perspective.

You have to specify glTexCoord4f with some other value for the homogenous coordinate then 1.0.

The correct values can be obtained from a real 3D matrix transformation resulting in the same screen coordinates but with depth in the picture.

With these wall images, this seems to start a game engine. If that's the case use a 3D model and enable perspective correction and the artifact goes away.

CocoaSpud
08-17-2000, 10:15 AM
Thanks for the help. My goal was to create wall sets for an existing game using textures. I used ortho2d so that I could create polygons at the exact x,y where the game expected each wall to be located. Since I don't know about 3d I was hoping to avoid dealing with it.

Are there any tutorials on finding the correct value for q in glTexCoord4f(s,t,r,q)?

Richard