test5.cc (5100B)
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 #include <string.h> 19 #ifndef _WIN32 20 #include <unistd.h> 21 #endif 22 23 #include "app.h" 24 #include "util.h" 25 #include "matrix.h" 26 #include "program.h" 27 #include "debugtext.h" 28 29 #include <math.h> 30 31 #include <SDL_keysym.h> 32 33 class TestApp : public App { 34 public: 35 TestApp(); 36 int init(void); 37 int render(void); 38 private: 39 void *texdata; 40 unsigned texw, texh; 41 42 GLuint tex0; 43 GLuint aVertex, aNormal, aTexCoord; 44 GLuint uMV, uMVP, uLight, uTexture; 45 46 Program pgm; 47 DebugText debugtext; 48 49 float camx, camy, camz; 50 float camrx, camry, camrz; 51 52 mat4 Projection; 53 54 float a; 55 56 struct model *m; 57 }; 58 59 TestApp::TestApp() : App(), 60 camx(0), camy(0), camz(-5), 61 camrx(0), camry(0), camrz(0), 62 a(0) { 63 } 64 65 int TestApp::init(void) { 66 if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1))) 67 return -1; 68 69 if (!(m = load_wavefront_obj("cube.obj"))) 70 return -1; 71 72 glViewport(0, 0, width(), height()); 73 glClearColor(0, 0, 0, 0); 74 glClearDepth(1.0f); 75 76 if (pgm.compile("test5.vs","test5.fs")) 77 return -1; 78 79 aVertex = pgm.getAttribID("aVertex"); 80 aNormal = pgm.getAttribID("aNormal"); 81 aTexCoord = pgm.getAttribID("aTexCoord"); 82 uMVP = pgm.getUniformID("uMVP"); 83 uMV = pgm.getUniformID("uMV"); 84 uLight = pgm.getUniformID("uLight"); 85 uTexture = pgm.getUniformID("uTexture"); 86 87 glGenTextures(1, &tex0); 88 89 glBindTexture(GL_TEXTURE_2D, tex0); 90 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texw, texh, 0, GL_RGBA, 91 GL_UNSIGNED_BYTE, texdata); 92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 94 95 Projection.setPerspective(D2R(60.0), aspect(), 1.0, 100.0); 96 97 debugtext.init(32,32); 98 return 0; 99 } 100 101 int TestApp::render(void) { 102 mat4 MVP; 103 mat4 MV; 104 mat4 Model; 105 mat4 View; 106 vec4 light(0.0,0.0,0.0,1.0); 107 108 float vz = cosf(D2R(camry)); 109 float vx = -sinf(D2R(camry)); 110 float vz2 = cosf(D2R(camry + 90.0)); 111 float vx2 = -sinf(D2R(camry + 90.0)); 112 113 if (keydown(SDLK_w)) { camx += vx * 0.1; camz += vz * 0.1; } 114 if (keydown(SDLK_s)) { camx -= vx * 0.1; camz -= vz * 0.1; } 115 if (keydown(SDLK_a)) { camx += vx2 * 0.1; camz += vz2 * 0.1; } 116 if (keydown(SDLK_d)) { camx -= vx2 * 0.1; camz -= vz2 * 0.1; } 117 118 if (keydown(SDLK_q)) camry += 3.0; 119 if (keydown(SDLK_e)) camry -= 3.0; 120 121 if (keydown(SDLK_r)) camrx -= 1.0; 122 if (keydown(SDLK_f)) camrx += 1.0; 123 124 if (keydown(SDLK_x)) { camrx = 0; camrz = 0; } 125 126 if (camrx < -45.0) camrx = -45.0; 127 if (camrx > 45.0) camrx = 45.0; 128 129 View.identity().translate(-camx, camy, camz) 130 .rotateY(D2R(camry)).rotateX(D2R(camrx)); 131 a += 1.0; 132 if (a > 360.0) a = 0.0; 133 134 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 135 pgm.use(); 136 137 glEnable(GL_TEXTURE_2D); 138 glEnable(GL_DEPTH_TEST); 139 glEnable(GL_CULL_FACE); 140 glDisable(GL_BLEND); 141 glFrontFace(GL_CCW); 142 143 glActiveTexture(GL_TEXTURE0); 144 glBindTexture(GL_TEXTURE_2D, tex0); 145 146 glUniform1i(uTexture, 0); 147 148 glBindBuffer(GL_ARRAY_BUFFER, 0); 149 150 glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 8*4, m->vdata); 151 glVertexAttribPointer(aNormal, 3, GL_FLOAT, GL_FALSE, 8*4, m->vdata + 3); 152 glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 8*4, m->vdata + 6); 153 glEnableVertexAttribArray(aVertex); 154 glEnableVertexAttribArray(aNormal); 155 glEnableVertexAttribArray(aTexCoord); 156 157 Model.identity().translate(20, 40, 30); 158 light = Model * light; 159 light = View * light; 160 glUniform4fv(uLight, 1, light); 161 162 Model.identity(); 163 MV.mul(Model,View); 164 MVP.mul(MV,Projection); 165 166 glUniformMatrix4fv(uMV, 1, GL_FALSE, MV); 167 glUniformMatrix4fv(uMVP, 1, GL_FALSE, MVP); 168 glDrawElements(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, m->idx); 169 170 Model.identity().translate(-3, 0, 0); 171 MV.mul(Model,View); 172 MVP.mul(MV,Projection); 173 174 glUniformMatrix4fv(uMV, 1, GL_FALSE, MV); 175 glUniformMatrix4fv(uMVP, 1, GL_FALSE, MVP); 176 glDrawElements(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, m->idx); 177 178 Model.identity().translate(3, 0, 0).rotateY(D2R(a)); 179 MV.mul(Model,View); 180 MVP.mul(MV,Projection); 181 182 glUniformMatrix4fv(uMV, 1, GL_FALSE, MV); 183 glUniformMatrix4fv(uMVP, 1, GL_FALSE, MVP); 184 glDrawElements(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, m->idx); 185 186 glDisableVertexAttribArray(aVertex); 187 glDisableVertexAttribArray(aNormal); 188 glDisableVertexAttribArray(aTexCoord); 189 190 debugtext.clear(); 191 debugtext.printf("Hello, Test #5\n"); 192 debugtext.printf("Cam @ %6.3f %6.3f\n", camx, camz); 193 debugtext.printf("\n%d fps\n", fps()); 194 debugtext.render(); 195 196 return 0; 197 } 198 199 int main(int argc, char **argv) { 200 TestApp app; 201 app.setOptions(argc, argv); 202 return app.run(); 203 } 204