1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| void CModelRendererGL3::Draw(const CModelBase* _pModel, const Component* _akComponent, unsigned int _uiNbComponents, const CCamera* _pCamera) const
{
DisplayType iDisplay = _pModel->GetDisplayType();
if (iDisplay != NONE)
{
const CVector3& kPosition = _pModel->GetPosition();
const Pixel& kColor = _pModel->GetColor();
// Projection matrix : 45° Field of View, 16:9 ratio, display range : 0.1 unit <-> 100 units
glm::mat4 Projection = glm::perspective(45.0f, 16.0f / 9.0f, 0.1f, 100.0f);
// Camera matrix
const CVector3& kCamPos = _pCamera->GetPosition();
CVector3 kLookAt = _pCamera->GetLookAt();
glm::mat4 View = glm::lookAt(
glm::vec3(kCamPos.x, kCamPos.y, kCamPos.z),
glm::vec3(kLookAt.x, kLookAt.y, kLookAt.z),
glm::vec3(0,0,1) // Head is up
);
glm::mat4 Model = glm::translate(glm::mat4(1.0f),glm::vec3(kPosition.x, kPosition.y, kPosition.z));
const Rotation& kRotation = _pModel->GetRotation();
Model = glm::rotate(Model, kRotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
Model = glm::rotate(Model, kRotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
Model = glm::rotate(Model, kRotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
//glm::mat4 MVP = Projection * View * Model; //!< now done by GPU
glBindVertexArray(m_uiVAO);
m_kProgram.Use();
{
glm::vec3 in_Position(kPosition.x, kPosition.y, kPosition.z);
m_kProgram.SetUniformValue("in_Position", glm::value_ptr(in_Position));
glm::vec4 in_Color(kColor.r, kColor.g, kColor.b, kColor.a);
m_kProgram.SetUniformValue("in_Color", glm::value_ptr(in_Color));
float in_Scale = _pModel->GetScale();
m_kProgram.SetUniformValue("in_Scale", &in_Scale);
float in_Alpha = _pModel->GetAlpha();
m_kProgram.SetUniformValue("in_Alpha", &in_Alpha);
m_kProgram.SetUniformValue("Model", glm::value_ptr(Model));
m_kProgram.SetUniformValue("View", glm::value_ptr(View));
m_kProgram.SetUniformValue("Projection", glm::value_ptr(Projection));
float iRenderType = 0.0f + iDisplay;
m_kProgram.SetUniformValue("renderType", &iRenderType);
//!< fill VBO
unsigned int uiComponents =_uiNbComponents;
const Component* akComponents = _akComponent;
unsigned int uiNbVertices = 0;
for (unsigned int i = 0; i < uiComponents; ++i)
{
uiNbVertices += akComponents[i].iFaces * 3;
}
GLfloat* afVertices = new BOUSDK_HEAP GLfloat[uiNbVertices*3]; //!< *3 cause each vertex is x,y,z coordinates
GLfloat* afNormals = new BOUSDK_HEAP GLfloat[uiNbVertices*3];
GLfloat* afTexCoords = new BOUSDK_HEAP GLfloat[uiNbVertices*2]; //!< *2 cause each point is u,v coordinates
unsigned int uiPointCoordId = 0;
unsigned int uiTexCoordId = 0;
for (unsigned int i = 0; i < uiComponents; ++i)
{
for (int j = 0; j < akComponents[i].iFaces; ++j)
{
for (int k = 0; k < 3; ++k)
{
afNormals[uiPointCoordId] = akComponents[i].akFaces[j].akNormal[k].x;
afVertices[uiPointCoordId++] = akComponents[i].akFaces[j].akVertice[k].x;
afNormals[uiPointCoordId] = akComponents[i].akFaces[j].akNormal[k].y;
afVertices[uiPointCoordId++] = akComponents[i].akFaces[j].akVertice[k].y;
afNormals[uiPointCoordId] = akComponents[i].akFaces[j].akNormal[k].z;
afVertices[uiPointCoordId++] = akComponents[i].akFaces[j].akVertice[k].z;
afTexCoords[uiTexCoordId++] = akComponents[i].akFaces[j].akTexVertice[k].x;
afTexCoords[uiTexCoordId++] = akComponents[i].akFaces[j].akTexVertice[k].y;
}
}
}
// sûrement là que je me chie ?!?!
glBindBuffer(GL_ARRAY_BUFFER, m_uiVBOVertices);
glBufferData(GL_ARRAY_BUFFER, uiNbVertices * 3 * sizeof(GLfloat), afVertices, GL_STATIC_DRAW);
int err = glGetError();
m_kProgram.EnableAttribute("vVertex", 3);
glBindBuffer(GL_ARRAY_BUFFER, m_uiVBOTexCoords);
glBufferData(GL_ARRAY_BUFFER, uiNbVertices * 2 * sizeof(GLfloat), afTexCoords, GL_STATIC_DRAW);
m_kProgram.EnableAttribute("vTexCoord", 2);
float values = 0.0f;
m_kProgram.SetUniformValue("textureMap", &values);
if (akComponents[0].pTexture)
akComponents[0].pTexture->Apply();
SafeDeleteArray(afVertices);
SafeDeleteArray(afNormals);
SafeDeleteArray(afTexCoords);
switch (iDisplay)
{
case FULL:
case NOTEXTURE:
glDrawArrays(GL_TRIANGLES, 0, uiNbVertices*3);
break;
case POINTS:
glDrawArrays(GL_POINTS, 0, uiNbVertices*3);
break;
case LINES:
break;
case NONE:
default:
break;
}
}
m_kProgram.Unuse();
glBindVertexArray(0);
CTexture::ResetApplies();
}
} |
Partager