Jump to content
  • Advertisement
Sign in to follow this  
entone

read vertices from file and render the object

This topic is 3882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, i want to render a 3D object by reading all its vertices from file. the file is in the form of : 1.3 2.1 3.2 -0.3 2.7 -3.8 etc. can u suggest me any idea, thanks

Share this post


Link to post
Share on other sites
Advertisement
You failed to specify which language you're using, so I'll just assume C, C++

C style

float x,y,z;
FILE* fp = fopen(mesh.obj);
fscanf(fp, "%f %f %f", &x, &y, &z);

or

C++ style

float x,y,z;
ifstream infile(mesh.obj);
infile >> x;
infile >> y;
infile >> z

Share this post


Link to post
Share on other sites
sorry for that, am using VC++
here is the code fragment:
double x,y,z;
ifstream f;
f.open("file.txt");

f>>x>>y>>z;
glBegin(GL_TRIANGLES);
while(!f.eof())
{
glVertex3d(x,y,z);
f>>x>>y>>z;
}
glVertex3d(x,y,z)
f.close();
glEnd()

This code works fine for the data below:
10 -10 0
0 -10 0
10 10 0

but i got black screen when i change the value of z (vlaue of the last column)
i.e:

10 -10 10
0 -10 10
10 10 10

?

Share this post


Link to post
Share on other sites
1. You haven't provided enough information for us to know why the second triangle doesn't render, but my guess is that it's because it's behind the camera (remember, in its default orientation the OpenGL 'camera' points down the -z axis).

2. Don't use open() and close(); instead, open the file in the constructor and let it close itself when it goes out of scope.

3. You probably don't want to be doing file IO inside an inner rendering loop. Consider reading the vertex data from the file, storing it in an array, and then rendering from the array.

Share this post


Link to post
Share on other sites
Ok, i wrote a code which displays an image on the screen using VC++ and OpenGL by reading (x,y,z) cordinates, each pixel, from file. But i didn't get clear image as i expected. So, can i use ray tracing algorithm to get clear scene? do u have any idea how to use it?

Thanks,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!