hello.cc (5646B)
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 <stdio.h> 17 #include <stdlib.h> 18 19 #include "app.h" 20 #include "matrix.h" 21 #include "util.h" 22 #include "textgrid.h" 23 #include "shared.h" 24 #include "Effect.h" 25 26 // idx, src, dst, count, offset, stride, divisor 27 static VertexAttrDesc layout[] = { 28 { 0, SRC_FLOAT, DST_FLOAT, 3, 0, 32, 0 }, 29 { 1, SRC_FLOAT, DST_FLOAT, 3, 12, 32, 0 }, 30 { 2, SRC_FLOAT, DST_FLOAT, 2, 24, 32, 0 }, 31 { 3, SRC_INT8, DST_NORMALIZED, 4, 0, 4, 1 }, 32 }; 33 34 #define SZ 32 35 #define SZh (SZ / 2) 36 #define SZe (SZ * SZ * SZ) 37 #define SZb (SZe * 4) 38 39 static char location[SZb]; 40 static int lcount = 0; 41 42 class TestApp : public App { 43 public: 44 TestApp(); 45 int init(void); 46 void render(void); 47 void release(void); 48 void build(void); 49 void onKeyUp(unsigned code); 50 void onResize(void); 51 52 private: 53 float t; 54 55 Effect *e; 56 57 IndexBuffer ibuf; 58 VertexBuffer vbuf; 59 VertexBuffer lbuf; 60 VertexAttributes attr; 61 62 UniformBuffer scn; 63 UniformBuffer mat; 64 UniformBuffer obj; 65 66 TextGrid text; 67 68 mat4 proj; 69 float zoom, rx, ry; 70 float nx,ny; 71 struct model *m; 72 }; 73 74 TestApp::TestApp() : App(), t(0.0f), zoom(0), rx(0), ry(0), nx(0), ny(0) { 75 } 76 77 void TestApp::release(void) { 78 } 79 80 void TestApp::build(void) { 81 float scale = SZh; 82 int x, y, z, n = 0; 83 for (z = -SZh; z < SZh; z++) { 84 for (x = -SZh; x < SZh; x++) { 85 for (y = -SZh; y < SZh; y++) { 86 float fx = x/scale * 0.50f + nx; 87 float fy = y/scale * 0.50f; 88 float fz = z/scale * 0.50f + ny; 89 #if 0 90 if (snoise(fx,fy,fz) > 0.1) 91 continue; 92 #else 93 float sn = 94 snoise(fx,fz) + 95 snoise(fx*2.0f,fz*2.0f) / 4.0f + 96 snoise(fx*4.0f,fz*4.0f) / 8.0f; 97 if (sn < y/scale) 98 continue; 99 #endif 100 location[n+0] = x; 101 location[n+1] = y; 102 location[n+2] = z; 103 location[n+3] = 1; 104 n += 4; 105 } 106 } 107 } 108 lcount = n / 4; 109 printx("Wrote %d locations\n", lcount); 110 111 lbuf.load(location, lcount * 4); 112 } 113 114 int TestApp::init(void) { 115 VertexBuffer *data[] = { 116 &vbuf, &vbuf, &vbuf, &lbuf, 117 }; 118 119 if (!(e = Effect::load("simple"))) 120 return -1; 121 if (!(m = load_wavefront_obj("unitcubeoid.obj"))) 122 return error("cannot load model"); 123 printx("Object Loaded. %d vertices, %d indices.\n", m->vcount, m->icount); 124 125 vbuf.load(m->vdata, 32 * m->vcount); 126 ibuf.load(m->idx, 2 * m->icount); 127 128 build(); 129 zoom = SZ; 130 131 attr.init(layout, data, sizeof(layout) / sizeof(layout[0])); 132 ibuf.use(); 133 134 if (text.init(16, 16, width/16, height/16)) 135 return -1; 136 137 onResize(); 138 139 return 0; 140 } 141 142 void TestApp::onResize(void) { 143 proj.setPerspective(D2R(50.0), width / (float) height, 0.1f, 250.0f); 144 text.resize(width/16, height/16); 145 } 146 147 static float rate = 90.0; 148 149 void TestApp::onKeyUp(unsigned code) { 150 switch (code) { 151 case SDL_SCANCODE_P: { 152 Effect *e2 = Effect::load("simple"); 153 if (e2) { 154 delete e; 155 e = e2; 156 } 157 break; 158 } 159 case SDL_SCANCODE_RETURN: 160 fullscreen(!isFullscreen()); 161 break; 162 } 163 } 164 165 void TestApp::render(void) { 166 int update = 0; 167 168 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 169 glEnable(GL_DEPTH_TEST); 170 171 if (mouseBTN & 1) { 172 float dx = ((float) mouseDX) / 400.0f; 173 float dy = ((float) mouseDY) / 400.0f; 174 ry += dx * rate; 175 rx += dy * rate; 176 if (rx < 0.0) rx += 360.0; 177 else if (rx > 360.0) rx -= 360.0; 178 if (ry < 0.0) ry += 360.0; 179 else if (ry > 360.0) ry -= 360.0; 180 } 181 if (mouseBTN & 2) { 182 float dy = ((float) mouseDY) / 400.0f; 183 zoom += dy * rate; 184 if (zoom < 5.0) zoom = 5.0; 185 if (zoom > 100.0) zoom = 100.0; 186 } 187 188 if (keydown(SDL_SCANCODE_A)) { nx -= 0.01; update = 1; } 189 if (keydown(SDL_SCANCODE_D)) { nx += 0.01; update = 1; } 190 if (keydown(SDL_SCANCODE_W)) { ny -= 0.01; update = 1; } 191 if (keydown(SDL_SCANCODE_S)) { ny += 0.01; update = 1; } 192 if (update) 193 build(); 194 195 196 struct ubScene scene; 197 struct ubObject object; 198 struct ubMaterial material; 199 200 mat4 model, view, tmp; 201 202 view.identity().rotateX(D2R(10)).rotateY(t).translate(0, 0, -10); 203 view.identity().rotateY(D2R(ry)).rotateX(D2R(rx)).translate(0, 0, -zoom); 204 model.identity().translate(0.5, 0.5, 0.5); 205 object.mvp = model * view * proj; 206 object.mv = model * view; 207 208 scene.Ortho.setOrtho(0, width, height, 0, -1.0, 1.0); 209 scene.OrthoSize.set(width, height, 0, 0); 210 scene.TextGrid.set(16, 16, width / 16, height / 16); 211 scene.LightColor.set(1.0, 1.0, 1.0); 212 scene.LightPosition = view * vec4(0, 1, 0, 0); 213 scene.LightPosition.w = 0; 214 215 material.Ambient.set(0.325,0.325,0.325,1.0); 216 material.Diffuse.set(1.0,1.0,1.0,1.0); 217 material.Specular.set(1.0,1.0,1.0,1.0); 218 material.Color.set(0,1,0,1); 219 material.Shininess = 250.0f; 220 221 scn.load(&scene, sizeof(scene)); 222 mat.load(&material, sizeof(material)); 223 obj.load(&object, sizeof(object)); 224 scn.use(U_SCENE); 225 mat.use(U_MATERIAL); 226 obj.use(U_OBJECT); 227 228 e->apply(); 229 attr.use(); 230 glDrawElementsInstanced(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, NULL, lcount); 231 232 text.clear(); 233 text.printf(0, 0, "rx: %8.4f", rx); 234 text.printf(0, 1, "ry: %8.4f", ry); 235 text.printf(0, 2, "zm: %8.4f", zoom); 236 text.printf(0, -1, "%d fps", fps); 237 238 glDisable(GL_DEPTH_TEST); 239 glEnable(GL_BLEND); 240 text.render(); 241 glDisable(GL_BLEND); 242 } 243 244 App *createApp(void) { 245 return new TestApp(); 246 }