diff options
author | ccolin | 2020-12-26 01:21:28 +0100 |
---|---|---|
committer | ccolin | 2020-12-26 01:21:28 +0100 |
commit | a6d3f3d779b031f8ca70510abe90fade909a8d0d (patch) | |
tree | 10f50d6ad13ea0216779b18443bd2958b9f4d79e /src | |
parent | c0116528a7b618d185ac688b31e0fa8752924375 (diff) |
textures and whatnot i forgot to commit for a while lol
Diffstat (limited to 'src')
-rw-r--r-- | src/drone_controller.cc | 23 | ||||
-rw-r--r-- | src/opengl_mesh.cc | 15 | ||||
-rw-r--r-- | src/opengl_mesh.hh | 5 | ||||
-rw-r--r-- | src/opengl_widget.cc | 21 |
4 files changed, 50 insertions, 14 deletions
diff --git a/src/drone_controller.cc b/src/drone_controller.cc index c625ca0..2c956dd 100644 --- a/src/drone_controller.cc +++ b/src/drone_controller.cc @@ -62,16 +62,23 @@ Drone::Drone() { tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0]; tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1]; tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2]; + tinyobj::real_t ts = attrib.texcoords[2*idx.texcoord_index+0]; + tinyobj::real_t tt = attrib.texcoords[2*idx.texcoord_index+1]; verts.append(vx); verts.append(vy); verts.append(vz); + verts.append(ts); + verts.append(tt); + // qDebug() << "vert" << vx << vy << vz << "tex" << ts << tt; } index_offset += fv; } } - mesh = new OpenGLMesh(verts); - // mesh = new OpenGLMesh(QVector<float>(attrib.vertices.begin(), attrib.vertices.end())); + QOpenGLTexture *texture = new QOpenGLTexture(QImage(":/mdl/dji600.jpg").mirrored()); + // texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); + // texture->setMagnificationFilter(QOpenGLTexture::Linear); + mesh = new OpenGLMesh(verts, texture); mesh_initialized = true; } OpenGLWidget::instance->meshes.append(*mesh); @@ -133,6 +140,18 @@ DroneController::DroneController(const QJsonObject &json) } } + OpenGLWidget::instance->makeCurrent(); + QOpenGLTexture *ground_tex = new QOpenGLTexture(QImage(":/mdl/ground.jpg").mirrored()); + OpenGLMesh *ground = new OpenGLMesh({ + -100, 0, -100, 0, 0, + 100, 0, -100, 1, 0, + -100, 0, 100, 0, 1, + 100, 0, -100, 1, 0, + -100, 0, 100, 0, 1, + 100, 0, 100, 1, 1, + }, ground_tex); + OpenGLWidget::instance->meshes.append(*ground); + OpenGLWidget::instance->doneCurrent(); connect(&timer, &QTimer::timeout, this, &DroneController::step); } diff --git a/src/opengl_mesh.cc b/src/opengl_mesh.cc index cca91da..1d7d1b7 100644 --- a/src/opengl_mesh.cc +++ b/src/opengl_mesh.cc @@ -1,19 +1,24 @@ #include "opengl_mesh.hh" - #include "opengl_widget.hh" +#include <QOpenGLFunctions> + -OpenGLMesh::OpenGLMesh(QVector<float> verts) { +OpenGLMesh::OpenGLMesh(QVector<float> verts, QOpenGLTexture *tex) + :tex(tex) { OpenGLWidget::instance->makeCurrent(); QOpenGLFunctions_4_4_Core *glf = OpenGLWidget::instance; - nverts = verts.size() / 3; + nverts = verts.size() / 5; glf->glGenVertexArrays(1, &vao); glf->glGenBuffers(1, &vbo); glf->glBindVertexArray(vao); glf->glBindBuffer(GL_ARRAY_BUFFER, vbo); - glf->glBufferData(GL_ARRAY_BUFFER, nverts * 3 * sizeof (GLfloat), verts.data(), GL_STATIC_DRAW); - glf->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glf->glBufferData(GL_ARRAY_BUFFER, nverts * 5 * sizeof (float), verts.data(), GL_STATIC_DRAW); + glf->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof (float), 0); glf->glEnableVertexAttribArray(0); + glf->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof (float), (void *) (3 * sizeof (float))); + glf->glEnableVertexAttribArray(1); + tex->bind(); glf->glBindVertexArray(0); OpenGLWidget::instance->doneCurrent(); } diff --git a/src/opengl_mesh.hh b/src/opengl_mesh.hh index e3b8ad2..5038816 100644 --- a/src/opengl_mesh.hh +++ b/src/opengl_mesh.hh @@ -3,15 +3,16 @@ #include <QMatrix4x4> #include <QVector> -#include <QOpenGLFunctions> +#include <QOpenGLTexture> struct OpenGLMesh { GLuint vao, vbo; unsigned nverts; QMatrix4x4 mat; + QOpenGLTexture *tex; - OpenGLMesh(QVector<float> verts); + OpenGLMesh(QVector<float> verts, QOpenGLTexture *tex); }; diff --git a/src/opengl_widget.cc b/src/opengl_widget.cc index f10a8c6..ca216b3 100644 --- a/src/opengl_widget.cc +++ b/src/opengl_widget.cc @@ -6,24 +6,31 @@ static const GLchar *vertex_shader_source = R"glsl( #version 330 core -layout(location = 0) in vec3 pos; +layout(location = 0) in vec3 in_pos; +layout(location = 1) in vec2 in_uv; + +out vec2 uv; uniform mat4 proj; uniform mat4 view; uniform mat4 model; void main() { - gl_Position = proj * view * model * vec4(pos, 1.0); + gl_Position = proj * view * model * vec4(in_pos, 1.0); + uv = in_uv; } )glsl"; static const GLchar *fragment_shader_source = R"glsl( #version 330 core +in vec2 uv; out vec4 final_col; +uniform sampler2D tex; + void main() { - final_col = vec4(0, 0, 0, 1); + final_col = texture(tex, uv); } )glsl"; @@ -91,7 +98,7 @@ void OpenGLWidget::initializeGL() { GLuint shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); - glBindFragDataLocation(shader_program, 0, "out_color"); + glBindFragDataLocation(shader_program, 0, "final_col"); glLinkProgram(shader_program); glGetProgramiv(shader_program, GL_LINK_STATUS, &status); if (status != GL_TRUE) { @@ -119,6 +126,8 @@ void OpenGLWidget::initializeGL() { trans.translate(0, 0, -5); glUniformMatrix4fv(view_attr, 1, GL_FALSE, trans.data()); + glUniform1i(glGetUniformLocation(shader_program, "tex"), 0); + glClearColor(1, 1, 1, 0); glEnable(GL_DEPTH_TEST); @@ -130,7 +139,7 @@ void OpenGLWidget::initializeGL() { void OpenGLWidget::resizeGL(int w, int h) { QMatrix4x4 projection; - projection.perspective(FOV, (float) w/h, .1, 100); + projection.perspective(FOV, (float) w/h, .1, 1000); glUniformMatrix4fv(proj_attr, 1, GL_FALSE, projection.data()); } @@ -141,9 +150,11 @@ void OpenGLWidget::paintGL() { trans.translate(0, 0, -cam_dist); QMatrix4x4 view = trans * rot; glUniformMatrix4fv(view_attr, 1, GL_FALSE, view.data()); + glActiveTexture(GL_TEXTURE0); for (const OpenGLMesh &mesh : meshes) { glUniformMatrix4fv(model_attr, 1, GL_FALSE, mesh.mat.data()); glBindVertexArray(mesh.vao); + mesh.tex->bind(); glDrawArrays(GL_TRIANGLES, 0, mesh.nverts); } } |