简体   繁体   中英

Error - 'initializing': cannot convert from 'glm::vec2' to 'glm::vec3'

I am making a 3D game engine using C++, following this tutorial .

I am using GLM, and I am getting the error that is in my title.
I have an array of a custom struct. It saves position, color, and texcoords.

"Vertex" Struct

struct Vertex {
glm::vec3 position;
glm::vec3 color;
glm::vec2 texcoord;
};

"Vertices" Array:

Vertex vertices[] = {

//POSITION
glm::vec3(0.0f, 0.5f, 0.f),
glm::vec3(-0.5f, -0.5f, 0.f),
glm::vec3(0.5f, -0.5f, 0.f),

//COLOR
glm::vec3(1.f, 0.f, 0.f),
glm::vec3(0.0f, 1.f, 0.f),
glm::vec3(0.0f, 0.f, 1.f),

//TEXCOORDS
glm::vec2(0.f, 1.f),
glm::vec2(0.f, 0.f),
glm::vec2(1.f, 0.f)
};

When I click on the error in Visual Studio, it brings me to the end of my Vertice array.

I have googled and searched Stack Overflow, and I cannot find an answer to this problem. I have my full main.cpp file stored here , if that helps anything.

You're initializing an array of Vertex and trying to initialize it with types glm::vec3 and glm::vec2. The types don't match. What you need to do instead is:

Vertex vertices[size];
for(size_t i = 0; i < size; i++) {
    vertices[i] = Vertex( /* initialize however you need to */ );
}

This will initialize and array of size size and fill it with Vertex objects. Note that this will initialize the array on the stack, which means that you will only be able to store a small amount of vertices. What you should really do is something like:

std::vector<Vertex> vertices(size);
for(auto& vertex : vertices) { // range-based for
    vertex = Vertex{ glm::vec3(x,y,z), glm::vec3(x,y,z), glm::vec2(u,v) };
}

More info on stack vs heap allocation: https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ , ranged based for: https://www.geeksforgeeks.org/range-based-loop-c/ and std::vector: https://www.geeksforgeeks.org/vector-in-cpp-stl/

You declare the array, thus each array element must be braced in intializer.

Vertex vertices[] = {
// First element
{
glm::vec3(0.0f, 0.5f, 0.f),  //POSITION
glm::vec3(1.f, 0.f, 0.f),  //COLOR
glm::vec2(0.f, 1.f),  // TEXTCOORD
},
// Second element
{
glm::vec3(-0.5f, -0.5f, 0.f),
glm::vec3(0.f, 1.f, 0.f),
glm::vec2(0.f, 0.f),
},
// Third element
{
glm::vec3(0.5f, -0.5f, 0.f),
glm::vec3(0.f, 0.f, 1.f),
glm::vec2(1.f, 0.f)
}
};


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM