void TriangleScene::initialise()
{
//return if already initialized
if(mInitialized)
return;
if(!initializeOpenGLFunctions())
{
std::cerr << "Modern OpenGL Functions could not be initialized." << std::endl;
std::exit(EXIT_FAILURE);
}
GL_CHECK_ERRORS;
//make the projection and model-view matrix
//identity matrices
mProjectionMatrix = glm::mat4(1.0f);
mModelViewMatrix = glm::mat4(1.0f);
//load and compile the shaders
loadShaders();
//setup the triangle geometry
mVertices[0].color = glm::vec3(1.0,0.0,0.0);
mVertices[1].color = glm::vec3(0.0,1.0,0.0);
mVertices[2].color = glm::vec3(0.0,0.0,1.0);
mVertices[0].position = glm::vec3(-1,-1,0);
mVertices[1].position = glm::vec3(0,1,0);
mVertices[2].position = glm::vec3(1,-1,0);
//setup the triangle indices
mIndices[0] = 0;
mIndices[1] = 1;
mIndices[2] = 2;
//check for opengl errors
GL_CHECK_ERRORS;
//setup the triangle vao and vbo stuff
glGenVertexArrays(1,&mVaoID);
glGenBuffers(1,&mVerticesID);
glGenBuffers(1,&mIndicesID);
//get the size of the Vertex struct
GLsizei stride = sizeof(Vertex);
glBindVertexArray(mVaoID);
glBindBuffer(GL_ARRAY_BUFFER,mVerticesID);
//pass triangle vertices to the buffer object
glBufferData(GL_ARRAY_BUFFER, //specifies the target buffer object
sizeof(mVertices),//specifies the size in bytes
&mVertices[0], // pointer to the data
GL_STATIC_DRAW); //usage pattern of the data
GL_CHECK_ERRORS;
glEnableVertexAttribArray(GLuint(0));
glVertexAttribPointer(GLuint(0), //index of the generic vertex attribute to be modified
3,//the number of components per generic vertex attribute
GL_FLOAT, //the data type of each component in the array
GL_FALSE,//if the fixed point data to be normalized or not
stride,
0);
GL_CHECK_ERRORS;
//enable vertex attribute array for the color
glEnableVertexAttribArray(GLuint(1));
glVertexAttribPointer(GLuint(1),
3,
GL_FLOAT,
GL_FALSE,
stride,
(const GLvoid*)offsetof(Vertex,color));
GL_CHECK_ERRORS;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mIndicesID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(mIndices),&mIndices[0],GL_STATIC_DRAW);
glDisableVertexAttribArray(GLuint(0));
glDisableVertexAttribArray(GLuint(1));
glBindVertexArray(0);
GL_CHECK_ERRORS;
mInitialized = true;
}