Working on model loading and added poker table

This commit is contained in:
2021-05-09 14:38:08 -07:00
parent f3ffc93927
commit d6e3b4aad1
21 changed files with 1297 additions and 38 deletions

View File

@ -0,0 +1,81 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "position.h"
positiondebug_t positionDebugCreate(
primitive_t *debugPrimitive,
texture_t *fontTexture, tileset_t *fontTileset
) {
positiondebug_t debug = {
.primitive = debugPrimitive,
.textTexture = fontTexture,
.textTileset = fontTileset,
.x = 0, .y = 0, .z = 0,
.pitch = 0, .yaw = 0, .roll = 0,
.scaleX = 1, .scaleY = 1, .scaleZ = 1
};
debug.textBatch = spriteBatchCreate(POSITION_DEBUG_FONT_CHARS_MAX);
return debug;
}
void positionDebugRender(shader_t *shader, positiondebug_t *debug) {
float speed = 0.5;
// Update values.
if(inputIsDown(INPUT_DEBUG_FINE)) speed *= 0.1;
if(inputIsDown(INPUT_DEBUG_UP)) debug->z -= TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_DOWN)) debug->z += TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_LEFT)) debug->x -= TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_RIGHT)) debug->x += TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_LOWER)) debug->y -= TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_RAISE)) debug->y += TIME_STATE.delta * speed;
if(inputIsDown(INPUT_DEBUG_MINUS)) {
debug->scaleX -= TIME_STATE.delta * speed;
debug->scaleY -= TIME_STATE.delta * speed;
debug->scaleZ -= TIME_STATE.delta * speed;
}
if(inputIsDown(INPUT_DEBUG_PLUS)) {
debug->scaleX += TIME_STATE.delta * speed;
debug->scaleY += TIME_STATE.delta * speed;
debug->scaleZ += TIME_STATE.delta * speed;
}
// Render object
shaderUsePositionAndScale(shader,
debug->x, debug->y, debug->z,
mathDeg2Rad(debug->pitch),mathDeg2Rad(debug->yaw),mathDeg2Rad(debug->roll),
debug->scaleX, debug->scaleY, debug->scaleZ
);
primitiveDraw(debug->primitive, 0, -1);
// Render debug text
char text[POSITION_DEBUG_FONT_CHARS_MAX];
sprintf(text, "%.2f, %.2f, %.2f\n%.2f, %.2f, %.2f \n%.2f, %.2f, %.2f",
debug->x, debug->y, debug->z,
debug->pitch, debug->yaw, debug->roll,
debug->scaleX, debug->scaleY, debug->scaleZ
);
shaderUseTexture(shader, debug->textTexture);
shaderUsePosition(shader,
0, 1, 0,
mathDeg2Rad(-90), mathDeg2Rad(0), 0
);
spriteBatchFlush(debug->textBatch);
fontSpriteBatchBuffer(debug->textBatch, debug->textTileset, text,
FONT_CENTER_X, FONT_CENTER_Y, 0,
-1, 0.1
);
spriteBatchDraw(debug->textBatch, 0, -1);
}
void positionDebugDispose(positiondebug_t *debug) {
spriteBatchDispose(debug->textBatch);
}

View File

@ -0,0 +1,27 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../primitive.h"
#include "../texture.h"
#include "../tileset.h"
#include "../gui/font.h"
#include "../spritebatch.h"
#include "../shader.h"
#include "../../input/input.h"
#include "../../util/math.h"
positiondebug_t positionDebugCreate(
primitive_t *debugPrimitive,
texture_t *fontTexture, tileset_t *fontTileset
);
void positionDebugRender(shader_t *shader, positiondebug_t *debug);
void positionDebugDispose(positiondebug_t *debug);

View File

