Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


C++ D3DXVECTOR3 insertion operator


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Mko000   Members   -  Reputation: 103

Like
0Likes
Like

Posted 15 December 2013 - 02:39 PM

Hi guys. New here and I couldn't find a solution to my problem in older topics...
 
I want to take the 3 vectors describing my models (position, scale, rotation) and save them to a text file. Essentially create a map. I designate my models with integers but that is not important.
I have a struct ModelData for that: one int for model type and three D3DXVECTOR3. I pull it from all the models, create a vector and send it to my savemap method.
 
So I defined an insertion operator for the ModelData, and also for D3DXVECTOR3.
But I am not getting the correct output. For some reason the D3DXVECTOR3 operator is not being used. By tracing I found that it is being casted to float*. 
 
Any ideas what I am doing wrong? Here are the code parts:



#pragma once
struct ModelData {
   int type;
   D3DXVECTOR3 position, size, rotation;
}; 
 
std::ostream& operator<<(std::ostream& os, const D3DXVECTOR3& vector) {
   os << vector.x << 'x' << vector.y << 'x' << vector.z;
   return os;
}

std::ostream& operator<<(std::ostream& os, const ModelData& data) {
   os << data.type << ' ' << data.position << ' ' << data.rotation << ' ' << data.size << std::endl;
   return os;
}

Like this I get what I am pretty sure is the address in HEX of data.position.x, data.rotation.x, and data.size.x as output. If I dereference data.position and so on, I get the integer part of the first float (x).



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9673

Like
2Likes
Like

Posted 15 December 2013 - 04:16 PM

Your code, as posted, works for me on MSVC 2012.
#include <iostream>
#include <d3dx9.h>

struct ModelData {
   int type;
   D3DXVECTOR3 position, size, rotation;
}; 
 
std::ostream& operator<<(std::ostream& os, const D3DXVECTOR3& vector) {
   os << vector.x << 'x' << vector.y << 'x' << vector.z;
   return os;
}

std::ostream& operator<<(std::ostream& os, const ModelData& data) {
   os << data.type << ' ' << data.position << ' ' << data.rotation << ' ' << data.size << std::endl;
   return os;
}

int main(int, char **) {
  ModelData m = { 0, D3DXVECTOR3(1, 1, 1), D3DXVECTOR3(2, 2, 2), D3DXVECTOR3(3, 3, 3)};
  std::cout << m;

  return 0;
}
This produces "0 1x1x1 3x3x3 2x2x2" as output. Try to produce a minimal code example that demonstrates your problem.

#3 Mko000   Members   -  Reputation: 103

Like
0Likes
Like

Posted 16 December 2013 - 11:58 AM

Thank you

You are right, after creating the topic I ran it and it worked just fine. Must have fixed the problem while editing the code for this topic. Maybe it was because I was trying to write the operator for ofstream, and then changed it to ostream. I'm not sure which change made it work.

Looks like I just messed something up.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS