2016-11-19 09:28:01 +00:00
|
|
|
#include "graphics/graphics.h"
|
2018-01-22 16:28:33 +00:00
|
|
|
#include "data/rasterizer.h"
|
2017-04-13 02:48:17 +00:00
|
|
|
#include "event/event.h"
|
2017-01-21 03:55:54 +00:00
|
|
|
#include "math/mat4.h"
|
|
|
|
#include "math/vec3.h"
|
2016-11-19 09:28:01 +00:00
|
|
|
#include "util.h"
|
2018-07-17 06:51:11 +00:00
|
|
|
#include "lib/glfw.h"
|
2017-08-10 03:02:02 +00:00
|
|
|
#include "lib/stb/stb_image.h"
|
2016-09-27 07:24:28 +00:00
|
|
|
#define _USE_MATH_DEFINES
|
2016-07-16 05:39:17 +00:00
|
|
|
#include <stdlib.h>
|
2018-07-04 20:51:35 +00:00
|
|
|
#include <string.h>
|
2016-09-27 07:24:28 +00:00
|
|
|
#include <math.h>
|
2016-09-14 00:02:23 +00:00
|
|
|
|
2016-09-27 06:48:09 +00:00
|
|
|
static GraphicsState state;
|
2016-07-07 07:04:24 +00:00
|
|
|
|
2017-04-13 02:48:17 +00:00
|
|
|
static void onCloseWindow(GLFWwindow* window) {
|
|
|
|
if (window == state.window) {
|
2018-07-26 20:40:15 +00:00
|
|
|
lovrEventPush((Event) { .type = EVENT_QUIT, .data.quit = { false, 0 } });
|
2017-04-13 02:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 05:16:13 +00:00
|
|
|
// Base
|
|
|
|
|
2016-08-10 06:28:17 +00:00
|
|
|
void lovrGraphicsInit() {
|
2018-07-17 06:51:11 +00:00
|
|
|
// This page intentionally left blank
|
2016-09-28 03:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 22:13:57 +00:00
|
|
|
void lovrGraphicsDestroy() {
|
2018-02-23 03:18:36 +00:00
|
|
|
if (!state.initialized) return;
|
2018-07-17 22:04:34 +00:00
|
|
|
while (state.pipeline > 0) {
|
|
|
|
lovrGraphicsPopPipeline();
|
|
|
|
}
|
2016-11-19 22:06:41 +00:00
|
|
|
lovrGraphicsSetShader(NULL);
|
2017-08-09 00:53:03 +00:00
|
|
|
lovrGraphicsSetFont(NULL);
|
2018-03-24 02:31:32 +00:00
|
|
|
lovrGraphicsSetCanvas(NULL, 0);
|
2018-07-17 06:51:11 +00:00
|
|
|
for (int i = 0; i < MAX_DEFAULT_SHADERS; i++) {
|
2018-02-26 08:59:03 +00:00
|
|
|
lovrRelease(state.defaultShaders[i]);
|
2017-08-09 04:02:28 +00:00
|
|
|
}
|
2018-02-26 08:59:03 +00:00
|
|
|
lovrRelease(state.defaultMaterial);
|
|
|
|
lovrRelease(state.defaultFont);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrRelease(state.defaultMesh);
|
2018-07-17 10:35:01 +00:00
|
|
|
lovrGpuDestroy();
|
2018-02-23 03:18:36 +00:00
|
|
|
memset(&state, 0, sizeof(GraphicsState));
|
2016-10-04 22:13:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 06:28:17 +00:00
|
|
|
void lovrGraphicsPresent() {
|
2017-04-13 02:48:17 +00:00
|
|
|
glfwSwapBuffers(state.window);
|
2018-07-17 10:35:01 +00:00
|
|
|
lovrGpuPresent();
|
2016-07-07 07:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 08:14:09 +00:00
|
|
|
void lovrGraphicsCreateWindow(int w, int h, bool fullscreen, int msaa, const char* title, const char* icon) {
|
2018-02-23 03:18:36 +00:00
|
|
|
lovrAssert(!state.window, "Window is already created");
|
|
|
|
|
2018-05-13 06:58:38 +00:00
|
|
|
#ifndef EMSCRIPTEN
|
2018-02-23 09:28:42 +00:00
|
|
|
if ((state.window = glfwGetCurrentContext()) == NULL) {
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
2018-07-15 07:01:23 +00:00
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
|
2018-02-23 09:28:42 +00:00
|
|
|
glfwWindowHint(GLFW_SAMPLES, msaa);
|
2018-07-15 07:01:23 +00:00
|
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
2018-02-23 09:28:42 +00:00
|
|
|
glfwWindowHint(GLFW_SRGB_CAPABLE, state.gammaCorrect);
|
2018-05-13 06:58:38 +00:00
|
|
|
#else
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
|
|
|
glfwWindowHint(GLFW_SAMPLES, msaa);
|
|
|
|
glfwWindowHint(GLFW_SRGB_CAPABLE, state.gammaCorrect);
|
2017-08-10 03:02:02 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-23 09:28:42 +00:00
|
|
|
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
|
|
|
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
|
|
|
if (fullscreen) {
|
|
|
|
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
|
|
|
|
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
|
|
|
|
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
|
|
|
|
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
|
|
|
|
}
|
2017-08-10 03:02:02 +00:00
|
|
|
|
2018-07-17 21:53:21 +00:00
|
|
|
state.msaa = msaa;
|
2018-02-23 09:28:42 +00:00
|
|
|
state.window = glfwCreateWindow(w ? w : mode->width, h ? h : mode->height, title, fullscreen ? monitor : NULL, NULL);
|
|
|
|
if (!state.window) {
|
|
|
|
glfwTerminate();
|
|
|
|
lovrThrow("Could not create window");
|
|
|
|
}
|
2017-08-10 03:02:02 +00:00
|
|
|
|
2018-02-23 09:28:42 +00:00
|
|
|
if (icon) {
|
|
|
|
GLFWimage image;
|
|
|
|
image.pixels = stbi_load(icon, &image.width, &image.height, NULL, 3);
|
|
|
|
glfwSetWindowIcon(state.window, 1, &image);
|
|
|
|
free(image.pixels);
|
|
|
|
}
|
2017-08-10 03:02:02 +00:00
|
|
|
|
2018-02-23 09:28:42 +00:00
|
|
|
glfwMakeContextCurrent(state.window);
|
|
|
|
glfwSetWindowCloseCallback(state.window, onCloseWindow);
|
2018-05-13 06:58:38 +00:00
|
|
|
#ifndef EMSCRIPTEN
|
2018-02-23 09:28:42 +00:00
|
|
|
}
|
2017-08-10 03:02:02 +00:00
|
|
|
|
|
|
|
glfwSwapInterval(0);
|
2017-11-23 22:19:20 +00:00
|
|
|
#endif
|
2018-07-17 10:35:01 +00:00
|
|
|
lovrGpuInit(state.gammaCorrect, glfwGetProcAddress);
|
2018-03-22 05:03:03 +00:00
|
|
|
VertexFormat format;
|
|
|
|
vertexFormatInit(&format);
|
|
|
|
vertexFormatAppend(&format, "lovrPosition", ATTR_FLOAT, 3);
|
|
|
|
vertexFormatAppend(&format, "lovrNormal", ATTR_FLOAT, 3);
|
|
|
|
vertexFormatAppend(&format, "lovrTexCoord", ATTR_FLOAT, 2);
|
2018-08-02 04:05:30 +00:00
|
|
|
state.defaultMesh = lovrMeshCreate(64, format, MESH_TRIANGLES, USAGE_STREAM);
|
2017-08-10 03:02:02 +00:00
|
|
|
lovrGraphicsReset();
|
2018-02-23 03:18:36 +00:00
|
|
|
state.initialized = true;
|
2017-08-10 03:02:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:53:21 +00:00
|
|
|
void lovrGraphicsGetDimensions(int* width, int* height) {
|
|
|
|
glfwGetFramebufferSize(state.window, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lovrGraphicsGetMSAA() {
|
|
|
|
return state.msaa;
|
|
|
|
}
|
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
void lovrGraphicsSetCamera(Camera* camera, bool clear) {
|
|
|
|
if (!camera) {
|
|
|
|
int width, height;
|
|
|
|
lovrGraphicsGetDimensions(&width, &height);
|
2018-07-21 12:30:13 +00:00
|
|
|
state.camera.stereo = false;
|
2018-07-17 06:51:11 +00:00
|
|
|
state.camera.canvas = NULL;
|
2018-07-21 12:30:13 +00:00
|
|
|
state.camera.viewport[0][0] = 0;
|
|
|
|
state.camera.viewport[0][1] = 0;
|
|
|
|
state.camera.viewport[0][2] = width;
|
|
|
|
state.camera.viewport[0][3] = height;
|
|
|
|
mat4_identity(state.camera.viewMatrix[0]);
|
|
|
|
mat4_perspective(state.camera.projection[0], .01f, 100.f, 67 * M_PI / 180., (float) width / height);
|
2018-07-17 06:51:11 +00:00
|
|
|
} else {
|
|
|
|
state.camera = *camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clear) {
|
2018-07-17 10:35:01 +00:00
|
|
|
int canvasCount = state.camera.canvas != NULL;
|
2018-07-17 06:51:11 +00:00
|
|
|
Color backgroundColor = lovrGraphicsGetBackgroundColor();
|
2018-07-17 10:35:01 +00:00
|
|
|
lovrGpuClear(&state.camera.canvas, canvasCount, &backgroundColor, &(float) { 1. }, &(int) { 0 });
|
2018-07-17 06:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 05:16:13 +00:00
|
|
|
// State
|
|
|
|
|
2018-07-16 00:05:06 +00:00
|
|
|
void lovrGraphicsReset() {
|
2018-07-17 22:04:34 +00:00
|
|
|
while (state.pipeline > 0) {
|
|
|
|
lovrGraphicsPopPipeline();
|
|
|
|
}
|
2018-07-16 00:05:06 +00:00
|
|
|
state.transform = 0;
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsSetCamera(NULL, false);
|
|
|
|
lovrGraphicsSetBackgroundColor((Color) { 0, 0, 0, 1 });
|
2018-07-16 00:05:06 +00:00
|
|
|
lovrGraphicsSetBlendMode(BLEND_ALPHA, BLEND_ALPHA_MULTIPLY);
|
|
|
|
lovrGraphicsSetCanvas(NULL, 0);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsSetColor((Color) { 1, 1, 1, 1 });
|
2018-07-16 00:05:06 +00:00
|
|
|
lovrGraphicsSetCullingEnabled(false);
|
|
|
|
lovrGraphicsSetDefaultFilter((TextureFilter) { .mode = FILTER_TRILINEAR });
|
|
|
|
lovrGraphicsSetDepthTest(COMPARE_LEQUAL, true);
|
|
|
|
lovrGraphicsSetFont(NULL);
|
|
|
|
lovrGraphicsSetLineWidth(1);
|
|
|
|
lovrGraphicsSetPointSize(1);
|
|
|
|
lovrGraphicsSetShader(NULL);
|
|
|
|
lovrGraphicsSetStencilTest(COMPARE_NONE, 0);
|
|
|
|
lovrGraphicsSetWinding(WINDING_COUNTERCLOCKWISE);
|
|
|
|
lovrGraphicsSetWireframe(false);
|
|
|
|
lovrGraphicsOrigin();
|
|
|
|
}
|
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
void lovrGraphicsPushPipeline() {
|
|
|
|
lovrAssert(++state.pipeline < MAX_PIPELINES, "Unbalanced pipeline stack (more pushes than pops?)");
|
|
|
|
memcpy(&state.pipelines[state.pipeline], &state.pipelines[state.pipeline - 1], sizeof(Pipeline));
|
2018-07-17 22:04:34 +00:00
|
|
|
for (int i = 0; i < state.pipelines[state.pipeline].canvasCount; i++) {
|
|
|
|
lovrRetain(state.pipelines[state.pipeline].canvas[i]);
|
|
|
|
}
|
|
|
|
lovrRetain(state.pipelines[state.pipeline].font);
|
|
|
|
lovrRetain(state.pipelines[state.pipeline].shader);
|
2018-07-17 06:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsPopPipeline() {
|
2018-07-17 22:04:34 +00:00
|
|
|
for (int i = 0; i < state.pipelines[state.pipeline].canvasCount; i++) {
|
|
|
|
lovrRelease(state.pipelines[state.pipeline].canvas[i]);
|
|
|
|
}
|
|
|
|
lovrRelease(state.pipelines[state.pipeline].font);
|
|
|
|
lovrRelease(state.pipelines[state.pipeline].shader);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrAssert(--state.pipeline >= 0, "Unbalanced pipeline stack (more pops than pushes?)");
|
|
|
|
}
|
|
|
|
|
2017-08-02 08:25:56 +00:00
|
|
|
Color lovrGraphicsGetBackgroundColor() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].backgroundColor;
|
2016-07-28 02:48:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 08:25:56 +00:00
|
|
|
void lovrGraphicsSetBackgroundColor(Color color) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].backgroundColor = color;
|
2016-07-28 02:48:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 11:03:36 +00:00
|
|
|
void lovrGraphicsGetBlendMode(BlendMode* mode, BlendAlphaMode* alphaMode) {
|
2018-07-15 01:26:02 +00:00
|
|
|
*mode = state.pipelines[state.pipeline].blendMode;
|
|
|
|
*alphaMode = state.pipelines[state.pipeline].blendAlphaMode;
|
2017-03-12 11:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetBlendMode(BlendMode mode, BlendAlphaMode alphaMode) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].blendMode = mode;
|
|
|
|
state.pipelines[state.pipeline].blendAlphaMode = alphaMode;
|
2017-03-12 11:03:36 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 08:37:02 +00:00
|
|
|
void lovrGraphicsGetCanvas(Canvas** canvas, int* count) {
|
2018-07-17 06:51:11 +00:00
|
|
|
*count = state.pipelines[state.pipeline].canvasCount;
|
|
|
|
memcpy(canvas, state.pipelines[state.pipeline].canvas, *count);
|
2018-02-15 08:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetCanvas(Canvas** canvas, int count) {
|
2018-07-17 06:51:11 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
lovrRetain(canvas[i]);
|
2018-02-15 08:37:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
for (int i = 0; i < state.pipelines[state.pipeline].canvasCount; i++) {
|
|
|
|
lovrRelease(state.pipelines[state.pipeline].canvas[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(state.pipelines[state.pipeline].canvas, canvas, count * sizeof(Canvas*));
|
|
|
|
state.pipelines[state.pipeline].canvasCount = count;
|
2018-02-15 08:37:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 08:25:56 +00:00
|
|
|
Color lovrGraphicsGetColor() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].color;
|
2016-09-29 03:11:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 08:25:56 +00:00
|
|
|
void lovrGraphicsSetColor(Color color) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].color = color;
|
2016-09-29 03:11:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 08:14:09 +00:00
|
|
|
bool lovrGraphicsIsCullingEnabled() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].culling;
|
2016-09-14 00:02:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 08:14:09 +00:00
|
|
|
void lovrGraphicsSetCullingEnabled(bool culling) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].culling = culling;
|
2016-07-16 02:17:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 07:54:33 +00:00
|
|
|
TextureFilter lovrGraphicsGetDefaultFilter() {
|
|
|
|
return state.defaultFilter;
|
2017-02-03 23:16:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 07:54:33 +00:00
|
|
|
void lovrGraphicsSetDefaultFilter(TextureFilter filter) {
|
|
|
|
state.defaultFilter = filter;
|
2017-02-03 23:16:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 05:50:47 +00:00
|
|
|
void lovrGraphicsGetDepthTest(CompareMode* mode, bool* write) {
|
2018-07-15 01:26:02 +00:00
|
|
|
*mode = state.pipelines[state.pipeline].depthTest;
|
|
|
|
*write = state.pipelines[state.pipeline].depthWrite;
|
2017-02-17 08:48:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 03:11:52 +00:00
|
|
|
void lovrGraphicsSetDepthTest(CompareMode mode, bool write) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].depthTest = mode;
|
|
|
|
state.pipelines[state.pipeline].depthWrite = write;
|
2016-11-27 02:58:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 08:36:29 +00:00
|
|
|
Font* lovrGraphicsGetFont() {
|
2018-07-15 01:26:02 +00:00
|
|
|
if (!state.pipelines[state.pipeline].font) {
|
2017-08-09 06:49:17 +00:00
|
|
|
if (!state.defaultFont) {
|
2018-01-22 16:28:33 +00:00
|
|
|
Rasterizer* rasterizer = lovrRasterizerCreate(NULL, 32);
|
|
|
|
state.defaultFont = lovrFontCreate(rasterizer);
|
2018-03-24 02:31:32 +00:00
|
|
|
lovrRelease(rasterizer);
|
2017-08-09 06:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 08:36:29 +00:00
|
|
|
lovrGraphicsSetFont(state.defaultFont);
|
|
|
|
}
|
|
|
|
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].font;
|
2017-08-08 08:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetFont(Font* font) {
|
2018-02-26 08:59:03 +00:00
|
|
|
lovrRetain(font);
|
2018-07-15 01:26:02 +00:00
|
|
|
lovrRelease(state.pipelines[state.pipeline].font);
|
|
|
|
state.pipelines[state.pipeline].font = font;
|
2017-08-08 08:36:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 04:26:01 +00:00
|
|
|
bool lovrGraphicsIsGammaCorrect() {
|
|
|
|
return state.gammaCorrect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetGammaCorrect(bool gammaCorrect) {
|
|
|
|
state.gammaCorrect = gammaCorrect;
|
|
|
|
}
|
|
|
|
|
2016-10-01 20:48:31 +00:00
|
|
|
float lovrGraphicsGetLineWidth() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].lineWidth;
|
2016-10-01 20:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetLineWidth(float width) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].lineWidth = width;
|
2016-10-01 20:48:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 01:38:49 +00:00
|
|
|
float lovrGraphicsGetPointSize() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].pointSize;
|
2016-11-13 01:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetPointSize(float size) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].pointSize = size;
|
2016-11-13 01:38:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 08:32:15 +00:00
|
|
|
Shader* lovrGraphicsGetShader() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].shader;
|
2017-08-08 08:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetShader(Shader* shader) {
|
2018-08-09 01:07:48 +00:00
|
|
|
lovrAssert(!shader || lovrShaderGetType(shader) == SHADER_GRAPHICS, "Compute shaders can not be set as the active shader");
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrRetain(shader);
|
|
|
|
lovrRelease(state.pipelines[state.pipeline].shader);
|
|
|
|
state.pipelines[state.pipeline].shader = shader;
|
2017-08-08 08:32:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 02:37:03 +00:00
|
|
|
void lovrGraphicsGetStencilTest(CompareMode* mode, int* value) {
|
2018-07-15 01:26:02 +00:00
|
|
|
*mode = state.pipelines[state.pipeline].stencilMode;
|
|
|
|
*value = state.pipelines[state.pipeline].stencilValue;
|
2017-12-19 02:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lovrGraphicsSetStencilTest(CompareMode mode, int value) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].stencilMode = mode;
|
|
|
|
state.pipelines[state.pipeline].stencilValue = value;
|
2017-12-19 02:37:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 07:54:33 +00:00
|
|
|
Winding lovrGraphicsGetWinding() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].winding;
|
2016-10-03 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 07:54:33 +00:00
|
|
|
void lovrGraphicsSetWinding(Winding winding) {
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].winding = winding;
|
2016-10-03 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 08:14:09 +00:00
|
|
|
bool lovrGraphicsIsWireframe() {
|
2018-07-15 01:26:02 +00:00
|
|
|
return state.pipelines[state.pipeline].wireframe;
|
2016-10-03 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 08:14:09 +00:00
|
|
|
void lovrGraphicsSetWireframe(bool wireframe) {
|
2017-08-02 07:54:33 +00:00
|
|
|
#ifndef EMSCRIPTEN
|
2018-07-15 01:26:02 +00:00
|
|
|
state.pipelines[state.pipeline].wireframe = wireframe;
|
2017-08-02 07:54:33 +00:00
|
|
|
#endif
|
2016-10-03 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 05:16:13 +00:00
|
|
|
// Transforms
|
|
|
|
|
2017-08-10 08:05:04 +00:00
|
|
|
void lovrGraphicsPush() {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrAssert(++state.transform < MAX_TRANSFORMS, "Unbalanced matrix stack (more pushes than pops?)");
|
|
|
|
mat4_init(state.transforms[state.transform], state.transforms[state.transform - 1]);
|
2016-09-21 07:55:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 08:05:04 +00:00
|
|
|
void lovrGraphicsPop() {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrAssert(--state.transform >= 0, "Unbalanced matrix stack (more pops than pushes?)");
|
2016-09-21 07:55:53 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 22:26:05 +00:00
|
|
|
void lovrGraphicsOrigin() {
|
2018-03-05 07:06:34 +00:00
|
|
|
mat4_identity(state.transforms[state.transform]);
|
2016-09-21 22:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 07:06:34 +00:00
|
|
|
void lovrGraphicsTranslate(float x, float y, float z) {
|
|
|
|
mat4_translate(state.transforms[state.transform], x, y, z);
|
2016-09-23 04:53:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 07:06:34 +00:00
|
|
|
void lovrGraphicsRotate(float angle, float ax, float ay, float az) {
|
|
|
|
mat4_rotate(state.transforms[state.transform], angle, ax, ay, az);
|
2016-09-23 04:53:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 07:06:34 +00:00
|
|
|
void lovrGraphicsScale(float x, float y, float z) {
|
|
|
|
mat4_scale(state.transforms[state.transform], x, y, z);
|
2016-09-23 04:53:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 07:06:34 +00:00
|
|
|
void lovrGraphicsMatrixTransform(mat4 transform) {
|
|
|
|
mat4_multiply(state.transforms[state.transform], transform);
|
2016-09-29 07:21:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 00:05:06 +00:00
|
|
|
// Drawing
|
2016-09-29 05:10:03 +00:00
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
VertexPointer lovrGraphicsGetVertexPointer(uint32_t count) {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrMeshResize(state.defaultMesh, count);
|
|
|
|
return lovrMeshMapVertices(state.defaultMesh, 0, count, false, true);
|
2016-09-30 02:39:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 00:05:06 +00:00
|
|
|
void lovrGraphicsClear(Color* color, float* depth, int* stencil) {
|
2018-07-17 06:51:11 +00:00
|
|
|
Pipeline* pipeline = &state.pipelines[state.pipeline];
|
|
|
|
if (pipeline->canvasCount > 0) {
|
2018-07-17 10:35:01 +00:00
|
|
|
lovrGpuClear(pipeline->canvas, pipeline->canvasCount, color, depth, stencil);
|
2018-07-17 06:51:11 +00:00
|
|
|
} else {
|
2018-07-17 21:53:21 +00:00
|
|
|
lovrGpuClear(&state.camera.canvas, state.camera.canvas != NULL, color, depth, stencil);
|
2018-07-16 00:05:06 +00:00
|
|
|
}
|
2018-07-17 06:51:11 +00:00
|
|
|
}
|
2018-07-16 00:05:06 +00:00
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
void lovrGraphicsDraw(DrawOptions* draw) {
|
2018-07-16 00:05:06 +00:00
|
|
|
Shader* shader = state.pipelines[state.pipeline].shader ? state.pipelines[state.pipeline].shader : state.defaultShaders[draw->shader];
|
|
|
|
if (!shader) shader = state.defaultShaders[draw->shader] = lovrShaderCreateDefault(draw->shader);
|
|
|
|
|
|
|
|
Mesh* mesh = draw->mesh;
|
|
|
|
if (!mesh) {
|
|
|
|
int drawCount = draw->range.count ? draw->range.count : (draw->index.count ? draw->index.count : draw->vertex.count);
|
2018-07-17 06:51:11 +00:00
|
|
|
mesh = state.defaultMesh;
|
2018-07-16 00:05:06 +00:00
|
|
|
lovrMeshSetDrawMode(mesh, draw->mode);
|
|
|
|
lovrMeshSetDrawRange(mesh, draw->range.start, drawCount);
|
|
|
|
if (draw->vertex.count) {
|
|
|
|
VertexPointer vertexPointer = lovrGraphicsGetVertexPointer(draw->vertex.count);
|
|
|
|
memcpy(vertexPointer.raw, draw->vertex.data, draw->vertex.count * 8 * sizeof(float));
|
|
|
|
if (draw->index.count) {
|
|
|
|
IndexPointer indexPointer = lovrMeshWriteIndices(mesh, draw->index.count, sizeof(uint16_t));
|
|
|
|
memcpy(indexPointer.shorts, draw->index.data, draw->index.count * sizeof(uint16_t));
|
|
|
|
} else {
|
|
|
|
lovrMeshWriteIndices(mesh, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Material* material = draw->material;
|
|
|
|
if (!material) {
|
2018-07-17 06:51:11 +00:00
|
|
|
if (!state.defaultMaterial) {
|
2018-07-18 07:34:21 +00:00
|
|
|
state.defaultMaterial = lovrMaterialCreate();
|
2018-07-16 00:05:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
material = state.defaultMaterial;
|
|
|
|
|
2018-07-16 00:05:06 +00:00
|
|
|
for (int i = 0; i < MAX_MATERIAL_TEXTURES; i++) {
|
|
|
|
lovrMaterialSetTexture(material, i, draw->textures[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
DrawCommand command = {
|
|
|
|
.mesh = mesh,
|
2018-07-16 00:05:06 +00:00
|
|
|
.shader = shader,
|
|
|
|
.material = material,
|
2018-07-17 06:51:11 +00:00
|
|
|
.camera = state.camera,
|
2018-07-16 00:05:06 +00:00
|
|
|
.pipeline = state.pipelines[state.pipeline],
|
|
|
|
.instances = draw->instances
|
2018-07-17 06:51:11 +00:00
|
|
|
};
|
2018-07-16 00:05:06 +00:00
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
mat4_init(command.transform, state.transforms[state.transform]);
|
2018-07-16 00:05:06 +00:00
|
|
|
if (draw->transform) {
|
2018-07-17 06:51:11 +00:00
|
|
|
mat4_multiply(command.transform, draw->transform);
|
2018-07-16 00:05:06 +00:00
|
|
|
}
|
2018-07-17 06:51:11 +00:00
|
|
|
|
2018-07-17 10:35:01 +00:00
|
|
|
lovrGpuDraw(&command);
|
2018-07-16 00:05:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
void lovrGraphicsPoints(uint32_t count) {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.mode = MESH_POINTS,
|
|
|
|
.range = { 0, count }
|
|
|
|
});
|
2016-11-08 07:16:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
void lovrGraphicsLine(uint32_t count) {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.mode = MESH_LINE_STRIP,
|
|
|
|
.range = { 0, count }
|
|
|
|
});
|
2016-09-29 07:00:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
void lovrGraphicsTriangle(DrawMode mode, Material* material, float points[9]) {
|
2016-11-23 04:43:22 +00:00
|
|
|
if (mode == DRAW_MODE_LINE) {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-15 06:50:06 +00:00
|
|
|
.material = material,
|
2018-07-14 00:12:30 +00:00
|
|
|
.mode = MESH_LINE_LOOP,
|
|
|
|
.vertex.count = 3,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
points[0], points[1], points[2], 0, 0, 0, 0, 0,
|
|
|
|
points[3], points[4], points[5], 0, 0, 0, 0, 0,
|
|
|
|
points[6], points[7], points[8], 0, 0, 0, 0, 0
|
|
|
|
}
|
|
|
|
});
|
2016-11-23 04:43:22 +00:00
|
|
|
} else {
|
2017-01-20 02:50:51 +00:00
|
|
|
float normal[3];
|
|
|
|
vec3_cross(vec3_init(normal, &points[0]), &points[3]);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-15 06:50:06 +00:00
|
|
|
.material = material,
|
2018-07-14 00:12:30 +00:00
|
|
|
.mode = MESH_TRIANGLES,
|
|
|
|
.vertex.count = 3,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
points[0], points[1], points[2], normal[0], normal[1], normal[2], 0, 0,
|
|
|
|
points[3], points[4], points[5], normal[0], normal[1], normal[2], 0, 0,
|
|
|
|
points[6], points[7], points[8], normal[0], normal[1], normal[2], 0, 0
|
|
|
|
}
|
|
|
|
});
|
2016-11-23 04:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 03:02:28 +00:00
|
|
|
void lovrGraphicsPlane(DrawMode mode, Material* material, mat4 transform) {
|
2016-10-04 03:56:45 +00:00
|
|
|
if (mode == DRAW_MODE_LINE) {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.transform = transform,
|
|
|
|
.material = material,
|
|
|
|
.mode = MESH_LINE_LOOP,
|
|
|
|
.vertex.count = 4,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
-.5, .5, 0, 0, 0, 0, 0, 0,
|
|
|
|
.5, .5, 0, 0, 0, 0, 0, 0,
|
|
|
|
.5, -.5, 0, 0, 0, 0, 0, 0,
|
|
|
|
-.5, -.5, 0, 0, 0, 0, 0, 0
|
|
|
|
}
|
|
|
|
});
|
2016-10-04 03:56:45 +00:00
|
|
|
} else if (mode == DRAW_MODE_FILL) {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.transform = transform,
|
|
|
|
.material = material,
|
|
|
|
.mode = MESH_TRIANGLE_STRIP,
|
|
|
|
.vertex.count = 4,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
-.5, .5, 0, 0, 0, -1, 0, 1,
|
|
|
|
-.5, -.5, 0, 0, 0, -1, 0, 0,
|
|
|
|
.5, .5, 0, 0, 0, -1, 1, 1,
|
|
|
|
.5, -.5, 0, 0, 0, -1, 1, 0
|
|
|
|
}
|
|
|
|
});
|
2016-10-04 03:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 03:02:28 +00:00
|
|
|
void lovrGraphicsBox(DrawMode mode, Material* material, mat4 transform) {
|
2016-10-01 19:53:15 +00:00
|
|
|
if (mode == DRAW_MODE_LINE) {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.transform = transform,
|
|
|
|
.material = material,
|
|
|
|
.mode = MESH_LINES,
|
|
|
|
.vertex.count = 8,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
// Front
|
|
|
|
-.5, .5, -.5, 0, 0, 0, 0, 0,
|
|
|
|
.5, .5, -.5, 0, 0, 0, 0, 0,
|
|
|
|
.5, -.5, -.5, 0, 0, 0, 0, 0,
|
|
|
|
-.5, -.5, -.5, 0, 0, 0, 0, 0,
|
|
|
|
|
|
|
|
// Back
|
|
|
|
-.5, .5, .5, 0, 0, 0, 0, 0,
|
|
|
|
.5, .5, .5, 0, 0, 0, 0, 0,
|
|
|
|
.5, -.5, .5, 0, 0, 0, 0, 0,
|
|
|
|
-.5, -.5, .5, 0, 0, 0, 0, 0
|
|
|
|
},
|
|
|
|
.index.count = 24,
|
|
|
|
.index.data = (uint16_t[]) {
|
|
|
|
0, 1, 1, 2, 2, 3, 3, 0, // Front
|
|
|
|
4, 5, 5, 6, 6, 7, 7, 4, // Back
|
|
|
|
0, 4, 1, 5, 2, 6, 3, 7 // Connections
|
|
|
|
}
|
|
|
|
});
|
2016-10-01 19:10:38 +00:00
|
|
|
} else {
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.transform = transform,
|
|
|
|
.material = material,
|
|
|
|
.mode = MESH_TRIANGLE_STRIP,
|
|
|
|
.vertex.count = 26,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
// Front
|
|
|
|
-.5, -.5, -.5, 0, 0, -1, 0, 0,
|
|
|
|
-.5, .5, -.5, 0, 0, -1, 0, 1,
|
|
|
|
.5, -.5, -.5, 0, 0, -1, 1, 0,
|
|
|
|
.5, .5, -.5, 0, 0, -1, 1, 1,
|
|
|
|
|
|
|
|
// Right
|
|
|
|
.5, .5, -.5, 1, 0, 0, 0, 1,
|
|
|
|
.5, .5, .5, 1, 0, 0, 1, 1,
|
|
|
|
.5, -.5, -.5, 1, 0, 0, 0, 0,
|
|
|
|
.5, -.5, .5, 1, 0, 0, 1, 0,
|
|
|
|
|
|
|
|
// Back
|
|
|
|
.5, -.5, .5, 0, 0, 1, 0, 0,
|
|
|
|
.5, .5, .5, 0, 0, 1, 0, 1,
|
|
|
|
-.5, -.5, .5, 0, 0, 1, 1, 0,
|
|
|
|
-.5, .5, .5, 0, 0, 1, 1, 1,
|
|
|
|
|
|
|
|
// Left
|
|
|
|
-.5, .5, .5, -1, 0, 0, 0, 1,
|
|
|
|
-.5, .5, -.5, -1, 0, 0, 1, 1,
|
|
|
|
-.5, -.5, .5, -1, 0, 0, 0, 0,
|
|
|
|
-.5, -.5, -.5, -1, 0, 0, 1, 0,
|
|
|
|
|
|
|
|
// Bottom
|
|
|
|
-.5, -.5, -.5, 0, -1, 0, 0, 0,
|
|
|
|
.5, -.5, -.5, 0, -1, 0, 1, 0,
|
|
|
|
-.5, -.5, .5, 0, -1, 0, 0, 1,
|
|
|
|
.5, -.5, .5, 0, -1, 0, 1, 1,
|
|
|
|
|
|
|
|
// Adjust
|
|
|
|
.5, -.5, .5, 0, 1, 0, 0, 1,
|
|
|
|
-.5, .5, -.5, 0, 1, 0, 0, 1,
|
|
|
|
|
|
|
|
// Top
|
|
|
|
-.5, .5, -.5, 0, 1, 0, 0, 1,
|
|
|
|
-.5, .5, .5, 0, 1, 0, 0, 0,
|
|
|
|
.5, .5, -.5, 0, 1, 0, 1, 1,
|
|
|
|
.5, .5, .5, 0, 1, 0, 1, 0
|
|
|
|
}
|
|
|
|
});
|
2016-10-01 19:10:38 +00:00
|
|
|
}
|
2016-09-30 06:18:51 +00:00
|
|
|
}
|
2016-10-24 22:02:23 +00:00
|
|
|
|
2017-11-26 03:02:28 +00:00
|
|
|
void lovrGraphicsArc(DrawMode mode, ArcMode arcMode, Material* material, mat4 transform, float theta1, float theta2, int segments) {
|
2017-11-22 19:32:30 +00:00
|
|
|
if (fabsf(theta1 - theta2) >= 2 * M_PI) {
|
|
|
|
theta1 = 0;
|
|
|
|
theta2 = 2 * M_PI;
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
bool hasCenterPoint = arcMode == ARC_MODE_PIE && fabsf(theta1 - theta2) < 2 * M_PI;
|
|
|
|
uint32_t count = segments + 1 + hasCenterPoint;
|
|
|
|
VertexPointer vertices = lovrGraphicsGetVertexPointer(count);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrMeshWriteIndices(state.defaultMesh, 0, 0);
|
2017-11-22 19:32:30 +00:00
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
if (hasCenterPoint) {
|
2018-08-02 04:06:16 +00:00
|
|
|
memcpy(vertices.floats, ((float[]) { 0, 0, 0, 0, 0, 1, .5, .5 }), 8 * sizeof(float));
|
2018-07-14 00:12:30 +00:00
|
|
|
vertices.floats += 8;
|
2017-11-22 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float theta = theta1;
|
|
|
|
float angleShift = (theta2 - theta1) / (float) segments;
|
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
for (int i = 0; i <= segments; i++) {
|
|
|
|
float x = cos(theta) * .5;
|
|
|
|
float y = sin(theta) * .5;
|
2018-08-02 04:06:16 +00:00
|
|
|
memcpy(vertices.floats, ((float[]) { x, y, 0, 0, 0, 1, x + .5, 1 - (y + .5) }), 8 * sizeof(float));
|
2018-07-14 00:12:30 +00:00
|
|
|
vertices.floats += 8;
|
2018-03-22 05:03:03 +00:00
|
|
|
theta += angleShift;
|
2017-11-22 04:53:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.transform = transform,
|
|
|
|
.material = material,
|
|
|
|
.mode = mode == DRAW_MODE_LINE ? (arcMode == ARC_MODE_OPEN ? MESH_LINE_STRIP : MESH_LINE_LOOP) : MESH_TRIANGLE_FAN,
|
|
|
|
.range = { 0, count }
|
|
|
|
});
|
2017-11-22 04:53:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 03:02:28 +00:00
|
|
|
void lovrGraphicsCircle(DrawMode mode, Material* material, mat4 transform, int segments) {
|
|
|
|
lovrGraphicsArc(mode, ARC_MODE_OPEN, material, transform, 0, 2 * M_PI, segments);
|
2017-11-22 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 03:02:28 +00:00
|
|
|
void lovrGraphicsCylinder(Material* material, float x1, float y1, float z1, float x2, float y2, float z2, float r1, float r2, bool capped, int segments) {
|
2017-06-21 03:54:22 +00:00
|
|
|
float axis[3] = { x1 - x2, y1 - y2, z1 - z2 };
|
|
|
|
float n[3] = { x1 - x2, y1 - y2, z1 - z2 };
|
|
|
|
float p[3];
|
|
|
|
float q[3];
|
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
uint32_t vertexCount = ((capped && r1) * (segments + 2) + (capped && r2) * (segments + 2) + 2 * (segments + 1));
|
|
|
|
uint32_t indexCount = 3 * segments * ((capped && r1) + (capped && r2) + 2);
|
2017-06-21 03:54:22 +00:00
|
|
|
|
2018-03-22 05:03:03 +00:00
|
|
|
VertexPointer vertices = lovrGraphicsGetVertexPointer(vertexCount);
|
2018-07-17 06:51:11 +00:00
|
|
|
IndexPointer indices = lovrMeshWriteIndices(state.defaultMesh, indexCount, sizeof(uint32_t));
|
2018-03-22 05:03:03 +00:00
|
|
|
float* baseVertex = vertices.floats;
|
2017-06-21 03:54:22 +00:00
|
|
|
|
|
|
|
vec3_init(p, n);
|
|
|
|
|
|
|
|
if (n[0] == 0 && n[2] == 0) {
|
|
|
|
p[0] += 1;
|
|
|
|
} else {
|
|
|
|
p[1] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3_init(q, p);
|
|
|
|
vec3_cross(q, n);
|
|
|
|
vec3_cross(n, q);
|
|
|
|
vec3_init(p, n);
|
|
|
|
vec3_normalize(p);
|
|
|
|
vec3_normalize(q);
|
|
|
|
vec3_normalize(axis);
|
|
|
|
|
|
|
|
#define PUSH_CYLINDER_VERTEX(x, y, z, nx, ny, nz) \
|
2018-03-22 05:03:03 +00:00
|
|
|
*vertices.floats++ = x; \
|
|
|
|
*vertices.floats++ = y; \
|
|
|
|
*vertices.floats++ = z; \
|
|
|
|
*vertices.floats++ = nx; \
|
|
|
|
*vertices.floats++ = ny; \
|
|
|
|
*vertices.floats++ = nz; \
|
|
|
|
*vertices.floats++ = 0; \
|
|
|
|
*vertices.floats++ = 0;
|
2017-06-21 03:54:22 +00:00
|
|
|
#define PUSH_CYLINDER_TRIANGLE(i1, i2, i3) \
|
2018-03-22 05:03:03 +00:00
|
|
|
*indices.ints++ = i1; \
|
|
|
|
*indices.ints++ = i2; \
|
|
|
|
*indices.ints++ = i3; \
|
2017-06-21 03:54:22 +00:00
|
|
|
|
|
|
|
// Ring
|
|
|
|
for (int i = 0; i <= segments; i++) {
|
|
|
|
float theta = i * (2 * M_PI) / segments;
|
|
|
|
n[0] = cos(theta) * p[0] + sin(theta) * q[0];
|
|
|
|
n[1] = cos(theta) * p[1] + sin(theta) * q[1];
|
|
|
|
n[2] = cos(theta) * p[2] + sin(theta) * q[2];
|
|
|
|
PUSH_CYLINDER_VERTEX(x1 + r1 * n[0], y1 + r1 * n[1], z1 + r1 * n[2], n[0], n[1], n[2]);
|
|
|
|
PUSH_CYLINDER_VERTEX(x2 + r2 * n[0], y2 + r2 * n[1], z2 + r2 * n[2], n[0], n[1], n[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top
|
2018-03-22 05:03:03 +00:00
|
|
|
int topOffset = (segments + 1) * 2;
|
2017-06-21 03:54:22 +00:00
|
|
|
if (capped && r1 != 0) {
|
|
|
|
PUSH_CYLINDER_VERTEX(x1, y1, z1, axis[0], axis[1], axis[2]);
|
|
|
|
for (int i = 0; i <= segments; i++) {
|
2018-03-22 05:03:03 +00:00
|
|
|
int j = i * 2 * 8;
|
|
|
|
PUSH_CYLINDER_VERTEX(baseVertex[j + 0], baseVertex[j + 1], baseVertex[j + 2], axis[0], axis[1], axis[2]);
|
2017-06-21 03:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bottom
|
2018-03-22 05:03:03 +00:00
|
|
|
int bottomOffset = (segments + 1) * 2 + (1 + segments + 1) * (capped && r1 != 0);
|
2017-06-21 03:54:22 +00:00
|
|
|
if (capped && r2 != 0) {
|
2018-03-22 05:03:03 +00:00
|
|
|
PUSH_CYLINDER_VERTEX(x2, y2, z1, -axis[0], -axis[1], -axis[2]);
|
2017-06-21 03:54:22 +00:00
|
|
|
for (int i = 0; i <= segments; i++) {
|
2018-03-22 05:03:03 +00:00
|
|
|
int j = i * 2 * 8 + 8;
|
|
|
|
PUSH_CYLINDER_VERTEX(baseVertex[j + 0], baseVertex[j + 1], baseVertex[j + 2], -axis[0], -axis[1], -axis[2]);
|
2017-06-21 03:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indices
|
|
|
|
for (int i = 0; i < segments; i++) {
|
2018-03-22 05:03:03 +00:00
|
|
|
int j = 2 * i;
|
2017-06-21 03:54:22 +00:00
|
|
|
PUSH_CYLINDER_TRIANGLE(j, j + 1, j + 2);
|
|
|
|
PUSH_CYLINDER_TRIANGLE(j + 1, j + 3, j + 2);
|
|
|
|
|
|
|
|
if (capped && r1 != 0) {
|
|
|
|
PUSH_CYLINDER_TRIANGLE(topOffset, topOffset + i + 1, topOffset + i + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capped && r2 != 0) {
|
|
|
|
PUSH_CYLINDER_TRIANGLE(bottomOffset, bottomOffset + i + 1, bottomOffset + i + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef PUSH_CYLINDER_VERTEX
|
|
|
|
#undef PUSH_CYLINDER_TRIANGLE
|
2018-03-22 05:03:03 +00:00
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.material = material,
|
|
|
|
.mode = MESH_TRIANGLES,
|
|
|
|
.range = { 0, indexCount }
|
|
|
|
});
|
2017-06-21 03:54:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 03:02:28 +00:00
|
|
|
void lovrGraphicsSphere(Material* material, mat4 transform, int segments) {
|
2018-03-22 05:03:03 +00:00
|
|
|
VertexPointer vertices = lovrGraphicsGetVertexPointer((segments + 1) * (segments + 1));
|
2018-07-17 06:51:11 +00:00
|
|
|
IndexPointer indices = lovrMeshWriteIndices(state.defaultMesh, segments * segments * 6, sizeof(uint32_t));
|
2017-06-22 06:10:45 +00:00
|
|
|
|
|
|
|
for (int i = 0; i <= segments; i++) {
|
|
|
|
float v = i / (float) segments;
|
|
|
|
for (int j = 0; j <= segments; j++) {
|
|
|
|
float u = j / (float) segments;
|
|
|
|
|
2017-08-16 04:21:49 +00:00
|
|
|
float x = sin(u * 2 * M_PI) * sin(v * M_PI);
|
2017-06-22 06:10:45 +00:00
|
|
|
float y = cos(v * M_PI);
|
2017-08-16 04:21:49 +00:00
|
|
|
float z = -cos(u * 2 * M_PI) * sin(v * M_PI);
|
2018-08-02 04:06:16 +00:00
|
|
|
memcpy(vertices.floats, ((float[]) { x, y, z, x, y, z, u, 1 - v }), 8 * sizeof(float));
|
2018-07-18 02:20:40 +00:00
|
|
|
vertices.floats += 8;
|
2017-06-22 06:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < segments; i++) {
|
|
|
|
unsigned int offset0 = i * (segments + 1);
|
|
|
|
unsigned int offset1 = (i + 1) * (segments + 1);
|
|
|
|
for (int j = 0; j < segments; j++) {
|
2018-07-14 00:12:30 +00:00
|
|
|
unsigned int i0 = offset0 + j;
|
|
|
|
unsigned int i1 = offset1 + j;
|
2018-08-02 04:06:16 +00:00
|
|
|
memcpy(indices.ints, ((uint32_t[]) { i0, i1, i0 + 1, i1, i1 + 1, i0 + 1 }), 6 * sizeof(uint32_t));
|
2018-07-18 02:20:40 +00:00
|
|
|
indices.ints += 6;
|
2017-06-22 06:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.transform = transform,
|
|
|
|
.material = material,
|
|
|
|
.mode = MESH_TRIANGLES,
|
|
|
|
.range = { 0, segments * segments * 6 }
|
|
|
|
});
|
2017-06-22 06:10:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 23:56:00 +00:00
|
|
|
void lovrGraphicsSkybox(Texture* texture, float angle, float ax, float ay, float az) {
|
2018-07-09 07:51:09 +00:00
|
|
|
TextureType type = lovrTextureGetType(texture);
|
2018-03-05 10:02:25 +00:00
|
|
|
lovrAssert(type == TEXTURE_CUBE || type == TEXTURE_2D, "Only 2D and cube textures can be used as skyboxes");
|
2018-07-15 01:26:02 +00:00
|
|
|
lovrGraphicsPushPipeline();
|
2018-02-26 10:10:53 +00:00
|
|
|
lovrGraphicsSetWinding(WINDING_COUNTERCLOCKWISE);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.shader = type == TEXTURE_CUBE ? SHADER_CUBE : SHADER_PANO,
|
2018-07-15 06:50:06 +00:00
|
|
|
.textures[TEXTURE_DIFFUSE] = texture,
|
|
|
|
.textures[TEXTURE_ENVIRONMENT_MAP] = texture,
|
2018-07-14 00:12:30 +00:00
|
|
|
.mode = MESH_TRIANGLE_STRIP,
|
|
|
|
.vertex.count = 4,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
-1, 1, 1, 0, 0, 0, 0, 0,
|
|
|
|
-1, -1, 1, 0, 0, 0, 0, 0,
|
|
|
|
1, 1, 1, 0, 0, 0, 0, 0,
|
|
|
|
1, -1, 1, 0, 0, 0, 0, 0
|
|
|
|
}
|
|
|
|
});
|
2018-07-15 01:26:02 +00:00
|
|
|
lovrGraphicsPopPipeline();
|
2016-10-24 22:02:23 +00:00
|
|
|
}
|
2017-02-03 23:16:30 +00:00
|
|
|
|
2017-03-16 03:12:56 +00:00
|
|
|
void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign) {
|
2017-08-08 08:36:29 +00:00
|
|
|
Font* font = lovrGraphicsGetFont();
|
2017-08-02 02:58:24 +00:00
|
|
|
float scale = 1 / font->pixelDensity;
|
|
|
|
float offsety;
|
2018-03-22 05:03:03 +00:00
|
|
|
uint32_t vertexCount;
|
|
|
|
uint32_t maxVertices = strlen(str) * 6;
|
|
|
|
VertexPointer vertexPointer = lovrGraphicsGetVertexPointer(maxVertices);
|
|
|
|
lovrFontRender(font, str, wrap, halign, valign, vertexPointer, &offsety, &vertexCount);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrMeshWriteIndices(state.defaultMesh, 0, 0);
|
2017-08-02 02:58:24 +00:00
|
|
|
|
|
|
|
lovrGraphicsPush();
|
2018-03-05 07:06:34 +00:00
|
|
|
lovrGraphicsMatrixTransform(transform);
|
|
|
|
lovrGraphicsScale(scale, scale, scale);
|
|
|
|
lovrGraphicsTranslate(0, offsety, 0);
|
2018-07-15 01:26:02 +00:00
|
|
|
lovrGraphicsPushPipeline();
|
|
|
|
state.pipelines[state.pipeline].depthWrite = false;
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.shader = SHADER_FONT,
|
2018-07-15 06:50:06 +00:00
|
|
|
.textures[TEXTURE_DIFFUSE] = font->texture,
|
2018-07-14 00:12:30 +00:00
|
|
|
.mode = MESH_TRIANGLES,
|
|
|
|
.range = { 0, vertexCount }
|
|
|
|
});
|
2018-07-15 01:26:02 +00:00
|
|
|
lovrGraphicsPopPipeline();
|
2017-08-02 02:58:24 +00:00
|
|
|
lovrGraphicsPop();
|
2017-02-03 23:16:30 +00:00
|
|
|
}
|
2017-08-08 09:13:07 +00:00
|
|
|
|
2018-03-19 19:12:34 +00:00
|
|
|
void lovrGraphicsFill(Texture* texture) {
|
2018-07-15 01:26:02 +00:00
|
|
|
lovrGraphicsPushPipeline();
|
2018-03-19 19:47:35 +00:00
|
|
|
lovrGraphicsSetDepthTest(COMPARE_NONE, false);
|
2018-07-17 06:51:11 +00:00
|
|
|
lovrGraphicsDraw(&(DrawOptions) {
|
2018-07-14 00:12:30 +00:00
|
|
|
.shader = SHADER_FILL,
|
2018-07-15 06:50:06 +00:00
|
|
|
.textures[TEXTURE_DIFFUSE] = texture,
|
2018-07-14 00:12:30 +00:00
|
|
|
.mode = MESH_TRIANGLE_STRIP,
|
|
|
|
.vertex.count = 4,
|
|
|
|
.vertex.data = (float[]) {
|
|
|
|
-1, 1, 0, 0, 0, 0, 0, 1,
|
|
|
|
-1, -1, 0, 0, 0, 0, 0, 0,
|
|
|
|
1, 1, 0, 0, 0, 0, 1, 1,
|
|
|
|
1, -1, 0, 0, 0, 0, 1, 0
|
|
|
|
}
|
|
|
|
});
|
2018-07-15 01:26:02 +00:00
|
|
|
lovrGraphicsPopPipeline();
|
2018-03-11 07:18:07 +00:00
|
|
|
}
|