@ -12,14 +12,74 @@ tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character) {
return tileset->divisions + i;
}
void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
fontmeasure_t fontMeasure(char *string, float charWidth, float charHeight) {
int32_t i;
float x, y;
char c;
fontmeasure_t measure = {
.height = 0, .lines = 1, .width = 0
};
i = 0;
y = 0;
x = 0;
while(true) {
c = string[i];
if(c == '\0') break;
i++;
if(c == '\n') {
measure.width = mathMax(x, measure.width);
x = 0;
y += charHeight;
measure.lines++;
continue;
} else if(c == ' ') {
x += charWidth;
continue;
}
x += charWidth;
}
measure.width = mathMax(x, measure.width);
measure.height = y + charHeight;
return measure;
}
fontmeasure_t fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
char *string, float x, float y, float z, float charWidth, float charHeight
) {
int32_t i;
char c;
tilesetdiv_t *div;
float cx, cy;
fontmeasure_t measure;
// Detect char dimensions
if(charWidth == -1) charWidth = tileset->divX * (charHeight / tileset->divY);
if(charHeight == -1) charHeight = tileset->divY * (charWidth / tileset->divX);
// Position the text.
if(x == FONT_CENTER_X ||
y == FONT_CENTER_Y ||
x == FONT_RIGHT_X
) {
measure = fontMeasure(string, charWidth, charHeight);
if(x == FONT_CENTER_X) {
x = -(measure.width/2);
} else if(x == FONT_RIGHT_X) {
x = -measure.width;
}
if(y == FONT_CENTER_Y) y = -(measure.height/2);
}
// Begin buffering the sprite batch
measure.width = 0;
measure.height = 0;
measure.lines = 1;
i = 0;
cx = x, cy = y;
@ -28,20 +88,28 @@ void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
if(c == '\0') break;
i++;
// Special chars
if(c == '\n') {
measure.width = mathMax(cx-x, measure.width);
cx = x;
cy += charWidth;
cy += charHeight;
measure.lines++;
continue;
} else if(c == ' ') {
cx += charHeight;
cx += charWidth;
continue;
}
div = fontGetCharacterDivision(tileset, c);
spriteBatchQuad(batch, -1,
cx, cy, z, charWidth, charHeight,
div->x0, div->y0, div->x1, div->y1
div->x0, div->y1, div->x1, div->y0
);
cx += charWidth;
}
measure.width = mathMax(cx-x, measure.width);
measure.height = cy-y + charHeight;
return measure;
}

View File

@ -8,8 +8,7 @@
#pragma once
#include <dawn/dawn.h>
#include "../spritebatch.h"
#define FONT_CHAR_START 33
#include "../../util/math.h"
/**
* Get the division for a given character.
@ -20,6 +19,16 @@
*/
tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character);
/**
* Measures a string's fully rendered size.
*
* @param string The string to measure
* @param charWidth The width of each character.
* @param charHeight The height of each character.
* @return The measured string.
*/
fontmeasure_t fontMeasure(char *string, float charWidth, float charHeight);
/**
* Renders a set of font characters to the sprite. Coordinates are anchored to
* the top left (0,0) origin.
@ -30,9 +39,10 @@ tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character);
* @param x Position in X space.
* @param y Position in Y space.
* @param z Position in Z space.
* @param charWidth Width of each character.
* @param charHeight Height of each character.
* @param charWidth Width of each character. Set to -1 to use the height ratio.
* @param charHeight Height of each character. Set to -1 to be the width ratio.
* @returns The string measurement.
*/
void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
fontmeasure_t fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
char *string, float x, float y, float z, float charWidth, float charHeight
);

View File

@ -131,13 +131,41 @@ void shaderUsePosition(shader_t *shader,
//Rotation, we do each axis individually
axis[0] = 1, axis[1] = 0, axis[2] = 0;
glm_rotate(MATRIX_POSITION, pitch, axis);
axis[0] = 0, axis[1] = 1;
glm_rotate(MATRIX_POSITION, yaw, axis);
axis[1] = 0, axis[2] = 1;
glm_rotate(MATRIX_POSITION, roll, axis);
//Send to the shader.
glUniformMatrix4fv(shader->uniModl, 1, GL_FALSE, MATRIX_POSITION[0]);
}
void shaderUsePositionAndScale(shader_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ
) {
mat4 MATRIX_POSITION;
vec3 axis;
// Identify mat.
glm_mat4_identity(MATRIX_POSITION);
// Position
axis[0] = x, axis[1] = y, axis[2] = z;
glm_translate(MATRIX_POSITION, axis);
// Rotation
axis[0] = 1, axis[1] = 0, axis[2] = 0;
glm_rotate(MATRIX_POSITION, pitch, axis);
axis[0] = 0, axis[1] = 1;
glm_rotate(MATRIX_POSITION, yaw, axis);
axis[1] = 0, axis[2] = 1;
glm_rotate(MATRIX_POSITION, roll, axis);
// Scale
axis[0] = scaleX, axis[1] = scaleY, axis[2] = scaleZ;
glm_scale(MATRIX_POSITION, axis);
glUniformMatrix4fv(shader->uniModl, 1, GL_FALSE, MATRIX_POSITION[0]);
}

View File

@ -61,4 +61,26 @@ void shaderUseTexture(shader_t *shader, texture_t *texture);
void shaderUsePosition(shader_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll
);
/**
* Set's the current translation matrix onto the shader for the next
* render to use. Also provides scaling controls.
*
* @param shader Shader to attach to.
* @param x X coordinate (world space).
* @param y Y coordinate (world space).
* @param z Z coordinate (world space).
* @param pitch Pitch of the object (local space).
* @param yaw Yaw of the object (local space).
* @param roll Roll of the object (local space).
* @param scaleX X scale of model (1 being 100% scaled).
* @param scaleY Y scale of model (1 being 100% scaled).
* @param scaleZ Z scale of model (1 being 100% scaled).
*/
void shaderUsePositionAndScale(shader_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ
);