129 lines
3.7 KiB
C++
129 lines
3.7 KiB
C++
#include "Plane.h"
|
|
|
|
|
|
Plane::Plane(int size, int numcell)
|
|
{
|
|
this->size = size;
|
|
this->ncell = numcell;
|
|
|
|
setup(size, numcell);
|
|
}
|
|
|
|
int nvert2;
|
|
|
|
void Plane::setup(float size, int nSquares)
|
|
{
|
|
std::vector<glm::vec3> vlists;
|
|
std::vector<glm::vec3> clists;
|
|
|
|
|
|
// parameters:
|
|
float maxX = size / 2, maxY = size / 2;
|
|
float minX = -size / 2, minY = -size / 2;
|
|
|
|
int x, y, v[3], i;
|
|
float xp, yp, xd, yd;
|
|
v[2] = 0;
|
|
xd = (maxX - minX) / ((float)nSquares);
|
|
yd = (maxY - minY) / ((float)nSquares);
|
|
|
|
|
|
for (x = 0, xp = minX; x < nSquares; x++, xp += xd) {
|
|
for (y = 0, yp = minY, i = x; y < nSquares; y++, i++, yp += yd) {
|
|
|
|
clists.push_back(glm::vec3(0, 1, 0));
|
|
clists.push_back(glm::vec3(0, 1, 0));
|
|
clists.push_back(glm::vec3(0, 1, 0));
|
|
clists.push_back(glm::vec3(0, 1, 0));
|
|
clists.push_back(glm::vec3(0, 1, 0));
|
|
clists.push_back(glm::vec3(0, 1, 0));
|
|
|
|
|
|
vlists.push_back(glm::vec3(xp, -0.1, yp));
|
|
vlists.push_back(glm::vec3(xp, -0.1, yp + yd));
|
|
vlists.push_back(glm::vec3(xp + xd, -0.1, yp + yd));
|
|
|
|
vlists.push_back(glm::vec3(xp, -0.1, yp));
|
|
vlists.push_back(glm::vec3(xp + xd, -0.1, yp + yd));
|
|
vlists.push_back(glm::vec3(xp + xd, -0.1, yp));
|
|
|
|
|
|
|
|
} // end of for j
|
|
}// end of for i
|
|
|
|
nvert2 = vlists.size();
|
|
|
|
//create vao
|
|
glGenVertexArrays(1, &vaoHandle);
|
|
glBindVertexArray(vaoHandle);
|
|
|
|
//create vbo for vertices
|
|
glGenBuffers(1, &vbo_cube_vertices);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vlists.size() * 3, vlists.data(), GL_STATIC_DRAW);
|
|
glVertexAttribPointer(
|
|
0, // attribute
|
|
3, // number of elements per vertex, here (x,y,z,1)
|
|
GL_FLOAT, // the type of each element
|
|
GL_FALSE, // take our values as-is
|
|
0, // no extra data between each position
|
|
0 // offset of first element
|
|
);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
|
|
//create vbo for colors
|
|
glGenBuffers(1, &vbo_cube_colors);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_colors);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*clists.size() * 3, clists.data(), GL_STATIC_DRAW);
|
|
glVertexAttribPointer(
|
|
1, // attribute
|
|
3, // number of elements per vertex, here (R,G,B)
|
|
GL_FLOAT, // the type of each element
|
|
GL_FALSE, // take our values as-is
|
|
0, // no extra data between each position
|
|
0 // offset of first element
|
|
);
|
|
glEnableVertexAttribArray(1);
|
|
|
|
vlists.clear();
|
|
clists.clear();
|
|
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
void Plane::resize(int size, int numcell)
|
|
{
|
|
setup(size, numcell);
|
|
}
|
|
|
|
void Plane::draw(ShaderProgram * shader, glm::mat4x4 proj_matrix, glm::mat4x4 view_matrix)
|
|
{
|
|
|
|
_model.glPushMatrix();
|
|
effectTransformations();
|
|
glBindVertexArray(vaoHandle);
|
|
glm::mat4 mvpMatrix = proj_matrix * view_matrix * _model.getMatrix();
|
|
glUniformMatrix4fv(shader->uniform("mvp"), 1, GL_FALSE, glm::value_ptr(mvpMatrix));
|
|
glm::mat4 modelview = view_matrix * _model.getMatrix();
|
|
shader->addUniform("LightLocation");
|
|
glUniform4fv(shader->uniform("LightLocation"), 1, glm::value_ptr(view_matrix * glm::vec4(0, 15, 0, 1)));
|
|
shader->addUniform("LightIntensity");
|
|
glUniform3fv(shader->uniform("LightIntensity"), 1, glm::value_ptr(glm::vec3(0.7, 0.7, 0.7)));
|
|
glUniformMatrix4fv(shader->uniform("ModelViewMatrix"), 1, GL_FALSE, glm::value_ptr(modelview));
|
|
glUniform4fv(shader->uniform("LightDirection"), 1, glm::value_ptr(view_matrix * glm::vec4(0, -15, 0, 1)));
|
|
glm::mat4 inverseModelView = glm::inverse(modelview);
|
|
glm::mat3 normalMatrix = glm::mat3(glm::transpose(inverseModelView));
|
|
glUniformMatrix3fv(shader->uniform("NormalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, nvert2 * 3);
|
|
glBindVertexArray(0);
|
|
|
|
_model.glPopMatrix();
|
|
|
|
}
|
|
DrawableType Plane::getType()
|
|
{
|
|
return DrawableType::LIGHTED_PLANE;
|
|
} |