WIP: introduced a shader that does the clipping

This commit is contained in:
Andreas Fabri 2015-12-11 10:52:35 +01:00
parent 8cdef0eadc
commit e7e5769e9c
8 changed files with 84 additions and 18 deletions

View File

@ -23,6 +23,7 @@
</qresource> </qresource>
<qresource prefix="/cgal/Polyhedron_3"> <qresource prefix="/cgal/Polyhedron_3">
<file alias="about.html">resources/about.html</file> <file alias="about.html">resources/about.html</file>
<file>resources/shader_c3t3.v</file>
<file>resources/shader_with_light.v</file> <file>resources/shader_with_light.v</file>
<file>resources/shader_with_light.f</file> <file>resources/shader_with_light.f</file>
<file>resources/shader_without_light.f</file> <file>resources/shader_without_light.f</file>

View File

@ -104,6 +104,7 @@ Scene_c3t3_item::Scene_c3t3_item()
need_changed = false; need_changed = false;
startTimer(0); startTimer(0);
connect(frame, SIGNAL(modified()), this, SLOT(changed())); connect(frame, SIGNAL(modified()), this, SLOT(changed()));
std::cerr << "A \n";
c3t3_changed(); c3t3_changed();
setRenderingMode(FlatPlusEdges); setRenderingMode(FlatPlusEdges);
compile_shaders(); compile_shaders();
@ -129,7 +130,8 @@ Scene_c3t3_item::Scene_c3t3_item(const C3t3& c3t3)
ws_vertex.resize(0); ws_vertex.resize(0);
need_changed = false; need_changed = false;
startTimer(0); startTimer(0);
connect(frame, SIGNAL(modified()), this, SLOT(changed())); // connect(frame, SIGNAL(modified()), this, SLOT(changed()));
std::cerr << "B \n";
c3t3_changed(); c3t3_changed();
setRenderingMode(FlatPlusEdges); setRenderingMode(FlatPlusEdges);
compile_shaders(); compile_shaders();
@ -182,19 +184,23 @@ Scene_c3t3_item::c3t3()
void void
Scene_c3t3_item::changed() Scene_c3t3_item::changed()
{ {
std::cerr << "changed()\n";
need_changed = true; need_changed = true;
} }
void Scene_c3t3_item::timerEvent(QTimerEvent* /*event*/) void Scene_c3t3_item::timerEvent(QTimerEvent* /*event*/)
{ // just handle deformation - paint like selection is handled in eventFilter() { // just handle deformation - paint like selection is handled in eventFilter()
if(need_changed) { if(need_changed) {
c3t3_changed(); std::cerr << "C \n";
invalidate_buffers();
need_changed = false;
} }
} }
void void
Scene_c3t3_item::c3t3_changed() Scene_c3t3_item::c3t3_changed()
{ {
std::cerr << "c3t3_changedn";
// Update colors // Update colors
// Fill indices map and get max subdomain value // Fill indices map and get max subdomain value
indices_.clear(); indices_.clear();
@ -212,9 +218,7 @@ Scene_c3t3_item::c3t3_changed()
// Rebuild histogram // Rebuild histogram
build_histogram(); build_histogram();
//compute_elements();
this->invalidate_buffers();
need_changed = false;
} }
QPixmap QPixmap
@ -472,10 +476,12 @@ void Scene_c3t3_item::draw(CGAL::Three::Viewer_interface* viewer) const {
initialize_buffers(viewer); initialize_buffers(viewer);
} }
vaos[Facets]->bind(); vaos[Facets]->bind();
program = getShaderProgram(PROGRAM_WITH_LIGHT); program = getShaderProgram(PROGRAM_C3T3);
attrib_buffers(viewer, PROGRAM_WITH_LIGHT); attrib_buffers(viewer, PROGRAM_C3T3);
program->bind(); program->bind();
//program->setAttributeValue("colors", this->color()); QVector4D cp(this->plane().a(),this->plane().b(),this->plane().c(),this->plane().d());
program->setUniformValue("cutplane", cp);
viewer->glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(positions_poly.size() / 3)); viewer->glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(positions_poly.size() / 3));
program->release(); program->release();
vaos[Facets]->release(); vaos[Facets]->release();
@ -763,7 +769,7 @@ void Scene_c3t3_item::initialize_buffers(CGAL::Three::Viewer_interface *viewer)c
{ {
//vao containing the data for the facets //vao containing the data for the facets
{ {
program = getShaderProgram(PROGRAM_WITH_LIGHT, viewer); program = getShaderProgram(PROGRAM_C3T3, viewer);
program->bind(); program->bind();
vaos[Facets]->bind(); vaos[Facets]->bind();
@ -920,6 +926,7 @@ void Scene_c3t3_item::initialize_buffers(CGAL::Three::Viewer_interface *viewer)c
void Scene_c3t3_item::compute_elements() const void Scene_c3t3_item::compute_elements() const
{ {
std::cerr << "compute_elements\n";
positions_lines.clear(); positions_lines.clear();
positions_poly.clear(); positions_poly.clear();
normals.clear(); normals.clear();

View File

@ -58,11 +58,6 @@ public:
void c3t3_changed(); void c3t3_changed();
void contextual_changed()
{
if (frame->isManipulated() || frame->isSpinning())
invalidate_buffers();
}
const C3t3& c3t3() const; const C3t3& c3t3() const;
C3t3& c3t3(); C3t3& c3t3();

View File

@ -464,6 +464,16 @@ void Viewer::attrib_buffers(int program_name) const {
program->setUniformValue("spec_power", 51.8f); program->setUniformValue("spec_power", 51.8f);
program->setUniformValue("is_two_side", is_both_sides); program->setUniformValue("is_two_side", is_both_sides);
break; break;
case PROGRAM_C3T3:
program->setUniformValue("mvp_matrix", mvp_mat);
program->setUniformValue("mv_matrix", mv_mat);
program->setUniformValue("light_pos", position);
program->setUniformValue("light_diff",diffuse);
program->setUniformValue("light_spec", specular);
program->setUniformValue("light_amb", ambient);
program->setUniformValue("spec_power", 51.8f);
program->setUniformValue("is_two_side", is_both_sides);
break;
case PROGRAM_WITHOUT_LIGHT: case PROGRAM_WITHOUT_LIGHT:
program->setUniformValue("mvp_matrix", mvp_mat); program->setUniformValue("mvp_matrix", mvp_mat);
program->setUniformValue("mv_matrix", mv_mat); program->setUniformValue("mv_matrix", mv_mat);
@ -884,6 +894,29 @@ QOpenGLShaderProgram* Viewer::getShaderProgram(int name) const
switch(name) switch(name)
{ {
/// @TODO: factorize this code /// @TODO: factorize this code
case PROGRAM_C3T3:
if(d->shader_programs[PROGRAM_C3T3])
{
return d->shader_programs[PROGRAM_C3T3];
}
else
{
QOpenGLShaderProgram *program = new QOpenGLShaderProgram(viewer);
if(!program->addShaderFromSourceFile(QOpenGLShader::Vertex,":/cgal/Polyhedron_3/resources/shader_c3t3.v"))
{
std::cerr<<"adding vertex shader FAILED"<<std::endl;
}
if(!program->addShaderFromSourceFile(QOpenGLShader::Fragment,":/cgal/Polyhedron_3/resources/shader_with_light.f"))
{
std::cerr<<"adding fragment shader FAILED"<<std::endl;
}
program->link();
d->shader_programs[PROGRAM_C3T3] = program;
return program;
}
break;
case PROGRAM_WITH_LIGHT: case PROGRAM_WITH_LIGHT:
if(d->shader_programs[PROGRAM_WITH_LIGHT]) if(d->shader_programs[PROGRAM_WITH_LIGHT])
{ {

View File

@ -0,0 +1,22 @@
#version 120
attribute highp vec4 vertex;
attribute highp vec3 normals;
attribute highp vec3 colors;
uniform highp mat4 mvp_matrix;
uniform highp mat4 mv_matrix;
uniform highp vec4 cutplane;
varying highp vec4 fP;
varying highp vec3 fN;
varying highp vec4 color;
void main(void)
{
if(vertex.x * cutplane.x + vertex.y * cutplane.y + vertex.z * cutplane.z + cutplane.w > 0){
//color = vec4(colors, 1.0);
color = vec4(0.0, 1.0, 1.0, 0.0);
} else {
color = vec4(0.0, 0.0, 1.0, 1.0);
}
fP = mv_matrix * vertex;
fN = mat3(mv_matrix)* normals;
gl_Position = mvp_matrix * vertex;
}

View File

@ -10,6 +10,7 @@ uniform highp float spec_power ;
uniform int is_two_side; uniform int is_two_side;
uniform bool is_selected; uniform bool is_selected;
void main(void) { void main(void) {
if(color.w > 0.0){
highp vec3 L = light_pos.xyz - fP.xyz; highp vec3 L = light_pos.xyz - fP.xyz;
highp vec3 V = -fP.xyz; highp vec3 V = -fP.xyz;
highp vec3 N; highp vec3 N;
@ -31,4 +32,9 @@ void main(void) {
gl_FragColor = vec4(ret_color.r+70.0/255.0, ret_color.g+70.0/255.0, ret_color.b+70.0/255.0, 1.0); gl_FragColor = vec4(ret_color.r+70.0/255.0, ret_color.g+70.0/255.0, ret_color.b+70.0/255.0, 1.0);
else else
gl_FragColor = ret_color; gl_FragColor = ret_color;
}else{
discard;
} }
}

View File

@ -60,6 +60,7 @@ public:
PROGRAM_WITH_TEXTURED_EDGES, PROGRAM_WITH_TEXTURED_EDGES,
PROGRAM_INSTANCED, PROGRAM_INSTANCED,
PROGRAM_INSTANCED_WIRE, PROGRAM_INSTANCED_WIRE,
PROGRAM_C3T3,
NB_OF_PROGRAMS }; NB_OF_PROGRAMS };
typedef CGAL::Three::Scene_interface::Bbox Bbox; typedef CGAL::Three::Scene_interface::Bbox Bbox;

View File

@ -50,6 +50,7 @@ public:
PROGRAM_WITH_TEXTURED_EDGES, PROGRAM_WITH_TEXTURED_EDGES,
PROGRAM_INSTANCED, PROGRAM_INSTANCED,
PROGRAM_INSTANCED_WIRE, PROGRAM_INSTANCED_WIRE,
PROGRAM_C3T3,
NB_OF_PROGRAMS }; NB_OF_PROGRAMS };
Viewer_interface(QWidget* parent) : QGLViewer(CGAL::Qt::createOpenGLContext(), parent) {} Viewer_interface(QWidget* parent) : QGLViewer(CGAL::Qt::createOpenGLContext(), parent) {}