Cheetah Software  1.0
Checkerboard.cpp
Go to the documentation of this file.
1 
6 #include "Checkerboard.h"
7 
8 #include <stdio.h>
9 
17 Checkerboard::Checkerboard(float xSize, float ySize, size_t xSquares,
18  size_t ySquares) {
19  _size[0] = xSize;
20  _size[1] = ySize;
21  _squares[0] = xSquares;
22  _squares[1] = ySquares;
23  // computeVertices();
24 }
25 
29 void Checkerboard::computeVertices(std::vector<float>& vertices,
30  std::vector<float>& normals,
31  std::vector<float>& colors) {
32  // 3 channels, n*m squares, 2 triangles, 3 vertices per triangle
33  size_t dataSize = 3 * _squares[0] * _squares[1] * 3 * 2;
34  vertices.clear();
35  normals.clear();
36  colors.clear();
37  vertices.reserve(dataSize);
38  normals.reserve(dataSize);
39  colors.reserve(dataSize);
40 
41  const float xStep = _size[0] / (float)_squares[0];
42  const float yStep = _size[1] / (float)_squares[1];
43 
44  for (size_t i = 0; i < _squares[0]; i++) {
45  for (size_t j = 0; j < _squares[1]; j++) {
46  // add normals and color
47  const float* squareColor = (i + j) % 2 ? _darkColor : _lightColor;
48  for (size_t k = 0; k < 6; k++) {
49  for (size_t m = 0; m < 3; m++) {
50  colors.push_back(squareColor[m]);
51  normals.push_back(m == 2 ? 1 : 0);
52  }
53  }
54 
55  const float xStart = xStep * i;
56  const float yStart = yStep * j;
57 
58  // tri 1
59  vertices.push_back(xStart);
60  vertices.push_back(yStart);
61  vertices.push_back(0);
62 
63  vertices.push_back(xStart);
64  vertices.push_back(yStart + yStep);
65  vertices.push_back(0);
66 
67  vertices.push_back(xStart + xStep);
68  vertices.push_back(yStart);
69  vertices.push_back(0);
70 
71  // tri 2
72  vertices.push_back(xStart);
73  vertices.push_back(yStart + yStep);
74  vertices.push_back(0);
75 
76  vertices.push_back(xStart + xStep);
77  vertices.push_back(yStart + yStep);
78  vertices.push_back(0);
79 
80  vertices.push_back(xStart + xStep);
81  vertices.push_back(yStart);
82  vertices.push_back(0);
83  }
84  }
85 }
Checkerboard(float xSize, float ySize, size_t xSquares, size_t ySquares)
void computeVertices(std::vector< float > &vertices, std::vector< float > &normals, std::vector< float > &colors)
const float * _lightColor
Definition: Checkerboard.h:41
size_t _squares[2]
Definition: Checkerboard.h:43
float _size[2]
Definition: Checkerboard.h:42
const float * _darkColor
Definition: Checkerboard.h:40