Last active
March 6, 2016 21:41
-
-
Save Craigson/dfb3bacaa4b22d440716 to your computer and use it in GitHub Desktop.
Phong lighting experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "cinder/app/App.h" | |
| #include "cinder/app/RendererGl.h" | |
| #include "cinder/gl/gl.h" | |
| #include "cinder/CameraUi.h" | |
| #include "cinder/Utilities.h" | |
| using namespace ci; | |
| using namespace ci::app; | |
| using namespace std; | |
| class Day38App : public App { | |
| public: | |
| void setup() override; | |
| void mouseDown( MouseEvent event ) override; | |
| void update() override; | |
| void draw() override; | |
| void setupGlsl(); | |
| void saveGif(); | |
| CameraPersp mCam; | |
| CameraUi mCamUi; | |
| gl::GlslProgRef mGlsl; | |
| gl::BatchRef mBatch; | |
| gl::BatchRef mLight; | |
| vec3 lightColor; | |
| vec3 objectColor; | |
| vec4 lightPos; | |
| vec4 eyePos; | |
| float theta; | |
| }; | |
| void Day38App::setup() | |
| { | |
| mCam.lookAt(vec3(0,0,5), vec3(0)); | |
| mCam.setPerspective(45.f, getWindowAspectRatio(), 1.f, 1000.f); | |
| lightPos = vec4(0.f); | |
| theta = 0.0; | |
| setupGlsl(); | |
| mCamUi = CameraUi(&mCam, getWindow()); | |
| auto cube = geom::Sphere().subdivisions(128); | |
| mBatch = gl::Batch::create(cube, mGlsl); | |
| auto sphere = geom::Sphere(); | |
| auto scale = geom::Scale(0.1f,0.1f,0.1f); | |
| auto shader = gl::ShaderDef().color(); | |
| auto rotate = geom::Rotate(angleAxis(2.1f, vec3(1.0,0.,0.f))); | |
| mLight = gl::Batch::create(sphere >> scale >> rotate, gl::getStockShader(shader)); | |
| gl::enableDepthRead(); | |
| gl::enableDepthWrite(); | |
| } | |
| void Day38App::mouseDown( MouseEvent event ) | |
| { | |
| } | |
| void Day38App::update() | |
| { | |
| lightPos.x = 1.5f * cos(theta); | |
| lightPos.y = 1.5f * sin(theta); | |
| lightPos.z = 1.9f; | |
| theta += 0.00523; | |
| eyePos = vec4(mCam.getEyePoint(), 1.); | |
| mGlsl->uniform("uLightColor", vec3( 1.0f, 1.0f, 1.0f)); | |
| mGlsl->uniform("uObjectColor", vec3(1.0f, 0.5f, 0.31f)); | |
| mGlsl->uniform("uCam", vec3( gl::getModelView() * eyePos)); | |
| mGlsl->uniform("uLightPos", vec3( gl::getModelView() * lightPos)); //WE MULTIPLY BY getModelView BECAUSE ALL OUR LIGHTING CALCULATIONS ARE BEING DONE IN WORLD-SPACE | |
| cout << lightPos << endl; | |
| } | |
| void Day38App::draw() | |
| { | |
| gl::setMatrices(mCam); | |
| gl::clear( Color( .0, 0., 0. ) ); | |
| { | |
| gl::ScopedMatrices push; | |
| // gl::rotate(theta, vec3(1.,0.,0.)); | |
| mBatch->draw(); | |
| } | |
| { | |
| gl::ScopedMatrices push; | |
| gl::translate(lightPos.x, lightPos.y, lightPos.z); | |
| mLight->draw(); | |
| } | |
| //if (getElapsedFrames() > 240.f) saveGif(); | |
| } | |
| void Day38App::setupGlsl() | |
| { | |
| mGlsl = gl::GlslProg::create(loadAsset("phong.vert"), loadAsset("phong.frag")); | |
| } | |
| void Day38App::saveGif() | |
| { | |
| if (getElapsedFrames() % 2 == 0 && getElapsedFrames() < 361) writeImage( getDocumentsDirectory() / "Cinder" / "screenshots"/ "100days" / "37"/ ("37-0" + toString( getElapsedFrames() ) + ".png" ), copyWindowSurface() ); | |
| cout << "saving image" << endl; | |
| } | |
| CINDER_APP( Day38App, RendererGl(RendererGl::Options().msaa(16)), | |
| [&](App::Settings *settings){ | |
| settings->setWindowSize(500,500); | |
| settings->setHighDensityDisplayEnabled(); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #version 150 | |
| uniform vec3 uLightPos; | |
| uniform vec3 uCam; | |
| uniform vec3 uLightColor; | |
| uniform vec3 uObjectColor; | |
| in vec4 vPosition; | |
| in vec3 vNormal; | |
| out vec4 oColor; | |
| void main() | |
| { | |
| // Ambient | |
| float ambientStrength = 0.1f; | |
| vec3 ambient = ambientStrength * uLightColor; | |
| // Diffuse | |
| vec3 norm = normalize(vNormal); | |
| vec3 lightDir = normalize(uLightPos - vPosition.xyz); | |
| float diff = max(dot(norm, lightDir), 0.0); | |
| vec3 diffuse = diff * uLightColor; | |
| // Specular | |
| float specularStrength = 0.9f; | |
| vec3 viewDir = normalize(uCam - vPosition.xyz); | |
| vec3 reflectDir = reflect(-lightDir, norm); | |
| float spec = pow(max(dot(viewDir, reflectDir), 0.0), 128); | |
| vec3 specular = specularStrength * spec * uLightColor; | |
| vec3 result = (ambient + diffuse + specular) * uObjectColor; | |
| oColor = vec4(result, 1.0f); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #version 150 | |
| uniform mat4 ciModelViewProjection; | |
| uniform mat4 ciModelView; | |
| uniform mat3 ciNormalMatrix; | |
| in vec4 ciPosition; | |
| in vec3 ciNormal; | |
| out vec4 vPosition; | |
| out vec3 vNormal; | |
| void main(void) | |
| { | |
| vNormal = ciNormalMatrix * ciNormal; | |
| vPosition = ciModelView * ciPosition; //THIS GIVES WORLD-SPACE POSITION OF THE FRAGMENT | |
| gl_Position = ciModelViewProjection * ciPosition; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment