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: Select all
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);
}
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.