diff --git a/Makefile b/Makefile index 7a0a708..ea6649a 100755 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ CXXFLAGS := $(CXXFLAGS) -g -DDEBUG endif -OBJECTS = main.o poly.o model.o light.o vec.o color.o thing.o animation.o +OBJECTS = main.o poly.o model.o light.o vec.o material.o color.o thing.o animation.o all: $(OBJECTS) @if [ ! -d $(BINDIR) ]; then \ diff --git a/include/color.h b/include/color.h index 62e0ba8..da87299 100755 --- a/include/color.h +++ b/include/color.h @@ -1,14 +1,25 @@ #ifndef _COLOR_H #define _COLOR_H +#include -class Color { +#include +#include + +class Color { + private: + float c[4]; + public: - float r; - float g; - float b; + Color(float r, float g, float b); - Color(float _r, float _g, float _b); - void drawColor(); + float *v(); + float r(); + float g(); + float b(); + Color* clone(); + void vardump(std::string whitespace); + void drawColor(); + void drawColor(GLenum pname); }; #endif diff --git a/include/light.h b/include/light.h index d7f712c..ee6605c 100755 --- a/include/light.h +++ b/include/light.h @@ -32,4 +32,10 @@ AmbientLight(Color& _color, GLenum _lightnum); }; +class SpecularLight: public Light{ + public: + Vec* pos; + SpecularLight(Vec& _pos, Color& _color, GLenum _lightnum); +}; + #endif diff --git a/include/material.h b/include/material.h new file mode 100644 index 0000000..a559542 --- /dev/null +++ b/include/material.h @@ -0,0 +1,23 @@ +#ifndef _MATERIAL_H +#define _MATERIAL_H + +#include "color.h" +#include + +class Material { + private: + Color* ambient; //ambient and diffuse + Color* specular; + + public: + Material(); + Material(Color _ambient); + Material(Color _ambient, Color _specular); + + Material* clone(); + + void vardump(std::string whitespace); + void ptdump(std::string whitespace); + void drawMaterial(); +}; +#endif diff --git a/include/model.h b/include/model.h index 4966dd3..697d929 100755 --- a/include/model.h +++ b/include/model.h @@ -31,6 +31,7 @@ int numFaces(); void vardump(std::string whitespace); void setDelta(float d); + void setMaterial(Color ambient, Color specular); float getDelta(); void showNormals(); void hideNormals(); diff --git a/include/poly.h b/include/poly.h index a6fa1b8..6bff3f8 100755 --- a/include/poly.h +++ b/include/poly.h @@ -5,6 +5,7 @@ #include "spoly.h" #include "thing.h" #include "color.h" +#include "material.h" #include #include @@ -18,14 +19,16 @@ Vec* rotation; Vec* center; Vec* normal; - Color* color; + Material* material; float delta; bool drawNormals; float scaleFactor; + public: Poly(); Poly(std::vector & _points); Poly(std::vector & _points, Color _color); + Poly(std::vector & _points, Material _material); ~Poly(); void setCenter(Vec c); void setCenter(Vec* c); @@ -40,9 +43,13 @@ int numPoints(); void vardump(std::string whitespace); void addPoint(Vec* p); + void setColor(Color c); void setColor(Color* c); - Color* getColor(); + void setMaterial(Material m); + void setMaterial(Material* m); + Material* getMaterial(); + void setDelta(float d); void showNormals(); void hideNormals(); diff --git a/sources/color.cpp b/sources/color.cpp index c3149fa..007c099 100755 --- a/sources/color.cpp +++ b/sources/color.cpp @@ -1,18 +1,44 @@ #include "color.h" -#include -#include - -Color::Color(float _r, float _g, float _b) { - r = _r; - g = _g; - b = _b; +Color::Color(float r, float g, float b) { + c[0] = r; + c[1] = g; + c[2] = b; + c[3] = 1.0f; + //r = _r; + //g = _g; + //b = _b; } void Color::drawColor() { - glColor4f(r, g, b, 1.0f); + glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c); +} + +void Color::drawColor(GLenum pname) { + glMaterialfv(GL_FRONT, pname, c); } Color* Color::clone() { - return new Color(r,g,b); + Color* c = new Color(r(),g(),b()); + return c; +} + +void Color::vardump(std::string whitespace) { + printf("%sColor(%f, %f, %f)\n", whitespace.c_str(), + c[0], c[1], c[2]); +} + +float* Color::v() { + return c; +} +float Color::r() { + return c[0]; +} + +float Color::g() { + return c[1]; +} + +float Color::b() { + return c[2]; } diff --git a/sources/light.cpp b/sources/light.cpp index 2cdb4cc..eaf9e7b 100755 --- a/sources/light.cpp +++ b/sources/light.cpp @@ -26,14 +26,25 @@ } - DiffuseLight::DiffuseLight(Vec& _pos, Color& _color, GLenum _lightnum) { pos = _pos.clone(); col = _color.clone(); num = _lightnum; - GLfloat LightDiffuse[]= { col->r, col->g, col->b, 1.0f }; - glLightfv(num, GL_DIFFUSE, LightDiffuse); + glLightfv(num, GL_DIFFUSE, col->v()); + GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; + glLightfv(num, GL_POSITION,LightPosition); + enable(); +} + + + +SpecularLight::SpecularLight(Vec& _pos, Color& _color, GLenum _lightnum) { + pos = _pos.clone(); + col = _color.clone(); + num = _lightnum; + + glLightfv(num, GL_SPECULAR, col->v()); GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; glLightfv(num, GL_POSITION,LightPosition); enable(); @@ -44,7 +55,6 @@ AmbientLight::AmbientLight(Color& _color, GLenum _lightnum) { col = _color.clone(); num = _lightnum; - GLfloat LightAmbient[]= { 0.1f, 0.1f, 0.1f, 1.0f }; - glLightfv(num, GL_AMBIENT, LightAmbient); + glLightfv(num, GL_AMBIENT, col->v()); enable(); } diff --git a/sources/main.cpp b/sources/main.cpp index 4504c59..a894578 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -48,6 +48,7 @@ #endif #include "vec.h" +#include "material.h" #include "color.h" #include "thing.h" #include "poly.h" @@ -276,7 +277,7 @@ std::vector top; std::vector rear; std::vector front; - + bottom.push_back(ulh); bottom.push_back(urh); bottom.push_back(urv); @@ -313,6 +314,8 @@ cube->addFace(new Poly(top, blue)); cube->addFace(new Poly(rear, yellow)); cube->addFace(new Poly(front, white)); + + printf("cube ok\n"); cube->setPosition(cubePos); //cube->setCenter(cubeCen); @@ -334,6 +337,9 @@ Model* myModel = importModel(modelfile); printf("imported %s : %d faces\n", modelfile, myModel->numFaces()); Vec myCen = Vec(5,-10,5); + + //myModel->setCenter(myCen); + //myModel->setMaterial(Color(1,0,1), Color(1,0,1)); myModel->setPosition(myCen); myModel->showNormals(); myModel->setDelta(PI/2.0); @@ -388,17 +394,22 @@ //enable light glEnable(GL_LIGHTING); + + GLfloat global_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f }; + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); //light info - Vec l1_pos(0.0f, -10.0f, -10.0f); - Color l1_col(1.0f, 1.0f, 1.0f); + //Color l0_col(0.1f, 0.1f, 0.1f); + //AmbientLight light0(l0_col, GL_LIGHT0); + //Color l0_col(0.3f, 0.3f, 0.3f); + //Vec l0_pos(0.0f, 10.0f, 10.0f); + //SpecularLight light0(l0_pos, l0_col, GL_LIGHT0); - Color l0_col(0.5f, 0.5f, 0.5f); + //Vec l1_pos(10.0f, 10.0f, 10.0f); + //Color l1_col(0.0f, 0.5f, 1.0f); + //DiffuseLight light1(l1_pos, l1_col, GL_LIGHT1); - AmbientLight light0(l0_col, GL_LIGHT0); - DiffuseLight light1(l1_pos, l1_col, GL_LIGHT1); - - glEnable(GL_COLOR_MATERIAL); + //glEnable(GL_COLOR_MATERIAL); glShadeModel(GL_SMOOTH); @@ -407,7 +418,6 @@ /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// // Mainloop: - float deltacolor = 0.0f; int react_keyevent = 0; @@ -437,7 +447,7 @@ } cube->showNormals(); - + while (!done) { ///////////////////// // Do some rendering: @@ -463,7 +473,7 @@ //initial viewer position glLoadIdentity(); - + // move viewer myView->draw(); @@ -505,15 +515,16 @@ done = true; } - // lights etc. + //allow to hit keys only every few frames. if (react_keyevent == 0) { react_keyevent = 10; + // lights if (keystate[SDLK_1]) { - light0.flip(); + //light0.flip(); } else if (keystate[SDLK_2]) { - light1.flip(); + //light1.flip(); } else { react_keyevent = 0; diff --git a/sources/material.cpp b/sources/material.cpp new file mode 100644 index 0000000..f089e6d --- /dev/null +++ b/sources/material.cpp @@ -0,0 +1,43 @@ +#include "material.h" + +#include +#include + +Material::Material() { + ambient = new Color(0,0,0); + specular = new Color(0.1,0.1,0.1); + ptdump("m0"); +} + +Material::Material(Color _ambient) { + ambient = _ambient.clone(); + specular = new Color(0,0,0); + ptdump("m1"); +} + +Material::Material(Color _ambient, Color _specular) { + ambient = _ambient.clone(); + specular = _specular.clone(); + ptdump("m2"); +} + +void Material::drawMaterial() { + ambient->drawColor(); + specular->drawColor(GL_SPECULAR); +} + +Material* Material::clone() { + Color a = *ambient; + Color s = *specular; + return new Material(a, s); +} + +void Material::ptdump(std::string id) { + printf("M::%s %p %p %p\n", id.c_str(), this, ambient, specular); +} + +void Material::vardump(std::string whitespace) { + printf("%sMaterial:\n", whitespace.c_str()); + ambient->vardump(whitespace + " "); + specular->vardump(whitespace + " "); +} diff --git a/sources/model.cpp b/sources/model.cpp index 266ac45..0a0319f 100755 --- a/sources/model.cpp +++ b/sources/model.cpp @@ -31,6 +31,13 @@ } } +void Model::setMaterial(Color ambient, Color specular) { + Material m = Material(ambient, specular); + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->setMaterial(m); + } +} + Vec Model::calcCenter() { Vec s(0,0,0); diff --git a/sources/poly.cpp b/sources/poly.cpp index fc9970f..b748875 100755 --- a/sources/poly.cpp +++ b/sources/poly.cpp @@ -17,30 +17,47 @@ position = new Vec(0,0,0); center = new Vec(0,0,0); rotation = new Vec(0,0,0); - color = new Color(1,0,0); + material = new Material(); normal = new Vec(0,0,0); } -Poly::Poly(std::vector & _points) { - Poly(_points, Color(1,0,0)); +Poly::Poly(std::vector & _points) { + Poly(_points, Material()); + //TODO: this wont work. segmentation fault. + material->ptdump("b"); } Poly::Poly(std::vector & _points, Color _color) { delta = 0.0f; - scaleFactor = 1.0f; drawNormals = false; position = new Vec(0,0,0); - rotation = new Vec(0,0,0); + rotation = new Vec(0,0,0); + center = new Vec(0,0,0); normal = new Vec(0,0,0); - color = _color.clone(); + + Color c2 = Color(0,0,0); + material = new Material(_color, c2); for (std::vector::size_type i = 0 ; i < _points.size(); i++ ) { addPoint(_points.at(i)); } + //setCenter(calcCenter()); +} - //setCenter(calcCenter()); - +Poly::Poly(std::vector & _points, Material _material) { + delta = 0.0f; + drawNormals = false; + position = new Vec(0,0,0); + rotation = new Vec(0,0,0); + material = _material.clone(); + center = new Vec(0,0,0); + normal = new Vec(0,0,0); + + for (std::vector::size_type i = 0 ; i < _points.size(); i++ ) { + addPoint(_points.at(i)); + } + //setCenter(calcCenter()); } void Poly::calcNormal() { @@ -54,7 +71,7 @@ Poly::~Poly() { delete position; delete rotation; - delete color; + delete material; delete normal; } @@ -223,7 +240,7 @@ glNormal3fv(n.c); } - color->drawColor(); + material->drawMaterial(); glVertex3fv((*initpoints.at(i)).c); } @@ -238,11 +255,10 @@ } void Poly::vardump(std::string whitespace) { - printf("%sPoly(%d points, center: [%f, %f, %f], rotation: [%f, %f, %f], color: [%f, %f, %f])\n", + printf("%sPoly(%d points, center: [%f, %f, %f], rotation: [%f, %f, %f]\n", whitespace.c_str(), initpoints.size(), position->x(), position->y(), position->z(), - rotation->x(), rotation->y(), rotation->z(), - color->r, color->g, color->b + rotation->x(), rotation->y(), rotation->z() ); for (std::vector::size_type i = 0 ; i < initpoints.size(); i++ ) { @@ -260,17 +276,32 @@ } void Poly::setColor(Color c) { - color = c.clone(); + delete material; + material = new Material(c, Color(0,0,0)); } void Poly::setColor(Color* c) { - color = c->clone(); + delete material; + Color sc = *c; + material = new Material(sc, Color(0,0,0)); } -Color* Poly::getColor() { - return color; +Material* Poly::getMaterial() { + printf("got material %f", material); + return material; } +void Poly::setMaterial(Material *m){ + delete material; + material = m->clone(); +} + +void Poly::setMaterial(Material m){ + delete material; + material = m.clone(); +} + + void Poly::setDelta(float d) { delta = d; } @@ -297,12 +328,11 @@ } np->setDelta(delta); + np->setMaterial(material); np->setPosition(position); np->setCenter(center); np->setRotation(rotation); - np->setColor(color); np->scale(scaleFactor); - return np; } diff --git a/sources/thing.cpp b/sources/thing.cpp index f21e376..ebc0aed 100644 --- a/sources/thing.cpp +++ b/sources/thing.cpp @@ -50,7 +50,7 @@ bgcolor = c; } void View::draw() { - glClearColor(bgcolor->r, bgcolor->g, bgcolor->b, 1.0f); + //glClearColor(bgcolor->r, bgcolor->g, bgcolor->b, 1.0f); glRotatef(rotation->x(), 1.0f, 0.0f, 0.0f); glRotatef(rotation->y(), 0.0f, 1.0f, 0.0f);