Skip to content

Commit

Permalink
Placed all of the shader code into a conditional define to allow diff…
Browse files Browse the repository at this point in the history
…erent shaders in mac and other OSs
  • Loading branch information
RussellGarwood committed Feb 7, 2020
1 parent 3a20870 commit e292e8f
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 8 deletions.
31 changes: 31 additions & 0 deletions SPIERSview/resources/lightingFragmentShaderTextured_mac.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#version 330

uniform float ambientReflection;
uniform float diffuseReflection;
uniform float specularReflection;
uniform float shininess;
uniform float alpha;

in vec3 varyingNormal;
in vec3 varyingLightDirection;
in vec3 varyingViewerDirection;
in vec4 varyingColour;

out vec4 fragColor;

void main(void)
{
vec4 specularColor=varyingColour;
vec4 diffuseColor=varyingColour/1.5;
vec4 ambientColor=varyingColour/4.0;

vec3 normal = normalize(varyingNormal);
if (!gl_FrontFacing) normal=-normal;
vec3 lightDirection = normalize(varyingLightDirection);
vec3 viewerDirection = normalize(varyingViewerDirection);
vec4 ambientIllumination = ambientReflection * ambientColor;
vec4 diffuseIllumination = diffuseReflection * max(0.0, dot(lightDirection, normal)) * diffuseColor;
vec4 specularIllumination = specularReflection * pow(max(0.0, dot(-reflect(lightDirection, normal), viewerDirection)), shininess) * specularColor;
fragColor=ambientIllumination + diffuseIllumination + specularIllumination;
fragColor[3]=alpha;
}
31 changes: 31 additions & 0 deletions SPIERSview/resources/lightingFragmentShader_mac.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#version 130

uniform vec4 ambientColor;
uniform vec4 diffuseColor;
uniform vec4 specularColor;
uniform float ambientReflection;
uniform float diffuseReflection;
uniform float specularReflection;
uniform float shininess;
uniform float alpha;

in vec3 varyingNormal;
in vec3 varyingLightDirection;
in vec3 varyingViewerDirection;

out vec4 fragColor;

void main(void)
{

vec3 normal = normalize(varyingNormal);
if (!gl_FrontFacing) normal=-normal;

vec3 lightDirection = normalize(varyingLightDirection);
vec3 viewerDirection = normalize(varyingViewerDirection);
vec4 ambientIllumination = ambientReflection * ambientColor;
vec4 diffuseIllumination = diffuseReflection * max(0.0, dot(lightDirection, normal)) * diffuseColor;
vec4 specularIllumination = specularReflection * pow(max(0.0, dot(-reflect(lightDirection, normal), viewerDirection)), shininess) * specularColor;
fragColor=ambientIllumination + diffuseIllumination + specularIllumination;
fragColor[3]=alpha;
}
26 changes: 26 additions & 0 deletions SPIERSview/resources/lightingVertexShaderTextured_mac.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 130

uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;
uniform vec3 lightPosition;

in vec4 vertex;
in vec3 normal;
in vec4 colour;

out vec3 varyingNormal;
out vec3 varyingLightDirection;
out vec3 varyingViewerDirection;
out vec4 varyingColour;

void main(void)
{
varyingColour=colour;
vec4 eyeVertex = mvMatrix * vertex;
eyeVertex /= eyeVertex.w;
varyingNormal = normalMatrix * normal;
varyingLightDirection = lightPosition - eyeVertex.xyz;
varyingViewerDirection = -eyeVertex.xyz;
gl_Position = mvpMatrix * vertex;
}
25 changes: 25 additions & 0 deletions SPIERSview/resources/lightingVertexShader_mac.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#version 130

uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;
uniform vec3 lightPosition;

in vec4 vertex;
in vec3 normal;

out vec3 varyingNormal;
out vec3 varyingLightDirection;
out vec3 varyingViewerDirection;

void main(void)
{


vec4 eyeVertex = mvMatrix * vertex;
eyeVertex /= eyeVertex.w;
varyingNormal = normalMatrix * normal;
varyingLightDirection = lightPosition - eyeVertex.xyz;
varyingViewerDirection = -eyeVertex.xyz;
gl_Position = mvpMatrix * vertex;
}
22 changes: 20 additions & 2 deletions SPIERSview/src/gl3widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ GlWidget::GlWidget(QWidget *parent)

// Set GL surface format
QSurfaceFormat surfaceFormat;

#ifdef __APPLE__
surfaceFormat.setMajorVersion(3);
surfaceFormat.setMinorVersion(2);
#endif

#ifndef __APPLE__
surfaceFormat.setMajorVersion(GL_MAJOR);
surfaceFormat.setMinorVersion(GL_MINOR);
#endif

surfaceFormat.setRenderableType(QSurfaceFormat::OpenGL);
setFormat(surfaceFormat);

Expand Down Expand Up @@ -83,12 +92,21 @@ void GlWidget::initializeGL()
glfunctions->glEnable(GL_BLEND);
glfunctions->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

#ifdef __APPLE__
lightingShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/lightingVertexShader_mac.vsh");
lightingShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/lightingFragmentShader_mac.fsh");
lightingShaderProgramForColour.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/lightingVertexShaderTextured_mac.vsh");
lightingShaderProgramForColour.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/lightingFragmentShaderTextured_mac.fsh");
#endif

#ifndef __APPLE__
lightingShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/lightingVertexShader.vsh");
lightingShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/lightingFragmentShader.fsh");
lightingShaderProgram.link();

lightingShaderProgramForColour.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/lightingVertexShaderTextured.vsh");
lightingShaderProgramForColour.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/lightingFragmentShaderTextured.fsh");
#endif

lightingShaderProgram.link();
lightingShaderProgramForColour.link();

// Initalize the GL Scale grid
Expand Down
6 changes: 0 additions & 6 deletions SPIERSview/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ MainWindow::MainWindow(QWidget *parent)
StereoGroup->addAction(ui->actionOrthographic_View);
ui->actionNo_Stereo->setChecked(true);

QGLFormat f;
f.setVersion(GL_MAJOR, GL_MINOR);
f.setSampleBuffers(true);
f.setSampleBuffers(true);
f.setSamples(1);

gl3widget = new GlWidget(ui->frameVTK);
gllayout = new QHBoxLayout;
gllayout->addWidget(gl3widget);
Expand Down
4 changes: 4 additions & 0 deletions SPIERSview/view.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
<file alias="lightingFragmentShaderTextured.fsh">resources/lightingFragmentShaderTextured.fsh</file>
<file alias="lightingVertexShaderTextured.vsh">resources/lightingVertexShaderTextured.vsh</file>
<file>resources/SPIERSviewIcon.svg</file>
<file alias="lightingVertexShaderTextured_mac.vsh">resources/lightingVertexShaderTextured_mac.vsh</file>
<file alias="lightingVertexShader_mac.vsh">resources/lightingVertexShader_mac.vsh</file>
<file alias="lightingFragmentShaderTextured_mac.fsh">resources/lightingFragmentShaderTextured_mac.fsh</file>
<file alias="lightingFragmentShader_mac.fsh">resources/lightingFragmentShader_mac.fsh</file>
</qresource>
</RCC>

0 comments on commit e292e8f

Please sign in to comment.