diff options
author | don bright <hugh.m.bright@gmail.com> | 2013-02-24 17:21:45 (GMT) |
---|---|---|
committer | don bright <hugh.m.bright@gmail.com> | 2013-02-24 17:21:45 (GMT) |
commit | 707118ce9485f1c703bf6cc99bad3affc2a48c10 (patch) | |
tree | e88a90b086f7cb7d2cf331554f5cae7b0fca2def /src/OffscreenView.cc | |
parent | 91896e0d9680a5919ff31d82162a855f5ef2b08c (diff) |
move enable_opencsg_shaders, fix indenting, split out display_opencsg_warning
also update docs, and try to make enable_opencsg_shaders the same between
qgl and offscreengl
Diffstat (limited to 'src/OffscreenView.cc')
-rw-r--r-- | src/OffscreenView.cc | 340 |
1 files changed, 173 insertions, 167 deletions
diff --git a/src/OffscreenView.cc b/src/OffscreenView.cc index aa8570e..1a5949a 100644 --- a/src/OffscreenView.cc +++ b/src/OffscreenView.cc @@ -16,25 +16,30 @@ OffscreenView::OffscreenView(size_t width, size_t height) { - object_rot << 35, 0, 25; - this->ctx = create_offscreen_context(width, height); - if ( this->ctx == NULL ) throw -1; - initializeGL(); - GLView::resizeGL(width, height); + object_rot << 35, 0, 25; + this->ctx = create_offscreen_context(width, height); + if ( this->ctx == NULL ) throw -1; + initializeGL(); + GLView::resizeGL(width, height); } OffscreenView::~OffscreenView() { - teardown_offscreen_context(this->ctx); + teardown_offscreen_context(this->ctx); +} + +#ifdef ENABLE_OPENCSG +void OffscreenView::display_opencsg_warning() +{ + fprintf(stderr, "OpenSCAD recommended OpenGL version is 2.0. \n"); } void OffscreenView::enable_opencsg_shaders() { -#ifdef ENABLE_OPENCSG - const char *openscad_disable_gl20_env = getenv("OPENSCAD_DISABLE_GL20"); - if (openscad_disable_gl20_env && !strcmp(openscad_disable_gl20_env, "0")) { - openscad_disable_gl20_env = NULL; - } + const char *openscad_disable_gl20_env = getenv("OPENSCAD_DISABLE_GL20"); + if (openscad_disable_gl20_env && !strcmp(openscad_disable_gl20_env, "0")) { + openscad_disable_gl20_env = NULL; + } // All OpenGL 2 contexts are OpenCSG capable if (GLEW_VERSION_2_0) { @@ -58,7 +63,7 @@ void OffscreenView::enable_opencsg_shaders() } if (!GLEW_VERSION_2_0 || !this->is_opencsg_capable) { - fprintf(stderr, "OpenSCAD recommended OpenGL version is 2.0. \n"); + display_opencsg_warning(); } if (opencsg_support && this->has_shaders) { @@ -84,201 +89,202 @@ void OffscreenView::enable_opencsg_shaders() tr shading */ - const char *vs_source = - "uniform float xscale, yscale;\n" - "attribute vec3 pos_b, pos_c;\n" - "attribute vec3 trig, mask;\n" - "varying vec3 tp, tr;\n" - "varying float shading;\n" - "void main() {\n" - " vec4 p0 = gl_ModelViewProjectionMatrix * gl_Vertex;\n" - " vec4 p1 = gl_ModelViewProjectionMatrix * vec4(pos_b, 1.0);\n" - " vec4 p2 = gl_ModelViewProjectionMatrix * vec4(pos_c, 1.0);\n" - " float a = distance(vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" - " float b = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w));\n" - " float c = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" - " float s = (a + b + c) / 2.0;\n" - " float A = sqrt(s*(s-a)*(s-b)*(s-c));\n" - " float ha = 2.0*A/a;\n" - " gl_Position = p0;\n" - " tp = mask * ha;\n" - " tr = trig;\n" - " vec3 normal, lightDir;\n" - " normal = normalize(gl_NormalMatrix * gl_Normal);\n" - " lightDir = normalize(vec3(gl_LightSource[0].position));\n" - " shading = abs(dot(normal, lightDir));\n" - "}\n"; + const char *vs_source = + "uniform float xscale, yscale;\n" + "attribute vec3 pos_b, pos_c;\n" + "attribute vec3 trig, mask;\n" + "varying vec3 tp, tr;\n" + "varying float shading;\n" + "void main() {\n" + " vec4 p0 = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + " vec4 p1 = gl_ModelViewProjectionMatrix * vec4(pos_b, 1.0);\n" + " vec4 p2 = gl_ModelViewProjectionMatrix * vec4(pos_c, 1.0);\n" + " float a = distance(vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" + " float b = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p1.x/p1.w, yscale*p1.y/p1.w));\n" + " float c = distance(vec2(xscale*p0.x/p0.w, yscale*p0.y/p0.w), vec2(xscale*p2.x/p2.w, yscale*p2.y/p2.w));\n" + " float s = (a + b + c) / 2.0;\n" + " float A = sqrt(s*(s-a)*(s-b)*(s-c));\n" + " float ha = 2.0*A/a;\n" + " gl_Position = p0;\n" + " tp = mask * ha;\n" + " tr = trig;\n" + " vec3 normal, lightDir;\n" + " normal = normalize(gl_NormalMatrix * gl_Normal);\n" + " lightDir = normalize(vec3(gl_LightSource[0].position));\n" + " shading = abs(dot(normal, lightDir));\n" + "}\n"; /* Inputs: tp && tr - if any components of tp < tr, use color2 (edge color) shading - multiplied by color1. color2 is is without lighting - */ - const char *fs_source = - "uniform vec4 color1, color2;\n" - "varying vec3 tp, tr, tmp;\n" - "varying float shading;\n" - "void main() {\n" - " gl_FragColor = vec4(color1.r * shading, color1.g * shading, color1.b * shading, color1.a);\n" - " if (tp.x < tr.x || tp.y < tr.y || tp.z < tr.z)\n" - " gl_FragColor = color2;\n" - "}\n"; - - GLuint vs = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vs, 1, (const GLchar**)&vs_source, NULL); - glCompileShader(vs); - - GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fs, 1, (const GLchar**)&fs_source, NULL); - glCompileShader(fs); - - 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, "trig"); - shaderinfo[4] = glGetAttribLocation(edgeshader_prog, "pos_b"); - shaderinfo[5] = glGetAttribLocation(edgeshader_prog, "pos_c"); - shaderinfo[6] = glGetAttribLocation(edgeshader_prog, "mask"); - shaderinfo[7] = glGetUniformLocation(edgeshader_prog, "xscale"); - shaderinfo[8] = glGetUniformLocation(edgeshader_prog, "yscale"); - - 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) { - int loglen; - char logbuffer[1000]; - glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); - fprintf(stderr, "OpenGL Program Linker Error:\n%.*s", loglen, logbuffer); - } else { - int loglen; - char logbuffer[1000]; - glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); - if (loglen > 0) { - fprintf(stderr, "OpenGL Program Link OK:\n%.*s", loglen, logbuffer); - } - glValidateProgram(edgeshader_prog); - glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); - if (loglen > 0) { - fprintf(stderr, "OpenGL Program Validation results:\n%.*s", loglen, logbuffer); - } - } - } -#endif + */ + const char *fs_source = + "uniform vec4 color1, color2;\n" + "varying vec3 tp, tr, tmp;\n" + "varying float shading;\n" + "void main() {\n" + " gl_FragColor = vec4(color1.r * shading, color1.g * shading, color1.b * shading, color1.a);\n" + " if (tp.x < tr.x || tp.y < tr.y || tp.z < tr.z)\n" + " gl_FragColor = color2;\n" + "}\n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, (const GLchar**)&vs_source, NULL); + glCompileShader(vs); + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, (const GLchar**)&fs_source, NULL); + glCompileShader(fs); + + 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, "trig"); + shaderinfo[4] = glGetAttribLocation(edgeshader_prog, "pos_b"); + shaderinfo[5] = glGetAttribLocation(edgeshader_prog, "pos_c"); + shaderinfo[6] = glGetAttribLocation(edgeshader_prog, "mask"); + shaderinfo[7] = glGetUniformLocation(edgeshader_prog, "xscale"); + shaderinfo[8] = glGetUniformLocation(edgeshader_prog, "yscale"); + + 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) { + int loglen; + char logbuffer[1000]; + glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); + fprintf(stderr, "OpenGL Program Linker Error:\n%.*s", loglen, logbuffer); + } else { + int loglen; + char logbuffer[1000]; + glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); + if (loglen > 0) { + fprintf(stderr, "OpenGL Program Link OK:\n%.*s", loglen, logbuffer); + } + glValidateProgram(edgeshader_prog); + glGetProgramInfoLog(edgeshader_prog, sizeof(logbuffer), &loglen, logbuffer); + if (loglen > 0) { + fprintf(stderr, "OpenGL Program Validation results:\n%.*s", loglen, logbuffer); + } + } + } } +#endif void OffscreenView::initializeGL() { - glEnable(GL_DEPTH_TEST); - glDepthRange(-FAR_FAR_AWAY, +FAR_FAR_AWAY); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - 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); - - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - - + glEnable(GL_DEPTH_TEST); + glDepthRange(-FAR_FAR_AWAY, +FAR_FAR_AWAY); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + 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); + + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + glEnable(GL_COLOR_MATERIAL); +#ifdef ENABLE_OPENCSG + enable_opencsg_shaders(); +#endif } void OffscreenView::paintGL() { - glEnable(GL_LIGHTING); + glEnable(GL_LIGHTING); - if (orthomode) setupOrtho(); - else setupPerspective(); + if (orthomode) setupOrtho(); + else setupPerspective(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); - glClearColor(1.0f, 1.0f, 0.92f, 1.0f); + glClearColor(1.0f, 1.0f, 0.92f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - gluLookAt(this->camera_eye[0], this->camera_eye[1], this->camera_eye[2], - this->camera_center[0], this->camera_center[1], this->camera_center[2], - 0.0, 0.0, 1.0); + gluLookAt(this->camera_eye[0], this->camera_eye[1], this->camera_eye[2], + this->camera_center[0], this->camera_center[1], this->camera_center[2], + 0.0, 0.0, 1.0); - // glRotated(object_rot[0], 1.0, 0.0, 0.0); - // glRotated(object_rot[1], 0.0, 1.0, 0.0); - // glRotated(object_rot[2], 0.0, 0.0, 1.0); + // glRotated(object_rot[0], 1.0, 0.0, 0.0); + // glRotated(object_rot[1], 0.0, 1.0, 0.0); + // glRotated(object_rot[2], 0.0, 0.0, 1.0); - // Large gray axis cross inline with the model + // Large gray axis cross inline with the model // FIXME: This is always gray - adjust color to keep contrast with background - if (showaxes) - { - glLineWidth(1); - glColor3d(0.5, 0.5, 0.5); - glBegin(GL_LINES); - double l = 3*(this->camera_center - this->camera_eye).norm(); - glVertex3d(-l, 0, 0); - glVertex3d(+l, 0, 0); - glVertex3d(0, -l, 0); - glVertex3d(0, +l, 0); - glVertex3d(0, 0, -l); - glVertex3d(0, 0, +l); - glEnd(); - } - - glDepthFunc(GL_LESS); - glCullFace(GL_BACK); - glDisable(GL_CULL_FACE); - - glLineWidth(2); - glColor3d(1.0, 0.0, 0.0); - - if (this->renderer) { - this->renderer->draw(showfaces, showedges); - } + if (showaxes) + { + glLineWidth(1); + glColor3d(0.5, 0.5, 0.5); + glBegin(GL_LINES); + double l = 3*(this->camera_center - this->camera_eye).norm(); + glVertex3d(-l, 0, 0); + glVertex3d(+l, 0, 0); + glVertex3d(0, -l, 0); + glVertex3d(0, +l, 0); + glVertex3d(0, 0, -l); + glVertex3d(0, 0, +l); + glEnd(); + } + + glDepthFunc(GL_LESS); + glCullFace(GL_BACK); + glDisable(GL_CULL_FACE); + + glLineWidth(2); + glColor3d(1.0, 0.0, 0.0); + + if (this->renderer) { + this->renderer->draw(showfaces, showedges); + } } bool OffscreenView::save(const char *filename) { - return save_framebuffer(this->ctx, filename); + return save_framebuffer(this->ctx, filename); } bool OffscreenView::save(std::ostream &output) { - return save_framebuffer(this->ctx, output); + return save_framebuffer(this->ctx, output); } std::string OffscreenView::getRendererInfo() const { - std::stringstream out; + std::stringstream out; - out << glew_dump() - << offscreen_context_getinfo(this->ctx); + out << glew_dump() + << offscreen_context_getinfo(this->ctx); - return out.str(); + return out.str(); } /* void OffscreenView::setCamera(const Eigen::Vector3d &pos, const Eigen::Vector3d ¢er) { - this->camera_eye = pos; - this->camera_center = center; + this->camera_eye = pos; + this->camera_center = center; } */ |