diff options
author | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-06-29 11:03:15 (GMT) |
---|---|---|
committer | clifford <clifford@b57f626f-c46c-0410-a088-ec61d464b74c> | 2009-06-29 11:03:15 (GMT) |
commit | b8ce36f7e7e76c5cd6c51bc44f651ea43f85a8d7 (patch) | |
tree | 8a8fb60cd2f79854b2a3588d885d9ff301b7810a | |
parent | c16507211f270805f5223c3d84af6b989f173551 (diff) |
Clifford Wolf:
Added support for OpenGL Lights
git-svn-id: http://svn.clifford.at/openscad/trunk@35 b57f626f-c46c-0410-a088-ec61d464b74c
-rw-r--r-- | glview.cc | 24 | ||||
-rw-r--r-- | mainwin.cc | 33 | ||||
-rw-r--r-- | openscad.h | 3 | ||||
-rw-r--r-- | polyset.cc | 14 | ||||
-rw-r--r-- | primitives.cc | 2 |
5 files changed, 67 insertions, 9 deletions
@@ -41,6 +41,8 @@ GLView::GLView(QWidget *parent) : QGLWidget(parent) for (int i = 0; i < 10; i++) shaderinfo[i] = 0; + useLights = false; + setMouseTracking(true); } @@ -164,11 +166,31 @@ void GLView::paintGL() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); + + if (useLights) + { + GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; + GLfloat light_position0[] = {-1.0, -1.0, +1.0, 0.0}; + GLfloat light_position1[] = {+1.0, +1.0, -1.0, 0.0}; + + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT0, GL_POSITION, light_position0); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT1, GL_POSITION, light_position1); + glEnable(GL_LIGHT1); + glEnable(GL_LIGHTING); + glEnable(GL_NORMALIZE); + } + else + { + glDisable(GL_LIGHTING); + } + glRotated(object_rot_y, 1.0, 0.0, 0.0); glRotated(object_rot_z, 0.0, 0.0, 1.0); glDepthFunc(GL_LESS); - glDisable(GL_LIGHTING); #if 0 glLineWidth(1); @@ -87,6 +87,11 @@ MainWindow::MainWindow(const char *filename) actViewModeThrownTogether->setCheckable(true); menu->addSeparator(); + + actViewModeLights = menu->addAction("Lights", this, SLOT(viewModeLights())); + actViewModeLights->setCheckable(true); + + menu->addSeparator(); menu->addAction("Top"); menu->addAction("Bottom"); menu->addAction("Left"); @@ -365,7 +370,7 @@ void MainWindow::actionCompile() static void report_func(const class AbstractNode*, void *vp, int mark) { QProgressDialog *pd = (QProgressDialog*)vp; - int v = (mark*100.0) / progress_report_count; + int v = (int)((mark*100.0) / progress_report_count); pd->setValue(v < 100 ? v : 99); QApplication::processEvents(); } @@ -514,6 +519,9 @@ static void renderGLviaOpenCSG(void *vp) } if (m->root_chain) { + GLint *shaderinfo = m->screen->shaderinfo; + if (m->screen->useLights) + shaderinfo = NULL; std::vector<OpenCSG::Primitive*> primitives; int j = 0; for (int i = 0;; i++) @@ -524,15 +532,17 @@ static void renderGLviaOpenCSG(void *vp) { OpenCSG::render(primitives, OpenCSG::Goldfeather, OpenCSG::NoDepthComplexitySampling); glDepthFunc(GL_EQUAL); - glUseProgram(m->screen->shaderinfo[0]); + if (shaderinfo) + glUseProgram(shaderinfo[0]); for (; j < i; j++) { if (m->root_chain->types[j] == CSGTerm::DIFFERENCE) { - m->root_chain->polysets[j]->render_surface(PolySet::COLOR_CUTOUT, m->screen->shaderinfo); + m->root_chain->polysets[j]->render_surface(PolySet::COLOR_CUTOUT, shaderinfo); } else { - m->root_chain->polysets[j]->render_surface(PolySet::COLOR_MATERIAL, m->screen->shaderinfo); + m->root_chain->polysets[j]->render_surface(PolySet::COLOR_MATERIAL, shaderinfo); } } - glUseProgram(0); + if (shaderinfo) + glUseProgram(0); for (unsigned int k = 0; k < primitives.size(); k++) { delete primitives[k]; } @@ -645,10 +655,12 @@ static void renderGLThrownTogether(void *vp) for (int i = 0; i < m->root_chain->polysets.size(); i++) { if (m->root_chain->types[i] == CSGTerm::DIFFERENCE) { m->root_chain->polysets[i]->render_surface(PolySet::COLOR_CUTOUT); - m->root_chain->polysets[i]->render_edges(PolySet::COLOR_CUTOUT); + if (!m->screen->useLights) + m->root_chain->polysets[i]->render_edges(PolySet::COLOR_CUTOUT); } else { m->root_chain->polysets[i]->render_surface(PolySet::COLOR_MATERIAL); - m->root_chain->polysets[i]->render_edges(PolySet::COLOR_MATERIAL); + if (!m->screen->useLights) + m->root_chain->polysets[i]->render_edges(PolySet::COLOR_MATERIAL); } } #endif @@ -664,3 +676,10 @@ void MainWindow::viewModeThrownTogether() screen->updateGL(); } +void MainWindow::viewModeLights() +{ + screen->useLights = !screen->useLights; + screen->updateGL(); + actViewModeLights->setChecked(screen->useLights); +} + @@ -432,6 +432,7 @@ public: double w_h_ratio; GLint shaderinfo[11]; + bool useLights; GLView(QWidget *parent = NULL); @@ -506,6 +507,7 @@ public: QAction *actViewModeCGALGrid; #endif QAction *actViewModeThrownTogether; + QAction *actViewModeLights; void viewModeActionsUncheck(); private slots: @@ -517,6 +519,7 @@ private slots: void viewModeCGALGrid(); #endif void viewModeThrownTogether(); + void viewModeLights(); }; extern AbstractModule *parse(const char *text, int debug); @@ -51,6 +51,14 @@ void PolySet::setmatrix(double m[16]) static void gl_draw_triangle(GLint *shaderinfo, const PolySet::Point *p0, const PolySet::Point *p1, const PolySet::Point *p2, bool e0, bool e1, bool e2) { + double ax = p1->x - p0->x, bx = p1->x - p2->x; + double ay = p1->y - p0->y, by = p1->y - p2->y; + double az = p1->z - p0->z, bz = p1->z - p2->z; + double nx = ay*bz - az*by; + double ny = az*bx - ax*bz; + double nz = ax*by - ay*bx; + double nl = sqrt(nx*nx + ny*ny + nz*nz); + glNormal3d(nx / nl, ny / nl, nz / nl); #ifdef ENABLE_OPENCSG if (shaderinfo) { double e0f = e0 ? 2.0 : -1.0; @@ -87,6 +95,9 @@ void PolySet::render_surface(colormode_e colormode, GLint *shaderinfo) const glMultMatrixd(m); if (colormode == COLOR_MATERIAL) { glColor3ub(249, 215, 44); + GLfloat light_diffuse[] = {255 / 255.0, 236 / 255.0, 94 / 255.0, 1.0}; + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); #ifdef ENABLE_OPENCSG if (shaderinfo) { glUniform4f(shaderinfo[1], 249 / 255.0, 215 / 255.0, 44 / 255.0, 1.0); @@ -96,6 +107,9 @@ void PolySet::render_surface(colormode_e colormode, GLint *shaderinfo) const } if (colormode == COLOR_CUTOUT) { glColor3ub(157, 203, 81); + GLfloat light_diffuse[] = {171 / 255.0, 216 / 255.0, 86 / 255.0, 1.0}; + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse); #ifdef ENABLE_OPENCSG if (shaderinfo) { glUniform4f(shaderinfo[1], 157 / 255.0, 203 / 255.0, 81 / 255.0, 1.0); diff --git a/primitives.cc b/primitives.cc index 1430d10..ac80ac5 100644 --- a/primitives.cc +++ b/primitives.cc @@ -141,7 +141,7 @@ int get_fragments_from_r(double r, double fs, double fa) return (int)ceil(fmax(fmin(360.0 / fa, r*M_PI / fs), 5)); } -PolySet *PrimitiveNode::render_polyset(render_mode_e mode) const +PolySet *PrimitiveNode::render_polyset(render_mode_e) const { PolySet *p = new PolySet(); |