Jump to content
  • Advertisement
Sign in to follow this  
twan

empty screen when reading vertex data from file

This topic is 5462 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

I'm trying to read vertex data from a file. With the following code i get no error but also nothing on the screen. If i put the data in the source code (VERTEX data[]={-1.0, -1.0, -1.0, 0.0, 1.0} etc etc) then the cube is displayed on the screen. It's confusing because i realy don't know what i'm doing wrong in the following code :
  int i;
  VOID* pBuf;

  VERTEX *data = new VERTEX[Header.iVertices];

  for(i=0;i<Header.iVertices;i++)
  {
    fscanf(file, "%f %f %f %f %f", &data.x, &data.y, &data.z, &data.tu, &data.tv); 
  }
  
  pD3D_Device->CreateVertexBuffer(Header.iVertices * sizeof CustomVertex), D3DUSAGE_WRITEONLY, CustomVertex, D3DPOOL_MANAGED, &pVertexBuffer, NULL)))

  if(FAILED(pVertexBuffer->Lock(0, 0, &pBuf, 0)))
  {
    return false;
  }

  memcpy(pBuf, data, Header.iVertices * sizeof(CustomVertex));

  pVertexBuffer->Unlock();

  delete[] data;

Share this post


Link to post
Share on other sites
Advertisement
Try changing your for loop to this:

for(i=0;i<Header.iVertices;i++)
{
fscanf(file, "%f %f %f %f %f", &data.x, &data.y, &data.z, &data.tu, &data.tv);
char szBuff[256];
sprintf(szBuff, "%f %f %f %f %f\n", data.x, data.y, data.z, data.tu, data.tv);
OutputDebugString(szBuff);
}




That'll print out the vertices being red to the debug window, so you can see if you're reading valid data.
What is the output from running that?
I suspect that fscanf() is failing somehow. You should check the return value, it should return 5 every time.

Share this post


Link to post
Share on other sites
This is the output. Wich seems correct in my opinion.

-1.000000 -1.000000 -1.000000 0.000000 1.000000
-1.000000 1.000000 -1.000000 0.000000 0.000000
1.000000 1.000000 -1.000000 1.000000 0.000000
1.000000 1.000000 -1.000000 1.000000 0.000000
1.000000 -1.000000 -1.000000 1.000000 1.000000
-1.000000 -1.000000 -1.000000 0.000000 1.000000
1.000000 -1.000000 1.000000 0.000000 1.000000
1.000000 1.000000 1.000000 0.000000 0.000000
-1.000000 1.000000 1.000000 1.000000 0.000000
-1.000000 1.000000 1.000000 1.000000 0.000000
-1.000000 -1.000000 1.000000 1.000000 1.000000
1.000000 -1.000000 1.000000 0.000000 1.000000
-1.000000 1.000000 -1.000000 0.000000 1.000000
-1.000000 1.000000 1.000000 0.000000 0.000000
1.000000 1.000000 1.000000 1.000000 0.000000
1.000000 1.000000 1.000000 1.000000 0.000000
1.000000 1.000000 -1.000000 1.000000 1.000000
-1.000000 1.000000 -1.000000 0.000000 1.000000
1.000000 -1.000000 -1.000000 0.000000 1.000000
1.000000 -1.000000 1.000000 0.000000 0.000000
-1.000000 -1.000000 1.000000 1.000000 0.000000
-1.000000 -1.000000 1.000000 1.000000 0.000000
-1.000000 -1.000000 -1.000000 1.000000 1.000000
1.000000 -1.000000 -1.000000 0.000000 1.000000
-1.000000 -1.000000 1.000000 0.000000 1.000000
-1.000000 1.000000 1.000000 0.000000 0.000000
-1.000000 1.000000 -1.000000 1.000000 0.000000
-1.000000 1.000000 -1.000000 1.000000 0.000000
-1.000000 -1.000000 -1.000000 1.000000 1.000000
-1.000000 -1.000000 1.000000 0.000000 1.000000
1.000000 -1.000000 -1.000000 0.000000 1.000000
1.000000 1.000000 -1.000000 0.000000 0.000000
1.000000 1.000000 1.000000 1.000000 0.000000
1.000000 1.000000 1.000000 1.000000 0.000000
1.000000 -1.000000 1.000000 1.000000 1.000000
1.000000 -1.000000 -1.000000 0.000000 1.000000

