Hello all
Im begginer so bare with me here , i like to load 3ds model into simple opengl program , what i did is to use as refernce 2 links i found : http://pastebin.com/CZ82VYWT and My linkhttp://www.donkerdump.nl/node/207 i take the code from the redbook the cube example and tryed to marage between the 2
but all the time i keep getting Unhandled exception at 0x00000000 glGenBuffer exception . in the CModel3DS::CreateVBO() method
here is my code :
// Enable lighting and set the position of the light
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
GLfloat pos[] = { 0.0, 4.0, 4.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
m_model = lib3ds_file_load(filename.c_str());
// If loading the model failed, we throw an exception
if(!m_model)
{
throw strcat("Unable to load ", filename.c_str());
}
}
// Count the total number of faces this model has
void CModel3DS::GetFaces()
{
assert(m_model != NULL);
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 faces
m_TotalFaces += mesh->faces;
}
}
// Copy vertices and normals to the memory of the GPU
void CModel3DS::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];
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(&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); <<<---------------------------------------------------------------------------------HERE IS MY EXCEPTION
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);
// Clean up our allocated memory
delete vertices;
delete normals;
// We no longer need lib3ds
lib3ds_file_free(m_model);
m_model = NULL;
}
// Render the model using Vertex Buffer Objects
void CModel3DS:: Draw() const
{
assert(m_TotalFaces != 0);
// Enable vertex and normal arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_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);
glGenBuffer is OpenGL 1.5 and at least on Windows not part of core OpenGL. You are probably using an extension manager like GLEW or GLEE, otherwise this would not even compile. While I'm not familiar with GLEE, GLEW needs a call to glewInit() after you have set up an OpenGL context but before you make any call to extension functions. GLEE will need something similar.
You need to initialize GLEW efter creating your window. Without the window, you don't have an active rendering context, and without the rendering context, there are no functions that GLEW can load.
Thanks allot for your answers i did as you suggest and set the glewInit() to be the last after the window is created and the window does shown all right and i don't have any run-time error/exceptions
but i don't see the model in my window . i know there is a model ( somewhere in memory ) [font="arial, sans-serif"]Beacose[/font][font="arial, sans-serif"] [/font]i have its faces count and fopen does open the file .
but my window is totally black. maybe its have to do something with the camera / view ? how can i check this ? UPDATE
i found out why only the window is showing , the glew code never executed beacose the glutMainLoop(); but it in a loop
when i put the glutMainLoop(); after the glewInit(); as the last line in the code i still getting the the old exception error with the buffers.
so what else i have to do ....
// Enable lighting and set the position of the light
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
GLfloat pos[] = { 0.0, 4.0, 4.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
Try putting the glew initialization inside of your initializeGL function. I would also implement a check to see if the required extensions were loaded, and if not, fall back to a failsafe alternative using system memory. Additionally, use a debugger to find out the order in which your functions get called. Its also a good idea to test glError in your debug build, and perhaps store the intermediate value in a function scope variable for even easier overview of what happens when debugging
Actually, that is really bad code. You need to check the GL version before using a function. VBO itself appeared in GL 1.5.
Get the spec files from opengl.org and look them up. There are also the reference files for GL 2.1, 3.3 and 4.1.
Sig: http://glhlib.sourceforge.net an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
It can be version of my OpenGL?
well what im using is : freeglut that is the free opengl for windows and glew as the extention implantation ( if i understand it right )
by the way even if i replace the freeglut with the glut-3.7.6 it gives me the same error
if i check versions in code this is what i get after :
Max texture size: 2048 x 2048
Max texture coordinates: 8
Max vertex texture image units: 0
Max texture image units: 16
Max geometry texture units: 0
Max anisotropic filtering value: 2
Max number of light sources: 16
Max viewport size: 2048 x 2048
Max uniform vertex components: 0
Max uniform fragment components: 0
Max geometry uniform components: 0
Max varying floats: 0
Max samples: 0
Max draw buffers: 0
OpenGL driver version check (Current: 6.14.10.4670, Latest known: 6.14.10.4642):
Latest version of display drivers found
According the database, you are running the latest display drivers for your video card.
Compiled vertex array support
This feature improves OpenGL performance by using video memory to cache transformed vertices.
Multitexture support
This feature accelerates complex rendering such as lightmaps or environment mapping.
Secondary color support
This feature provides an alternate method of coloring specular highlights on polygons.
S3TC compression support
This feature improves texture mapping performance in some applications by using lossy compression.
Texture edge clamp support
This feature improves texturing quality by adding clamping control to edge texel filtering.
Vertex program support
This feature enables a wide variety of effects via flexible vertex programming (equivalent to DX8 Vertex Shader.)
Fragment program support
This feature enables a wide variety of effects via per pixel programming (equivalent to DX9 Pixel Shader.)
Texture anisotropic filtering support
This feature improves the quality of texture mapping on oblique surfaces.
Occlusion test support
This feature provides hardware accelerated culling for objects.
No OpenGL Shading Language support
This may break compatibility for applications using per pixel shading.
No Frame buffer object support
This may break compatibility for applications using render to texture functions.
Extension verification:
GL_ARB_debug_output was not found, but has the entry point glGetPointerv
GL_ARB_imaging was not found, but has the entry point glBlendEquation
GL_ARB_occlusion_query has been added to the extensions list of Intel Broadwater G
It seems your opengl renderer is only 1.4, but you have the arb extension. Basically, this means you should use glGenBuffersARB, and glBindBufferARB, etc, not glGenBuffers and glBindBuffer and the likes, since those functions require opengl version 1.5 or higher. So you need to create multiple paths, and decide during runtime which path to take depending on the supported gl version and extensions.
It seems your opengl renderer is only 1.4, but you have the arb extension. Basically, this means you should use glGenBuffersARB, and glBindBufferARB, etc, not glGenBuffers and glBindBuffer and the likes, since those functions require opengl version 1.5 or higher. So you need to create multiple paths, and decide during runtime which path to take depending on the supported gl version and extensions.