Clifford Wolf:

Improvements in pseudo wireframe shader



git-svn-id: http://svn.clifford.at/openscad/trunk@25 b57f626f-c46c-0410-a088-ec61d464b74c
stl_dim
clifford 2009-06-26 17:07:40 +00:00
parent cc913b4d8f
commit 7b153c2092
4 changed files with 51 additions and 47 deletions

View File

@ -38,7 +38,9 @@ GLView::GLView(QWidget *parent) : QGLWidget(parent)
renderfunc = NULL;
renderfunc_vp = NULL;
edgeshader_prog = 0;
for (int i = 0; i < 10; i++)
shaderinfo[i] = 0;
setMouseTracking(true);
}
@ -59,21 +61,21 @@ void GLView::initializeGL()
if (glewIsSupported("GL_VERSION_2_0"))
{
char *vs_source =
"attribute float e1, e2, e3;\n"
"varying float ve1, ve2, ve3;\n"
const char *vs_source =
"attribute vec3 tripos;\n"
"varying vec3 tp;\n"
"void main() {\n"
" gl_FrontColor = gl_Color;\n"
" gl_Position = ftransform();\n"
" ve1 = e1; ve2 = e2; ve3 = e3;\n"
" gl_Position = ftransform();\n"
" tp = tripos;\n"
"}\n";
char *fs_source =
"varying float ve1, ve2, ve3;\n"
const char *fs_source =
"uniform vec4 color1, color2;\n"
"varying vec3 tp;\n"
"void main() {\n"
" gl_FragColor = vec4(249.0/255.0, 215.0/255.0, 44.0/255.0, 1.0);\n"
" if (ve1 > 0.95 || ve2 > 0.95 || ve3 > 0.95)\n"
" gl_FragColor = vec4(255.0/255.0, 236.0/255.0, 94.0/255.0, 1.0);\n"
" gl_FragColor = color1;\n"
" if (tp.x > 0.95 || tp.y > 0.95 || tp.z > 0.95)\n"
" gl_FragColor = color2;\n"
"}\n";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
@ -84,11 +86,21 @@ void GLView::initializeGL()
glShaderSource(fs, 1, (const GLchar**)&fs_source, NULL);
glCompileShader(fs);
edgeshader_prog = glCreateProgram();
GLuint edgeshader_prog = glCreateProgram();
glAttachShader(edgeshader_prog, vs);
glAttachShader(edgeshader_prog, fs);
glLinkProgram(edgeshader_prog);
shaderinfo[0] = edgeshader_prog;
shaderinfo[1] = glGetUniformLocation(edgeshader_prog, "color1");
shaderinfo[2] = glGetUniformLocation(edgeshader_prog, "color2");
shaderinfo[3] = glGetAttribLocation(edgeshader_prog, "tripos");
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
fprintf(stderr, "OpenGL Error: %s\n", gluErrorString(err));
}
GLint status;
glGetProgramiv(edgeshader_prog, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {

View File

@ -419,9 +419,6 @@ static void renderGLviaOpenCSG(void *vp)
if (m->root_chain) {
std::vector<OpenCSG::Primitive*> primitives;
GLint e1 = glGetAttribLocation(m->screen->edgeshader_prog, "e1");
GLint e2 = glGetAttribLocation(m->screen->edgeshader_prog, "e2");
GLint e3 = glGetAttribLocation(m->screen->edgeshader_prog, "e3");
int j = 0;
for (int i = 0;; i++)
{
@ -431,12 +428,12 @@ static void renderGLviaOpenCSG(void *vp)
{
OpenCSG::render(primitives, OpenCSG::Goldfeather, OpenCSG::NoDepthComplexitySampling);
glDepthFunc(GL_EQUAL);
glUseProgram(m->screen->edgeshader_prog);
glUseProgram(m->screen->shaderinfo[0]);
for (; j < i; j++) {
if (m->root_chain->types[j] == CSGTerm::DIFFERENCE) {
m->root_chain->polysets[j]->render_surface(PolySet::COLOR_CUTOUT, e1, e2, e3);
m->root_chain->polysets[j]->render_surface(PolySet::COLOR_CUTOUT, m->screen->shaderinfo);
} else {
m->root_chain->polysets[j]->render_surface(PolySet::COLOR_MATERIAL, e1, e2, e3);
m->root_chain->polysets[j]->render_surface(PolySet::COLOR_MATERIAL, m->screen->shaderinfo);
}
}
glUseProgram(0);

View File

@ -314,7 +314,7 @@ public:
COLOR_CUTOUT
};
void render_surface(colormode_e colormode, GLint e1 = 0, GLint e2 = 0, GLint e3 = 0) const;
void render_surface(colormode_e colormode, GLint *shaderinfo = NULL) const;
void render_edges(colormode_e colormode) const;
#ifdef ENABLE_CGAL
@ -428,7 +428,7 @@ public:
double object_rot_z;
double w_h_ratio;
GLuint edgeshader_prog;
GLint shaderinfo[10];
GLView(QWidget *parent = NULL);

View File

@ -53,33 +53,22 @@ static void set_opengl_normal(double x1, double y1, double z1, double x2, double
glNormal3d(-nx, -ny, -nz);
}
static void set_triangle_point_data(GLint e1, GLint e2, GLint e3)
void PolySet::render_surface(colormode_e colormode, GLint *shaderinfo) const
{
static int state = 0;
if (state == 0) {
glVertexAttrib1d(e1, 1.0);
glVertexAttrib1d(e2, 1.0);
glVertexAttrib1d(e3, 0.0);
}
if (state == 1) {
glVertexAttrib1d(e1, 0.0);
glVertexAttrib1d(e2, 1.0);
glVertexAttrib1d(e3, 1.0);
}
if (state == 2) {
glVertexAttrib1d(e1, 1.0);
glVertexAttrib1d(e2, 0.0);
glVertexAttrib1d(e3, 1.0);
}
state = (state + 1) % 3;
}
void PolySet::render_surface(colormode_e colormode, GLint e1, GLint e2, GLint e3) const
{
if (colormode == COLOR_MATERIAL)
if (colormode == COLOR_MATERIAL) {
glColor3ub(249, 215, 44);
if (colormode == COLOR_CUTOUT)
if (shaderinfo) {
glUniform4f(shaderinfo[1], 249 / 255.0, 215 / 255.0, 44 / 255.0, 1.0);
glUniform4f(shaderinfo[2], 255 / 255.0, 236 / 255.0, 94 / 255.0, 1.0);
}
}
if (colormode == COLOR_CUTOUT) {
glColor3ub(157, 203, 81);
if (shaderinfo) {
glUniform4f(shaderinfo[1], 157 / 255.0, 203 / 255.0, 81 / 255.0, 1.0);
glUniform4f(shaderinfo[2], 171 / 255.0, 216 / 255.0, 86 / 255.0, 1.0);
}
}
for (int i = 0; i < polygons.size(); i++) {
const Polygon *poly = &polygons[i];
glBegin(GL_POLYGON);
@ -88,8 +77,14 @@ void PolySet::render_surface(colormode_e colormode, GLint e1, GLint e2, GLint e3
poly->at(2).x, poly->at(2).y, poly->at(2).z);
for (int j = 0; j < poly->size(); j++) {
const Point *p = &poly->at(j);
if (e1 && e2 && e3)
set_triangle_point_data(e1, e2, e3);
if (shaderinfo) {
if (j%3 == 0)
glVertexAttrib3d(shaderinfo[3], 1.0, 1.0, 0.0);
if (j%3 == 1)
glVertexAttrib3d(shaderinfo[3], 1.0, 0.0, 1.0);
if (j%3 == 2)
glVertexAttrib3d(shaderinfo[3], 0.0, 1.0, 1.0);
}
glVertex3d(p->x, p->y, p->z);
}
glEnd();