-1

Hi I am trying to create a ray tracer that renders a polygonized, triangle-based model.

I have a point 3D struct in point3d.h that holds x,y, and z coordinates.

#ifndef __POINT3D_H__
#define __POINT3D_H__

#include <iostream>

using namespace std;

struct Point3D
{
    double x;
    double y;
    double z;

    Point3D() : x(0.0), y(0.0), z(0.0) {}
    Point3D(const double & nx, const double & ny, const double & nz) : x(nx), y(ny), z(nz) {}

    Point3D operator+(const Point3D & rhs) const { 
     return Point3D(x + rhs.x, y + rhs.y, z + rhs.z); }

    Point3D operator-(const Point3D & rhs) const { 
     return Point3D(x - rhs.x, y - rhs.y, z - rhs.z); }

    Point3D operator*(double val) const { 
     return Point3D(x * val, y * val, z * val); }

    Point3D operator/(double val) const { 
     return Point3D(x / val, y / val, z / val); }

    Point3D operator+=(const Point3D & rhs) { 
     x += rhs.x; y += rhs.y; z += rhs.z; return *this; }

    Point3D operator-=(const Point3D & rhs) { 
     x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; }

    Point3D operator*=(double val) { 
     x *= val; y *= val; z *= val; return *this; }

    Point3D operator/=(double val) { 
     x /= val; y /= val; z /= val; return *this; }

    void print() {
     cout << '(' << x << ',' << y << ',' << z << ')'; 
    }
};

#endif

Here is where I try to use the * operator to multiple two Point3Ds together

Point3D phong(Point3D mColor, Point3D lColor, Point3D L, Point3D N, Point3D R, Point3D V) 
{
 Point3D k(1.0, 1.0, 1.0);
 Point3D ambient = mColor * k.x;

 Point3D diffuse_angle = ((N * L) / (length(N) * length(L)));
 Point3D diffuse = lColor * k.y * diffuse_angle; 

 Point3D specular_angle = ((R * V) / (length(R) * length(V)));
 double specular_x = pow(specular_angle.x, 100.0);
 double specular_y = pow(specular_angle.y, 100.0);
 double specular_z = pow(specular_angle.z, 100.0);
 Point3D specular_power(specular_x, specular_y, specular_z);
 Point3D specular = lColor * k.z * specular_power;

 return ambient + (lColor * (diffuse + specular)); 
}

When I try to multiple two Point3D's together, I am getting a no match error. Here is where the code fails. I feel like it is a simple mistake but I cannot figure it out. I am including the Point3d header file as follows: #include "point3d.h".

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
ryank
  • 395
  • 2
  • 6
  • 19

4 Answers4

2
Point3D operator*(double val) const

You have just this version, Point3D * double and nothing else, but you are trying to use this operator for Point3D * Point3D. Point3D is not implicitly constructible from double, so this is why you have compilation error.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
1
Point3D operator*(double val) const { 

This is for multiplication Point3D * double. And by

N * L

you are trying to do Point3D * Point3D.

You can rectify this either by providing proper operator* for your class OR provide a conversion from double to your class through single argument constructor. Although I prefer former.

ravi
  • 10,994
  • 1
  • 18
  • 36
1

You should need a function like this

Point3D operator *(Point3D &temp) const {

}

Since you don't have function to multiply two 3d points you are getting errors.Try adding this function.

Praburaj
  • 613
  • 8
  • 21
0

You need a function for the operation Point3D * Point3D, which can't be adapted for the call of Point3D::operator*(double val). Such as:

Point3D operator*(const Point3D & rhs) const {
    return Point3D(x * rhs.x, y * rhs.y, z * rhs.z); }
songyuanyao
  • 169,198
  • 16
  • 310
  • 405