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

use the method of extended class and of derived
PushBack (push_back()) elements of QStringList to vector<string>
Create a class using objects of another class
C++ char*[] to char** conversion
C++11 constructor inheritance through variadic templates
SetNamedSecurityInfo execution restarts system
expected unqualified-id before ')' token [closed]
How to measure the time of 100 executions of the program? [closed]
How can I convert a list of .png to a list of pdf in makefile?
How to get the real and imaginary parts of a complex matrix separately in CUDA?
Handling TCP connection failure in c++ cassandra client using a thrift wrapper library
ACM Tehran Asia Region Regional 2011 :Pausotopic messages [closed]
Wrong version of overridden [] operator called in class
Cocos2d-x high DesignResolution and iOS simulator display bug
C++/Boost: Passing a member function as a parameter to be used in boost::bind
improving the speed of the execution of that piece of code

Categories

HOME
google-maps
monitoring
powerbuilder
boost
raspberry-pi3
sage
crc
google-vr
windows-authentication
geometry
interaction
restcomm
cllocationmanager
filenet-p8
twitter-typeahead
j
odbc
media
mysql-error-1064
summary
boto
wikitude
cut
django-channels
amazon-lightsail
http-caching
ddos
na
pdb-files
puma
jung
gmail-contextual-gadgets
android-runonuithread
lift
3d-modelling
inline
android-device-monitor
instrumentation
laravel-excel
lighttable
getelementsbyclassname
rt
inputstreamreader
measurement-studio
monocle-scala
ucos
maven-resources-plugin
erwin
scodec
data-transfer
google-api-v3
leshan
sequencing
pubsubhubbub
countdowntimer
reportviewer2008
swig-template
codeigniter-restserver
classname
reducers
gethostbyname
jquery-ui-resizable
struts2-jquery-grid
jseparator
hottowel
sin
template-strings
du
xmlpullparser
apache-roller
ontouchevent
dynamicquery
allocation
coderunner
usleep
workbench
duplicate-removal
textile
form-helpers
apache-commons-exec
intel-galileo
facebook-app-center
hiding
opengl-es-1.1
java-service-wrapper
icefaces-1.8
camping
pubchem
python-sockets
wind-river-workbench
expandoobject
juggernaut
raw-ethernet
iphone-sdk-4.1
windows-live-writer
alasset
abbreviation
flashlite

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App