object.cc (4366B)
1 /* Copyright 2013 Brian Swetland <swetland@frotz.net> 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "app.h" 17 #include "matrix.h" 18 #include "shared.h" 19 #include "util.h" 20 21 #include "texturefont.h" 22 23 #include "Model.h" 24 #include "Effect.h" 25 #include "Renderable.h" 26 27 class TestApp : public App { 28 public: 29 TestApp(); 30 int init(void); 31 void render(void); 32 void onKeyUp(unsigned code); 33 private: 34 float r; 35 36 Model *m; 37 Effect *e; 38 39 Renderable *grid; 40 Effect *grid_effect; 41 42 UniformBuffer obj, mat, scn; 43 mat4 proj; 44 45 TextureFont *font; 46 Text *text; 47 48 Renderable *fullscreen; 49 Effect *copy; 50 51 Effect *vblur; 52 Effect *hblur; 53 FrameBuffer fb0; 54 FrameBuffer fb1; 55 56 int postproc; 57 }; 58 59 TestApp::TestApp() : App(), r(0.0) { } 60 61 int TestApp::init(void) { 62 /* resources for scene */ 63 if (!(m = Model::load("unitcubeoid"))) 64 return error("cannot load cube object"); 65 66 if (!(e = Effect::load("simple+SPECULAR"))) 67 return error("could not load simple effect"); 68 69 proj.setPerspective(D2R(90.0), width / (float) height, 0.1f, 250.0f); 70 71 if (!(font = TextureFont::load("orbitron-bold-72"))) 72 return error("cannot load font"); 73 74 text = Text::create(font); 75 76 /* resources for post-processing */ 77 hblur = Effect::load("rectangle+HBLUR"); 78 vblur = Effect::load("rectangle+VBLUR"); 79 copy = Effect::load("rectangle+COPY+SKIPLINES"); 80 81 fb0.init(width, height); 82 fb1.init(width, height); 83 84 fullscreen = Renderable::createFullscreenQuad(); 85 grid = Renderable::createXZgrid(10.0, 20); 86 grid_effect = Effect::load("flat"); 87 88 postproc = 0; 89 return 0; 90 } 91 92 void TestApp::onKeyUp(unsigned code) { 93 if (code == SDL_SCANCODE_SPACE) 94 postproc = !postproc; 95 } 96 97 void TestApp::render(void) { 98 struct ubScene scene; 99 struct ubObject object; 100 struct ubMaterial material; 101 mat4 model, view, tmp; 102 103 /* if we're applying post-processing, we render to an offscreen buffer */ 104 if (postproc) 105 fb0.use(); 106 107 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 108 glEnable(GL_DEPTH_TEST); 109 110 r += 1.0; 111 if (r > 360.0) r = 0.0; 112 113 view.camera(vec3(0, 1.0, 3.0), vec3(0, 1.0, 0.0), vec3(0, 1, 0)); 114 115 model.identity(); 116 object.mvp = model * view * proj; 117 object.mv = model * view; 118 obj.load(&object, sizeof(object)); 119 obj.use(U_OBJECT); 120 grid_effect->apply(); 121 grid->render(); 122 123 scene.Ortho.setOrtho(0, width, height, 0, -1.0, 1.0); 124 scene.LightColor.set(1.0, 1.0, 1.0); 125 scene.LightPosition = view * vec4(0, 1, 0, 0); 126 scene.LightPosition.w = 0; 127 scn.load(&scene, sizeof(scene)); 128 scn.use(U_SCENE); 129 130 material.Ambient.set(0.325,0.325,0.325,1.0); 131 material.Diffuse.set(1.0,1.0,1.0,1.0); 132 material.Specular.set(0, 0, 0, 0); 133 material.Shininess = 50.0f; 134 material.Color.set(1, 0, 0, 1); 135 mat.load(&material, sizeof(material)); 136 mat.use(U_MATERIAL); 137 138 model.identity().rotateY(D2R(r)).translate(0, 0.5, 0.0); 139 object.mvp = model * view * proj; 140 object.mv = model * view; 141 obj.load(&object, sizeof(object)); 142 e->apply(); 143 m->render(); 144 145 model.identity().translate(-1.5, 0.5, 0.0); 146 object.mvp = model * view * proj; 147 object.mv = model * view; 148 obj.load(&object, sizeof(object)); 149 e->apply(); 150 m->render(); 151 152 material.Specular.set(1,1,1,1); 153 mat.load(&material, sizeof(material)); 154 model.identity().translate(1.5, 0.5, 0.0); 155 object.mvp = model * view * proj; 156 object.mv = model * view; 157 obj.load(&object, sizeof(object)); 158 e->apply(); 159 m->render(); 160 161 text->clear(); 162 text->puts(100, 100, "Hello World!"); 163 text->render(); 164 165 if (postproc) { 166 glDisable(GL_DEPTH_TEST); 167 168 fb1.use(); 169 glClear(GL_COLOR_BUFFER_BIT); 170 vblur->apply(); 171 fb0.useTexture(0); 172 fullscreen->render(); 173 174 fb0.use(); 175 glClear(GL_COLOR_BUFFER_BIT); 176 hblur->apply(); 177 fb1.useTexture(0); 178 fullscreen->render(); 179 180 glBindFramebuffer(GL_FRAMEBUFFER, 0); 181 glViewport(0, 0, width, height); 182 glClear(GL_COLOR_BUFFER_BIT); 183 copy->apply(); 184 fb0.useTexture(0); 185 fullscreen->render(); 186 } 187 } 188 189 App *createApp(void) { 190 return new TestApp(); 191 }