git-svn-id: svn://db.shs.com.ru/libs@45 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2015-11-23 17:07:25 +00:00
parent 104a7f99ad
commit 48addec20f
32 changed files with 738 additions and 541 deletions

View File

@@ -47,9 +47,42 @@ void GLVBO::destroy() {
}
void GLVBO::calculateBinormals() {
tangents_.clear();
bitangents_.clear();
if (vertices_.isEmpty() || texcoords_.isEmpty()) return;
int vcnt = vertices_.size() / 3;
if (texcoords_.size() != vcnt * 2) return;
int tcnt = vcnt / 3;
//qDebug() << "calculateBinormals" << vcnt << tcnt << vertices_.size() << texcoords_.size() << "...";
for (int t = 0; t < tcnt; ++t) {
int vi = t*9, ti = t*6;
Vector3d v0(vertices_[vi + 0], vertices_[vi + 1], vertices_[vi + 2]);
Vector3d v1(vertices_[vi + 3], vertices_[vi + 4], vertices_[vi + 5]);
Vector3d v2(vertices_[vi + 6], vertices_[vi + 7], vertices_[vi + 8]);
Vector2d t0(texcoords_[ti + 0], texcoords_[ti + 1]);
Vector2d t1(texcoords_[ti + 2], texcoords_[ti + 3]);
Vector2d t2(texcoords_[ti + 4], texcoords_[ti + 5]);
Vector3d dv1 = v1 - v0, dv2 = v2 - v0;
Vector2d dt1 = t1 - t0, dt2 = t2 - t0;
Vector3d tan;
Vector3d bitan;
tan = (dv1 * dt2.y - dv2 * dt1.y).normalize();
bitan = (dv2 * dt1.x - dv1 * dt2.x).normalize();
for (int i = 0; i < 3; ++i) {
tangents_ << tan.x << tan.y << tan.z;
bitangents_ << bitan.x << bitan.y << bitan.z;
}
//qDebug() << " t" << t << vi << ti << dv1.toQVector3D() << "...";
}
//qDebug() << "calculateBinormals" << vcnt << tcnt << tangents_.size();
}
bool GLVBO::rebuffer(bool clear_) {
QVector<GLfloat> data;
//data.clear();
calculateBinormals();
data << vertices_;
if (!normals_.isEmpty()) {
data << normals_;
@@ -63,6 +96,10 @@ bool GLVBO::rebuffer(bool clear_) {
data << colors_;
has_colors = true;
} else has_colors = false;
if (!tangents_.isEmpty()) {
data << tangents_ << bitangents_;
has_binormals = true;
} else has_binormals = false;
//glBindVertexArray(va_);
//qDebug() << "load buffer" << data.size() << buffer_;
glBindBuffer(GL_ARRAY_BUFFER, buffer_);
@@ -84,7 +121,7 @@ void GLVBO::draw(GLenum type, QGLShaderProgram * prog, bool simplest) {
void * offset = (void*)(vert_count * 3 * sizeof(GLfloat));
//glBindVertexArray(va_);
void * offsets[3] = {0, 0, 0};
void * offsets[5] = {0, 0, 0, 0, 0};
if (has_normals) {
offsets[0] = offset;
offset = (void*)(llong(offset) + vert_count * 3 * sizeof(GLfloat));
@@ -95,6 +132,12 @@ void GLVBO::draw(GLenum type, QGLShaderProgram * prog, bool simplest) {
}
if (has_colors) {
offsets[2] = offset;
offset = (void*)(llong(offset) + vert_count * 4 * sizeof(GLfloat));
}
if (has_binormals) {
offsets[3] = offset;
offset = (void*)(llong(offset) + vert_count * 3 * sizeof(GLfloat));
offsets[4] = offset;
}
glBindBuffer(GL_ARRAY_BUFFER, buffer_);
@@ -103,7 +146,7 @@ void GLVBO::draw(GLenum type, QGLShaderProgram * prog, bool simplest) {
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
int loc = prog->attributeLocation("_qgl_Vertex");
int loc = prog->attributeLocation("_qgl_Vertex"), loc2 = 0;
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 3, GL_FLOAT, 0, 0, 0);
loc = prog->attributeLocation("qgl_Normal");
@@ -124,6 +167,17 @@ void GLVBO::draw(GLenum type, QGLShaderProgram * prog, bool simplest) {
glVertexAttribPointer(loc, 4, GL_FLOAT, 0, 0, offsets[2]);
} else
glDisableVertexAttribArray(loc);
loc = prog->attributeLocation("qgl_Tangent");
loc2 = prog->attributeLocation("qgl_Bitangent");
if (has_binormals) {
glEnableVertexAttribArray(loc);
glEnableVertexAttribArray(loc2);
glVertexAttribPointer(loc, 3, GL_FLOAT, 0, 0, offsets[3]);
glVertexAttribPointer(loc2, 3, GL_FLOAT, 0, 0, offsets[4]);
} else {
glDisableVertexAttribArray(loc);
glDisableVertexAttribArray(loc2);
}
} else {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);