c++


Converting std::vector of Vertices stucts to float*


What would be the best way of converting std::vector of Vertices to float*? I have vtx as my original data, which contains two vertices with position, normal and uv and I have std::vector of vertices v with the same position, normal and uv. What I am trying to achieve is getting the same memory layout and data as vtx into vtx2 using std::vector v. I tried copying the memory from v to vtx2 using memcpy but when I print them they are ordered in different way.
#include <iostream>
#include <vector>
using namespace std;
struct Vector3
{
float x;
float y;
float z;
};
struct Vector2
{
float x;
float y;
};
struct Vertex
{
Vector3 position;
Vector3 normal;
Vector2 uv;
};
int main(int argc, char *argv[])
{
const int n = 16;
float* vtx = new float[n];
// Vertex 1
// Position
vtx[0] = 1.0f;
vtx[1] = 2.0f;
vtx[2] = 3.0f;
// Normal
vtx[3] = 0.1f;
vtx[4] = 0.2f;
vtx[5] = 0.3f;
// UV
vtx[6] = 0.0f;
vtx[7] = 1.0f;
vtx += 8;
// Vertex 2
// Position
vtx[0] = 4.0f;
vtx[1] = 5.0f;
vtx[2] = 6.0f;
// Normal
vtx[3] = 0.2f;
vtx[4] = 0.3f;
vtx[5] = 0.4f;
// UV
vtx[6] = 0.0f;
vtx[7] = 1.0f;
vtx += 8;
for (int i = n; i>0; i--)
{
cout << *(vtx + i * -1) << endl;
}
vector<Vertex> v;
Vertex vt;
// Vertex 1
// Position
Vector3 pos1 = {1.0, 2.0, 3.0};
vt.position = pos1;
// Normal
Vector3 normal1 = {0.1, 0.2, 0.3};
vt.position = normal1;
// UV
Vector2 uv1 = {0.0, 1.0};
vt.uv = uv1;
v.push_back(vt);
// Vertex 2
// Position
Vector3 pos2 = {4.0, 5.0, 6.0};
vt.position = pos2;
// Normal
Vector3 normal2 = {0.2, 0.3, 0.4};
vt.position = normal2;
// UV
Vector2 uv2 = {0.0, 1.0};
vt.uv = uv2;
v.push_back(vt);
float* vtx2 = new float[n];
memcpy(vtx2, &v[0], v.size() * sizeof(Vertex));
for (int i = n; i>0; i--)
{
cout << *(vtx2 + i * -1) << endl;
}
delete[] vtx;
delete[] vtx2;
return 0;
}
There is a bug in your code:
vt.position = normal1
should read
vt.normal = normal1
And similarly for the second vertex in your vector. Upon fixing that you may find the output matches (it does for me), but it may depend on how your compiler is padding structs.
For example, forcing a different alignment on Vector3 using struct Vector3 {...} __attribute__ ((aligned (16))); will generate "corrupted" output.
#include <cstring>
#include <iostream>
#include <vector>
#include <cstddef>
using namespace std;
struct Vector3
{
float x;
float y;
float z;
};
struct Vector2
{
float x;
float y;
};
struct Vertex
{
Vector3 position;
Vector3 normal;
Vector2 uv;
};
int main(int argc, char *argv[])
{
const int n = 16;
float* vtx1 = new float[n];
float* vtx = vtx1;
cout << offsetof(Vertex, normal) << " " << offsetof(Vertex, uv) << " " << sizeof(Vertex) << "\n";
// Vertex 1
// Position
vtx[0] = 1.0f;
vtx[1] = 2.0f;
vtx[2] = 3.0f;
// Normal
vtx[3] = 0.1f;
vtx[4] = 0.2f;
vtx[5] = 0.3f;
// UV
vtx[6] = 0.0f;
vtx[7] = 1.0f;
vtx += 8;
// Vertex 2
// Position
vtx[0] = 4.0f;
vtx[1] = 5.0f;
vtx[2] = 6.0f;
// Normal
vtx[3] = 0.2f;
vtx[4] = 0.3f;
vtx[5] = 0.4f;
// UV
vtx[6] = 0.0f;
vtx[7] = 1.0f;
vtx += 8;
for (int i = n; i>0; i--)
{
cout << *(vtx + i * -1) << endl;
}
cout << "\n";
vector<Vertex> v;
Vertex vt;
// Vertex 1
// Position
Vector3 pos1 = {1.0, 2.0, 3.0};
vt.position = pos1;
// Normal
Vector3 normal1 = {0.1, 0.2, 0.3};
vt.normal = normal1;
// UV
Vector2 uv1 = {0.0, 1.0};
vt.uv = uv1;
v.push_back(vt);
// Vertex 2
// Position
Vector3 pos2 = {4.0, 5.0, 6.0};
vt.position = pos2;
// Normal
Vector3 normal2 = {0.2, 0.3, 0.4};
vt.normal = normal2;
// UV
Vector2 uv2 = {0.0, 1.0};
vt.uv = uv2;
v.push_back(vt);
float* vtx2 = new float[n];
vtx = vtx2;
memcpy(vtx, &v[0], n*sizeof(float));
vtx += n;
for (int i = n; i>0; i--)
{
cout << *(vtx + i * -1) << endl;
}
delete[] vtx1;
delete[] vtx2;
return 0;
}
Here is some corrected code with .normal instead of .position, it doesn't delete random memory by deleting vtx and the second print loop is fixed to show the data in the array instead of the 16 bytes of memory preceding it. It also prints the struct size and offsets in the first line. If you don't get 12 24 32 as the first line, your compiler is padding the structs with empty space which is causing your problems. You can use struct Vertex __attribute__((packed)) to prevent this on GCC or clang. Other compilers have different ways of doing it.

