PDA

View Full Version : vbo obj loader, problem with textures



newone
02-06-2012, 10:40 AM
Firstly, like always i want to apologize about my language;]


http://img859.imageshack.us/img859/3476/asdqoe.png

http://img190.imageshack.us/img190/7605/asd2gy.png

http://img252.imageshack.us/img252/517/skinp.jpg

First img shows texture on cube, everything is looking fine
Second img shows texture on apple - obj takes from one of webs with free obj models, like you see texture is looking bad, this texture i take from website with obj, in first img i show this texture on cube
Third img is showing texture from first and second img

Like you see something is wrong in cube texture is looking fine, but in apple from where is this texture i get only one color

i have it in all obj models, which i download

function which is setting texture:


void Objloader::SetTexture()
{
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture.m_gl_texture_id);
glBindTexture(GL_TEXTURE_2D, texture.m_gl_texture_id);
glTexImage2D(GL_TEXTURE_2D,
0,
4, //GL_RGBA,
texture.getWidth(),
texture.getHeight(),
0,
GL_RGB,
GL_UNSIGNED_BYTE,
texture.getPixmapPointer());

//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}


to load img i use devil, but like you see img is showing, so i dont show function, which is loading img

vbo_init:


glGenBuffers(1, &vb);
glBindBuffer(GL_ARRAY_BUFFER, vb);
glBufferData(GL_ARRAY_BUFFER, sizeof(vo_data), vo_data, GL_STREAM_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(xyz), BUFFER_OFFSET( offsetof(xyz, x) ));
glTexCoordPointer(2, GL_FLOAT, sizeof(xyz), BUFFER_OFFSET( offsetof(xyz, u) ));
drawing:

glDrawArrays( GL_TRIANGLES, 0, sth );
structure:


struct xyz{
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat u;
GLfloat v;
};

texcoords, are fine, i checked it


ok the problem is with texcoords, because they only work on 1 or 0, when it is for example 0.6743, texture dont work, i don't know why

BionicBytes
02-07-2012, 04:45 AM
glGenBuffers(1, &vb);
glBindBuffer(GL_ARRAY_BUFFER, vb);
glBufferData(GL_ARRAY_BUFFER, sizeof(vo_data), vo_data, GL_STREAM_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(xyz), BUFFER_OFFSET( offsetof(xyz, x) ));
glTexCoordPointer(2, GL_FLOAT, sizeof(xyz), BUFFER_OFFSET( offsetof(xyz, u) ));


Your VBO setup code is missing one thing - which may not be important as it depends upon if the state has been modified else where.
glClientActiveTextureARB(GL_TEXTURE0);
before glEnableClientState(GL_TEXTURE_COORD_ARRAY).....


I'm not sure what these compile to:
BUFFER_OFFSET( offsetof(xyz, x) ));
and
BUFFER_OFFSET( offsetof(xyz, u) ));

but you should check they compile to 0 and 12 respecively as the texture coordinates are 12 bytes from the start of the structure.


I don't know what you are rendering with (fixed func or shader) but the VBO must be bound and pointers setup when you render the model as below:


glBindBuffer(GL_ARRAY_BUFFER, vb);
glEnableClientState(GL_VERTEX_ARRAY);
glClientActiveTextureARB(GL_TEXTURE0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(xyz), BUFFER_OFFSET( offsetof(xyz, x) ));
glTexCoordPointer(2, GL_FLOAT, sizeof(xyz), BUFFER_OFFSET( offsetof(xyz, u) ));

.....render.....

glDisbleClientState(GL_VERTEX_ARRAY);
glClientActiveTextureARB(GL_TEXTURE0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);

The VBO allocation code you posted is not enough and at render time you must perform the above code to render the model properly.