textures and whatnot i forgot to commit for a while lol

This commit is contained in:
ccolin 2020-12-26 01:21:28 +01:00
parent c0116528a7
commit a6d3f3d779
12 changed files with 59 additions and 18 deletions

BIN
mdl/black.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -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

View File

@ -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

BIN
mdl/ground.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 MiB

BIN
mdl/mdl.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@ -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>

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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);
};

View File

@ -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);
}
}