PDA

View Full Version : viewing my loaded object



nick2price
11-18-2008, 10:18 AM
I am using this code which can load in a 3ds max object. It first loaded in a spaceship, and everything worked good and i could see it on the screen. I then created my own 3ds max car, loaded it in but nothing is on the screen. When it had the rotation on it, i could occasionally see it flash through then off again. How do i get it to just be there straight infront of me? This is the code





#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include "coursework2.h"
#include "texture.h"
#include "3dsloader.h"


int screen_width=640;
int screen_height=480;



int filling=1; //0=OFF 1=ON


obj_type object;



/************************************************** ********
*
* SUBROUTINE init()
*
* Used to initialize OpenGL and to setup our world
*
************************************************** *******/

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black
glShadeModel(GL_SMOOTH); // Type of shading for the polygons

// Viewport transformation
glViewport(0,0,screen_width,screen_height);

// Projection transformation
glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations
glLoadIdentity(); // We initialize the projection matrix as identity
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,10000.0f); // We define the "viewing volume"

glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping

Load3DS (&amp;object,"car.3ds");

object.id_texture=LoadBitmap("carTex.bmp"); // The Function LoadBitmap() return the current texture ID


}



/************************************************** ********
*
* SUBROUTINE resize(int,int)
*
* This routine must be called everytime we resize our window.
*
************************************************** *******/

void resize (int width, int height)
{
screen_width=width; // We obtain the new screen width values and store it
screen_height=height; // Height value

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // We clear both the color and the depth buffer so to draw the next frame
glViewport(0,0,screen_width,screen_height); // Viewport transformation

glMatrixMode(GL_PROJECTION); // Projection transformation
glLoadIdentity(); // We initialize the projection matrix as identity
gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,10000.0f);

glutPostRedisplay (); // This command redraw the scene (it calls the same routine of glutDisplayFunc)
}




/************************************************** ********
*
* SUBROUTINE display()
*
* This is our main rendering subroutine, called each frame
*
************************************************** *******/

void display(void)
{
int l_index;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // This clear the background color to dark blue
glMatrixMode(GL_MODELVIEW); // Modeling transformation
glLoadIdentity(); // Initialize the model matrix as identity


glBindTexture(GL_TEXTURE_2D, object.id_texture); // We set the active texture

glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)
for (l_index=0;l_index<object.polygons_qty;l_index++)
{
//----------------- FIRST VERTEX -----------------
// Texture coordinates of the first vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].a ].u,
object.mapcoord[ object.polygon[l_index].a ].v);
// Coordinates of the first vertex
glVertex3f( object.vertex[ object.polygon[l_index].a ].x,
object.vertex[ object.polygon[l_index].a ].y,
object.vertex[ object.polygon[l_index].a ].z); //Vertex definition

//----------------- SECOND VERTEX -----------------
// Texture coordinates of the second vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].b ].u,
object.mapcoord[ object.polygon[l_index].b ].v);
// Coordinates of the second vertex
glVertex3f( object.vertex[ object.polygon[l_index].b ].x,
object.vertex[ object.polygon[l_index].b ].y,
object.vertex[ object.polygon[l_index].b ].z);

//----------------- THIRD VERTEX -----------------
// Texture coordinates of the third vertex
glTexCoord2f( object.mapcoord[ object.polygon[l_index].c ].u,
object.mapcoord[ object.polygon[l_index].c ].v);
// Coordinates of the Third vertex
glVertex3f( object.vertex[ object.polygon[l_index].c ].x,
object.vertex[ object.polygon[l_index].c ].y,
object.vertex[ object.polygon[l_index].c ].z);
}
glEnd();

glFlush(); // This force the execution of OpenGL commands
glutSwapBuffers(); // In double buffered mode we invert the positions of the visible buffer and the writing buffer
}



/************************************************** ********
*
* The main routine
*
************************************************** *******/

int main(int argc, char **argv)
{
// We use the GLUT utility to initialize the window, to handle the input and to interact with the windows system
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screen_width,screen_height);
glutInitWindowPosition(0,0);
glutCreateWindow("Coursework2");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc (resize);
init();
glutMainLoop();

return(0);
}


Thanks for the help

ZbuffeR
11-18-2008, 11:51 AM
Check model position and scale. In 3dsmax you can have a number of "relative" transformations, be sure to reset all.

nick2price
11-18-2008, 12:52 PM
I have resetted all transformations. How do i check the model position and scale though?

ZbuffeR
11-18-2008, 01:27 PM
center it in 3dsmax.
you can also try to find the min and max values for x, y, z coordinates of vertices.

nick2price
11-18-2008, 01:43 PM
I have tried yet another new model, and its working great. When i didnt have the texture added and the rotation going (in opengl) nothing was displayed. But with them both back on now, it is displayed. I am just going to mess about for a little bit cos i wanna see if i can take the current rotation off and have the car face on driving towards me.
Just one quick question. I want to add scenery, this is different to texture mapping isnt it? What i need to know is the best way to do this. Is it to create a 3 sided cube, 2 sides and back?
cheers

ZbuffeR
11-18-2008, 02:20 PM
scenery ? you mean a background ?
A mapped cube enclosing the whole scene and camera works great. You can stay with 3 faces if you don't need full 360 background.

_NK47
11-19-2008, 03:36 AM
my two cents: loaded models reside in modelspace which you then transform to world space (eye space in OpenGL) via matrices. if your object in modelspace is translated by some point say on z axis -100 then it would be probably behind your camera if you just translate it to z+50 in eyespace. it depends on the model and artist how and why they position certain objects in modelspace. but make sure you know where it is so you can think of where its gonna be transformed in eyespace. generally its hard to tell why an object is not visible. more likely because of wrong transformations but heck it could be even clipped by the far clipping plane or something.