Exemple #1
0
void MyView::
windowViewWillStart(std::shared_ptr<tygra::Window> window)
{
    /*
     *  Compile shaders and create a shader program
     */
    GLint compile_status = 0;
    
    GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    std::string vertex_shader_string = tygra::stringFromFile("water_vs.glsl");
    const char *vertex_shader_code = vertex_shader_string.c_str();
    glShaderSource(vertex_shader, 1,
                   (const GLchar **) &vertex_shader_code, NULL);
    glCompileShader(vertex_shader);
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &compile_status);
    if (compile_status != GL_TRUE) {
        const int string_length = 1024;
        GLchar log[string_length]= "";
        glGetShaderInfoLog(vertex_shader, string_length, NULL, log);
        std::cerr << log << std::endl;
    }
 
    GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    std::string fragment_shader_string = tygra::stringFromFile("water_fs.glsl");
    const char *fragment_shader_code = fragment_shader_string.c_str();
    glShaderSource(fragment_shader, 1,
                   (const GLchar **) &fragment_shader_code, NULL);
    glCompileShader(fragment_shader);
    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &compile_status);
    if (compile_status != GL_TRUE) {
        const int string_length = 1024;
        GLchar log[string_length]= "";
        glGetShaderInfoLog(fragment_shader, string_length, NULL, log);
        std::cerr << log << std::endl;
    }

    water_sp_ = glCreateProgram();
    glAttachShader(water_sp_, vertex_shader);
    glBindAttribLocation(water_sp_, kVertexPosition, "vertex_position");
    glDeleteShader(vertex_shader);
    glAttachShader(water_sp_, fragment_shader);
    glBindFragDataLocation(water_sp_, 0, "fragment_colour");
    glDeleteShader(fragment_shader);
    glLinkProgram(water_sp_);

    GLint link_status = 0;
    glGetProgramiv(water_sp_, GL_LINK_STATUS, &link_status);
    if (link_status != GL_TRUE) {
        const int string_length = 1024;
        GLchar log[string_length]= "";
        glGetProgramInfoLog(water_sp_, string_length, NULL, log);
        std::cerr << log << std::endl;
    }


    /*
     *  TODO: Create vertex buffers and a vertex array object for the water mesh
     */
    std::vector<int> elements(waterSim_->IndexCount());
    waterSim_->GetIndices(elements.data());
	
    glGenBuffers(1, &waterMesh_.position_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, waterMesh_.position_vbo);
	glBufferData(GL_ARRAY_BUFFER, waterSim_->VertexCount() * sizeof(glm::vec3), nullptr, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

	glGenBuffers(1, &waterMesh_.normal_vbo);
	glBindBuffer(GL_ARRAY_BUFFER, waterMesh_.normal_vbo);
	glBufferData(GL_ARRAY_BUFFER, waterSim_->VertexCount() * sizeof(glm::vec3), nullptr, GL_DYNAMIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glGenBuffers(1, &waterMesh_.tex_coord_vbo);
	glBindBuffer(GL_ARRAY_BUFFER, waterMesh_.tex_coord_vbo);
	glBufferData(GL_ARRAY_BUFFER, waterSim_->VertexCount() * sizeof(glm::vec3), nullptr, GL_DYNAMIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &waterMesh_.element_vbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, waterMesh_.element_vbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
        elements.size() * sizeof(unsigned int),
        elements.data(),
        GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    waterMesh_.element_count = elements.size();

    glGenVertexArrays(1, &waterMesh_.vao);
    glBindVertexArray(waterMesh_.vao);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, waterMesh_.element_vbo);
	
    glBindBuffer(GL_ARRAY_BUFFER, waterMesh_.position_vbo);
	glEnableVertexAttribArray(kVertexPosition);
	glVertexAttribPointer(kVertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), TGL_BUFFER_OFFSET(0));

	glBindBuffer(GL_ARRAY_BUFFER, waterMesh_.normal_vbo);
	glEnableVertexAttribArray(kVertexNormal);
	glVertexAttribPointer(kVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), TGL_BUFFER_OFFSET(1));

	glBindBuffer(GL_ARRAY_BUFFER, waterMesh_.tex_coord_vbo);
	glEnableVertexAttribArray(kVertexTexcoord);
	glVertexAttribPointer(kVertexTexcoord, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), TGL_BUFFER_OFFSET(2));

	glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned int), elements.data(), GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}
/*
*	\brief Create the mesh
*/
void Mesh::Create( 
		Vertex *vertices,								//! A pointer to an array of verticies
		const int noofVerticies,						//! The number of verticies in the above array
		unsigned int *elements,							//! A pointer to an array of elements
		const int noofElements							//! The number of elements in the above array
	)
{
	// verticies
	glGenVertexArrays(1, &m_vao);
	glBindVertexArray(m_vao);
	glGenBuffers(1, &m_vertexVBO);
	glBindBuffer(GL_ARRAY_BUFFER, m_vertexVBO);
	glBufferData(
		GL_ARRAY_BUFFER,
		noofVerticies * sizeof(Vertex),
		vertices,
		GL_STATIC_DRAW
		);
	m_vertexCount = noofVerticies;

	// positon
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(
		0, 
		3, 
		GL_FLOAT, 
		GL_FALSE,
		sizeof(Vertex), 
		TGL_BUFFER_OFFSET(0)
		);

	// normal
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(
		1, 
		3, 
		GL_FLOAT, 
		GL_FALSE,
		sizeof(Vertex), 
		TGL_BUFFER_OFFSET(3 * sizeof(float))
		);

	// tangents
	glEnableVertexAttribArray(2);
	glVertexAttribPointer(
		2, 
		3, 
		GL_FLOAT, 
		GL_FALSE,
		sizeof(Vertex), 
		TGL_BUFFER_OFFSET(6 * sizeof(float))
		);

	// tex-coords
	glEnableVertexAttribArray(3);
	glVertexAttribPointer(
		3, 
		2, 
		GL_FLOAT, 
		GL_FALSE,
		sizeof(Vertex), 
		TGL_BUFFER_OFFSET(9 * sizeof(float))
		);

	// elements
	glGenBuffers(1, &m_elementVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementVBO);
	glBufferData(
		GL_ELEMENT_ARRAY_BUFFER,
		noofElements * sizeof(unsigned int),
		elements,
		GL_STATIC_DRAW
		);

	m_elementCount = noofElements;
}