Related Links

Using Eigen in custom tensorflow op
Create JSON structure based on input variables
C++ range for loop different than regular for loop [closed]
C++ boost thread causes segmentation fault when instantiated twice
c++ - 3 bears in a vector of a struct, how can I change this code so the specific bear at the exit is removed?
Reading from excel/text file without skipping whitespaces unless made by a tab
How to initialize const size array in constructor without explicitly stating the size in class declaration?
Reading txt file into 2d array C++
How can I mark a function with an attribute conditionally on the value of a parameter?
Read a text file and store into two different arrays: a string array and float array
converting constructor: How do you explain a function given different arguments in c++
Background in Qt multiplies instead of fitting into window size
C++ pause this program using cin.get()
linking error in utility::conversions::to_string_t in qt creator
readLine() not being saved properly
Check socket with select() before using send()

Categories

HOME
coldfusion
objective-c
google-maps-api-3
reporting-services
analytics
text
uiviewcontroller
outlook-addin
ejb
terraform
shoutem
rhel
relation
category
amazon-route53
prestashop-1.6
google-fusion-tables
pad
netbeans-7.3
mysql-error-1064
osx-elcapitan
face-api
btrfs
amazon-lightsail
indexof
github-api
messagebox
edi
pi4j
pdb-files
byte
cad
get-eventlog
easymock
outlook-web-app
data-storage
shieldui
3d-modelling
watchdog
oracle-sql-data-modeler
parasoft
laravel-excel
winlims
api-manager
ctags
matlab-guide
opscenter
sift
nosuchelementexception
magicalrecord
optional
synopsys-vcs
cpio
launch-agent
aide-ide
ldflags
mockery
spock-reports
dart-polymer
ilmerge
ienumerable
rouge
anonymity
dfdl
rgraph
mongoose-web-server
android-6.0.1
idiorm
cosmos
gridsplitter
7digital
ssas-2008
gcdasyncsocket
ms-query
approximation
kermit
jxa
interspire-shopping-cart
networkcredentials
member
out
restkit-0.20
bonjour
policies
offlineapps
harfbuzz
boost-spirit-karma
rs485
struts2-tiles-plugin
tandem
uipicker
imagecreatefrompng
opengl-es-1.1
jwebunit
cgimagesource
emacs-dirtree
keymando
wymeditor
nspasteboard
spaces
crashrpt
nscoder
blogml
jquery-ui-plugins
reverse-debugging

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile