OpenGL模型加载二 opengl 网格Mesh

一、编写Mesh类
h文件
#pragma once
#include <glm/glm.hpp>
#include <GL/glew.h>
#include
#include
#include “Shader.h”
using std::vector;
using std::string;
struct Vertex {
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoord;
};
struct Texture {
unsigned int id;
string type;
string path;
};
class Mesh
{
public:
//网格数据
vector vertices;
vector indices;
vector textures;
Mesh(float* vertices);
Mesh(vector vertices, vector indices, vector textures);
void Draw(Shader* shader);

private:
unsigned int VAO, VBO, EBO;
void setupMesh();
};
cpp文件
#include “Mesh.h”

Mesh::Mesh(float* vertices)
{
this->vertices.resize(36);
memcpy(&(this->vertices[0]), vertices, 368sizeof(float));
setupMesh();
}

Mesh::Mesh(vector vertices, vector indices, vector textures):
vertices(vertices),
indices(indices),
textures(textures)
{
setupMesh();
}

void Mesh::Draw(Shader* shader)
{
for (unsigned int i = 0; i < textures.size(); i++)
{
if (textures[i].type == “texture_diffuse”) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
shader->SetUniform1i(“material.diffuse”, 0);
}
else if (textures[i].type == “texture_specular”) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
shader->SetUniform1i(“material.specular”, 1);
}
}
glBindVertexArray(VAO);
//glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glActiveTexture(GL_TEXTURE0);
}

void Mesh::setupMesh()
{
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

/*glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indices.size(), &indices[0], GL_STATIC_DRAW);*/

//vertex Poisitions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
//vertex normal
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,Normal));
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(GL_FLOAT)));
//vertex texture coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoord));
//glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(6*sizeof(GL_FLOAT)));

glBindVertexArray(0);

}

二、测试Mesh类
使用此构造函数Mesh(float* vertices);测试前期的立方体数据

1、将Init and Load Models to VAO,VBO区块内容替换
Mesh cube(vertices);
2、注释掉//Set Material ->Texture部分
3、调用Mesh的Draw函数绘制立方体
cube.Draw(pshader);

三、Mesh的功能
1、setupMesh
传递pos normal textcoord 方位到 vertextShader
绑定VAO VBO EBO
设置 VBO 顶点数据pos 法向量数据normal 贴图坐标数据textcoord
设置 EBO 索引数据 indices
2、Draw
传递texture贴图数据到fragmentShader,并调用 glDrawxxx绘制
diffuse
specular
。。。
代码Git地址.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值