test2.cc (3736B)
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 <unistd.h> 19 #include <string.h> 20 21 #include "app.h" 22 #include "util.h" 23 #include "program.h" 24 #include "matrix.h" 25 26 #include <math.h> 27 28 class TestApp : public App { 29 public: 30 int init(void); 31 int render(void); 32 private: 33 void *texdata; 34 unsigned texw, texh; 35 36 GLuint tex0; 37 GLuint aVertex, aTexCoord; 38 GLuint uMVP, uTexture; 39 40 Program pgm; 41 mat4 perspective; 42 mat4 MVP; 43 44 float a; 45 }; 46 47 static GLfloat data[] = { 48 -0.5f, 0.5f, 0.5f, 0.25, 0.75, 49 -0.5f, -0.5f, 0.5f, 0.25, 0.50, 50 0.5f, -0.5f, 0.5f, 0.50, 0.50, 51 0.5f, 0.5f, 0.5f, 0.50, 0.75, 52 53 0.5f, 0.5f, 0.5f, 0.50, 0.75, 54 0.5f, -0.5f, 0.5f, 0.50, 0.50, 55 0.5f, -0.5f, -0.5f, 0.75, 0.50, 56 0.5f, 0.5f, -0.5f, 0.75, 0.75, 57 58 0.5f, 0.5f, -0.5f, 0.75, 0.75, 59 0.5f, -0.5f, -0.5f, 0.75, 0.50, 60 -0.5f, -0.5f, -0.5f, 1.00, 0.50, 61 -0.5f, 0.5f, -0.5f, 1.00, 0.75, 62 63 -0.5f, 0.5f, -0.5f, 0.00, 0.75, 64 -0.5f, -0.5f, -0.5f, 0.00, 0.50, 65 -0.5f, -0.5f, 0.5f, 0.25, 0.50, 66 -0.5f, 0.5f, 0.5f, 0.25, 0.75, 67 68 -0.5f, 0.5f, -0.5f, 0.25, 1.00, 69 -0.5f, 0.5f, 0.5f, 0.25, 0.75, 70 0.5f, 0.5f, 0.5f, 0.50, 0.75, 71 0.5f, 0.5f, -0.5f, 0.50, 1.00, 72 73 -0.5f, -0.5f, 0.5f, 0.25, 0.50, 74 -0.5f, -0.5f, -0.5f, 0.25, 0.25, 75 0.5f, -0.5f, -0.5f, 0.50, 0.25, 76 0.5f, -0.5f, 0.5f, 0.50, 0.50, 77 }; 78 79 static GLubyte indices[] = { 80 0, 1, 2, 3, 0, 2, 81 4, 5, 6, 7, 4, 6, 82 8, 9, 10, 11, 8, 10, 83 12, 13, 14, 15, 12, 14, 84 16, 17, 18, 19, 16, 18, 85 20, 21, 22, 23, 20, 22, 86 }; 87 88 int TestApp::init(void) { 89 a = 0.0; 90 91 if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1))) 92 return -1; 93 94 perspective.setPerspective(D2R(60.0), aspect(), 1.0, 10.0); 95 96 glViewport(0, 0, width(), height()); 97 glClearColor(0, 0, 0, 0); 98 glClearDepth(1.0f); 99 100 if (pgm.compile("test1.vs", "test1.fs")) 101 return -1; 102 103 aVertex = pgm.getAttribID("aVertex"); 104 aTexCoord = pgm.getAttribID("aTexCoord"); 105 uMVP = pgm.getUniformID("uMVP"); 106 uTexture = pgm.getUniformID("uTexture"); 107 108 glEnable(GL_TEXTURE_2D); 109 glEnable(GL_DEPTH_TEST); 110 glEnable(GL_CULL_FACE); 111 glFrontFace(GL_CCW); 112 113 glGenTextures(1, &tex0); 114 115 glBindTexture(GL_TEXTURE_2D, tex0); 116 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texw, texh, 0, GL_RGBA, 117 GL_UNSIGNED_BYTE, texdata); 118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 120 121 return 0; 122 } 123 124 int TestApp::render(void) { 125 mat4 camera; 126 127 camera.identity(); 128 camera.rotateY(D2R(a)); 129 camera.rotateX(D2R(25.0)); 130 camera.translate(0, 0, -3.0); 131 132 MVP = camera * perspective; 133 134 a += 1.0; 135 if (a > 360.0) a = 0.0; 136 137 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 138 pgm.use(); 139 140 glActiveTexture(GL_TEXTURE0); 141 glBindTexture(GL_TEXTURE_2D, tex0); 142 143 glUniformMatrix4fv(uMVP, 1, GL_FALSE, MVP); 144 glUniform1i(uTexture, 0); 145 146 glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 5*4, data); 147 glEnableVertexAttribArray(aVertex); 148 149 glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 5*4, data + 3); 150 glEnableVertexAttribArray(aTexCoord); 151 152 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices); 153 154 return 0; 155 } 156 157 int main(int argc, char **argv) { 158 TestApp app; 159 app.setOptions(argc, argv); 160 return app.run(); 161 } 162