AGL
A graphics library
agl::TriangleMesh Class Reference

Base class for indexed triangle meshes. More...

#include <triangle_mesh.h>

Public Member Functions

virtual void render () const
 Draw this mesh. More...
 
- Public Member Functions inherited from agl::Mesh
GLuint vao () const
 Return the vertex array object corresponding to this mesh.
 
bool hasUV () const
 Return whether this mesh has UV coordinates defined.
 
bool isDynamic () const
 Query whether or not this is a dynamic mesh. More...
 

Protected Member Functions

void initBuffers (std::vector< GLuint > *indices, std::vector< GLfloat > *points, std::vector< GLfloat > *normals, std::vector< GLfloat > *texCoords=nullptr, std::vector< GLfloat > *tangents=nullptr)
 Call initBuffers from init() to set the data for this mesh. More...
 
- Protected Member Functions inherited from agl::Mesh
int numVertices () const
 Get the number of vertices.
 
void setVertexData (VertexAttribute type, int vertexId, const glm::vec4 &data)
 Set vertex properties. More...
 
glm::vec4 vertexData (VertexAttribute type, int vertexId) const
 Get vertex properties. More...
 
virtual void setIsDynamic (bool on)
 Set whether or not this is a dynamic mesh. More...
 
virtual void init ()=0
 Override init to specifiy vertex data for the mesh. More...
 
void initBuffers (std::vector< GLfloat > *points, std::vector< GLfloat > *normals, std::vector< GLfloat > *texCoords=nullptr, std::vector< GLfloat > *tangents=nullptr)
 Call initBuffers from init() to set the data for this mesh. More...
 
virtual void deleteBuffers ()
 

Protected Attributes

GLuint _nIndices = 0
 
- Protected Attributes inherited from agl::Mesh
GLuint _nVerts = 0
 
GLuint _vao = 0
 
bool _hasUV = false
 
bool _isDynamic = false
 
bool _initialized = false
 
std::vector< GLuint > _buffers
 
std::vector< GLfloat > _data [5]
 

Additional Inherited Members

- Protected Types inherited from agl::Mesh
enum  VertexAttribute {
  INDEX = 0, POSITION, NORMAL, UV,
  TANGENT, NUM_ATTRIBUTES
}
 

Detailed Description

Base class for indexed triangle meshes.

Override this class to create your own meshes.

// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"
#include "agl/mesh/plane.h"

using glm::vec2;
using glm::vec4;
using glm::vec3;

class UndulateMesh : public agl::Plane {
 public:
  UndulateMesh(int xsize, int ysize) : Plane(1, 1, xsize, ysize) {
    setIsDynamic(true);
    init();  // initialize the mesh rather than wait for first frame
  }

  void update(float elapsedTime) {
    for (int i = 0; i < numVertices(); i++) {
      vec3 p = vec3(vertexData(POSITION, i));
      setVertexData(POSITION, i, vec4(position(p, elapsedTime), 0));
      setVertexData(NORMAL, i, vec4(normal(p, elapsedTime), 0));
    }
  }

  vec3 position(const vec3& p, float t) {
    float angle = t;
    float frequency = 7.0;
    float amplitude = 0.05;

    float heightFn = (angle + frequency * p[0] * frequency * p[2]);
    float y = amplitude * sin(heightFn);
    return vec3(p.x, y, p.z);
  }

  vec3 normal(const vec3& p, float t) {
    float eps = 0.001;
    vec3 x = position(p+vec3(eps, 0, 0), t) - position(p-vec3(eps, 0, 0), t);
    vec3 z = position(p+vec3(0, 0, eps), t) - position(p-vec3(0, 0, eps), t);
    vec3 y = glm::cross(z, x);
    return normalize(y);
  }
};

class MyWindow : public agl::Window {
 public:
  void setup() {
    perspective(glm::radians(30.0), 1, 0.1, 100);
    renderer.setUniform("Material.specular", vec3(1.0, 0.2, 0.8));
    renderer.setUniform("Material.ambient", vec3(0.3, 0.0, 0.2));
  }

  void draw() {
    _mesh.update(elapsedTime());
    renderer.rotate(kPI * 0.2, vec3(1, 0, 0));
    renderer.mesh(_mesh);
  }

  UndulateMesh _mesh = UndulateMesh(100, 100);
};

int main() {
  MyWindow window;
  window.run();
}

Member Function Documentation

◆ initBuffers()

void agl::TriangleMesh::initBuffers ( std::vector< GLuint > *  indices,
std::vector< GLfloat > *  points,
std::vector< GLfloat > *  normals,
std::vector< GLfloat > *  texCoords = nullptr,
std::vector< GLfloat > *  tangents = nullptr 
)
protected

Call initBuffers from init() to set the data for this mesh.

See also
init()
setIsDynamic(bool)

◆ render()

void agl::TriangleMesh::render ( ) const
virtual

Draw this mesh.

Typically, users do not need to call this function. It is called from Renderer.

See also
Renderer::mesh(const TriangleMesh&)

Implements agl::Mesh.


The documentation for this class was generated from the following files: