Skip to content

Commit

Permalink
added shaders, but not on linux because of the bad cs department grap…
Browse files Browse the repository at this point in the history
…hics drivers
  • Loading branch information
Evan Wallace committed May 8, 2011
1 parent 1a275f0 commit e825e8a
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 7 deletions.
12 changes: 11 additions & 1 deletion cs224final.pro
Expand Up @@ -11,6 +11,12 @@ release {
}

# DEFINES += ANIM_DEBUG
DEFINES += USE_FLOAT_RTT

# floating-point render targets are horribly broken on the cs department machines
linux {
DEFINES -= USE_FLOAT_RTT
}

HEADERS += \
util/vector.h \
Expand Down Expand Up @@ -71,7 +77,11 @@ RESOURCES += \
resources.qrc

OTHER_FILES += \
README
README \
shaders/normaldepth.vert \
shaders/normaldepth.frag \
shaders/finalcomposite.vert \
shaders/finalcomposite.frag



Expand Down
123 changes: 122 additions & 1 deletion mainwindow.ui
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<height>611</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -106,6 +106,9 @@
<string>Brush</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QRadioButton" name="brushAddOrSubtract">
<property name="text">
Expand All @@ -120,6 +123,22 @@
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>10</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="brushRadiusLabel">
<property name="text">
Expand Down Expand Up @@ -160,6 +179,43 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="materials">
<property name="title">
<string>Material</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QRadioButton" name="materialCurvature">
<property name="text">
<string>Curvature</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="materialMapleCandy">
<property name="text">
<string>Maple Candy</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="materialMetal">
<property name="text">
<string>Metal</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="materialRedWax">
<property name="text">
<string>Red Wax</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="emptySpace" native="true">
<property name="sizePolicy">
Expand Down Expand Up @@ -1052,6 +1108,70 @@
</hint>
</hints>
</connection>
<connection>
<sender>materialCurvature</sender>
<signal>toggled(bool)</signal>
<receiver>MainWindow</receiver>
<slot>materialChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>108</x>
<y>494</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>305</y>
</hint>
</hints>
</connection>
<connection>
<sender>materialMapleCandy</sender>
<signal>toggled(bool)</signal>
<receiver>MainWindow</receiver>
<slot>materialChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>108</x>
<y>513</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>305</y>
</hint>
</hints>
</connection>
<connection>
<sender>materialMetal</sender>
<signal>toggled(bool)</signal>
<receiver>MainWindow</receiver>
<slot>materialChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>108</x>
<y>532</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>305</y>
</hint>
</hints>
</connection>
<connection>
<sender>materialRedWax</sender>
<signal>toggled(bool)</signal>
<receiver>MainWindow</receiver>
<slot>materialChanged()</slot>
<hints>
<hint type="sourcelabel">
<x>108</x>
<y>551</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>305</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>modeChanged()</slot>
Expand All @@ -1076,5 +1196,6 @@
<slot>brushWeightChanged(int)</slot>
<slot>brushModeAddOrSubtract()</slot>
<slot>brushModeSmooth()</slot>
<slot>materialChanged()</slot>
</slots>
</ui>
4 changes: 4 additions & 0 deletions resources.qrc
Expand Up @@ -9,5 +9,9 @@
<file>img/edit-delete.png</file>
<file>img/image-missing.png</file>
<file>img/process-stop.png</file>
<file>shaders/finalcomposite.frag</file>
<file>shaders/finalcomposite.vert</file>
<file>shaders/normaldepth.frag</file>
<file>shaders/normaldepth.vert</file>
</qresource>
</RCC>
67 changes: 67 additions & 0 deletions shaders/finalcomposite.frag
@@ -0,0 +1,67 @@
uniform sampler2D texture;
uniform vec2 windowSize;

void main()
{
// recover normal and depth, reject if background
vec2 coord = gl_FragCoord.xy / windowSize;
vec4 normalDepth = texture2D(texture, coord);
vec3 normal = normalDepth.xyz;
float depth = normalDepth.w;
if (depth == 0.0)
{
// color background differently
gl_FragColor = vec4(0.5 * (1.0 - length(coord - 0.5)));
return;
}

// change material in creases
vec2 dx = vec2(1.0 / windowSize.x, 0.0);
vec2 dy = vec2(0.0, 1.0 / windowSize.y);
vec3 xneg = texture2D(texture, coord - dx).xyz;
vec3 yneg = texture2D(texture, coord - dy).xyz;
vec3 xpos = texture2D(texture, coord + dx).xyz;
vec3 ypos = texture2D(texture, coord + dy).xyz;
float curvature = (cross(xneg, xpos).y - cross(yneg, ypos).x) * 4.0 / depth;

#if MATERIAL == 0
// direct curvature visualization
vec3 light = vec3(0.0);
vec3 ambient = vec3(curvature + 0.5);
vec3 diffuse = vec3(0.0);
vec3 specular = vec3(0.0);
float shininess = 0.0;
#elif MATERIAL == 1
// maple candy shader
curvature = clamp(0.5 + curvature * 2.0, 0.0, 1.0);
vec3 light = normalize(vec3(0.0, 1.0, 10.0));
vec3 ambient = vec3(0.1, 0.05, 0.0);
vec3 diffuse = mix(vec3(0.5), vec3(0.5, 0.3, 0.1), curvature);
vec3 specular = vec3(0.0);
float shininess = 0.0;
#elif MATERIAL == 2
// metal shader
float corrosion = clamp(-curvature * 3.0, 0.0, 1.0);
float shine = clamp(curvature * 5.0, 0.0, 1.0);
vec3 light = normalize(vec3(0.0, 1.0, 10.0));
vec3 ambient = vec3(0.15, 0.1, 0.1);
vec3 diffuse = mix(mix(vec3(0.3, 0.25, 0.2), vec3(0.45, 0.5, 0.5), corrosion), vec3(0.5, 0.4, 0.3), shine) - ambient;
vec3 specular = mix(vec3(0.0), vec3(1.0) - ambient - diffuse, shine);
float shininess = 128.0;
#elif MATERIAL == 3
// red wax shader
float dirt = clamp(0.25 - curvature * 4.0, 0.0, 1.0);
vec3 light = normalize(vec3(0.0, 1.0, 10.0));
vec3 ambient = vec3(0.1, 0.05, 0.0);
vec3 diffuse = mix(vec3(0.4, 0.15, 0.1), vec3(0.4, 0.3, 0.3), dirt) - ambient;
vec3 specular = mix(vec3(0.15) - ambient, vec3(0.0), dirt);
float shininess = 32.0;
#endif

// calculate final color
float cosAngle = dot(normal, light);
gl_FragColor.rgb =
ambient +
diffuse * max(0.0, cosAngle) +
specular * pow(max(0.0, cosAngle), shininess);
}
4 changes: 4 additions & 0 deletions shaders/finalcomposite.vert
@@ -0,0 +1,4 @@
void main()
{
gl_Position = ftransform();
}
7 changes: 7 additions & 0 deletions shaders/normaldepth.frag
@@ -0,0 +1,7 @@
varying vec3 pos;
varying vec3 normal;

void main()
{
gl_FragColor = vec4(normalize(normal).xyz, length(pos));
}
9 changes: 9 additions & 0 deletions shaders/normaldepth.vert
@@ -0,0 +1,9 @@
varying vec3 pos;
varying vec3 normal;

void main()
{
pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}
16 changes: 16 additions & 0 deletions ui/mainwindow.cpp
Expand Up @@ -42,6 +42,13 @@ MainWindow::MainWindow(QWidget *parent) :
ui->brushRadius->setValue(50);
ui->brushWeight->setValue(100);

#ifdef USE_FLOAT_RTT
ui->materialCurvature->setChecked(true);
#else
// Hide the material picker if we don't have shaders
ui->materials->setVisible(false);
#endif

setWindowState(windowState() | Qt::WindowMaximized);
fileNew();
}
Expand All @@ -59,6 +66,7 @@ void MainWindow::modeChanged()
else if (ui->actionSculptMesh->isChecked()) ui->view->setMode(MODE_SCULPT_MESH);

ui->brushSettings->setEnabled(ui->actionSculptMesh->isChecked());
ui->materials->setEnabled(ui->actionSculptMesh->isChecked());
}

void MainWindow::cameraChanged()
Expand Down Expand Up @@ -295,6 +303,14 @@ void MainWindow::brushWeightChanged(int value)
ui->brushWeightLabel->setText(QString("Strength: %1").arg(brushWeight));
}

void MainWindow::materialChanged()
{
if (ui->materialCurvature->isChecked()) ui->view->setMaterial(MATERIAL_CURVATURE);
else if (ui->materialMapleCandy->isChecked()) ui->view->setMaterial(MATERIAL_MAPLE_CANDY);
else if (ui->materialMetal->isChecked()) ui->view->setMaterial(MATERIAL_METAL);
else if (ui->materialRedWax->isChecked()) ui->view->setMaterial(MATERIAL_RED_WAX);
}

void MainWindow::updateMode()
{
Mesh &mesh = ui->view->getDocument().mesh;
Expand Down
1 change: 1 addition & 0 deletions ui/mainwindow.h
Expand Up @@ -39,6 +39,7 @@ public slots:
void brushModeSmooth();
void brushRadiusChanged(int value);
void brushWeightChanged(int value);
void materialChanged();

private:
Ui::MainWindow *ui;
Expand Down
4 changes: 4 additions & 0 deletions ui/meshsculpter.cpp
Expand Up @@ -203,6 +203,7 @@ bool MeshSculpterTool::mousePressed(QMouseEvent *event)
if (accel->hitTest(tracer.getEye(), tracer.getRayForPixel(event->x(), event->y()), result))
{
stampBrush(result.hit, result.normal);
if (view->mirrorChanges) stampBrush(result.hit * Mesh::symmetryFlip, result.normal * Mesh::symmetryFlip);
return true;
}

Expand All @@ -217,7 +218,10 @@ void MeshSculpterTool::mouseDragged(QMouseEvent *event)
Raytracer tracer;
HitTest result;
if (accel->hitTest(tracer.getEye(), tracer.getRayForPixel(event->x(), event->y()), result))
{
stampBrush(result.hit, result.normal);
if (view->mirrorChanges) stampBrush(result.hit * Mesh::symmetryFlip, result.normal * Mesh::symmetryFlip);
}
}

void MeshSculpterTool::mouseReleased(QMouseEvent *)
Expand Down

0 comments on commit e825e8a

Please sign in to comment.