26.06.2013 Views

Utilisation avancée des processeurs graphiques avec Qt

Utilisation avancée des processeurs graphiques avec Qt

Utilisation avancée des processeurs graphiques avec Qt

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

x); }<br />

<strong>Utilisation</strong> <strong>avancée</strong> <strong>des</strong> <strong>processeurs</strong> <strong>graphiques</strong> <strong>avec</strong> <strong>Qt</strong> par Rémi Achard Guillaume Belz<br />

inline int indexOfPoint(const int x, const int y) { return (y * vertices_by_x +<br />

// h<br />

QVector m_normals;<br />

// .cpp<br />

void HeightmapWidget::makeNormals()<br />

{<br />

const int num_neighbor = 8;<br />

}<br />

m_normals.resize(num_vertices);<br />

for(int z = 0; z < vertices_by_z; ++z)<br />

{<br />

for(int x = 0; x < vertices_by_x; ++x)<br />

{<br />

QVector3D normal;<br />

}<br />

}<br />

for(int i = 0; i < num_neighbor; i++)<br />

normal += computeNormal(x, z, i);<br />

normal.normalize();<br />

m_normals[indexOfPoint(x,z)] = normal;<br />

La fonction computeNormal se charge de calculer le vecteur normal d'un plan voisin du vertex en cours de traitement.<br />

Elle prend en paramètre les coordonnées du vertex dont la normale est en cours de calcul, (x ,z) et le numéro du<br />

plan voisin (compris entre 0 et 7) index (A à H sur l'image). Deux tableaux constants permettent d'obtenir la position<br />

relative d'un <strong>des</strong> huit points voisins en fonction du numéro de plan donné.<br />

index)<br />

QVector3D HeightmapWidget::computeNormal(const int x, const int z, const int<br />

{<br />

const int x_offset[] = { -1, 0, 1, 1, 1, 0, -1, -1, -1 };<br />

const int z_offset[] = { -1, -1, -1, 0, 1, 1, 1, 0, -1 };<br />

La position <strong>des</strong> deux points définissant le plan index est calculée, en tournant dans le sens anti-horaire ces deux<br />

points sont aux indices index, index+1. Une une vérification systématique de non débordement du cadre de l'image<br />

est effectuée, en cas de débordement un vecteur nul est renvoyé.<br />

int x_B = x + x_offset[index];<br />

if (x_B < 0 || x_B >= vertices_by_x) return QVector3D();<br />

int z_B = z + z_offset[index];<br />

if (z_B < 0 || z_B >= vertices_by_z) return QVector3D();<br />

int x_C = x + x_offset[index+1];<br />

if (x_C < 0 || x_C >= vertices_by_x) return QVector3D();<br />

int z_C = z + z_offset[index+1];<br />

if (z_C < 0 || z_C >= vertices_by_z) return QVector3D();<br />

Enfin, le produit vectoriel entre les deux vecteurs définissant le plan index est calculé. Pour cela, les coordonnées<br />

3D du vertex en cours de traitement et <strong>des</strong> deux points représentant le sous plan étudié sont récupérées.<br />

- 72 -<br />

http://gbelz.developpez.com/remi-achard/gpu-avance-<strong>avec</strong>-qt/modified/

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!