【图形学】树叶

1.绘制目标

树叶

【图形学】树叶_2d

2.核心代码

// 注意:纹理图像的位置,请根据你工程的实际路径设置!!!
cv::Mat texture_img = loadImage("./Textures/bark_08.jpg", TEX_W, TEX_H);
cv::Mat texture_img_GD = loadImage("./Textures/soil.jpg", TEX_W_GD, TEX_H_GD);
cv::Mat texture_img_LF = loadImage("./Textures/leaf_05.png", TEX_W_LF, TEX_H_LF);

// 加载 图片纹理
/// TO DO ///
glBindTexture(GL_TEXTURE_2D, BARK_TEXTURE_ID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_W, TEX_H, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_img.data);
glGenerateMipmap(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, BARK_TEXTURE_ID_GD);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_W_GD, TEX_H_GD, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_img_GD.data);
glGenerateMipmap(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, BARK_TEXTURE_ID_LF);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_W_LF, TEX_H_LF, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_img_LF.data);
glGenerateMipmap(GL_TEXTURE_2D);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
cout << "Load the texture image successfully!" << endl;


// 注意:三维模型的路径,请根据你实际工程的路径正确设置!!!
loadgroundmesh("./Meshes/Tree96-Ground.obj");
loadbranchmesh("./Meshes/Tree96-Branch.obj");
loadleafmesh("./Meshes/Tree96-Leaf.obj");


if (head == "v") { // 处理顶点坐标
// TO DO: 获取顶点的X, Y, Z坐标并将其存入m_verts数组
float v_x, v_y, v_z;
iss >> v_x >> v_y >> v_z;
m_verts.push_back(v_x);
m_verts.push_back(v_y);
m_verts.push_back(v_z);
}
else if (head == "vt") { // 处理纹理坐标
// TO DO: 获取纹理的s, t坐标并将其存入m_textures数组
float vt_x, vt_y, vt_z;
iss >> vt_x >> vt_y >> vt_z;
m_textures.push_back(vt_x);
m_textures.push_back(vt_y);
m_textures.push_back(vt_z);
}
else if (head == "vn") { // 处理法向量坐标
// TO DO: 获取法向量的X, Y, Z坐标并将其存入m_normals数组
float vn_x, vn_y, vn_z;
iss >> vn_x >> vn_y >> vn_z;
m_normals.push_back(vn_x);
m_normals.push_back(vn_y);
m_normals.push_back(vn_z);
}


if (head == "v") { // 处理顶点坐标
// TO DO: 获取顶点的X, Y, Z坐标并将其存入m_verts数组
float v_x, v_y, v_z;
iss >> v_x >> v_y >> v_z;
m_verts.push_back(v_x);
m_verts.push_back(v_y);
m_verts.push_back(v_z);
}
else if (head == "vt") { // 处理纹理坐标
// TO DO: 获取纹理的s, t坐标并将其存入m_textures数组
float vt_x, vt_y, vt_z;
iss >> vt_x >> vt_y;
m_textures.push_back(vt_x);
m_textures.push_back(vt_y);
}
else if (head == "vn") { // 处理法向量坐标
// TO DO: 获取法向量的X, Y, Z坐标并将其存入m_normals数组
float vn_x, vn_y, vn_z;
iss >> vn_x >> vn_y >> vn_z;
m_normals.push_back(vn_x);
m_normals.push_back(vn_y);
m_normals.push_back(vn_z);
}


void ObjFileLoader::get_vertex_at(unsigned int id, float* vertexCoords) {
assert(id >= 0 && id < m_verts.size());

// TO DO: 请从m_verts获取指定id处的X, Y, Z坐标并将其写入vertexCoords数组中
vertexCoords[0] = m_verts.at(id * 3);
vertexCoords[1] = m_verts.at(id * 3 + 1);
vertexCoords[2] = m_verts.at(id * 3 + 2);
}

void ObjFileLoader::get_texture_at(unsigned int id, float* textureCoords) {
assert(id >= 0 && id < m_textures.size());

// TO DO: 请从m_textures获取指定id处的s, t坐标并将其写入textureCoords数组中
textureCoords[0] = m_textures.at(id * 2);
textureCoords[1] = m_textures.at(id * 2 + 1);
}

void ObjFileLoader::get_normal_at(unsigned int id, float* normalVector)
{
assert(id >= 0 && id < m_normals.size());

// TO DO: 请从m_verts获取指定id处的X, Y, Z坐标并将其写入vertexCoords数组中
normalVector[0] = m_normals.at(id * 3);
normalVector[1] = m_normals.at(id * 3 + 1);
normalVector[2] = m_normals.at(id * 3 + 2);
}

3.运行结果

【图形学】树叶_2d_02


【图形学】树叶_纹理图像_03