mirror of https://github.com/bjornbytes/lovr.git
Material graphics state;
This commit is contained in:
parent
d658c80e57
commit
16915e4fa2
|
@ -340,6 +340,22 @@ int l_lovrGraphicsSetLineWidth(lua_State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsGetMaterial(lua_State* L) {
|
||||
Material* material = lovrGraphicsGetMaterial();
|
||||
if (material && !material->isDefault) {
|
||||
luax_pushtype(L, Material, material);
|
||||
} else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsSetMaterial(lua_State* L) {
|
||||
Material* material = lua_isnoneornil(L, 1) ? NULL : luax_checktype(L, 1, Material);
|
||||
lovrGraphicsSetMaterial(material);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsGetPointSize(lua_State* L) {
|
||||
lua_pushnumber(L, lovrGraphicsGetPointSize());
|
||||
return 1;
|
||||
|
@ -755,6 +771,8 @@ const luaL_Reg lovrGraphics[] = {
|
|||
{ "getSystemLimits", l_lovrGraphicsGetSystemLimits },
|
||||
{ "getLineWidth", l_lovrGraphicsGetLineWidth },
|
||||
{ "setLineWidth", l_lovrGraphicsSetLineWidth },
|
||||
{ "getMaterial", l_lovrGraphicsGetMaterial },
|
||||
{ "setMaterial", l_lovrGraphicsSetMaterial },
|
||||
{ "getPointSize", l_lovrGraphicsGetPointSize },
|
||||
{ "setPointSize", l_lovrGraphicsSetPointSize },
|
||||
{ "getShader", l_lovrGraphicsGetShader },
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "graphics/graphics.h"
|
||||
#include "graphics/shaders.h"
|
||||
#include "loaders/texture.h"
|
||||
#include "loaders/font.h"
|
||||
#include "event/event.h"
|
||||
|
@ -35,6 +36,7 @@ void lovrGraphicsDestroy() {
|
|||
for (int i = 0; i < DEFAULT_SHADER_COUNT; i++) {
|
||||
if (state.defaultShaders[i]) lovrRelease(&state.defaultShaders[i]->ref);
|
||||
}
|
||||
if (state.defaultMaterial) lovrRelease(&state.defaultMaterial->ref);
|
||||
if (state.defaultFont) lovrRelease(&state.defaultFont->ref);
|
||||
if (state.defaultTexture) lovrRelease(&state.defaultTexture->ref);
|
||||
glDeleteVertexArrays(1, &state.streamVAO);
|
||||
|
@ -115,6 +117,20 @@ void lovrGraphicsPrepare() {
|
|||
float color[4] = { state.color.r / 255., state.color.g / 255., state.color.b / 255., state.color.a / 255. };
|
||||
lovrShaderSetFloat(shader, "lovrColor", color, 4);
|
||||
|
||||
// Material
|
||||
Material* material = lovrGraphicsGetMaterial();
|
||||
|
||||
for (int i = 0; i < MAX_MATERIAL_COLORS; i++) {
|
||||
Color color = lovrMaterialGetColor(material, i);
|
||||
float data[4] = { color.r / 255., color.g / 255., color.b / 255., color.a / 255. };
|
||||
lovrShaderSetFloat(shader, lovrShaderColorUniforms[i], data, 4);
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_MATERIAL_TEXTURES; i++) {
|
||||
Texture* texture = lovrMaterialGetTexture(material, i);
|
||||
lovrShaderSetTexture(shader, lovrShaderTextureUniforms[i], &texture, 1);
|
||||
}
|
||||
|
||||
lovrGraphicsBindProgram(shader->id);
|
||||
lovrShaderBind(shader);
|
||||
}
|
||||
|
@ -353,6 +369,31 @@ void lovrGraphicsSetLineWidth(float width) {
|
|||
glLineWidth(width);
|
||||
}
|
||||
|
||||
Material* lovrGraphicsGetMaterial() {
|
||||
if (!state.material) {
|
||||
if (!state.defaultMaterial) {
|
||||
MaterialData* materialData = lovrMaterialDataCreateEmpty();
|
||||
state.defaultMaterial = lovrMaterialCreate(materialData, 1);
|
||||
}
|
||||
|
||||
lovrGraphicsSetMaterial(state.defaultMaterial);
|
||||
}
|
||||
|
||||
return state.material;
|
||||
}
|
||||
|
||||
void lovrGraphicsSetMaterial(Material* material) {
|
||||
if (state.material) {
|
||||
lovrRelease(&state.material->ref);
|
||||
}
|
||||
|
||||
state.material = material;
|
||||
|
||||
if (material) {
|
||||
lovrRetain(&material->ref);
|
||||
}
|
||||
}
|
||||
|
||||
float lovrGraphicsGetPointSize() {
|
||||
return state.pointSize;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "graphics/font.h"
|
||||
#include "graphics/material.h"
|
||||
#include "graphics/shader.h"
|
||||
#include "graphics/texture.h"
|
||||
#include "math/math.h"
|
||||
|
@ -71,6 +72,7 @@ typedef struct {
|
|||
GLFWwindow* window;
|
||||
Shader* defaultShaders[DEFAULT_SHADER_COUNT];
|
||||
DefaultShader defaultShader;
|
||||
Material* defaultMaterial;
|
||||
Font* defaultFont;
|
||||
Texture* defaultTexture;
|
||||
float transforms[MAX_TRANSFORMS + INTERNAL_TRANSFORMS][2][16];
|
||||
|
@ -85,6 +87,7 @@ typedef struct {
|
|||
Font* font;
|
||||
GraphicsLimits limits;
|
||||
float lineWidth;
|
||||
Material* material;
|
||||
float pointSize;
|
||||
Shader* shader;
|
||||
Winding winding;
|
||||
|
@ -132,6 +135,8 @@ void lovrGraphicsSetFont(Font* font);
|
|||
GraphicsLimits lovrGraphicsGetLimits();
|
||||
float lovrGraphicsGetLineWidth();
|
||||
void lovrGraphicsSetLineWidth(float width);
|
||||
Material* lovrGraphicsGetMaterial();
|
||||
void lovrGraphicsSetMaterial(Material* material);
|
||||
float lovrGraphicsGetPointSize();
|
||||
void lovrGraphicsSetPointSize(float size);
|
||||
Shader* lovrGraphicsGetShader();
|
||||
|
|
|
@ -11,6 +11,8 @@ Material* lovrMaterialCreate(MaterialData* materialData, int isDefault) {
|
|||
for (int i = 0; i < MAX_MATERIAL_TEXTURES; i++) {
|
||||
if (materialData->textures[i]) {
|
||||
material->textures[i] = lovrTextureCreate(TEXTURE_2D, &materialData->textures[i], 1);
|
||||
} else {
|
||||
material->textures[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
#include "graphics/shaders.h"
|
||||
|
||||
const char* lovrShaderColorUniforms[] = {
|
||||
"lovrDiffuseColor"
|
||||
};
|
||||
|
||||
const char* lovrShaderTextureUniforms[] = {
|
||||
"lovrDiffuseTexture"
|
||||
};
|
||||
|
||||
const char* lovrShaderVertexPrefix = ""
|
||||
#ifdef EMSCRIPTEN
|
||||
"#version 300 es \n"
|
||||
|
@ -28,7 +36,8 @@ const char* lovrShaderFragmentPrefix = ""
|
|||
"in vec2 texCoord; \n"
|
||||
"out vec4 lovrFragColor; \n"
|
||||
"uniform vec4 lovrColor; \n"
|
||||
"uniform sampler2D lovrTexture; \n";
|
||||
"uniform vec4 lovrDiffuseColor; \n"
|
||||
"uniform sampler2D lovrDiffuseTexture; \n";
|
||||
|
||||
const char* lovrShaderVertexSuffix = ""
|
||||
"void main() { \n"
|
||||
|
@ -38,7 +47,7 @@ const char* lovrShaderVertexSuffix = ""
|
|||
|
||||
const char* lovrShaderFragmentSuffix = ""
|
||||
"void main() { \n"
|
||||
" lovrFragColor = color(lovrColor, lovrTexture, texCoord); \n"
|
||||
" lovrFragColor = color(lovrColor, lovrDiffuseTexture, texCoord); \n"
|
||||
"}";
|
||||
|
||||
const char* lovrDefaultVertexShader = ""
|
||||
|
@ -48,7 +57,7 @@ const char* lovrDefaultVertexShader = ""
|
|||
|
||||
const char* lovrDefaultFragmentShader = ""
|
||||
"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n"
|
||||
" return graphicsColor * texture(image, uv); \n"
|
||||
" return graphicsColor * lovrDiffuseColor * texture(image, uv); \n"
|
||||
"}";
|
||||
|
||||
const char* lovrSkyboxVertexShader = ""
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
extern const char* lovrShaderColorUniforms[];
|
||||
extern const char* lovrShaderTextureUniforms[];
|
||||
extern const char* lovrShaderVertexPrefix;
|
||||
extern const char* lovrShaderVertexSuffix;
|
||||
extern const char* lovrShaderFragmentPrefix;
|
||||
|
|
Loading…
Reference in New Issue