我无法理解在尝试让我的项目工作时遇到的错误。 有人可能会指出我正确的方向吗?我是C ++的新手,所以我可能会在这里做一些至关重要的错误而不知道它。 我得到的错误消息如下:
Fehler 6 error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: struct Camera __thiscall Scene::getCamera(void)" (? getCamera@Scene@@QAE?AUCamera@@XZ)" in Funktion ""public: void __thiscall Renderer::render(void)" (?render@Renderer@@QAEXXZ)". ...\source\renderer.obj raytracer
Fehler 4 error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: class std::vector<class std::shared_ptr<class Shape>,class std::allocator<class std::shared_ptr<class Shape> > > __thiscall Scene::getShapes(void)const " (?getShapes@Scene@@QBE?AV?$vector@V?$shared_ptr@VShape@@@std@@V?$allocator@V?$shared_ptr@VShape@@@std@@@2@@std@@XZ)" in Funktion ""public: void __thiscall Renderer::render(void)" (?render@Renderer@@QAEXXZ)". ...source\renderer.obj raytracer
这是我的代码:
#include "color.hpp"
#include "pixel.hpp"
#include "ppmwriter.hpp"
#include <string>
#include <glm/glm.hpp>
#include <map>
#include <shape.h>
#include "sdfloader.h"
#include <optional_hit.h>
#include <scene.h>
Renderer::Renderer(Scene const& scene, unsigned w, unsigned h, std::string const& file)
: width_(w)
, height_(h)
, colorbuffer_(w*h, Color(0.0, 0.0, 0.0))
, filename_(file)
, ppm_(width_, height_)
, scene_(scene)
{}
void Renderer::render() {
camera_ = scene_.getCamera();
shapes_ = scene_.getShapes();
lights_ = scene_.getLights();
camera_.position = glm::vec3(0, 0, tan((90.0 - camera_.opening_angle / 2.0) * M_PI / 180.0) * (double(width_) / 2.0));
testOutput();
Ray first_ray;
for (unsigned y = 0; y < height_; ++y) {
for (unsigned x = 0; x < width_; ++x) {
Pixel p(x, y);
//first ray is created here
glm::vec3 poip = glm::vec3(double(x) - double(width_) / 2.0, double(y) - double(height_) / 2.0, 0);
first_ray.origin = camera_.position;
first_ray.direction = poip - camera_.position;
double delta = -1;
Shape *closest_obj;
for (unsigned int i = 0; i < shapes_.size(); ++i) {
double d = shapes_[i]->intersect(first_ray);
if (d > 1.0 && (d < delta || delta == -1)) {
delta = d;
closest_obj = shapes_[i].get();
}
}
if (delta == -1) {
p.color = Color(0.0, 0.0, 0.0);
} else {
glm::vec3 hit_point = first_ray.origin + (float) delta * first_ray.direction;
p.color = calculateColor(closest_obj, hit_point, first_ray);
}
// draw pixel
write(p);
}
}
ppm_.save(filename_);
}
void Renderer::write(Pixel const& p)
{
// flip pixels, because of opengl glDrawPixels
size_t buf_pos = (width_*p.y + p.x);
if (buf_pos >= colorbuffer_.size() || (int)buf_pos < 0) {
std::cerr << "Fatal Error Renderer::write(Pixel p) : "
<< "pixel out of ppm_ : "
<< (int)p.x << "," << (int)p.y
<< std::endl;
} else {
colorbuffer_[buf_pos] = p.color;
}
ppm_.write(p);
}
/*Optional_hit Renderer::intersect(Ray const& ray) const{
Optional_hit o;
Optional_hit temp;
std::vector<float> hits;*/
void Renderer::testOutput() {
std::cout << "Objects in scene:" << std::endl;
for (unsigned int i = 0; i < shapes_.size(); ++i) {
std::cout << "Object names: " << shapes_[i]->getName() << std::endl;
}
for (unsigned int i = 0; i < lights_.size(); ++i) {
std::cout << "Lightsource: " << lights_[i].getName() << std::endl;
}
std::cout << "Camera used: " << camera_.name << " | Focal Length: " << camera_.position.z << std::endl;
}
Color Renderer::calculateColor(const Shape* hit_obj, glm::vec3 const& hit_point, Ray const& first_ray) {
Color final_color = Color(0.0, 0.0, 0.0);
for (unsigned int i = 0; i < lights_.size(); ++i) {
glm::vec3 n = hit_obj->getNormalAt(hit_point);
glm::vec3 l = lights_[i].getPosition() - hit_point;
Ray sec_ray = Ray(hit_point, l);
Color diffuse_light;
Color specular_light;
if (!Shadow(sec_ray) && glm::dot(glm::normalize(n), glm::normalize(l)) > 0) {
Color Ip = lights_[i].getLD();
Color kd = hit_obj->getMaterial().getKD();
diffuse_light = (Ip * kd * glm::dot(glm::normalize(n), glm::normalize(l)));
glm::vec3 r = 2 * glm::dot(glm::normalize(n), glm::normalize(l)) * glm::normalize(n) - glm::normalize(l);
glm::vec3 v = glm::normalize(first_ray.direction);
v *= -1;
if (glm::dot(r, v) > 0) {
double m = hit_obj->getMaterial().getM();
Color ks = hit_obj->getMaterial().getKS();
specular_light = ks * pow(glm::dot(r, v), m);
} else {
specular_light = Color(0.0, 0.0, 0.0);
}
} else {
diffuse_light = Color(0.0, 0.0, 0.0);
specular_light = Color(0.0, 0.0, 0.0);
}
final_color += diffuse_light + specular_light;
}
Color Ia = lights_[0].getLA();
Color ka = hit_obj->getMaterial().getKA();
Color ambient_light = Ia * ka;
final_color += ambient_light;
return final_color;
}
场景头文件:
#define SCENE_HPP
#include <vector>
#include <memory>
#include <material.h>
#include <camera.h>
#include <light.h>
#include <shape.h>
#include <map>
struct Scene {
Scene();
void render() const;
void add(std::shared_ptr<Material> material);
void add(std::shared_ptr<Shape> shape);
void add(std::shared_ptr<Light> light);
void add(std::shared_ptr<Camera> camera);
std::vector<std::shared_ptr<Shape>> const& shapes() const;
std::vector<std::shared_ptr<Material>> const& materials() const;
std::vector<std::shared_ptr<Light>> const& lights() const;
std::vector<std::shared_ptr<Camera>> const& cameras() const;
void readFile(std::string file);
std::map<std::string, std::shared_ptr<Material>> getMaterials() const;
std::vector<std::shared_ptr<Shape>> getShapes() const;
std::vector<Light> getLights() const;
//std::vector<Camera> getCamera();
Camera getCamera();
Material checkMaterialName(std::string name);
std::vector<std::shared_ptr<Shape>> shapes_;
std::map<std::string, Material> materials_;
std::vector<std::shared_ptr<Light>> lights_;
std::vector<std::shared_ptr<Camera>> camera_;
unsigned width = 600;
unsigned height = 600;
};
#endif
和渲染器标题:
#include "color.hpp"
#include "pixel.hpp"
#include "ppmwriter.hpp"
#include <string>
#include <glm/glm.hpp>
#include <map>
#include <shape.h>
#include "sdfloader.h"
#include <optional_hit.h>
#include <scene.h>
class Renderer {
public:
Renderer(Scene const& scene, unsigned w, unsigned h, std::string const& file);
void render();
void write(Pixel const& p);
void testOutput();
Color calculateColor(const Shape* hit_obj, glm::vec3 const& hit_point, Ray const& prim_ray);
bool Shadow(Ray sec_ray);
inline std::vector<Color> const& colorbuffer() const {
return colorbuffer_;
}
unsigned get_width() const;
unsigned get_height() const;
std::string get_filename() const;
Scene get_scene() const;
std::vector<std::shared_ptr<Shape>> const& shapes() const;
// OptionalHit intersect(Ray const& ray) const;
//void render_scene(std::string file);
private:
unsigned width_;
unsigned height_;
std::vector<Color> colorbuffer_;
std::string filename_;
PpmWriter ppm_;
Scene scene_;
Camera camera_;
std::vector<Light> lights_;
std::vector<std::shared_ptr<Shape>> shapes_;
};
如果有人能够理解这一点,我们非常感谢任何帮助!