mardi 4 avril 2017

base class vector initialization from derived class's constructor

I'm playing around with OpenGL. I'm drawing several solids (tetrahedron, dodecahedron, etc.). The base class is Solid and in this example derived class is Tetrahedron. The base class has members vertices and indices. But only the derived class knows this information. How do I put this together so that the vertex and index information is in the derived class and not sent in from main?

solid.h

#include <cmath>
#include <vector>
#include <iostream>
#include <GL/gl3w.h>
#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include "mgl.h"

class Solid{
protected:
  // members
  std::vector<GLfloat> vertices;
  std::vector<GLfloat> indices;

public:
  Solid(std::vector<GLfloat>&&, std::vector<GLuint>&&);
  ~Solid();
  virtual void draw() const{};
};

solid.cpp

#include "solid.h"

Solid::Solid(std::vector<GLfloat>&& v, std::vector<GLuint>&& i) : vertices(std::move(v)), indices(std::move(i)){
  //OpenGL initialization...
  //move vertex information into GPU
}

tetrahedron.h

#include "solid.h"

class Tetrahedron : public Solid {
public:
  Tetrahedron() : Solid(
  {
    1, 1, 1,
    1, -1, -1,
   -1, 1, -1,
    -1, -1, 1
  }, {
    0, 1, 2,
    0, 1, 3,
    1, 2, 3,
    0, 2, 3
  }){}
  void draw() const override;
};

Inside main.cpp

Solid tetrahedron;
tetrahedron.draw();

However, I get the following errors:

main.cpp:19:9: error: no matching function for call to ‘Solid::Solid()’ Solid tetrahedron;

solid.cpp:3:111: error: no matching function for call to ‘std::vector<float>::vector(std::remove_reference<std::vector<unsigned int>&>::type)’

&& v, std::vector&& i) : vertices(std::move(v)), indices(std::move(i)){

Aucun commentaire:

Enregistrer un commentaire