opengl game engine [help]

Started by
17 comments, last by Adam West 12 years ago
Your problem most likely resides here (as I have tried to point out several times before in multiple threads). Have you tried debugging the problem? (I'm inferring a Visual Studio/Windows setup here.)
Advertisement

Your problem most likely resides here (as I have tried to point out several times before in multiple threads). Have you tried debugging the problem? (I'm inferring a Visual Studio/Windows setup here.)


nope.avi

strcat is fine, its the way the object memory is handeled

[quote name='fastcall22' timestamp='1333434321' post='4927796']
Your problem most likely resides here (as I have tried to point out several times before in multiple threads). Have you tried debugging the problem? (I'm inferring a Visual Studio/Windows setup here.)


nope.avi

strcat is fine, its the way the object memory is handeled
[/quote]

throw strcat("Unable to load ", filename.c_str());

is not fine, read the strcat documentation, while it isn't the cause of your problem it will cause problems if a model fails to load.

i see a few more things:

you are assuming that you'll have 3 times as many vertices and tex coords as faces, this isn't necessarily the case. It shouldn't be possible to have more than 3x the facecount though so in worst case you're only wasting memory, the 3ds format however does support indices which you aren't using, (so i assume you are exporting it so that faces never share vertices (otherwise it wouldn't render properly) its not a very efficient way to do things though. (as your VBOs will be alot bigger than they need to be).
(Other model formats might also allow the number of texcoords to be different from the number of vertices (.3ds doesn't seem to support that though)
(Its still worth reading the vertex and texcoord count from the mesh data rather than making assumptions about it)


This part might warrant a error check and could possibly be the source of your problem:


if(mesh->texels)
{
memcpy(&texCoords[FinishedFaces*3 + i], mesh->texelL[face->points[ i ]], sizeof(Lib3dsTexel));
//NEW
}



If there are no texture coordinates exported in the 3ds file nothing will get copied to texCoords and you'll be using an uninitialized buffer (containing random data) which could be the cause of your wierd results.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
okay fixed the strcat to just a normal filename and removed the if(mesh->textels) but still no dice.


#include "3dsloader.h"
#include "shader.h"

Object::Object(const char* filename)
{
m_TotalFaces = 0;
m_model = lib3ds_file_load(filename);
// If loading the model failed, we throw an exception
if(!m_model)
{
cout << ("Unable to load ", filename);
}
}
Object::~Object()
{
if(m_model) // if the file isn't freed yet
lib3ds_file_free(m_model); //free up memory
}

void Object::GetFaces()
{
m_TotalFaces = 0;
Lib3dsMesh * mesh;
// Loop through every mesh.
for(mesh = m_model->meshes;mesh != NULL;mesh = mesh->next)
{
// Add the number of faces this mesh has to the total number of faces.
m_TotalFaces += mesh->faces;
}
}
void Object::CreateVBO()
{
assert(m_model != NULL);
// Calculate the number of faces we have in total
GetFaces();
// Allocate memory for our vertices and normals
Lib3dsVector * vertices = new Lib3dsVector[m_TotalFaces * 3];
Lib3dsVector * normals = new Lib3dsVector[m_TotalFaces * 3];
Lib3dsTexel* texCoords = new Lib3dsTexel[m_TotalFaces * 3];

Lib3dsMesh * mesh;
unsigned int FinishedFaces = 0;
// Loop through all the meshes
for(mesh = m_model->meshes;mesh != NULL;mesh = mesh->next)
{
lib3ds_mesh_calculate_normals(mesh, &normals[FinishedFaces*3]);
// Loop through every face
for(unsigned int cur_face = 0; cur_face < mesh->faces;cur_face++)
{

Lib3dsFace * face = &mesh->faceL[cur_face];
for(unsigned int i = 0;i < 3;i++)
{

memcpy(&texCoords[FinishedFaces*3 + i], mesh->texelL[face->points[ i ]], sizeof(Lib3dsTexel));
//NEW

memcpy(&vertices[FinishedFaces*3 + i], mesh->pointL[face->points[ i ]].pos, sizeof(Lib3dsVector));
}

FinishedFaces++;
}
}

// Generate a Vertex Buffer Object and store it with our vertices
glGenBuffers(1, &m_VertexVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VertexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsVector) * 3 * m_TotalFaces, vertices, GL_STATIC_DRAW);
// Generate another Vertex Buffer Object and store the normals in it
glGenBuffers(1, &m_NormalVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_NormalVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsVector) * 3 * m_TotalFaces, normals, GL_STATIC_DRAW);
// Generate a third VBO and store the texture coordinates in it.
glGenBuffers(1, &m_TexCoordVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_TexCoordVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsTexel) * 3 * m_TotalFaces, texCoords, GL_STATIC_DRAW);

// Clean up our allocated memory
delete vertices;
delete normals;
delete texCoords;

// We no longer need lib3ds
lib3ds_file_free(m_model);
m_model = NULL;
}
void Object::applyTexture(const char*texfilename)
{

textureObject = SOIL_load_OGL_texture(texfilename,SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_MIPMAPS);

glBindTexture(GL_TEXTURE_2D,textureObject);// use our newest texture
}

void Object::Draw() const
{



// Enable vertex, normal and texture-coordinate arrays.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);



// Bind the VBO with the normals.
glBindBuffer(GL_ARRAY_BUFFER, m_NormalVBO);
// The pointer for the normals is NULL which means that OpenGL will use the currently bound VBO.
glNormalPointer(GL_FLOAT, 0, NULL);

glBindBuffer(GL_ARRAY_BUFFER, m_TexCoordVBO);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, m_VertexVBO);
glVertexPointer(3, GL_FLOAT, 0, NULL);
// Render the triangles.
glDrawArrays(GL_TRIANGLES, 0, m_TotalFaces * 3);


glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
I didn't tell you to remove if (mesh-texels)..., i told you to handle the case where texels is 0 allthough now it should most likely crash instead if you don't have texture data exported, (not really a good thing but atleast it rules out a lack of texturecoordinates as the source of the problem).

Have you tried drawing a quad using the texture ? does that look correct ? (if not its a problem with how you load the texture)
What does the teapot look like in your 3d modelling application ? (screenshots would be nice)
What does the teapot look like if you re-import the .3ds file in your 3d modelling application ? (again screenshots)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
tried a pre uv'd turbosquid model to see if it was my fault but the same thing happened to it aswell, there must be something missing in my code??
Did you try changing your UV coordinated from a 3 component container to a 2 component container (like I mentioned earlier)

Did you try changing your UV coordinated from a 3 component container to a 2 component container (like I mentioned earlier)


yes i did but that just made it crash strangely, unless i did it wrong this is what it looked like:


Lib3dsTexel* texCoords = new Lib3dsTexel[m_TotalFaces * 2];


then,

memcpy(&texCoords[FinishedFaces*2 + i], mesh->texelL[face->points[ i ]], sizeof(Lib3dsTexel));

lastly,





glGenBuffers(1, &m_TexCoordVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_TexCoordVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsTexel) * 2 * m_TotalFaces, texCoords, GL_STATIC_DRAW);




