add some sunlight

This commit is contained in:
ccolin 2020-12-26 21:46:12 +01:00
parent a6d3f3d779
commit ef37119f4e
3 changed files with 36 additions and 13 deletions

View File

@ -62,11 +62,17 @@ 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 nx = attrib.normals[3*idx.normal_index+0];
tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1];
tinyobj::real_t nz = attrib.normals[3*idx.normal_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(nx);
verts.append(ny);
verts.append(nz);
verts.append(ts);
verts.append(tt);
// qDebug() << "vert" << vx << vy << vz << "tex" << ts << tt;
@ -143,12 +149,12 @@ 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,
-100, 0, -100, 0, 1, 0, 0, 0,
100, 0, -100, 0, 1, 0, 1, 0,
-100, 0, 100, 0, 1, 0, 0, 1,
100, 0, -100, 0, 1, 0, 1, 0,
-100, 0, 100, 0, 1, 0, 0, 1,
100, 0, 100, 0, 1, 0, 1, 1,
}, ground_tex);
OpenGLWidget::instance->meshes.append(*ground);
OpenGLWidget::instance->doneCurrent();

View File

@ -8,16 +8,18 @@ OpenGLMesh::OpenGLMesh(QVector<float> verts, QOpenGLTexture *tex)
:tex(tex) {
OpenGLWidget::instance->makeCurrent();
QOpenGLFunctions_4_4_Core *glf = OpenGLWidget::instance;
nverts = verts.size() / 5;
nverts = verts.size() / 8;
glf->glGenVertexArrays(1, &vao);
glf->glGenBuffers(1, &vbo);
glf->glBindVertexArray(vao);
glf->glBindBuffer(GL_ARRAY_BUFFER, vbo);
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->glBufferData(GL_ARRAY_BUFFER, nverts * 8 * sizeof (float), verts.data(), GL_STATIC_DRAW);
glf->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof (float), 0);
glf->glEnableVertexAttribArray(0);
glf->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof (float), (void *) (3 * sizeof (float)));
glf->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof (float), (void *) (3 * sizeof (float)));
glf->glEnableVertexAttribArray(1);
glf->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof (float), (void *) (6 * sizeof (float)));
glf->glEnableVertexAttribArray(2);
tex->bind();
glf->glBindVertexArray(0);
OpenGLWidget::instance->doneCurrent();

View File

@ -7,9 +7,12 @@ static const GLchar *vertex_shader_source = R"glsl(
#version 330 core
layout(location = 0) in vec3 in_pos;
layout(location = 1) in vec2 in_uv;
layout(location = 1) in vec3 in_norm;
layout(location = 2) in vec2 in_uv;
out vec3 norm;
out vec2 uv;
out vec3 frag_pos;
uniform mat4 proj;
uniform mat4 view;
@ -17,20 +20,32 @@ uniform mat4 model;
void main() {
gl_Position = proj * view * model * vec4(in_pos, 1.0);
norm = in_norm;
uv = in_uv;
frag_pos = vec3(model * vec4(in_pos, 1.0));
}
)glsl";
static const GLchar *fragment_shader_source = R"glsl(
#version 330 core
in vec3 norm;
in vec2 uv;
in vec3 frag_pos;
out vec4 final_col;
uniform sampler2D tex;
void main() {
final_col = texture(tex, uv);
vec3 light_col = vec3(1, .964, .783);
vec3 ambient = light_col * .2;
vec3 light_dir = normalize(vec3(5, 10, -8));
float diff = max(dot(normalize(norm), light_dir), 0.0);
vec3 diffuse = diff * light_col;
final_col = texture(tex, uv) * vec4(ambient + diffuse, 1);
}
)glsl";
@ -139,7 +154,7 @@ void OpenGLWidget::initializeGL() {
void OpenGLWidget::resizeGL(int w, int h) {
QMatrix4x4 projection;
projection.perspective(FOV, (float) w/h, .1, 1000);
projection.perspective(FOV, (float) w/h, .01, 1000);
glUniformMatrix4fv(proj_attr, 1, GL_FALSE, projection.data());
}