graphics

experiments with opengl3.2/ogles3.3 on linux and win7
git clone http://frotz.net/git/graphics.git
Log | Files | Refs

Renderable.cc (2648B)


      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 "Renderable.h"
     17 
     18 class XZGrid : public Renderable {
     19 public:
     20 	XZGrid(float size, unsigned steps);
     21 	virtual void render(void);
     22 private:
     23 	VertexBuffer buf;
     24 	VertexAttributes attr;
     25 	unsigned count;
     26 };
     27 
     28 XZGrid::XZGrid(float size, unsigned steps) : Renderable() {
     29 	static VertexAttrDesc layout[] = {
     30 		{ 0, SRC_FLOAT, DST_FLOAT, 3, 0, 12, 0 },
     31 	};
     32 	count = (steps + 1) * 4;
     33 	VertexBuffer *vb = &buf;
     34 	float x, z;
     35  	float *varr = (float*) malloc(sizeof(float) * 3 * count);
     36 	float *vtx = varr;
     37 	float min = -(size / 2.0);
     38 	float max = (size / 2.0) + 0.0001;
     39 	float step = size / ((float) steps);
     40 	for (x = min; x < max; x += step) {
     41 		vtx[0] = x;
     42 		vtx[1] = 0.0;
     43 		vtx[2] = min;
     44 		vtx[3] = x;
     45 		vtx[4] = 0.0;
     46 		vtx[5] = max;
     47 		vtx += 6;
     48 	}
     49 	for (z = min; z < max; z += step) {
     50 		vtx[0] = min;
     51 		vtx[1] = 0.0;
     52 		vtx[2] = z;
     53 		vtx[3] = max;
     54 		vtx[4] = 0.0;
     55 		vtx[5] = z;
     56 		vtx += 6;
     57 	}
     58 	buf.load(varr, count * 3 * sizeof(float));
     59 	attr.init(layout, &vb, 1);
     60 }
     61 
     62 void XZGrid::render(void) {
     63 	attr.use();
     64 	glDrawArrays(GL_LINES, 0, count);
     65 }
     66 
     67 Renderable* Renderable::createXZgrid(float size, unsigned steps) {
     68 	return new XZGrid(size, steps);
     69 }
     70 
     71 static VertexAttrDesc fsq_layout[] = {
     72 	{ 0, SRC_FLOAT, DST_FLOAT,  2, 0, 16, 0 },
     73 	{ 1, SRC_FLOAT, DST_FLOAT,  2, 8, 16, 0 },
     74 };
     75 
     76 static float fsq_vertices[] = {     //  1,4    6
     77 	-1.0f,  1.0f,  0.0f,  1.0f, //   o  |  o
     78 	-1.0f, -1.0f,  0.0f,  0.0f, //      |
     79 	 1.0f, -1.0f,  1.0f,  0.0f, // -----|-----
     80 	-1.0f,  1.0f,  0.0f,  1.0f, //      |
     81 	 1.0f, -1.0f,  1.0f,  0.0f, //   o  |  o 
     82 	 1.0f,  1.0f,  1.0f,  1.0f, //   2    3,5
     83 };
     84 
     85 class FSQuad : public Renderable {
     86 public:
     87 	FSQuad();
     88 	virtual void render(void);
     89 private:
     90 	VertexBuffer vbuf;
     91 	VertexAttributes attr;
     92 };
     93 
     94 FSQuad::FSQuad() : Renderable() {
     95 	VertexBuffer *data[] = { &vbuf, &vbuf };
     96 	vbuf.load(fsq_vertices, sizeof(fsq_vertices));
     97 	attr.init(fsq_layout, data, 2);
     98 }
     99 
    100 void FSQuad::render(void) {
    101 	attr.use();
    102 	glDrawArrays(GL_TRIANGLES, 0, 6);
    103 }
    104 
    105 Renderable* Renderable::createFullscreenQuad(void) {
    106 	return new FSQuad();
    107 }