aboutsummaryrefslogtreecommitdiff
path: root/src/opengl_mesh.cc
blob: f77a70a816e9005ca141ab81590245f03a366dce (plain)
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
#include "opengl_mesh.hh"
#include "opengl_widget.hh"

#include <QOpenGLFunctions>


OpenGLMesh::OpenGLMesh(QVector<float> verts, QOpenGLTexture *tex, QOpenGLShaderProgram *program)
	:tex(tex),
	 program(program) {
	OpenGLWidget::instance->makeCurrent();
	QOpenGLExtraFunctions *glf = OpenGLWidget::instance;
	nverts = verts.size() / 8;
	glf->glGenVertexArrays(1, &vao);
	glf->glBindVertexArray(vao);
	glf->glGenBuffers(1, &vbo);
	glf->glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glf->glBufferData(GL_ARRAY_BUFFER, nverts * 8 * sizeof (float), verts.data(), GL_STATIC_DRAW);
	glf->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof (float), 0);
	glf->glEnableVertexAttribArray(0);
	glf->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof (float), (void *) (3 * sizeof (float)));
	glf->glEnableVertexAttribArray(1);
	glf->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof (float), (void *) (6 * sizeof (float)));
	glf->glEnableVertexAttribArray(2);
	OpenGLWidget::instance->doneCurrent();
}


void OpenGLMesh::draw(QOpenGLExtraFunctions *f, const QMatrix4x4 &mat) const {
	program->bind();
	program->setUniformValue("model", mat);
	f->glBindVertexArray(vao);
	if (tex) tex->bind();
	f->glDrawArrays(GL_TRIANGLES, 0, nverts);
	if (tex) tex->release();
}