 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Tue Sep 19, 2006 8:07 pm Post subject: How to correctly aply UV coords for texture? |
|
|
Hi folks,
I use an .obj file to render on the screen. Now that my vertex problems are over i'm having problems with my texture coordinates :(
see the pictures below. the first is how it should be and the second is how it is displayed on my psp :S
good:
and the wrong one:
Here is the code is use for loading and rendering the obj file (obj file is triangulated)
| Code: | void LoadObjectTriangles(const char *filename, MeshC *tmpObj) {
// loop through the mesh and find groups.
char ReadBuffer[256];
unsigned int vc=0,nc=0,tc=0, fc=0;
unsigned int i=0;
unsigned int iNumberOfFaces, iNumberOfVertices, iNumberOfTexCoords;
iNumberOfGroups = 0;
iNumberOfFaces = 0;
FILE *fp = NULL;
if ((fp = fopen(filename, "rb")) == NULL) { iNumberOfGroups = -1; return; }
while(!feof(fp))
{
fgets(ReadBuffer, 256, fp);
if (strncmp("g default", ReadBuffer, 9) == 0 )
{
iNumberOfGroups++;
}
else if (strncmp("v ", ReadBuffer, 2) == 0 )
{
vc++;
}
else if (strncmp("vt ", ReadBuffer, 3) == 0 )
{
tc++;
}
else if (strncmp("f ", ReadBuffer, 2) == 0 )
{
fc++;
}
}
fclose(fp);
iNumberOfFaces = fc;
iNumberOfVertices = vc;
iNumberOfTexCoords = tc;
ObjVertex Vertices[ iNumberOfVertices ];
ObjTexCoord TexCoords[ iNumberOfTexCoords ];
ObjFaceNew Faces[ iNumberOfFaces ];
iNumberOfGroups = 0;
vc=0;
nc=0;
tc=0;
fc=0;
if ((fp = fopen(filename, "rb")) == NULL) { iNumberOfGroups = -1; return; }
while(!feof(fp))
{
fgets(ReadBuffer, 256, fp);
if (strncmp("g default", ReadBuffer, 9) == 0 )
{
if (iNumberOfGroups == 0) {
}
else {
iNumberOfGroups++;
}
}
else if (strncmp("v ", ReadBuffer, 2) == 0 )
{
sscanf((ReadBuffer+2), "%f%f%f",&Vertices[ vc ].x, &Vertices[ vc ].y, &Vertices[ vc ].z);
vc++;
}
else if (strncmp("vt ", ReadBuffer, 3) == 0 )
{
sscanf((ReadBuffer+3), "%f%f",&TexCoords[ tc ].u, &TexCoords[ tc ].v);
tc++;
}
else if (strncmp("f ", ReadBuffer, 2) == 0 )
{
char *pSplitString = NULL;
int Waste;
i=0;
pSplitString = strtok((ReadBuffer+2)," \t\n");
do {
sscanf((pSplitString), "%d/%d/%d",&Faces[ fc ].vertices[ i ], &Faces[ fc ].textc[ i ],&Waste);
Faces[ fc ].textc[ i ] -= 1; // 1 down because the obj file objects start at 1 and arrays start at 0
Faces[ fc ].vertices[ i ] -= 1;
pSplitString = strtok(NULL," \t\n");
i += 1;
}
while( pSplitString );
fc++;
}
}
fclose(fp);
// All is read now for the optimization :)
tmpObj->Faces = (ObjFaceC*)malloc(iNumberOfFaces * sizeof(ObjFaceC));
tmpObj->Vertices = (ObjVertexB*)malloc(iNumberOfFaces * 3 * sizeof(ObjVertexB));
tmpObj->iNumberOfFaces = iNumberOfFaces;
int j,k,l;
l = 0;
for (j=0;j<iNumberOfFaces;j++)
{
for (k=0; k<3; k++)
{
tmpObj->Vertices[l].u = TexCoords[Faces[j].textc[k]].u;
tmpObj->Vertices[l].v = TexCoords[Faces[j].textc[k]].v;
tmpObj->Vertices[l].color = 0xffffff00;
tmpObj->Vertices[l].x = Vertices[Faces[j].vertices[k]].x;
tmpObj->Vertices[l].y = Vertices[Faces[j].vertices[k]].y;
tmpObj->Vertices[l].z = Vertices[Faces[j].vertices[k]].z;
l++;
}
}
sprintf(sBuffer, "Textures/UVtexturesHousePlatDak1024type3.png");
tmpObj->texture = loadImage(sBuffer);
sceKernelDcacheWritebackInvalidateAll();
}
void RenderObjectTriangles(MeshC *tmpObj) {
matrix_identity((float*)&world);
matrix_translate((float*)&world,0,0,0);
sceGuSetMatrix(GU_MODEL,&world);
sceGuTexMode(GU_PSM_8888, 0 ,0 ,0);
sceGuTexImage(0,tmpObj->texture->textureWidth,tmpObj->texture->textureHeight, tmpObj->texture->textureWidth, (void*)tmpObj->texture->data);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
sceGuTexFilter(GU_LINEAR, GU_LINEAR);
sceGuTexScale(1.0f, 1.0f);
sceGuTexOffset(0.0f, 0.0f);
//float u = 1.0f / ((float)tmpObj->texture->textureWidth);
//float v = 1.0f / ((float)tmpObj->texture->textureHeight);
//float u = ((float)tmpObj->texture->textureWidth);
//float v = ((float)tmpObj->texture->textureHeight);
//sceGuTexScale(u, v);
sceGuDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,tmpObj->iNumberOfFaces*3,0,tmpObj->Vertices);
}
|
Hope someone can help me out
greets ghoti
PS the blueish is because of lighting that is not the problem i mean its the strange way the texture is projected on my mesh.
scaling does not seem to work the way i commented out. _________________ My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Tue Sep 19, 2006 10:18 pm Post subject: |
|
|
This is kinda common problem. The problem is that different APIs define the texture in different ways. Some assume that the 0,0 texture coordinates are top-left, some bottom-left.
First make sure your texture loading stuff loads the texture the right way, and then adjust the texture coordinates to match it, flipping the V coordinate as needed. -V or 1-V should both do the trick.
Unfortunately I cant remember if the psp uses the same origin as opengl (bottom-left) or was it one of those top-left things. |
|
| Back to top |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Tue Sep 19, 2006 10:24 pm Post subject: |
|
|
Thanks, that did the trick, its funny i tried everything with offsets and scaling but i forgot the v flip of which i have read so much in pc programming :S
thanks _________________ My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|