glstuff

experiments with opengl2/ogles2/sdl
git clone http://frotz.net/git/glstuff.git
Log | Files | Refs

test3.cc (2844B)


      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 "matrix.h"
     24 #include "program.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 	struct model *m;
     47 };
     48 
     49 int TestApp::init(void) {
     50 	a = 0.0;
     51 
     52 	if (!(texdata = load_png_rgba("cube-texture.png", &texw, &texh, 1)))
     53 		return -1;
     54 
     55 	if (!(m = load_wavefront_obj("cube.obj")))
     56 		return -1;
     57 
     58 	perspective.setPerspective(D2R(60.0), aspect(), 1.0, 10.0);
     59 
     60 	glViewport(0, 0, width(), height());
     61 	glClearColor(0, 0, 0, 0);
     62 	glClearDepth(1.0f);
     63 
     64 	if (pgm.compile("test1.vs","test1.fs"))
     65 		return -1;
     66 
     67 	aVertex = pgm.getAttribID("aVertex");
     68 	aTexCoord = pgm.getAttribID("aTexCoord");
     69 	uMVP = pgm.getUniformID("uMVP");
     70 	uTexture = pgm.getUniformID("uTexture");
     71 
     72 	glEnable(GL_TEXTURE_2D);
     73 	glEnable(GL_DEPTH_TEST);
     74 	glEnable(GL_CULL_FACE);
     75 	glFrontFace(GL_CCW);
     76 
     77 	glGenTextures(1, &tex0);
     78 	
     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(void) {
     89 	mat4 camera;
     90 
     91 	camera.identity();
     92 	camera.rotateY(D2R(a));
     93 	camera.rotateX(D2R(25.0));
     94 	camera.translate(0, 0, -5.0);
     95 
     96 	MVP = camera * perspective;
     97 
     98 	a += 1.0;
     99 	if (a > 360.0) a = 0.0;
    100 
    101 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    102 	pgm.use();
    103 
    104 	glActiveTexture(GL_TEXTURE0);
    105 	glBindTexture(GL_TEXTURE_2D, tex0);
    106 
    107 	glUniformMatrix4fv(uMVP, 1, GL_FALSE, MVP);
    108 	glUniform1i(uTexture, 0);
    109 
    110 	glVertexAttribPointer(aVertex, 3, GL_FLOAT, GL_FALSE, 8*4, m->vdata);
    111 	glEnableVertexAttribArray(aVertex);
    112 
    113 	glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 8*4, m->vdata + 6);
    114 	glEnableVertexAttribArray(aTexCoord);
    115 
    116 	glDrawElements(GL_TRIANGLES, m->icount, GL_UNSIGNED_SHORT, m->idx);
    117 
    118 	return 0;
    119 }
    120 
    121 int main(int argc, char **argv) {
    122 	TestApp app;
    123 	app.setOptions(argc, argv);
    124 	return app.run();
    125 }
    126