Skip to content

Commit b78fdc8

Browse files
committed
fixing BRDF
1 parent 1d433e8 commit b78fdc8

9 files changed

+59
-20
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#############################################################################
22
# Makefile for building: raymini
3-
# Generated by qmake (2.01a) (Qt 4.8.3) on: dim. avr. 27 01:19:13 2014
3+
# Generated by qmake (2.01a) (Qt 4.8.3) on: mer. avr. 30 11:19:32 2014
44
# Project: raymini.pro
55
# Template: app
66
# Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ -o Makefile raymini.pro

Mesh.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void Mesh::clearGeometry () {
2323
vertices.clear ();
2424
}
2525

26+
2627
void Mesh::clearTopology () {
2728
triangles.clear ();
2829
}

Ray.cpp

+31-13
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,55 @@ bool Ray::intersect (const BoundingBox & bbox, Vec3Df & intersectionPoint) const
6262
return (true);
6363
}
6464

65-
bool Ray::intersectTriangle (const Triangle t, Vec3Df & intersectionPoint, Vec3Df p0, Vec3Df p1, Vec3Df p2) const { //std::vector<Vertex> & Vertices) const {
66-
//Vec3Df p0 = Vertices[t.getVertex(0)].getPos();
67-
//Vec3Df p1 = Vertices[t.getVertex(1)].getPos();
68-
//Vec3Df p2 = Vertices[t.getVertex(2)].getPos();
65+
float Ray::intersectTriangle (const Triangle t, Vec3Df & intersectionPoint, Vec3Df p0, Vec3Df p1, Vec3Df p2, float diffuse, float specular, Vec3Df n0, Vec3Df n1, Vec3Df n2, Vec3Df lightpos) const { //std::vector<Vertex> & Vertices) const {
66+
//Vec3Df p0 = Vertices[t.getVertex(0)].getNormal();
67+
//Vec3Df p1 = Vertices[t.getVertex(1)].getNormal();
68+
// Vec3Df p2 = Vertices[t.getVertex(2)].getNormal();
6969

7070
Vec3Df e0 = p1-p0;
7171
Vec3Df e1 = p2-p0;
7272
Vec3Df n = Vec3Df::crossProduct(e0,e1);
7373
n.normalize();
7474
Vec3Df q = Vec3Df::crossProduct(direction, e1);
75-
7675
float a = Vec3Df::dotProduct(e0, q);
77-
7876
if (Vec3Df::dotProduct(n,direction)>=0 || abs(a)<0.0 ){
79-
return false;
77+
return 1000;
8078
}
8179
Vec3Df s = (origin - p0)/a;
8280
Vec3Df r = Vec3Df::crossProduct(s, e0);
8381

8482
float b0 = Vec3Df::dotProduct(s,q);
8583
float b1 =Vec3Df::dotProduct(r,direction);
8684
float b2 = 1-b0-b1;
87-
8885
if (b0<0 || b1<0 || b2<0){
89-
return false;
86+
return 1000;
9087
}
9188
float k = Vec3Df::dotProduct(r,e1);
9289
if (k>=0) {
93-
intersectionPoint = origin + k*direction;
94-
std::cout << s << std::endl;
95-
return true;
90+
intersectionPoint = b2*p0+b0*p1+b1*p2;
91+
//intersectionPoint.normalize();
92+
//intersectionPoint = origin + k*direction;
93+
Vec3Df norm = b2*n0+b0*n1+b1*n2;
94+
//intersectionPoint = origin + k*direction;
95+
norm.normalize();
96+
Vec3Df wi = lightpos-intersectionPoint;
97+
wi.normalize();
98+
Vec3Df ri = 2*norm*Vec3Df::dotProduct(wi, norm)-wi;
99+
ri.normalize();
100+
float f = diffuse*(max(Vec3Df::dotProduct(norm, wi),0.f))+pow(specular*(max(Vec3Df::dotProduct(ri,-direction),0.f)),10);
101+
return f;
96102
}
97-
return false;
103+
return 1000;
98104
}
105+
106+
//float Ray::brdfPhong (const Triangle t,Vec3Df & intersectionPoint, Vec3Df p0, Vec3Df p1, Vec3Df p2, float diffuse) const{
107+
//Vec3Df n0 = Vertices[t.getVertex(0)].getNormal();
108+
//Vec3Df n1 = Vertices[t.getVertex(1)].getNormal();
109+
//Vec3Df n2 = Vertices[t.getVertex(2)].getNormal();
110+
//float inter= b0*p0+b1*p1+b2*p2;
111+
// Vec3Df norm = b0*n0+b1*n1+b2*n2;
112+
// norm.normalize();
113+
114+
// float f = diffuse*(Vec3Df::dotProduct(norm*ray));
115+
// return f;
116+
//}

Ray.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class Ray {
2929
inline Vec3Df & getDirection () { return direction; }
3030

3131
bool intersect (const BoundingBox & bbox, Vec3Df &intersectionPoint) const;
32-
bool intersectTriangle (const Triangle t, Vec3Df &intersectionPoint, Vec3Df p0, Vec3Df p1, Vec3Df p2) const ;//std::vector<Vertex> & Vertices) const;
32+
float intersectTriangle (const Triangle t, Vec3Df &intersectionPoint, Vec3Df p0, Vec3Df p1, Vec3Df p2, float diffuse, float specular, Vec3Df n0, Vec3Df n1, Vec3Df n2, Vec3Df lightpos) const ;//std::vector<Vertex> & Vertices) const;
33+
/// float brdfPhong (const Triangle t, Vec3Df & intersectionPoint, Vec3Df p0, Vec3Df p1, Vec3Df p2, float diffuse) const;
3334
private:
3435
Vec3Df origin;
3536
Vec3Df direction;

RayTracer.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ QImage RayTracer::render (const Vec3Df & camPos,
4949
const Vec3Df rangeBb = maxBb - minBb;
5050
QProgressDialog progressDialog ("Raytracing...", "Cancel", 0, 100);
5151
progressDialog.show ();
52+
5253
for (unsigned int i = 0; i < screenWidth; i++) {
5354
progressDialog.setValue ((100*i)/screenWidth);
5455
for (unsigned int j = 0; j < screenHeight; j++) {
@@ -68,18 +69,30 @@ QImage RayTracer::render (const Vec3Df & camPos,
6869
if (ray.intersect(o.getBoundingBox(), intersectionPoint)){
6970
std::vector<Vertex> Vertices = o.getMesh().getVertices();
7071
std::vector<Triangle> triangles = o.getMesh().getTriangles();
72+
7173
for (unsigned int l = 0; l< triangles.size();l++){
7274
Triangle t = triangles[l];
7375
Vec3Df p0 = Vertices[t.getVertex(0)].getPos();
7476
Vec3Df p1 = Vertices[t.getVertex(1)].getPos();
7577
Vec3Df p2 = Vertices[t.getVertex(2)].getPos();
76-
bool Intersection = ray.intersectTriangle(t , intersectionPoint, p0, p1, p2);//Vertices) ;
77-
if (Intersection) {
78-
float intersectionDistance = Vec3Df::squaredDistance ( intersectionPoint + o.getTrans (),
79-
camPos);
78+
Vec3Df n0 = Vertices[t.getVertex(0)].getNormal();
79+
Vec3Df n1 = Vertices[t.getVertex(1)].getNormal();
80+
Vec3Df n2 = Vertices[t.getVertex(2)].getNormal();
81+
Vec3Df lightpos = scene->getLights()[0].getPos();
82+
float diffuse = o.getMaterial().getDiffuse();
83+
float specular = o.getMaterial().getSpecular();
84+
float Intersection = ray.intersectTriangle(t , intersectionPoint, p0, p1, p2, diffuse,specular, n0, n1, n2, lightpos);//Vertices) ;
85+
86+
if (Intersection!=1000) {
87+
//float f = ray.brdfPhong(t ,intersectionPoint,p0, p1, p2, diffuse);
88+
float intersectionDistance = Vec3Df::squaredDistance ( intersectionPoint + o.getTrans (),
89+
camPos);
8090
if (intersectionDistance < smallestIntersectionDistance) {
81-
c = 255.f * (intersectionPoint);
91+
//Vec3Df tmp (1,1,1);
92+
c = Intersection * o.getMaterial().getColor() * 255.f;
93+
//c = Vec3Df(Intersection, Intersection, Intersection);
8294
smallestIntersectionDistance = intersectionDistance;
95+
//std::cout << Intersection << std::endl;
8396
}
8497
}
8598
}
@@ -91,6 +104,7 @@ QImage RayTracer::render (const Vec3Df & camPos,
91104
}
92105

93106
}
107+
94108
progressDialog.setValue (100);
95109
return image;
96110
}

Vertex.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ void Vertex::scaleToUnitBox (vector<Vertex> & vertices,
5050
for (unsigned int i = 0; i < vertices.size (); i++)
5151
vertices[i].setPos (Vec3Df::segment (center, vertices[i].getPos ()) / scaleToUnit);
5252
}
53+
//struct KDTree::kdtree (Voxel V, Rayon r){
54+
// if (V.feuille){
55+
// return smallestIntersection
56+
// }
57+
//}
5358

5459
void Vertex::normalizeNormals (vector<Vertex> & vertices) {
5560
for (std::vector<Vertex>::iterator it = vertices.begin ();

raymini

14 Bytes
Binary file not shown.

tmp/Ray.o

504 Bytes
Binary file not shown.

tmp/RayTracer.o

400 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)