textured fishe!!!!1!

This commit is contained in:
papush! 2021-10-06 23:41:17 +02:00
parent ea20c1f0fc
commit 1cbfdb0e3b
5 changed files with 1209 additions and 9 deletions

1171
OBJ_Loader.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,14 @@
#include "fish_painter.h"
#include "OBJ_Loader.h"
#include <QtMath>
#include <locale>
#include <QImage>
const GLfloat vertices[] = {
1, 0, 0,
-1, .5, 0,
-1, -.5, 0,
};
FishPainter::~FishPainter() {
if (texture) delete texture;
}
void FishPainter::create(QOpenGLExtraFunctions *glf) {
@ -25,15 +26,31 @@ void FishPainter::create(QOpenGLExtraFunctions *glf) {
qCritical() << program.log();
}
texture = new QOpenGLTexture(QImage(":/textures/fish.jpg").mirrored());
vao.create();
{QOpenGLVertexArrayObject::Binder bind(&vao);
vbo.create();
vbo.bind();
vbo.allocate(vertices, 9 * sizeof (GLfloat));
std::locale old_locale = std::locale::global(std::locale::classic()); // lmao posix sucks so much
objl::Loader loader;
if (!loader.LoadFile(":/meshes/fish.obj")) {
qCritical() << "Unable to load fish mesh.";
}
std::locale::global(old_locale);
std::vector<objl::Vertex> verts = loader.LoadedMeshes[0].Vertices;
n_verts = verts.size();
vbo.allocate(verts.data(), verts.size() * sizeof (objl::Vertex));
program.bind();
program.setAttributeBuffer("position", GL_FLOAT, 0, 3);
program.setAttributeBuffer("position", GL_FLOAT,
offsetof(objl::Vertex, Position),
3, sizeof (objl::Vertex));
program.setAttributeBuffer("uv", GL_FLOAT,
offsetof(objl::Vertex, TextureCoordinate),
2, sizeof (objl::Vertex));
program.enableAttributeArray("position");
program.enableAttributeArray("uv");
}
}
@ -46,6 +63,7 @@ void FishPainter::paint(QOpenGLExtraFunctions *glf,
{QOpenGLVertexArrayObject::Binder bind(&vao);
program.setUniformValue("projection", projection);
program.setUniformValue("view", view);
texture->bind();
QMatrix4x4 model;
for (const Fish &fish : fishes) {
model.setToIdentity();
@ -59,7 +77,7 @@ void FishPainter::paint(QOpenGLExtraFunctions *glf,
}
}
program.setUniformValue("model", model);
glf->glDrawArrays(GL_TRIANGLES, 0, 3);
glf->glDrawArrays(GL_TRIANGLES, 0, n_verts);
}
}
}

View File

@ -8,14 +8,18 @@
#include <QOpenGLShaderProgram>
#include <QOpenGLExtraFunctions>
#include <QMatrix4x4>
#include <QOpenGLTexture>
class FishPainter {
QOpenGLVertexArrayObject vao;
QOpenGLBuffer vbo;
QOpenGLShaderProgram program;
QOpenGLTexture *texture = nullptr;
size_t n_verts;
public:
~FishPainter();
void create(QOpenGLExtraFunctions *glf);
void paint(QOpenGLExtraFunctions *glf,
const QMatrix4x4 &projection,

View File

@ -1,3 +1,6 @@
varying vec2 fragment_uv;
uniform sampler2D texture;
void main() {
gl_FragColor = vec4(1, 0, .3, 1);
gl_FragColor = texture2D(texture, fragment_uv);
}

View File

@ -1,9 +1,13 @@
attribute vec3 position;
attribute vec2 uv;
varying vec2 fragment_uv;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
void main() {
fragment_uv = uv;
gl_Position = projection * view * model * vec4(position, 1);
}