summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordon bright <hugh.m.bright@gmail.com>2013-02-24 18:33:11 (GMT)
committerdon bright <hugh.m.bright@gmail.com>2013-02-24 18:33:11 (GMT)
commit76c5d5b55c66505af98ddbed70ca5e21719d3bb1 (patch)
tree3576a2c3cd9d7f5182f594d1d4d9491f6371c55e
parent707118ce9485f1c703bf6cc99bad3affc2a48c10 (diff)
merge enable_opencsg_shaders into GLView, remove duplicate code
from QGLView and OffscreenView
-rw-r--r--src/GLView.cc156
-rw-r--r--src/GLView.h7
-rw-r--r--src/OffscreenView.cc153
-rw-r--r--src/OffscreenView.h1
-rw-r--r--src/QGLView.cc152
-rw-r--r--src/QGLView.h3
6 files changed, 159 insertions, 313 deletions
diff --git a/src/GLView.cc b/src/GLView.cc
index 2f6d69f..0f918dc 100644
--- a/src/GLView.cc
+++ b/src/GLView.cc
@@ -1,7 +1,13 @@
#include "GLView.h"
#include "printutils.h"
-#include <iostream>
+#include "stdio.h"
+
+#ifdef _WIN32
+#include <GL/wglew.h>
+#elif !defined(__APPLE__)
+#include <GL/glxew.h>
+#endif
GLView::GLView()
{
@@ -93,6 +99,154 @@ void GLView::setupOrtho(bool offset)
-FAR_FAR_AWAY, +FAR_FAR_AWAY);
}
+#ifdef ENABLE_OPENCSG
+void GLView::enable_opencsg_shaders()
+{
+ 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) {
+ if (!openscad_disable_gl20_env) {
+ this->is_opencsg_capable = true;
+ this->has_shaders = true;
+ }
+ }
+
+ // If OpenGL < 2, check for extensions
+ else {
+ if (GLEW_ARB_framebuffer_object) this->is_opencsg_capable = true;
+ else if (GLEW_EXT_framebuffer_object && GLEW_EXT_packed_depth_stencil) {
+ this->is_opencsg_capable = true;
+ }
+#ifdef WIN32
+ else if (WGLEW_ARB_pbuffer && WGLEW_ARB_pixel_format) this->is_opencsg_capable = true;
+#elif !defined(__APPLE__)
+ else if (GLXEW_SGIX_pbuffer && GLXEW_SGIX_fbconfig) this->is_opencsg_capable = true;
+#endif
+ }
+
+ if (!GLEW_VERSION_2_0 || !this->is_opencsg_capable) {
+ display_opencsg_warning();
+ }
+
+ if (opencsg_support && this->has_shaders) {
+ /*
+ Uniforms:
+ 1 color1 - face color
+ 2 color2 - edge color
+ 7 xscale
+ 8 yscale
+
+ Attributes:
+ 3 trig
+ 4 pos_b
+ 5 pos_c
+ 6 mask
+
+ Other:
+ 9 width
+ 10 height
+
+ Outputs:
+ tp
+ 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";
+
+ /*
+ 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
+
/*
void initializeGL(); //
diff --git a/src/GLView.h b/src/GLView.h
index 041c4a5..aa5a677 100644
--- a/src/GLView.h
+++ b/src/GLView.h
@@ -3,10 +3,11 @@
/* GLView: A basic OpenGL rectangle for rendering images.
-Inherited by QGLview (for QT GUI) and OffscreenView.
+Inherited by QGLview (for Qt GUI) and OffscreenView (used in tests and
+for offscreen command-line rendering).
There are two different types of cameras. A 'gimbal' based camera set
-using position & euler-angles (object_trans/object_rot/distance) and a
+using translation & euler-angles (object_trans/object_rot/distance) and a
'plain' camera set using eye-position, 'look at' center point, and 'up'
*/
@@ -64,7 +65,7 @@ public:
GLint shaderinfo[11];
bool is_opencsg_capable;
bool has_shaders;
-// void enable_opencsg_shaders();
+ void enable_opencsg_shaders();
virtual void display_opencsg_warning() = 0;
bool opencsg_support;
int opencsg_id;
diff --git a/src/OffscreenView.cc b/src/OffscreenView.cc
index 1a5949a..0daf8a8 100644
--- a/src/OffscreenView.cc
+++ b/src/OffscreenView.cc
@@ -7,13 +7,6 @@
#include <cstdlib>
#include <sstream>
-#ifdef _WIN32
-#include <GL/wglew.h>
-#elif !defined(__APPLE__)
-#include <GL/glxew.h>
-#endif
-
-
OffscreenView::OffscreenView(size_t width, size_t height)
{
object_rot << 35, 0, 25;
@@ -33,152 +26,6 @@ void OffscreenView::display_opencsg_warning()
{
fprintf(stderr, "OpenSCAD recommended OpenGL version is 2.0. \n");
}
-
-void OffscreenView::enable_opencsg_shaders()
-{
- 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) {
- if (!openscad_disable_gl20_env) {
- this->is_opencsg_capable = true;
- this->has_shaders = true;
- }
- }
-
- // If OpenGL < 2, check for extensions
- else {
- if (GLEW_ARB_framebuffer_object) this->is_opencsg_capable = true;
- else if (GLEW_EXT_framebuffer_object && GLEW_EXT_packed_depth_stencil) {
- this->is_opencsg_capable = true;
- }
-#ifdef WIN32
- else if (WGLEW_ARB_pbuffer && WGLEW_ARB_pixel_format) this->is_opencsg_capable = true;
-#elif !defined(__APPLE__)
- else if (GLXEW_SGIX_pbuffer && GLXEW_SGIX_fbconfig) this->is_opencsg_capable = true;
-#endif
- }
-
- if (!GLEW_VERSION_2_0 || !this->is_opencsg_capable) {
- display_opencsg_warning();
- }
-
- if (opencsg_support && this->has_shaders) {
- /*
- Uniforms:
- 1 color1 - face color
- 2 color2 - edge color
- 7 xscale
- 8 yscale
-
- Attributes:
- 3 trig
- 4 pos_b
- 5 pos_c
- 6 mask
-
- Other:
- 9 width
- 10 height
-
- Outputs:
- tp
- 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";
-
- /*
- 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
void OffscreenView::initializeGL()
diff --git a/src/OffscreenView.h b/src/OffscreenView.h
index fd7c9bd..0175dee 100644
--- a/src/OffscreenView.h
+++ b/src/OffscreenView.h
@@ -25,7 +25,6 @@ public:
bool save(const char *filename);
std::string getRendererInfo() const;
#ifdef ENABLE_OPENCSG
- void enable_opencsg_shaders();
void display_opencsg_warning();
#endif
};
diff --git a/src/QGLView.cc b/src/QGLView.cc
index f1a0250..69c0580 100644
--- a/src/QGLView.cc
+++ b/src/QGLView.cc
@@ -51,12 +51,6 @@
# include <opencsg.h>
#endif
-#ifdef _WIN32
-#include <GL/wglew.h>
-#elif !defined(__APPLE__)
-#include <GL/glxew.h>
-#endif
-
QGLView::QGLView(QWidget *parent) : QGLWidget(parent)
{
init();
@@ -88,152 +82,6 @@ void QGLView::init()
#endif
}
-#ifdef ENABLE_OPENCSG
-void QGLView::enable_opencsg_shaders()
-{
- 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) {
- if (!openscad_disable_gl20_env) {
- this->is_opencsg_capable = true;
- this->has_shaders = true;
- }
- }
- // If OpenGL < 2, check for extensions
- else {
- if (GLEW_ARB_framebuffer_object) this->is_opencsg_capable = true;
- else if (GLEW_EXT_framebuffer_object && GLEW_EXT_packed_depth_stencil) {
- this->is_opencsg_capable = true;
- }
-#ifdef WIN32
- else if (WGLEW_ARB_pbuffer && WGLEW_ARB_pixel_format) this->is_opencsg_capable = true;
-#elif !defined(__APPLE__)
- else if (GLXEW_SGIX_pbuffer && GLXEW_SGIX_fbconfig) this->is_opencsg_capable = true;
-#endif
- }
-
- if (!GLEW_VERSION_2_0 || !this->is_opencsg_capable) {
- display_opencsg_warning();
- }
- if (opencsg_support && this->has_shaders) {
- /*
- Uniforms:
- 1 color1 - face color
- 2 color2 - edge color
- 7 xscale
- 8 yscale
-
- Attributes:
- 3 trig
- 4 pos_b
- 5 pos_c
- 6 mask
-
- Other:
- 9 width
- 10 height
-
- Outputs:
- tp
- 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";
-
- /*
- 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
-
void QGLView::initializeGL()
{
glEnable(GL_DEPTH_TEST);
diff --git a/src/QGLView.h b/src/QGLView.h
index da96b6a..8152c35 100644
--- a/src/QGLView.h
+++ b/src/QGLView.h
@@ -38,9 +38,6 @@ public:
void setOrthoMode(bool enabled) { this->orthomode = enabled; }
std::string getRendererInfo() const;
bool save(const char *filename);
-#ifdef ENABLE_OPENCSG
- void enable_opencsg_shaders();
-#endif
public:
QLabel *statusLabel;
contact: Jan Huwald // Impressum