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

How to use pthread to write a C++ class like “boost::shared_mutex”
Undefined reference to a superclass' virtual function
How many ways can fill in HxW rooms with 2x1 tatami mats? [closed]
CMake add_subdirectory not invoking the child CMakeLists.txt
How to explicitly get linear indices from arrayfire?
cppunit throws std::bad_alloc after upgrading gcc to 5.4.0
Program compiles but I think switch is ignored.
multiplication of two integer arrays
get and set Enum variable inside a class?
How to use make “using” keyword within a method to accept it as class level
std::array-like class copy constructor
Access violation when using delete
C++ SFML compile error + install
Resizing 2D vector C++, Setting to 0
Finding the shortest word ladder between two given words and a dictionary
Is this function doing something wrong with the sockets?

Categories

HOME
momentjs
drawing
knn
function
sql-server-2005
wolfram-mathematica
boost
servicestack
ais
put
virtocommerce
http-headers
distributed-database
worksheet-function
translate
flannel
shoutem
rhel
shogun
riscv
accurev
rinsim
stackdriver
media
gnome
mpeg
indexof
uilabel
affiliate
induction
podcast
gperftools
apache-hive
hana-cloud-platform
actor
android-optionsmenu
android-device-monitor
tycho
html.dropdownlistfor
autocorrect
carplay
winlims
xquery-sql
case-sensitive
orika
equation-solving
jquery-ui-tabs
unicode-string
android-gson
magicalrecord
bubble-sort
use-case
banking
unite.vim
ldflags
jwrapper
deferred-loading
smf
owner
nscoding
oracle-streams
wx
objdump
foundation
acl2
pull-request
sql-agent-job
cloudcontrol
wave
dynamicquery
string-search
automapper-3
angularjs-ng-show
nsurlprotocol
groff
mashup
postfix-operator
tandem
panning
outlook-2003
slimv
server-side-scripting
jquery-mobile-collapsible
grit
clang-complete
mfi
memory-profiling
bionic
glow
windows-networking
ant4eclipse
xml-comments
dynamic-css
uint32
application-design
codehighlighter
platform-independence

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