Share this post


Link to post
Share on other sites
Yup, that all looks right. Does your CustomVertex format have any other members in it like diffuse colour or RHW component? Its possible that they aren't being set.

What about if you print out the values of all the vertex coordinates before the CreateVertexBuffer() call? Are they exactly the same when you read from the file and when you declare them in the source code?

Share this post


Link to post
Share on other sites
huh ???????

I placed the next loop under the vertex declaration in the source and before the create vertex. So you get the following code

vertex declaration
loop
create vertexbuffer

for(i=0;i<Header.iVertices;i++)
{
char szBuff[256];
sprintf(szBuff, "%f %f %f %f %f\n", data.x, data.y, data.z, data.tu, data.tv);
OutputDebugString(szBuff);

}

My vertex format is
CustomVertex=D3DFVF_XYZ|D3DFVF_TEX1;

This is the output, completly different then a few lines before. For example this is my first row in the source {-1.0, -1.0, -1.0, 0.0, 1.0}, and the first row in the output is -1.000000 0.000000 1.000000 -1.000000 -1.000000.

The tu and tv are shifted to the front but why ?

-1.000000 0.000000 1.000000 -1.000000 -1.000000
-1.000000 0.000000 0.000000 -1.000000 1.000000
-1.000000 1.000000 0.000000 1.000000 1.000000
-1.000000 1.000000 0.000000 1.000000 1.000000
-1.000000 1.000000 1.000000 1.000000 -1.000000
-1.000000 0.000000 1.000000 -1.000000 -1.000000
1.000000 0.000000 1.000000 1.000000 -1.000000
1.000000 0.000000 0.000000 1.000000 1.000000
1.000000 1.000000 0.000000 -1.000000 1.000000
1.000000 1.000000 0.000000 -1.000000 1.000000
1.000000 1.000000 1.000000 -1.000000 -1.000000
1.000000 0.000000 1.000000 1.000000 -1.000000
-1.000000 0.000000 1.000000 -1.000000 1.000000
1.000000 0.000000 0.000000 -1.000000 1.000000
1.000000 1.000000 0.000000 1.000000 1.000000
1.000000 1.000000 0.000000 1.000000 1.000000
-1.000000 1.000000 1.000000 1.000000 1.000000
-1.000000 0.000000 1.000000 -1.000000 1.000000
-1.000000 0.000000 1.000000 1.000000 -1.000000
1.000000 0.000000 0.000000 1.000000 -1.000000
1.000000 1.000000 0.000000 -1.000000 -1.000000
1.000000 1.000000 0.000000 -1.000000 -1.000000
-1.000000 1.000000 1.000000 -1.000000 -1.000000
-1.000000 0.000000 1.000000 1.000000 -1.000000
1.000000 0.000000 1.000000 -1.000000 -1.000000
1.000000 0.000000 0.000000 -1.000000 1.000000
-1.000000 1.000000 0.000000 -1.000000 1.000000
-1.000000 1.000000 0.000000 -1.000000 1.000000
-1.000000 1.000000 1.000000 -1.000000 -1.000000
1.000000 0.000000 1.000000 -1.000000 -1.000000
-1.000000 0.000000 1.000000 1.000000 -1.000000
-1.000000 0.000000 0.000000 1.000000 1.000000
1.000000 1.000000 0.000000 1.000000 1.000000
1.000000 1.000000 0.000000 1.000000 1.000000
1.000000 1.000000 1.000000 1.000000 -1.000000
-1.000000 0.000000 1.000000 1.000000 -1.000000

Share this post


Link to post
Share on other sites
So thats printing the vertices when they're defined in your source file? How do you define them? There seems to be something wrong there...

Share this post


Link to post
Share on other sites
Ok i found the problem.

First i had:

struct VERTEX
{
float tu, tv;
float x, y, z;
};

when i red your post i changed it to:

struct VERTEX
{
float x, y, z;
float tu, tv;
};

and now it works :). I never taught this could be the problem.
Can you please explain why it went wrong in the first way because i can't imaging why it's wrong ?

Share this post


Link to post
Share on other sites
If you changed the structure of the vertex, then when you declare it in the file, you're specifying the texture coordinates first, and then the vertices.
The vertices read were probably all in reverse order. If you try to render something, and you don't see anything, try disabling culling with SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); Then D3D won't remove triangles that are back facing.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!