diff --git a/mdl/black.jpg b/mdl/black.jpg new file mode 100644 index 0000000..f9603f6 Binary files /dev/null and b/mdl/black.jpg differ diff --git a/mdl/dji600.blend b/mdl/dji600.blend index 3dbbd66..31fbb53 100644 Binary files a/mdl/dji600.blend and b/mdl/dji600.blend differ diff --git a/mdl/dji600_top.jpg b/mdl/dji600.jpg similarity index 100% rename from mdl/dji600_top.jpg rename to mdl/dji600.jpg diff --git a/mdl/dji600.mtl b/mdl/dji600.mtl index 4fd023f..bc6165d 100644 --- a/mdl/dji600.mtl +++ b/mdl/dji600.mtl @@ -1,4 +1,4 @@ -# Blender MTL File: 'untitled.blend' +# Blender MTL File: 'dji600.blend' # Material Count: 1 newmtl Material @@ -10,4 +10,4 @@ Ke 0.000000 0.000000 0.000000 Ni 1.450000 d 1.000000 illum 2 -map_Kd /tmp/dji600_top.jpg +map_Kd dji600.jpg diff --git a/mdl/dji600.obj b/mdl/dji600.obj index 2fac982..c460dc8 100644 --- a/mdl/dji600.obj +++ b/mdl/dji600.obj @@ -1,7 +1,7 @@ -# Blender v2.90.1 OBJ File: 'untitled.blend' +# Blender v2.90.1 OBJ File: 'dji600.blend' # www.blender.org mtllib dji600.mtl -o Cylinder +o dji600_Cylinder v -0.066940 0.000000 -0.115943 v 0.066940 0.000000 -0.115943 v 0.066940 0.000000 0.115943 diff --git a/mdl/ground.jpg b/mdl/ground.jpg new file mode 100644 index 0000000..67810be Binary files /dev/null and b/mdl/ground.jpg differ diff --git a/mdl/mdl.jpg b/mdl/mdl.jpg new file mode 100644 index 0000000..fedfc9e Binary files /dev/null and b/mdl/mdl.jpg differ diff --git a/resources.qrc b/resources.qrc index d7d5020..8facd60 100644 --- a/resources.qrc +++ b/resources.qrc @@ -3,5 +3,10 @@ mdl/dji600.obj mdl/dji600.mtl + mdl/dji600.jpg + mdl/test.obj + mdl/test.mtl + mdl/black.jpg + mdl/ground.jpg 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(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 -OpenGLMesh::OpenGLMesh(QVector verts) { + +OpenGLMesh::OpenGLMesh(QVector 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 #include -#include +#include struct OpenGLMesh { GLuint vao, vbo; unsigned nverts; QMatrix4x4 mat; + QOpenGLTexture *tex; - OpenGLMesh(QVector verts); + OpenGLMesh(QVector 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); } }