2020-01-17 12:05:23 -07:00
|
|
|
#include "ui.hpp"
|
2020-05-06 09:38:26 -06:00
|
|
|
#include <assert.h>
|
|
|
|
#include <map>
|
2020-05-26 14:27:01 -06:00
|
|
|
#include <cmath>
|
2020-08-20 09:16:44 -06:00
|
|
|
#include <iostream>
|
2020-01-17 12:05:23 -07:00
|
|
|
#include "common/util.h"
|
2020-11-11 21:31:46 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
#define NANOVG_GLES3_IMPLEMENTATION
|
|
|
|
#include "nanovg_gl.h"
|
|
|
|
#include "nanovg_gl_utils.h"
|
|
|
|
|
|
|
|
extern "C"{
|
|
|
|
#include "common/glutil.h"
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:16:44 -06:00
|
|
|
#include "paint.hpp"
|
|
|
|
#include "sidebar.hpp"
|
|
|
|
|
2020-10-20 04:14:19 -06:00
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
// TODO: this is also hardcoded in common/transformations/camera.py
|
2020-09-23 15:16:55 -06:00
|
|
|
// TODO: choose based on frame input size
|
2020-10-08 20:29:56 -06:00
|
|
|
#ifdef QCOM2
|
2020-10-20 04:14:19 -06:00
|
|
|
const float zoom = 1.5;
|
2020-10-08 20:29:56 -06:00
|
|
|
const mat3 intrinsic_matrix = (mat3){{
|
|
|
|
2648.0, 0.0, 1928.0/2,
|
|
|
|
0.0, 2648.0, 1208.0/2,
|
|
|
|
0.0, 0.0, 1.0
|
|
|
|
}};
|
2020-09-23 15:16:55 -06:00
|
|
|
#else
|
2020-10-20 04:14:19 -06:00
|
|
|
const float zoom = 2.35;
|
2020-01-17 12:05:23 -07:00
|
|
|
const mat3 intrinsic_matrix = (mat3){{
|
2020-10-20 04:14:19 -06:00
|
|
|
910., 0., 1164.0/2,
|
|
|
|
0., 910., 874.0/2,
|
2020-01-17 12:05:23 -07:00
|
|
|
0., 0., 1.
|
|
|
|
}};
|
2020-09-23 15:16:55 -06:00
|
|
|
#endif
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
// Projects a point in car to space to the corresponding point in full frame
|
|
|
|
// image space.
|
2020-11-11 21:31:46 -07:00
|
|
|
bool car_space_to_full_frame(const UIState *s, float in_x, float in_y, float in_z, float *out_x, float *out_y) {
|
|
|
|
const vec4 car_space_projective = (vec4){{in_x, in_y, in_z, 1.}};
|
2020-01-17 12:05:23 -07:00
|
|
|
// We'll call the car space point p.
|
|
|
|
// First project into normalized image coordinates with the extrinsics matrix.
|
2020-11-11 21:31:46 -07:00
|
|
|
const vec4 Ep4 = matvecmul(s->scene.extrinsic_matrix, car_space_projective);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
// The last entry is zero because of how we store E (to use matvecmul).
|
|
|
|
const vec3 Ep = {{Ep4.v[0], Ep4.v[1], Ep4.v[2]}};
|
|
|
|
const vec3 KEp = matvecmul3(intrinsic_matrix, Ep);
|
|
|
|
|
|
|
|
// Project.
|
2020-11-11 21:31:46 -07:00
|
|
|
*out_x = KEp.v[0] / KEp.v[2];
|
|
|
|
*out_y = KEp.v[1] / KEp.v[2];
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-11-12 12:30:04 -07:00
|
|
|
return *out_x >= 0 && *out_x <= s->fb_w && *out_y >= 0 && *out_y <= s->fb_h;
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
|
2020-05-02 12:27:03 -06:00
|
|
|
static void ui_draw_text(NVGcontext *vg, float x, float y, const char* string, float size, NVGcolor color, int font){
|
|
|
|
nvgFontFaceId(vg, font);
|
|
|
|
nvgFontSize(vg, size);
|
|
|
|
nvgFillColor(vg, color);
|
|
|
|
nvgText(vg, x, y, string, NULL);
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
static void draw_chevron(UIState *s, float x_in, float y_in, float sz,
|
|
|
|
NVGcolor fillColor, NVGcolor glowColor) {
|
2020-11-11 21:31:46 -07:00
|
|
|
float x, y;
|
|
|
|
if (!car_space_to_full_frame(s, x_in, y_in, 0.0, &x, &y)) {
|
2020-04-17 16:59:43 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
sz *= 30;
|
|
|
|
sz /= (x_in / 3 + 30);
|
|
|
|
if (sz > 30) sz = 30;
|
|
|
|
if (sz < 15) sz = 15;
|
2020-05-12 19:17:04 -06:00
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
// glow
|
|
|
|
float g_xo = sz/5;
|
|
|
|
float g_yo = sz/10;
|
2020-04-17 16:59:43 -06:00
|
|
|
nvgBeginPath(s->vg);
|
|
|
|
nvgMoveTo(s->vg, x+(sz*1.35)+g_xo, y+sz+g_yo);
|
|
|
|
nvgLineTo(s->vg, x, y-g_xo);
|
|
|
|
nvgLineTo(s->vg, x-(sz*1.35)-g_xo, y+sz+g_yo);
|
|
|
|
nvgClosePath(s->vg);
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgFillColor(s->vg, glowColor);
|
|
|
|
nvgFill(s->vg);
|
|
|
|
|
|
|
|
// chevron
|
|
|
|
nvgBeginPath(s->vg);
|
2020-04-17 16:59:43 -06:00
|
|
|
nvgMoveTo(s->vg, x+(sz*1.25), y+sz);
|
|
|
|
nvgLineTo(s->vg, x, y);
|
|
|
|
nvgLineTo(s->vg, x-(sz*1.25), y+sz);
|
|
|
|
nvgClosePath(s->vg);
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgFillColor(s->vg, fillColor);
|
|
|
|
nvgFill(s->vg);
|
|
|
|
}
|
|
|
|
|
2020-05-01 20:16:17 -06:00
|
|
|
static void ui_draw_circle_image(NVGcontext *vg, float x, float y, int size, int image, NVGcolor color, float img_alpha, int img_y = 0) {
|
|
|
|
const int img_size = size * 1.5;
|
|
|
|
nvgBeginPath(vg);
|
|
|
|
nvgCircle(vg, x, y + (bdr_s * 1.5), size);
|
|
|
|
nvgFillColor(vg, color);
|
|
|
|
nvgFill(vg);
|
|
|
|
ui_draw_image(vg, x - (img_size / 2), img_y ? img_y : y - (size / 4), img_size, img_size, image, img_alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_draw_circle_image(NVGcontext *vg, float x, float y, int size, int image, bool active) {
|
|
|
|
float bg_alpha = active ? 0.3f : 0.1f;
|
|
|
|
float img_alpha = active ? 1.0f : 0.15f;
|
|
|
|
ui_draw_circle_image(vg, x, y, size, image, nvgRGBA(0, 0, 0, (255 * bg_alpha)), img_alpha);
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:33:45 -06:00
|
|
|
static void draw_lead(UIState *s, const cereal::RadarState::LeadData::Reader &lead){
|
2020-04-17 16:59:43 -06:00
|
|
|
// Draw lead car indicator
|
|
|
|
float fillAlpha = 0;
|
|
|
|
float speedBuff = 10.;
|
|
|
|
float leadBuff = 40.;
|
2020-06-05 16:33:45 -06:00
|
|
|
float d_rel = lead.getDRel();
|
|
|
|
float v_rel = lead.getVRel();
|
2020-04-17 16:59:43 -06:00
|
|
|
if (d_rel < leadBuff) {
|
|
|
|
fillAlpha = 255*(1.0-(d_rel/leadBuff));
|
|
|
|
if (v_rel < 0) {
|
|
|
|
fillAlpha += 255*(-1*(v_rel/speedBuff));
|
2020-02-20 15:27:11 -07:00
|
|
|
}
|
2020-04-17 16:59:43 -06:00
|
|
|
fillAlpha = (int)(fmin(fillAlpha, 255));
|
|
|
|
}
|
2020-06-05 16:33:45 -06:00
|
|
|
draw_chevron(s, d_rel, lead.getYRel(), 25, nvgRGBA(201, 34, 49, fillAlpha), COLOR_YELLOW);
|
2020-02-20 15:27:11 -07:00
|
|
|
}
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
static void ui_draw_line(UIState *s, const vertex_data *v, const int cnt, NVGcolor *color, NVGpaint *paint) {
|
|
|
|
if (cnt == 0) return;
|
2020-07-03 20:42:26 -06:00
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgBeginPath(s->vg);
|
2020-11-12 12:30:04 -07:00
|
|
|
nvgMoveTo(s->vg, v[0].x, v[0].y);
|
2020-11-11 21:31:46 -07:00
|
|
|
for (int i = 1; i < cnt; i++) {
|
2020-11-12 12:30:04 -07:00
|
|
|
nvgLineTo(s->vg, v[i].x, v[i].y);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
nvgClosePath(s->vg);
|
2020-11-11 21:31:46 -07:00
|
|
|
if (color) {
|
|
|
|
nvgFillColor(s->vg, *color);
|
|
|
|
} else if (paint) {
|
|
|
|
nvgFillPaint(s->vg, *paint);
|
|
|
|
}
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgFill(s->vg);
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
static void update_track_data(UIState *s, const cereal::ModelDataV2::XYZTData::Reader &line, track_vertices_data *pvd) {
|
2020-01-17 12:05:23 -07:00
|
|
|
const UIScene *scene = &s->scene;
|
2020-11-11 21:31:46 -07:00
|
|
|
const float off = 0.5;
|
2020-11-17 11:19:12 -07:00
|
|
|
int max_idx = 0;
|
2020-11-11 21:31:46 -07:00
|
|
|
float lead_d;
|
|
|
|
if(s->sm->updated("radarState")) {
|
|
|
|
lead_d = scene->lead_data[0].getDRel()*2.;
|
|
|
|
} else {
|
|
|
|
lead_d = MAX_DRAW_DISTANCE;
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-11 21:31:46 -07:00
|
|
|
float path_length = (lead_d>0.)?lead_d-fmin(lead_d*0.35, 10.):MAX_DRAW_DISTANCE;
|
|
|
|
path_length = fmin(path_length, scene->max_distance);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
vertex_data *v = &pvd->v[0];
|
|
|
|
for (int i = 0; line.getX()[i] <= path_length and i < TRAJECTORY_SIZE; i++) {
|
|
|
|
v += car_space_to_full_frame(s, line.getX()[i], -line.getY()[i] - off, -line.getZ()[i], &v->x, &v->y);
|
|
|
|
max_idx = i;
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-11 21:31:46 -07:00
|
|
|
for (int i = max_idx; i >= 0; i--) {
|
|
|
|
v += car_space_to_full_frame(s, line.getX()[i], -line.getY()[i] + off, -line.getZ()[i], &v->x, &v->y);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-11 21:31:46 -07:00
|
|
|
pvd->cnt = v - pvd->v;
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
static void ui_draw_track(UIState *s, track_vertices_data *pvd) {
|
|
|
|
NVGpaint track_bg = nvgLinearGradient(s->vg, s->fb_w, s->fb_h, s->fb_w, s->fb_h * .4,
|
|
|
|
COLOR_WHITE, COLOR_WHITE_ALPHA(0));
|
|
|
|
ui_draw_line(s, &pvd->v[0], pvd->cnt, nullptr, &track_bg);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_frame(UIState *s) {
|
2020-09-03 17:32:55 -06:00
|
|
|
mat4 *out_mat;
|
2020-01-17 12:05:23 -07:00
|
|
|
if (s->scene.frontview) {
|
|
|
|
glBindVertexArray(s->frame_vao[1]);
|
|
|
|
out_mat = &s->front_frame_mat;
|
|
|
|
} else {
|
2020-09-03 17:32:55 -06:00
|
|
|
glBindVertexArray(s->frame_vao[0]);
|
2020-01-17 12:05:23 -07:00
|
|
|
out_mat = &s->rear_frame_mat;
|
|
|
|
}
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2020-09-03 17:32:55 -06:00
|
|
|
|
|
|
|
if (s->stream.last_idx >= 0) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, s->frame_texs[s->stream.last_idx]);
|
2020-09-08 08:33:27 -06:00
|
|
|
#ifndef QCOM
|
2020-09-03 17:32:55 -06:00
|
|
|
// this is handled in ion on QCOM
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->stream.bufs_info.width, s->stream.bufs_info.height,
|
|
|
|
0, GL_RGB, GL_UNSIGNED_BYTE, s->priv_hnds[s->stream.last_idx]);
|
2020-08-31 07:02:01 -06:00
|
|
|
#endif
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
glUseProgram(s->frame_program);
|
|
|
|
glUniform1i(s->frame_texture_loc, 0);
|
|
|
|
glUniformMatrix4fv(s->frame_transform_loc, 1, GL_TRUE, out_mat->v);
|
|
|
|
|
|
|
|
assert(glGetError() == GL_NO_ERROR);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, (const void*)0);
|
|
|
|
glDisableVertexAttribArray(0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
static void update_line_data(UIState *s, const cereal::ModelDataV2::XYZTData::Reader &line, float off, line_vertices_data *pvd, float max_distance) {
|
|
|
|
// TODO check that this doesn't overflow max vertex buffer
|
|
|
|
int max_idx;
|
|
|
|
vertex_data *v = &pvd->v[0];
|
|
|
|
for (int i = 0; ((i < TRAJECTORY_SIZE) and (line.getX()[i] < fmax(MIN_DRAW_DISTANCE, max_distance))); i++) {
|
|
|
|
v += car_space_to_full_frame(s, line.getX()[i], -line.getY()[i] - off, -line.getZ()[i] + 1.22, &v->x, &v->y);
|
|
|
|
max_idx = i;
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-11 21:31:46 -07:00
|
|
|
for (int i = max_idx - 1; i > 0; i--) {
|
|
|
|
v += car_space_to_full_frame(s, line.getX()[i], -line.getY()[i] + off, -line.getZ()[i] + 1.22, &v->x, &v->y);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-11 21:31:46 -07:00
|
|
|
pvd->cnt = v - pvd->v;
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
static void ui_draw_vision_lane_lines(UIState *s) {
|
2020-01-17 12:05:23 -07:00
|
|
|
const UIScene *scene = &s->scene;
|
2020-11-11 21:31:46 -07:00
|
|
|
// paint lanelines
|
|
|
|
line_vertices_data *pvd_ll = &s->lane_line_vertices[0];
|
|
|
|
for (int ll_idx = 0; ll_idx < 4; ll_idx++) {
|
|
|
|
if(s->sm->updated("modelV2")) {
|
|
|
|
update_line_data(s, scene->model.getLaneLines()[ll_idx], 0.025*scene->model.getLaneLineProbs()[ll_idx], pvd_ll + ll_idx, scene->max_distance);
|
|
|
|
}
|
|
|
|
NVGcolor color = nvgRGBAf(1.0, 1.0, 1.0, scene->lane_line_probs[ll_idx]);
|
|
|
|
ui_draw_line(s, (pvd_ll + ll_idx)->v, (pvd_ll + ll_idx)->cnt, &color, nullptr);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-27 15:25:52 -07:00
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
// paint road edges
|
|
|
|
line_vertices_data *pvd_re = &s->road_edge_vertices[0];
|
|
|
|
for (int re_idx = 0; re_idx < 2; re_idx++) {
|
|
|
|
if(s->sm->updated("modelV2")) {
|
|
|
|
update_line_data(s, scene->model.getRoadEdges()[re_idx], 0.025, pvd_re + re_idx, scene->max_distance);
|
|
|
|
}
|
|
|
|
NVGcolor color = nvgRGBAf(1.0, 0.0, 0.0, std::clamp<float>(1.0-scene->road_edge_stds[re_idx], 0.0, 1.0));
|
|
|
|
ui_draw_line(s, (pvd_re + re_idx)->v, (pvd_re + re_idx)->cnt, &color, nullptr);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-27 15:25:52 -07:00
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
// paint path
|
|
|
|
if(s->sm->updated("modelV2")) {
|
|
|
|
update_track_data(s, scene->model.getPosition(), &s->track_vertices);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-11-11 21:31:46 -07:00
|
|
|
ui_draw_track(s, &s->track_vertices);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw all world space objects.
|
|
|
|
static void ui_draw_world(UIState *s) {
|
|
|
|
const UIScene *scene = &s->scene;
|
2020-10-20 04:14:19 -06:00
|
|
|
|
2020-04-17 16:59:43 -06:00
|
|
|
nvgSave(s->vg);
|
2020-11-11 03:12:55 -07:00
|
|
|
|
|
|
|
// Don't draw on top of sidebar
|
|
|
|
nvgScissor(s->vg, scene->viz_rect.x, scene->viz_rect.y, scene->viz_rect.w, scene->viz_rect.h);
|
2020-05-12 19:17:04 -06:00
|
|
|
|
2020-10-20 04:14:19 -06:00
|
|
|
// Apply transformation such that video pixel coordinates match video
|
|
|
|
// 1) Put (0, 0) in the middle of the video
|
|
|
|
nvgTranslate(s->vg, s->video_rect.x + s->video_rect.w / 2, s->video_rect.y + s->video_rect.h / 2);
|
2020-08-20 09:16:44 -06:00
|
|
|
|
2020-10-20 04:14:19 -06:00
|
|
|
// 2) Apply same scaling as video
|
|
|
|
nvgScale(s->vg, zoom, zoom);
|
2020-08-20 09:16:44 -06:00
|
|
|
|
2020-10-20 04:14:19 -06:00
|
|
|
// 3) Put (0, 0) in top left corner of video
|
|
|
|
nvgTranslate(s->vg, -intrinsic_matrix.v[2], -intrinsic_matrix.v[5]);
|
2020-04-17 16:59:43 -06:00
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
// Draw lane edges and vision/mpc tracks
|
2020-11-11 21:31:46 -07:00
|
|
|
ui_draw_vision_lane_lines(s);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-07-23 14:28:58 -06:00
|
|
|
// Draw lead indicators if openpilot is handling longitudinal
|
|
|
|
if (s->longitudinal_control) {
|
|
|
|
if (scene->lead_data[0].getStatus()) {
|
|
|
|
draw_lead(s, scene->lead_data[0]);
|
|
|
|
}
|
|
|
|
if (scene->lead_data[1].getStatus() && (std::abs(scene->lead_data[0].getDRel() - scene->lead_data[1].getDRel()) > 3.0)) {
|
|
|
|
draw_lead(s, scene->lead_data[1]);
|
|
|
|
}
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
2020-04-17 16:59:43 -06:00
|
|
|
nvgRestore(s->vg);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_draw_vision_maxspeed(UIState *s) {
|
|
|
|
char maxspeed_str[32];
|
2020-06-05 16:33:45 -06:00
|
|
|
float maxspeed = s->scene.controls_state.getVCruise();
|
2020-01-17 12:05:23 -07:00
|
|
|
int maxspeed_calc = maxspeed * 0.6225 + 0.5;
|
|
|
|
if (s->is_metric) {
|
|
|
|
maxspeed_calc = maxspeed + 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_cruise_set = (maxspeed != 0 && maxspeed != SET_SPEED_NA);
|
|
|
|
|
|
|
|
int viz_maxspeed_w = 184;
|
|
|
|
int viz_maxspeed_h = 202;
|
2020-09-12 19:35:36 -06:00
|
|
|
int viz_maxspeed_x = s->scene.viz_rect.x + (bdr_s*2);
|
|
|
|
int viz_maxspeed_y = s->scene.viz_rect.y + (bdr_s*1.5);
|
2020-01-17 12:05:23 -07:00
|
|
|
int viz_maxspeed_xo = 180;
|
|
|
|
|
|
|
|
viz_maxspeed_xo = 0;
|
|
|
|
|
|
|
|
// Draw Background
|
2020-09-03 17:32:55 -06:00
|
|
|
ui_draw_rect(s->vg, viz_maxspeed_x, viz_maxspeed_y, viz_maxspeed_w, viz_maxspeed_h, COLOR_BLACK_ALPHA(100), 30);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
// Draw Border
|
2020-05-02 12:27:03 -06:00
|
|
|
NVGcolor color = COLOR_WHITE_ALPHA(100);
|
|
|
|
ui_draw_rect(s->vg, viz_maxspeed_x, viz_maxspeed_y, viz_maxspeed_w, viz_maxspeed_h, color, 20, 10);
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE);
|
2020-04-28 14:44:57 -06:00
|
|
|
const int text_x = viz_maxspeed_x + (viz_maxspeed_xo / 2) + (viz_maxspeed_w / 2);
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_text(s->vg, text_x, 148, "MAX", 26 * 2.5, COLOR_WHITE_ALPHA(is_cruise_set ? 200 : 100), s->font_sans_regular);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
if (is_cruise_set) {
|
|
|
|
snprintf(maxspeed_str, sizeof(maxspeed_str), "%d", maxspeed_calc);
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_text(s->vg, text_x, 242, maxspeed_str, 48 * 2.5, COLOR_WHITE, s->font_sans_bold);
|
2020-01-17 12:05:23 -07:00
|
|
|
} else {
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_text(s->vg, text_x, 242, "N/A", 42 * 2.5, COLOR_WHITE_ALPHA(100), s->font_sans_semibold);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_draw_vision_speed(UIState *s) {
|
2020-09-12 19:35:36 -06:00
|
|
|
const Rect &viz_rect = s->scene.viz_rect;
|
2020-06-05 16:33:45 -06:00
|
|
|
float v_ego = s->scene.controls_state.getVEgo();
|
|
|
|
float speed = v_ego * 2.2369363 + 0.5;
|
2020-05-02 12:27:03 -06:00
|
|
|
if (s->is_metric){
|
2020-06-05 16:33:45 -06:00
|
|
|
speed = v_ego * 3.6 + 0.5;
|
2020-05-02 12:27:03 -06:00
|
|
|
}
|
2020-01-17 12:05:23 -07:00
|
|
|
const int viz_speed_w = 280;
|
2020-09-12 19:35:36 -06:00
|
|
|
const int viz_speed_x = viz_rect.centerX() - viz_speed_w/2;
|
2020-01-17 12:05:23 -07:00
|
|
|
char speed_str[32];
|
|
|
|
|
|
|
|
nvgBeginPath(s->vg);
|
2020-09-12 19:35:36 -06:00
|
|
|
nvgRect(s->vg, viz_speed_x, viz_rect.y, viz_speed_w, header_h);
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE);
|
|
|
|
|
2020-05-02 12:27:03 -06:00
|
|
|
snprintf(speed_str, sizeof(speed_str), "%d", (int)speed);
|
2020-09-12 19:35:36 -06:00
|
|
|
ui_draw_text(s->vg, viz_rect.centerX(), 240, speed_str, 96*2.5, COLOR_WHITE, s->font_sans_bold);
|
|
|
|
ui_draw_text(s->vg, viz_rect.centerX(), 320, s->is_metric?"km/h":"mph", 36*2.5, COLOR_WHITE_ALPHA(200), s->font_sans_regular);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_draw_vision_event(UIState *s) {
|
|
|
|
const int viz_event_w = 220;
|
2020-09-12 19:35:36 -06:00
|
|
|
const int viz_event_x = s->scene.viz_rect.right() - (viz_event_w + bdr_s*2);
|
|
|
|
const int viz_event_y = s->scene.viz_rect.y + (bdr_s*1.5);
|
2020-06-05 16:33:45 -06:00
|
|
|
if (s->scene.controls_state.getDecelForModel() && s->scene.controls_state.getEnabled()) {
|
2020-01-17 12:05:23 -07:00
|
|
|
// draw winding road sign
|
|
|
|
const int img_turn_size = 160*1.5;
|
2020-04-13 16:38:45 -06:00
|
|
|
ui_draw_image(s->vg, viz_event_x - (img_turn_size / 4), viz_event_y + bdr_s - 25, img_turn_size, img_turn_size, s->img_turn, 1.0f);
|
2020-10-08 20:29:56 -06:00
|
|
|
} else if (s->scene.controls_state.getEngageable()) {
|
2020-01-17 12:05:23 -07:00
|
|
|
// draw steering wheel
|
|
|
|
const int bg_wheel_size = 96;
|
|
|
|
const int bg_wheel_x = viz_event_x + (viz_event_w-bg_wheel_size);
|
|
|
|
const int bg_wheel_y = viz_event_y + (bg_wheel_size/2);
|
2020-10-08 20:29:56 -06:00
|
|
|
const NVGcolor color = bg_colors[s->status];
|
2020-05-12 19:17:04 -06:00
|
|
|
|
2020-10-08 20:29:56 -06:00
|
|
|
ui_draw_circle_image(s->vg, bg_wheel_x, bg_wheel_y, bg_wheel_size, s->img_wheel, color, 1.0f, bg_wheel_y - 25);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_draw_vision_face(UIState *s) {
|
|
|
|
const int face_size = 96;
|
2020-09-12 19:35:36 -06:00
|
|
|
const int face_x = (s->scene.viz_rect.x + face_size + (bdr_s * 2));
|
|
|
|
const int face_y = (s->scene.viz_rect.bottom() - footer_h + ((footer_h - face_size) / 2));
|
2020-08-01 18:26:35 -06:00
|
|
|
ui_draw_circle_image(s->vg, face_x, face_y, face_size, s->img_face, s->scene.dmonitoring_state.getFaceDetected());
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:48:44 -06:00
|
|
|
static void ui_draw_driver_view(UIState *s) {
|
|
|
|
const UIScene *scene = &s->scene;
|
|
|
|
s->scene.uilayout_sidebarcollapsed = true;
|
2020-09-12 19:35:36 -06:00
|
|
|
const Rect &viz_rect = s->scene.viz_rect;
|
|
|
|
const int ff_xoffset = 32;
|
|
|
|
const int frame_x = viz_rect.x;
|
|
|
|
const int frame_w = viz_rect.w;
|
|
|
|
const int valid_frame_w = 4 * viz_rect.h / 3;
|
|
|
|
const int box_y = viz_rect.y;
|
|
|
|
const int box_h = viz_rect.h;
|
2020-04-15 17:48:44 -06:00
|
|
|
const int valid_frame_x = frame_x + (frame_w - valid_frame_w) / 2 + ff_xoffset;
|
|
|
|
|
|
|
|
// blackout
|
2020-08-01 18:26:35 -06:00
|
|
|
NVGpaint gradient = nvgLinearGradient(s->vg, scene->is_rhd ? valid_frame_x : (valid_frame_x + valid_frame_w),
|
|
|
|
box_y,
|
|
|
|
scene->is_rhd ? (valid_frame_w - box_h / 2) : (valid_frame_x + box_h / 2), box_y,
|
|
|
|
COLOR_BLACK, COLOR_BLACK_ALPHA(0));
|
|
|
|
ui_draw_rect(s->vg, scene->is_rhd ? valid_frame_x : (valid_frame_x + box_h / 2), box_y, valid_frame_w - box_h / 2, box_h, gradient);
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_rect(s->vg, scene->is_rhd ? valid_frame_x : valid_frame_x + box_h / 2, box_y, valid_frame_w - box_h / 2, box_h, COLOR_BLACK_ALPHA(144));
|
2020-04-15 17:48:44 -06:00
|
|
|
|
|
|
|
// borders
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_rect(s->vg, frame_x, box_y, valid_frame_x - frame_x, box_h, nvgRGBA(23, 51, 73, 255));
|
|
|
|
ui_draw_rect(s->vg, valid_frame_x + valid_frame_w, box_y, frame_w - valid_frame_w - (valid_frame_x - frame_x), box_h, nvgRGBA(23, 51, 73, 255));
|
2020-04-15 17:48:44 -06:00
|
|
|
|
|
|
|
// draw face box
|
2020-08-01 18:26:35 -06:00
|
|
|
if (scene->dmonitoring_state.getFaceDetected()) {
|
2020-06-05 16:33:45 -06:00
|
|
|
auto fxy_list = scene->driver_state.getFacePosition();
|
2020-06-10 16:15:51 -06:00
|
|
|
const float face_x = fxy_list[0];
|
|
|
|
const float face_y = fxy_list[1];
|
|
|
|
float fbox_x;
|
|
|
|
float fbox_y = box_y + (face_y + 0.5) * box_h - 0.5 * 0.6 * box_h / 2;;
|
2020-04-15 17:48:44 -06:00
|
|
|
if (!scene->is_rhd) {
|
2020-06-05 16:33:45 -06:00
|
|
|
fbox_x = valid_frame_x + (1 - (face_x + 0.5)) * (box_h / 2) - 0.5 * 0.6 * box_h / 2;
|
2020-04-15 17:48:44 -06:00
|
|
|
} else {
|
2020-06-05 16:33:45 -06:00
|
|
|
fbox_x = valid_frame_x + valid_frame_w - box_h / 2 + (face_x + 0.5) * (box_h / 2) - 0.5 * 0.6 * box_h / 2;
|
2020-04-15 17:48:44 -06:00
|
|
|
}
|
2020-08-01 18:26:35 -06:00
|
|
|
|
2020-06-05 16:33:45 -06:00
|
|
|
if (std::abs(face_x) <= 0.35 && std::abs(face_y) <= 0.4) {
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_rect(s->vg, fbox_x, fbox_y, 0.6 * box_h / 2, 0.6 * box_h / 2,
|
2020-06-05 16:33:45 -06:00
|
|
|
nvgRGBAf(1.0, 1.0, 1.0, 0.8 - ((std::abs(face_x) > std::abs(face_y) ? std::abs(face_x) : std::abs(face_y))) * 0.6 / 0.375),
|
2020-05-02 12:27:03 -06:00
|
|
|
35, 10);
|
2020-04-15 17:48:44 -06:00
|
|
|
} else {
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_rect(s->vg, fbox_x, fbox_y, 0.6 * box_h / 2, 0.6 * box_h / 2, nvgRGBAf(1.0, 1.0, 1.0, 0.2), 35, 10);
|
2020-04-15 17:48:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw face icon
|
|
|
|
const int face_size = 85;
|
2020-06-05 16:33:45 -06:00
|
|
|
const int x = (valid_frame_x + face_size + (bdr_s * 2)) + (scene->is_rhd ? valid_frame_w - box_h / 2:0);
|
|
|
|
const int y = (box_y + box_h - face_size - bdr_s - (bdr_s * 1.5));
|
2020-08-01 18:26:35 -06:00
|
|
|
ui_draw_circle_image(s->vg, x, y, face_size, s->img_face, scene->dmonitoring_state.getFaceDetected());
|
2020-04-15 17:48:44 -06:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
static void ui_draw_vision_header(UIState *s) {
|
2020-09-12 19:35:36 -06:00
|
|
|
const Rect &viz_rect = s->scene.viz_rect;
|
|
|
|
NVGpaint gradient = nvgLinearGradient(s->vg, viz_rect.x,
|
|
|
|
viz_rect.y+(header_h-(header_h/2.5)),
|
|
|
|
viz_rect.x, viz_rect.y+header_h,
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgRGBAf(0,0,0,0.45), nvgRGBAf(0,0,0,0));
|
2020-09-03 17:32:55 -06:00
|
|
|
|
2020-09-12 19:35:36 -06:00
|
|
|
ui_draw_rect(s->vg, viz_rect.x, viz_rect.y, viz_rect.w, header_h, gradient);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
ui_draw_vision_maxspeed(s);
|
|
|
|
ui_draw_vision_speed(s);
|
|
|
|
ui_draw_vision_event(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_draw_vision_footer(UIState *s) {
|
|
|
|
ui_draw_vision_face(s);
|
|
|
|
}
|
|
|
|
|
2020-10-08 20:29:56 -06:00
|
|
|
void ui_draw_vision_alert(UIState *s, cereal::ControlsState::AlertSize va_size, UIStatus va_color,
|
2020-01-17 12:05:23 -07:00
|
|
|
const char* va_text1, const char* va_text2) {
|
2020-05-06 09:38:26 -06:00
|
|
|
static std::map<cereal::ControlsState::AlertSize, const int> alert_size_map = {
|
|
|
|
{cereal::ControlsState::AlertSize::NONE, 0},
|
|
|
|
{cereal::ControlsState::AlertSize::SMALL, 241},
|
|
|
|
{cereal::ControlsState::AlertSize::MID, 390},
|
2020-09-12 19:35:36 -06:00
|
|
|
{cereal::ControlsState::AlertSize::FULL, s->fb_h}};
|
2020-05-06 09:38:26 -06:00
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
const UIScene *scene = &s->scene;
|
|
|
|
bool longAlert1 = strlen(va_text1) > 15;
|
|
|
|
|
2020-10-08 20:29:56 -06:00
|
|
|
NVGcolor color = bg_colors[va_color];
|
|
|
|
color.a *= s->alert_blinking_alpha;
|
2020-05-06 09:38:26 -06:00
|
|
|
int alr_s = alert_size_map[va_size];
|
|
|
|
|
2020-09-12 19:35:36 -06:00
|
|
|
const int alr_x = scene->viz_rect.x - bdr_s;
|
|
|
|
const int alr_w = scene->viz_rect.w + (bdr_s*2);
|
2020-05-06 09:38:26 -06:00
|
|
|
const int alr_h = alr_s+(va_size==cereal::ControlsState::AlertSize::NONE?0:bdr_s);
|
2020-09-12 19:35:36 -06:00
|
|
|
const int alr_y = s->fb_h-alr_h;
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-10-08 20:29:56 -06:00
|
|
|
ui_draw_rect(s->vg, alr_x, alr_y, alr_w, alr_h, color);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
NVGpaint gradient = nvgLinearGradient(s->vg, alr_x, alr_y, alr_x, alr_y+alr_h,
|
2020-10-08 20:29:56 -06:00
|
|
|
nvgRGBAf(0.0,0.0,0.0,0.05), nvgRGBAf(0.0,0.0,0.0,0.35));
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_rect(s->vg, alr_x, alr_y, alr_w, alr_h, gradient);
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-03-27 12:00:42 -06:00
|
|
|
nvgFillColor(s->vg, COLOR_WHITE);
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE);
|
|
|
|
|
2020-05-06 09:38:26 -06:00
|
|
|
if (va_size == cereal::ControlsState::AlertSize::SMALL) {
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_text(s->vg, alr_x+alr_w/2, alr_y+alr_h/2+15, va_text1, 40*2.5, COLOR_WHITE, s->font_sans_semibold);
|
2020-05-06 09:38:26 -06:00
|
|
|
} else if (va_size == cereal::ControlsState::AlertSize::MID) {
|
2020-05-02 12:27:03 -06:00
|
|
|
ui_draw_text(s->vg, alr_x+alr_w/2, alr_y+alr_h/2-45, va_text1, 48*2.5, COLOR_WHITE, s->font_sans_bold);
|
|
|
|
ui_draw_text(s->vg, alr_x+alr_w/2, alr_y+alr_h/2+75, va_text2, 36*2.5, COLOR_WHITE, s->font_sans_regular);
|
2020-05-06 09:38:26 -06:00
|
|
|
} else if (va_size == cereal::ControlsState::AlertSize::FULL) {
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgFontSize(s->vg, (longAlert1?72:96)*2.5);
|
2020-03-27 12:00:07 -06:00
|
|
|
nvgFontFaceId(s->vg, s->font_sans_bold);
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
|
|
|
|
nvgTextBox(s->vg, alr_x, alr_y+(longAlert1?360:420), alr_w-60, va_text1, NULL);
|
|
|
|
nvgFontSize(s->vg, 48*2.5);
|
2020-03-27 12:00:07 -06:00
|
|
|
nvgFontFaceId(s->vg, s->font_sans_regular);
|
2020-01-17 12:05:23 -07:00
|
|
|
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BOTTOM);
|
|
|
|
nvgTextBox(s->vg, alr_x, alr_h-(longAlert1?300:360), alr_w-60, va_text2, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 15:25:52 -07:00
|
|
|
static void ui_draw_vision_frame(UIState *s) {
|
2020-01-17 12:05:23 -07:00
|
|
|
const UIScene *scene = &s->scene;
|
2020-09-12 19:35:36 -06:00
|
|
|
const Rect &viz_rect = scene->viz_rect;
|
2020-10-08 20:29:56 -06:00
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
// Draw video frames
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2020-10-20 04:14:19 -06:00
|
|
|
glViewport(s->video_rect.x, s->video_rect.y, s->video_rect.w, s->video_rect.h);
|
2020-09-12 19:35:36 -06:00
|
|
|
glScissor(viz_rect.x, viz_rect.y, viz_rect.w, viz_rect.h);
|
2020-01-17 12:05:23 -07:00
|
|
|
draw_frame(s);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
|
2020-04-30 13:11:35 -06:00
|
|
|
glViewport(0, 0, s->fb_w, s->fb_h);
|
2020-11-27 15:25:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_draw_vision(UIState *s) {
|
|
|
|
const UIScene *scene = &s->scene;
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
// Draw augmented elements
|
2020-09-12 19:35:36 -06:00
|
|
|
if (!scene->frontview && scene->world_objects_visible) {
|
2020-01-17 12:05:23 -07:00
|
|
|
ui_draw_world(s);
|
|
|
|
}
|
|
|
|
// Set Speed, Current Speed, Status/Events
|
2020-04-15 17:48:44 -06:00
|
|
|
if (!scene->frontview) {
|
|
|
|
ui_draw_vision_header(s);
|
|
|
|
} else {
|
|
|
|
ui_draw_driver_view(s);
|
|
|
|
}
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-05-06 09:38:26 -06:00
|
|
|
if (scene->alert_size != cereal::ControlsState::AlertSize::NONE) {
|
2020-04-17 16:59:43 -06:00
|
|
|
ui_draw_vision_alert(s, scene->alert_size, s->status,
|
2020-08-27 12:57:31 -06:00
|
|
|
scene->alert_text1.c_str(), scene->alert_text2.c_str());
|
|
|
|
} else if (!scene->frontview) {
|
|
|
|
ui_draw_vision_footer(s);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 17:01:05 -06:00
|
|
|
static void ui_draw_background(UIState *s) {
|
2020-10-08 20:29:56 -06:00
|
|
|
const NVGcolor color = bg_colors[s->status];
|
|
|
|
glClearColor(color.r, color.g, color.b, 1.0);
|
2020-04-01 17:01:05 -06:00
|
|
|
glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
void ui_draw(UIState *s) {
|
2020-09-12 19:35:36 -06:00
|
|
|
s->scene.viz_rect = Rect{bdr_s * 3, bdr_s, s->fb_w - 4 * bdr_s, s->fb_h - 2 * bdr_s};
|
|
|
|
s->scene.ui_viz_ro = 0;
|
|
|
|
if (!s->scene.uilayout_sidebarcollapsed) {
|
|
|
|
s->scene.viz_rect.x = sbr_w + bdr_s;
|
|
|
|
s->scene.viz_rect.w = s->fb_w - s->scene.viz_rect.x - bdr_s;
|
|
|
|
s->scene.ui_viz_ro = -(sbr_w - 6 * bdr_s);
|
|
|
|
}
|
2020-09-03 17:32:55 -06:00
|
|
|
|
2020-11-27 15:25:52 -07:00
|
|
|
const bool draw_vision = s->started && s->active_app == cereal::UiLayoutState::App::NONE &&
|
|
|
|
s->status != STATUS_OFFROAD && s->vision_connected;
|
|
|
|
|
|
|
|
// GL drawing functions
|
2020-04-01 17:01:05 -06:00
|
|
|
ui_draw_background(s);
|
2020-11-27 15:25:52 -07:00
|
|
|
if (draw_vision) {
|
|
|
|
ui_draw_vision_frame(s);
|
|
|
|
}
|
2020-04-30 13:11:35 -06:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glViewport(0, 0, s->fb_w, s->fb_h);
|
2020-11-27 15:25:52 -07:00
|
|
|
|
|
|
|
// NVG drawing functions - should be no GL inside NVG frame
|
2020-04-30 13:11:35 -06:00
|
|
|
nvgBeginFrame(s->vg, s->fb_w, s->fb_h, 1.0f);
|
|
|
|
ui_draw_sidebar(s);
|
2020-11-27 15:25:52 -07:00
|
|
|
if (draw_vision) {
|
2020-06-29 14:54:23 -06:00
|
|
|
ui_draw_vision(s);
|
2020-05-12 19:17:04 -06:00
|
|
|
}
|
2020-04-30 13:11:35 -06:00
|
|
|
nvgEndFrame(s->vg);
|
|
|
|
glDisable(GL_BLEND);
|
2020-01-17 12:05:23 -07:00
|
|
|
}
|
|
|
|
|
2020-04-13 16:38:45 -06:00
|
|
|
void ui_draw_image(NVGcontext *vg, float x, float y, float w, float h, int image, float alpha){
|
|
|
|
nvgBeginPath(vg);
|
|
|
|
NVGpaint imgPaint = nvgImagePattern(vg, x, y, w, h, 0, image, alpha);
|
|
|
|
nvgRect(vg, x, y, w, h);
|
|
|
|
nvgFillPaint(vg, imgPaint);
|
|
|
|
nvgFill(vg);
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:27:03 -06:00
|
|
|
void ui_draw_rect(NVGcontext *vg, float x, float y, float w, float h, NVGcolor color, float r, int width) {
|
|
|
|
nvgBeginPath(vg);
|
2020-10-08 20:29:56 -06:00
|
|
|
r > 0 ? nvgRoundedRect(vg, x, y, w, h, r) : nvgRect(vg, x, y, w, h);
|
2020-05-02 12:27:03 -06:00
|
|
|
if (width) {
|
|
|
|
nvgStrokeColor(vg, color);
|
|
|
|
nvgStrokeWidth(vg, width);
|
|
|
|
nvgStroke(vg);
|
|
|
|
} else {
|
|
|
|
nvgFillColor(vg, color);
|
|
|
|
nvgFill(vg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 11:08:44 -06:00
|
|
|
void ui_draw_rect(NVGcontext *vg, float x, float y, float w, float h, NVGpaint &paint, float r){
|
2020-05-02 12:27:03 -06:00
|
|
|
nvgBeginPath(vg);
|
|
|
|
r > 0? nvgRoundedRect(vg, x, y, w, h, r) : nvgRect(vg, x, y, w, h);
|
|
|
|
nvgFillPaint(vg, paint);
|
|
|
|
nvgFill(vg);
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
static const char frame_vertex_shader[] =
|
2020-08-31 07:02:01 -06:00
|
|
|
#ifdef NANOVG_GL3_IMPLEMENTATION
|
2020-02-02 23:19:26 -07:00
|
|
|
"#version 150 core\n"
|
2020-08-31 07:02:01 -06:00
|
|
|
#else
|
|
|
|
"#version 300 es\n"
|
|
|
|
#endif
|
2020-02-02 23:19:26 -07:00
|
|
|
"in vec4 aPosition;\n"
|
|
|
|
"in vec4 aTexCoord;\n"
|
2020-01-17 12:05:23 -07:00
|
|
|
"uniform mat4 uTransform;\n"
|
2020-02-02 23:19:26 -07:00
|
|
|
"out vec4 vTexCoord;\n"
|
2020-01-17 12:05:23 -07:00
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = uTransform * aPosition;\n"
|
|
|
|
" vTexCoord = aTexCoord;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char frame_fragment_shader[] =
|
2020-08-31 07:02:01 -06:00
|
|
|
#ifdef NANOVG_GL3_IMPLEMENTATION
|
2020-02-02 23:19:26 -07:00
|
|
|
"#version 150 core\n"
|
2020-08-31 07:02:01 -06:00
|
|
|
#else
|
|
|
|
"#version 300 es\n"
|
|
|
|
#endif
|
2020-01-17 12:05:23 -07:00
|
|
|
"precision mediump float;\n"
|
|
|
|
"uniform sampler2D uTexture;\n"
|
2020-06-11 19:21:59 -06:00
|
|
|
"in vec4 vTexCoord;\n"
|
|
|
|
"out vec4 colorOut;\n"
|
2020-01-17 12:05:23 -07:00
|
|
|
"void main() {\n"
|
2020-06-11 19:21:59 -06:00
|
|
|
" colorOut = texture(uTexture, vTexCoord.xy);\n"
|
2020-01-17 12:05:23 -07:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const mat4 device_transform = {{
|
|
|
|
1.0, 0.0, 0.0, 0.0,
|
|
|
|
0.0, 1.0, 0.0, 0.0,
|
|
|
|
0.0, 0.0, 1.0, 0.0,
|
|
|
|
0.0, 0.0, 0.0, 1.0,
|
|
|
|
}};
|
|
|
|
|
|
|
|
// frame from 4/3 to 16/9 display
|
|
|
|
static const mat4 full_to_wide_frame_transform = {{
|
|
|
|
.75, 0.0, 0.0, 0.0,
|
|
|
|
0.0, 1.0, 0.0, 0.0,
|
|
|
|
0.0, 0.0, 1.0, 0.0,
|
|
|
|
0.0, 0.0, 0.0, 1.0,
|
|
|
|
}};
|
|
|
|
|
|
|
|
void ui_nvg_init(UIState *s) {
|
|
|
|
// init drawing
|
2020-06-29 14:54:23 -06:00
|
|
|
#ifdef QCOM
|
|
|
|
// on QCOM, we enable MSAA
|
|
|
|
s->vg = nvgCreate(0);
|
|
|
|
#else
|
2020-02-02 00:36:50 -07:00
|
|
|
s->vg = nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
|
2020-06-29 14:54:23 -06:00
|
|
|
#endif
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
assert(s->vg);
|
|
|
|
|
|
|
|
s->font_sans_regular = nvgCreateFont(s->vg, "sans-regular", "../assets/fonts/opensans_regular.ttf");
|
|
|
|
assert(s->font_sans_regular >= 0);
|
|
|
|
s->font_sans_semibold = nvgCreateFont(s->vg, "sans-semibold", "../assets/fonts/opensans_semibold.ttf");
|
|
|
|
assert(s->font_sans_semibold >= 0);
|
|
|
|
s->font_sans_bold = nvgCreateFont(s->vg, "sans-bold", "../assets/fonts/opensans_bold.ttf");
|
|
|
|
assert(s->font_sans_bold >= 0);
|
|
|
|
|
|
|
|
s->img_wheel = nvgCreateImage(s->vg, "../assets/img_chffr_wheel.png", 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_wheel != 0);
|
2020-01-17 12:05:23 -07:00
|
|
|
s->img_turn = nvgCreateImage(s->vg, "../assets/img_trafficSign_turn.png", 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_turn != 0);
|
2020-01-17 12:05:23 -07:00
|
|
|
s->img_face = nvgCreateImage(s->vg, "../assets/img_driver_face.png", 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_face != 0);
|
2020-03-09 20:13:32 -06:00
|
|
|
s->img_button_settings = nvgCreateImage(s->vg, "../assets/images/button_settings.png", 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_button_settings != 0);
|
2020-03-09 20:13:32 -06:00
|
|
|
s->img_button_home = nvgCreateImage(s->vg, "../assets/images/button_home.png", 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_button_home != 0);
|
2020-03-09 20:13:32 -06:00
|
|
|
s->img_battery = nvgCreateImage(s->vg, "../assets/images/battery.png", 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_battery != 0);
|
2020-03-09 20:13:32 -06:00
|
|
|
s->img_battery_charging = nvgCreateImage(s->vg, "../assets/images/battery_charging.png", 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_battery_charging != 0);
|
2020-03-09 20:13:32 -06:00
|
|
|
|
|
|
|
for(int i=0;i<=5;++i) {
|
|
|
|
char network_asset[32];
|
|
|
|
snprintf(network_asset, sizeof(network_asset), "../assets/images/network_%d.png", i);
|
|
|
|
s->img_network[i] = nvgCreateImage(s->vg, network_asset, 1);
|
2020-04-14 13:27:55 -06:00
|
|
|
assert(s->img_network[i] != 0);
|
2020-03-09 20:13:32 -06:00
|
|
|
}
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
// init gl
|
|
|
|
s->frame_program = load_program(frame_vertex_shader, frame_fragment_shader);
|
|
|
|
assert(s->frame_program);
|
|
|
|
|
|
|
|
s->frame_pos_loc = glGetAttribLocation(s->frame_program, "aPosition");
|
|
|
|
s->frame_texcoord_loc = glGetAttribLocation(s->frame_program, "aTexCoord");
|
|
|
|
|
|
|
|
s->frame_texture_loc = glGetUniformLocation(s->frame_program, "uTexture");
|
|
|
|
s->frame_transform_loc = glGetUniformLocation(s->frame_program, "uTransform");
|
|
|
|
|
|
|
|
glViewport(0, 0, s->fb_w, s->fb_h);
|
|
|
|
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
assert(glGetError() == GL_NO_ERROR);
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; i++) {
|
|
|
|
float x1, x2, y1, y2;
|
|
|
|
if (i == 1) {
|
|
|
|
// flip horizontally so it looks like a mirror
|
|
|
|
x1 = 0.0;
|
|
|
|
x2 = 1.0;
|
|
|
|
y1 = 1.0;
|
|
|
|
y2 = 0.0;
|
|
|
|
} else {
|
|
|
|
x1 = 1.0;
|
|
|
|
x2 = 0.0;
|
|
|
|
y1 = 1.0;
|
|
|
|
y2 = 0.0;
|
|
|
|
}
|
|
|
|
const uint8_t frame_indicies[] = {0, 1, 2, 0, 2, 3};
|
|
|
|
const float frame_coords[4][4] = {
|
|
|
|
{-1.0, -1.0, x2, y1}, //bl
|
|
|
|
{-1.0, 1.0, x2, y2}, //tl
|
|
|
|
{ 1.0, 1.0, x1, y2}, //tr
|
|
|
|
{ 1.0, -1.0, x1, y1}, //br
|
|
|
|
};
|
|
|
|
|
2020-09-03 17:32:55 -06:00
|
|
|
glGenVertexArrays(1, &s->frame_vao[i]);
|
2020-01-17 12:05:23 -07:00
|
|
|
glBindVertexArray(s->frame_vao[i]);
|
|
|
|
glGenBuffers(1, &s->frame_vbo[i]);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, s->frame_vbo[i]);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(frame_coords), frame_coords, GL_STATIC_DRAW);
|
|
|
|
glEnableVertexAttribArray(s->frame_pos_loc);
|
|
|
|
glVertexAttribPointer(s->frame_pos_loc, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(frame_coords[0]), (const void *)0);
|
|
|
|
glEnableVertexAttribArray(s->frame_texcoord_loc);
|
|
|
|
glVertexAttribPointer(s->frame_texcoord_loc, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(frame_coords[0]), (const void *)(sizeof(float) * 2));
|
|
|
|
glGenBuffers(1, &s->frame_ibo[i]);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->frame_ibo[i]);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(frame_indicies), frame_indicies, GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER,0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
2020-10-20 04:14:19 -06:00
|
|
|
s->video_rect = Rect{bdr_s * 3, bdr_s, s->fb_w - 4 * bdr_s, s->fb_h - 2 * bdr_s};
|
|
|
|
float zx = zoom * 2 * intrinsic_matrix.v[2] / s->video_rect.w;
|
|
|
|
float zy = zoom * 2 * intrinsic_matrix.v[5] / s->video_rect.h;
|
|
|
|
|
2020-09-12 19:35:36 -06:00
|
|
|
const mat4 frame_transform = {{
|
2020-10-20 04:14:19 -06:00
|
|
|
zx, 0.0, 0.0, 0.0,
|
|
|
|
0.0, zy, 0.0, 0.0,
|
2020-09-12 19:35:36 -06:00
|
|
|
0.0, 0.0, 1.0, 0.0,
|
|
|
|
0.0, 0.0, 0.0, 1.0,
|
|
|
|
}};
|
|
|
|
|
2020-01-17 12:05:23 -07:00
|
|
|
s->front_frame_mat = matmul(device_transform, full_to_wide_frame_transform);
|
|
|
|
s->rear_frame_mat = matmul(device_transform, frame_transform);
|
|
|
|
|
2020-09-03 17:32:55 -06:00
|
|
|
for(int i = 0; i < UI_BUF_COUNT; i++) {
|
2020-05-26 14:27:01 -06:00
|
|
|
s->khr[i] = 0;
|
2020-01-17 12:05:23 -07:00
|
|
|
s->priv_hnds[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|