test4.cc (2611B)
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 20 #include "app.h" 21 #include "util.h" 22 #include "matrix.h" 23 #include "program.h" 24 25 class TestApp : public App { 26 public: 27 int init(void); 28 int render(void); 29 private: 30 void *texdata; 31 unsigned texw, texh; 32 33 GLuint tex0; 34 GLuint aVertex, aTexCoord; 35 GLuint uMVP, uTexture; 36 37 Program pgm; 38 mat4 MVP; 39 }; 40 41 static GLfloat verts[] = { 42 -1, -1, 0, 43 -1, 1, 0, 44 1, -1, 0, 45 1, 1, 0, 46 -0.5f, -0.5f, 0.0f, 47 -0.5f, 0.5f, 0.0f, 48 0.5f, -0.5f, 0.0f, 49 0.5f, 0.5f, 0.0f, 50 }; 51 52 static GLfloat texcoords[] = { 53 0.0, 0.0, 54 0.0, 1.0, 55 1.0, 0.0, 56 1.0, 1.0, 57 }; 58 59 int TestApp::init(void) { 60 if (!(texdata = load_png_gray("texture.sdf.png", &texw, &texh, 1))) 61 return -1; 62 63 if (pgm.compile("test4.vs","test4.fs")) 64 return -1; 65 66 aVertex = pgm.getAttribID("aVertex"); 67 aTexCoord = pgm.getAttribID("aTexCoord"); 68 uMVP = pgm.getUniformID("uMVP"); 69 uTexture = pgm.getUniformID("uTexture"); 70 71 MVP.setOrtho(-aspect(), aspect(), -1, 1, 1, -1); 72 73 glViewport(0, 0, width(), height()); 74 glClearColor(0, 0, 0, 0); 75 glClearDepth(1.0f); 76 77 glEnable(GL_TEXTURE_2D); 78 79 glGenTextures(1, &tex0); 80 glBindTexture(GL_TEXTURE_2D, tex0); 81 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texw, texh, 0, GL_ALPHA, 82 GL_UNSIGNED_BYTE, texdata); 83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 85 86 return 0; 87 } 88 89 int TestApp::render(void) { 90 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 91 92 pgm.use(); 93 94 glActiveTexture(GL_TEXTURE0); 95 glBindTexture(GL_TEXTURE_2D, tex0); 96 97 glUniformMatrix4fv(uMVP, 1, GL_FALSE, MVP); 98 glUniform1i(uTexture, 0); 99 100 glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 0, verts); 101 glEnableVertexAttribArray(aVertex); 102 103 glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 0, texcoords); 104 glEnableVertexAttribArray(aTexCoord); 105 106 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 107 108 return 0; 109 } 110 111 int main(int argc, char **argv) { 112 TestApp app; 113 app.setOptions(argc, argv); 114 return app.run(); 115 } 116