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 | |
parent | c0116528a7b618d185ac688b31e0fa8752924375 (diff) |
textures and whatnot i forgot to commit for a while lol
-rw-r--r-- | mdl/black.jpg | bin | 0 -> 1176 bytes | |||
-rw-r--r-- | mdl/dji600.blend | bin | 670220 -> 679436 bytes | |||
-rw-r--r-- | mdl/dji600.jpg (renamed from mdl/dji600_top.jpg) | bin | 37269 -> 37269 bytes | |||
-rw-r--r-- | mdl/dji600.mtl | 4 | ||||
-rw-r--r-- | mdl/dji600.obj | 4 | ||||
-rw-r--r-- | mdl/ground.jpg | bin | 0 -> 6777206 bytes | |||
-rw-r--r-- | mdl/mdl.jpg | bin | 0 -> 33039 bytes | |||
-rw-r--r-- | resources.qrc | 5 | ||||
-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 |
12 files changed, 59 insertions, 18 deletions
diff --git a/mdl/black.jpg b/mdl/black.jpg Binary files differnew file mode 100644 index 0000000..f9603f6 --- /dev/null +++ b/mdl/black.jpg diff --git a/mdl/dji600.blend b/mdl/dji600.blend Binary files differindex 3dbbd66..31fbb53 100644 --- a/mdl/dji600.blend +++ b/mdl/dji600.blend diff --git a/mdl/dji600_top.jpg b/mdl/dji600.jpg Binary files differindex 83de2ad..83de2ad 100644 --- a/mdl/dji600_top.jpg +++ b/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 Binary files differnew file mode 100644 index 0000000..67810be --- /dev/null +++ b/mdl/ground.jpg diff --git a/mdl/mdl.jpg b/mdl/mdl.jpg Binary files differnew file mode 100644 index 0000000..fedfc9e --- /dev/null +++ b/mdl/mdl.jpg diff --git a/resources.qrc b/resources.qrc index d7d5020..8facd60 100644 --- a/resources.qrc +++ b/resources.qrc @@ -3,5 +3,10 @@ <qresource> <file>mdl/dji600.obj</file> <file>mdl/dji600.mtl</file> + <file>mdl/dji600.jpg</file> + <file>mdl/test.obj</file> + <file>mdl/test.mtl</file> + <file>mdl/black.jpg</file> + <file>mdl/ground.jpg</file> </qresource> </RCC> 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); } } |