Siddhesh Muley
Siddhesh Muley

Reputation: 1

only one value being added into the vectors

trying to plot vertices in opengl, but I am able to assign only one element from vertices to TaskTwoVerticesN, please help...! What am I doing wrong? please let me know if you require all the code and I can email it or post here

typedef struct Vertex {
    float XYZW[4];
    float RGBA[4];
    void SetCoords(float *coords) {
        XYZW[0] = coords[0];
        XYZW[1] = coords[1];
        XYZW[2] = coords[2];
        XYZW[3] = coords[3];
    }
    void SetColor(float *color) {
        RGBA[0] = color[0];
        RGBA[1] = color[1];
        RGBA[2] = color[2];
        RGBA[3] = color[3];
    }
};

Vertex Vertices[] =
{
    { { 1.0f, 1.0f, 0.0f, 1.0f },{ 0.0f, 0.0f, 0.0f, 1.0f } }, // 0
    { { 0.0f, 1.4f, 0.0f, 1.0f },{ 0.0f, 0.0f, 1.0f, 1.0f } }, // 
};

std::vector<Vertex>TaskTwoVerticesN;

void createObjects(void)
{
    TaskTwoVerticesN.clear();
    // and this, running them one by one in a for loop does not work either
    TaskTwoVerticesN.insert(TaskTwoVerticesN.begin(), Vertices, Vertices + (sizeof(Vertices)/sizeof(Vertices[0])));
}

main(){
    createObjects();
}

Upvotes: 0

Views: 40

Answers (1)

John Griffin
John Griffin

Reputation: 377

Actually, doctoring your code enough to run

int main(){
     createObjects();
     std::cout << ((sizeof(Vertices)/sizeof(Vertices[0]))) << std::endl;
     std::cout << TaskTwoVerticesN[0].XYZW[1] << std::endl;
     std::cout << TaskTwoVerticesN[1].XYZW[1] << std::endl;
     return 0;
}

Leads to this output

2
1
1.4

Thus, it appears that both elements were inserted as you intended. The only thing that I really changed was main(), some std::cout and removing typedef on your struct.

In sum, your insert is fine.

Upvotes: 1

Related Questions