it generated a massive memory exception :(

[quote name='Reloadead_' timestamp='1333565569' post='4928277']
Did you try changing your UV coordinated from a 3 component container to a 2 component container (like I mentioned earlier)


yes i did but that just made it crash strangely, unless i did it wrong this is what it looked like:


Lib3dsTexel* texCoords = new Lib3dsTexel[m_TotalFaces * 2];


then,

memcpy(&texCoords[FinishedFaces*2 + i], mesh->texelL[face->points[ i ]], sizeof(Lib3dsTexel));

lastly,





glGenBuffers(1, &m_TexCoordVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_TexCoordVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsTexel) * 2 * m_TotalFaces, texCoords, GL_STATIC_DRAW);




it generated a massive memory exception sad.png
[/quote]

That doesn't change the texture coordinate from 3 components to 2 components, that makes you only store texture coordinates for 2 of the 3 points on each triangle.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

[quote name='Adam West' timestamp='1333584946' post='4928340']
[quote name='Reloadead_' timestamp='1333565569' post='4928277']
Did you try changing your UV coordinated from a 3 component container to a 2 component container (like I mentioned earlier)


yes i did but that just made it crash strangely, unless i did it wrong this is what it looked like:


Lib3dsTexel* texCoords = new Lib3dsTexel[m_TotalFaces * 2];


then,

memcpy(&texCoords[FinishedFaces*2 + i], mesh->texelL[face->points[ i ]], sizeof(Lib3dsTexel));

lastly,





glGenBuffers(1, &m_TexCoordVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_TexCoordVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsTexel) * 2 * m_TotalFaces, texCoords, GL_STATIC_DRAW);




it generated a massive memory exception sad.png
[/quote]

That doesn't change the texture coordinate from 3 components to 2 components, that makes you only store texture coordinates for 2 of the 3 points on each triangle.
[/quote]

okay then, could you please tell me how to make it only 2 components?

This topic is closed to new replies.

Advertisement