git-svn-id: svn://db.shs.com.ru/libs@44 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
|
||||
GLVBO::GLVBO(GLenum usage_) {
|
||||
buffer_ = 0;
|
||||
va_ = 0;
|
||||
usage = usage_;
|
||||
changed = true;
|
||||
}
|
||||
@@ -32,12 +33,20 @@ GLVBO::~GLVBO() {
|
||||
|
||||
|
||||
void GLVBO::init() {
|
||||
if (!isIinit())
|
||||
if (!isIinit()) {
|
||||
//glGenVertexArrays(1, &va_);
|
||||
glGenBuffers(1, &buffer_);
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
|
||||
|
||||
void GLVBO::destroy() {
|
||||
deleteGLBuffer(buffer_);
|
||||
//deleteGLVertexArray(va_);
|
||||
}
|
||||
|
||||
|
||||
bool GLVBO::rebuffer(bool clear_) {
|
||||
QVector<GLfloat> data;
|
||||
//data.clear();
|
||||
@@ -54,9 +63,12 @@ bool GLVBO::rebuffer(bool clear_) {
|
||||
data << colors_;
|
||||
has_colors = true;
|
||||
} else has_colors = false;
|
||||
//glBindVertexArray(va_);
|
||||
//qDebug() << "load buffer" << data.size() << buffer_;
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer_);
|
||||
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(GLfloat), data.constData(), usage);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
//glBindVertexArray(0);
|
||||
vert_count = vertices_.size() / 3;
|
||||
changed = false;
|
||||
//qDebug() << "rebuff" << buffer_ << vert_count;
|
||||
@@ -65,35 +77,76 @@ bool GLVBO::rebuffer(bool clear_) {
|
||||
}
|
||||
|
||||
|
||||
void GLVBO::draw(GLenum type, bool simplest) {
|
||||
void GLVBO::draw(GLenum type, QGLShaderProgram * prog, bool simplest) {
|
||||
if (buffer_ == 0 || vert_count == 0) return;
|
||||
if (changed) rebuffer();
|
||||
//qDebug() << "draw" << vert_count;
|
||||
void * offset = (void*)(vert_count * 3 * sizeof(GLfloat));
|
||||
//glBindVertexArray(va_);
|
||||
|
||||
void * offsets[3] = {0, 0, 0};
|
||||
if (has_normals) {
|
||||
offsets[0] = offset;
|
||||
offset = (void*)(llong(offset) + vert_count * 3 * sizeof(GLfloat));
|
||||
}
|
||||
if (has_texcoords) {
|
||||
offsets[1] = offset;
|
||||
offset = (void*)(llong(offset) + vert_count * 2 * sizeof(GLfloat));
|
||||
}
|
||||
if (has_colors) {
|
||||
offsets[2] = offset;
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffer_);
|
||||
glMultiTexCoord3f(GL_TEXTURE2, 0.f, 1.f, 0.f);
|
||||
glVertexPointer(3, GL_FLOAT, 0, 0);
|
||||
if (!simplest) {
|
||||
if (prog) {
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_NORMAL_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
int loc = prog->attributeLocation("_qgl_Vertex");
|
||||
glEnableVertexAttribArray(loc);
|
||||
glVertexAttribPointer(loc, 3, GL_FLOAT, 0, 0, 0);
|
||||
loc = prog->attributeLocation("qgl_Normal");
|
||||
if (has_normals) {
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
glNormalPointer(GL_FLOAT, 0, offset);
|
||||
offset = (void*)(llong(offset) + vert_count * 3 * sizeof(GLfloat));
|
||||
} else glDisableClientState(GL_NORMAL_ARRAY);
|
||||
glEnableVertexAttribArray(loc);
|
||||
glVertexAttribPointer(loc, 3, GL_FLOAT, 0, 0, offsets[0]);
|
||||
} else
|
||||
glDisableVertexAttribArray(loc);
|
||||
loc = prog->attributeLocation("qgl_Texture");
|
||||
if (has_texcoords) {
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, offset);
|
||||
offset = (void*)(llong(offset) + vert_count * 2 * sizeof(GLfloat));
|
||||
} else glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glEnableVertexAttribArray(loc);
|
||||
glVertexAttribPointer(loc, 2, GL_FLOAT, 0, 0, offsets[1]);
|
||||
} else
|
||||
glDisableVertexAttribArray(loc);
|
||||
loc = prog->attributeLocation("qgl_Color");
|
||||
if (has_colors) {
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glColorPointer(4, GL_FLOAT, 0, offset);
|
||||
} else glDisableClientState(GL_COLOR_ARRAY);
|
||||
}/* else {
|
||||
glDisable(GL_NORMAL_ARRAY);
|
||||
glDisable(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisable(GL_COLOR_ARRAY);
|
||||
}*/
|
||||
glEnableVertexAttribArray(loc);
|
||||
glVertexAttribPointer(loc, 4, GL_FLOAT, 0, 0, offsets[2]);
|
||||
} else
|
||||
glDisableVertexAttribArray(loc);
|
||||
} else {
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glVertexPointer(3, GL_FLOAT, 0, 0);
|
||||
if (!simplest) {
|
||||
if (has_normals) {
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
glNormalPointer(GL_FLOAT, 0, offset);
|
||||
offset = (void*)(llong(offset) + vert_count * 3 * sizeof(GLfloat));
|
||||
} else glDisableClientState(GL_NORMAL_ARRAY);
|
||||
if (has_texcoords) {
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, offset);
|
||||
offset = (void*)(llong(offset) + vert_count * 2 * sizeof(GLfloat));
|
||||
} else glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
if (has_colors) {
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glColorPointer(4, GL_FLOAT, 0, offset);
|
||||
} else glDisableClientState(GL_COLOR_ARRAY);
|
||||
}
|
||||
}
|
||||
//qDebug() << "draw" << vert_count << buffer_ << offsets[0] << offsets[1] << offsets[3];
|
||||
glDrawArrays(type, 0, vert_count);
|
||||
//qDebug() << "draw" << vert_count << buffer_ << "done";
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
@@ -138,4 +191,4 @@ bool GLVBO::loadFromFile(const QString & filename) {
|
||||
return !isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user