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