mirror of https://github.com/bjornbytes/lovr.git
Anonymous materials for draw calls;
This commit is contained in:
parent
fe75c666a9
commit
0ee5d0b8f2
|
@ -57,7 +57,12 @@ int l_lovrMeshDrawInstanced(lua_State* L) {
|
||||||
int instances = luaL_checkinteger(L, 2);
|
int instances = luaL_checkinteger(L, 2);
|
||||||
float transform[16];
|
float transform[16];
|
||||||
luax_readtransform(L, 3, transform, 1);
|
luax_readtransform(L, 3, transform, 1);
|
||||||
lovrGraphicsDraw(&(GraphicsDraw) { .transform = transform, .mesh = mesh, .instances = instances });
|
lovrGraphicsDraw(&(GraphicsDraw) {
|
||||||
|
.transform = transform,
|
||||||
|
.mesh = mesh,
|
||||||
|
.material = lovrMeshGetMaterial(mesh),
|
||||||
|
.instances = instances
|
||||||
|
});
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -384,6 +384,7 @@ void lovrGraphicsLine(uint32_t count) {
|
||||||
void lovrGraphicsTriangle(DrawMode mode, Material* material, float points[9]) {
|
void lovrGraphicsTriangle(DrawMode mode, Material* material, float points[9]) {
|
||||||
if (mode == DRAW_MODE_LINE) {
|
if (mode == DRAW_MODE_LINE) {
|
||||||
lovrGraphicsDraw(&(GraphicsDraw) {
|
lovrGraphicsDraw(&(GraphicsDraw) {
|
||||||
|
.material = material,
|
||||||
.mode = MESH_LINE_LOOP,
|
.mode = MESH_LINE_LOOP,
|
||||||
.vertex.count = 3,
|
.vertex.count = 3,
|
||||||
.vertex.data = (float[]) {
|
.vertex.data = (float[]) {
|
||||||
|
@ -396,6 +397,7 @@ void lovrGraphicsTriangle(DrawMode mode, Material* material, float points[9]) {
|
||||||
float normal[3];
|
float normal[3];
|
||||||
vec3_cross(vec3_init(normal, &points[0]), &points[3]);
|
vec3_cross(vec3_init(normal, &points[0]), &points[3]);
|
||||||
lovrGraphicsDraw(&(GraphicsDraw) {
|
lovrGraphicsDraw(&(GraphicsDraw) {
|
||||||
|
.material = material,
|
||||||
.mode = MESH_TRIANGLES,
|
.mode = MESH_TRIANGLES,
|
||||||
.vertex.count = 3,
|
.vertex.count = 3,
|
||||||
.vertex.data = (float[]) {
|
.vertex.data = (float[]) {
|
||||||
|
@ -690,13 +692,12 @@ void lovrGraphicsSphere(Material* material, mat4 transform, int segments) {
|
||||||
void lovrGraphicsSkybox(Texture* texture, float angle, float ax, float ay, float az) {
|
void lovrGraphicsSkybox(Texture* texture, float angle, float ax, float ay, float az) {
|
||||||
TextureType type = lovrTextureGetType(texture);
|
TextureType type = lovrTextureGetType(texture);
|
||||||
lovrAssert(type == TEXTURE_CUBE || type == TEXTURE_2D, "Only 2D and cube textures can be used as skyboxes");
|
lovrAssert(type == TEXTURE_CUBE || type == TEXTURE_2D, "Only 2D and cube textures can be used as skyboxes");
|
||||||
MaterialTexture materialTexture = type == TEXTURE_CUBE ? TEXTURE_ENVIRONMENT_MAP : TEXTURE_DIFFUSE;
|
|
||||||
lovrGraphicsPushPipeline();
|
lovrGraphicsPushPipeline();
|
||||||
lovrGraphicsSetWinding(WINDING_COUNTERCLOCKWISE);
|
lovrGraphicsSetWinding(WINDING_COUNTERCLOCKWISE);
|
||||||
Material* material = lovrGraphicsGetDefaultMaterial();
|
|
||||||
lovrMaterialSetTexture(material, materialTexture, texture);
|
|
||||||
lovrGraphicsDraw(&(GraphicsDraw) {
|
lovrGraphicsDraw(&(GraphicsDraw) {
|
||||||
.shader = type == TEXTURE_CUBE ? SHADER_CUBE : SHADER_PANO,
|
.shader = type == TEXTURE_CUBE ? SHADER_CUBE : SHADER_PANO,
|
||||||
|
.textures[TEXTURE_DIFFUSE] = texture,
|
||||||
|
.textures[TEXTURE_ENVIRONMENT_MAP] = texture,
|
||||||
.mode = MESH_TRIANGLE_STRIP,
|
.mode = MESH_TRIANGLE_STRIP,
|
||||||
.vertex.count = 4,
|
.vertex.count = 4,
|
||||||
.vertex.data = (float[]) {
|
.vertex.data = (float[]) {
|
||||||
|
@ -706,7 +707,6 @@ void lovrGraphicsSkybox(Texture* texture, float angle, float ax, float ay, float
|
||||||
1, -1, 1, 0, 0, 0, 0, 0
|
1, -1, 1, 0, 0, 0, 0, 0
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
lovrMaterialSetTexture(material, materialTexture, NULL);
|
|
||||||
lovrGraphicsPopPipeline();
|
lovrGraphicsPopPipeline();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -724,18 +724,15 @@ void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAl
|
||||||
lovrGraphicsMatrixTransform(transform);
|
lovrGraphicsMatrixTransform(transform);
|
||||||
lovrGraphicsScale(scale, scale, scale);
|
lovrGraphicsScale(scale, scale, scale);
|
||||||
lovrGraphicsTranslate(0, offsety, 0);
|
lovrGraphicsTranslate(0, offsety, 0);
|
||||||
Material* material = lovrGraphicsGetDefaultMaterial();
|
|
||||||
lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, font->texture);
|
|
||||||
lovrGraphicsPushPipeline();
|
lovrGraphicsPushPipeline();
|
||||||
state.pipelines[state.pipeline].depthWrite = false;
|
state.pipelines[state.pipeline].depthWrite = false;
|
||||||
lovrGraphicsDraw(&(GraphicsDraw) {
|
lovrGraphicsDraw(&(GraphicsDraw) {
|
||||||
.shader = SHADER_FONT,
|
.shader = SHADER_FONT,
|
||||||
.material = material,
|
.textures[TEXTURE_DIFFUSE] = font->texture,
|
||||||
.mode = MESH_TRIANGLES,
|
.mode = MESH_TRIANGLES,
|
||||||
.range = { 0, vertexCount }
|
.range = { 0, vertexCount }
|
||||||
});
|
});
|
||||||
lovrGraphicsPopPipeline();
|
lovrGraphicsPopPipeline();
|
||||||
lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, NULL);
|
|
||||||
lovrGraphicsPop();
|
lovrGraphicsPop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -776,11 +773,9 @@ void lovrGraphicsStencil(StencilAction action, int replaceValue, StencilCallback
|
||||||
void lovrGraphicsFill(Texture* texture) {
|
void lovrGraphicsFill(Texture* texture) {
|
||||||
lovrGraphicsPushPipeline();
|
lovrGraphicsPushPipeline();
|
||||||
lovrGraphicsSetDepthTest(COMPARE_NONE, false);
|
lovrGraphicsSetDepthTest(COMPARE_NONE, false);
|
||||||
Material* material = lovrGraphicsGetDefaultMaterial();
|
|
||||||
lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, texture);
|
|
||||||
lovrGraphicsDraw(&(GraphicsDraw) {
|
lovrGraphicsDraw(&(GraphicsDraw) {
|
||||||
.shader = SHADER_FILL,
|
.shader = SHADER_FILL,
|
||||||
.material = material,
|
.textures[TEXTURE_DIFFUSE] = texture,
|
||||||
.mode = MESH_TRIANGLE_STRIP,
|
.mode = MESH_TRIANGLE_STRIP,
|
||||||
.vertex.count = 4,
|
.vertex.count = 4,
|
||||||
.vertex.data = (float[]) {
|
.vertex.data = (float[]) {
|
||||||
|
@ -790,7 +785,6 @@ void lovrGraphicsFill(Texture* texture) {
|
||||||
1, -1, 0, 0, 0, 0, 1, 0
|
1, -1, 0, 0, 0, 0, 1, 0
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, NULL);
|
|
||||||
lovrGraphicsPopPipeline();
|
lovrGraphicsPopPipeline();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -822,8 +816,18 @@ void lovrGraphicsDraw(GraphicsDraw* draw) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Material* material = draw->material ? draw->material : lovrMeshGetMaterial(mesh);
|
Material* material = draw->material;
|
||||||
material = material ? material : lovrGraphicsGetDefaultMaterial();
|
if (!material) {
|
||||||
|
material = state.defaultMaterial;
|
||||||
|
|
||||||
|
if (!material) {
|
||||||
|
material = state.defaultMaterial = lovrMaterialCreate(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_MATERIAL_TEXTURES; i++) {
|
||||||
|
lovrMaterialSetTexture(material, i, draw->textures[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
gpuDraw(&(GpuDrawCommand) {
|
gpuDraw(&(GpuDrawCommand) {
|
||||||
.layer = state.layers[state.layer],
|
.layer = state.layers[state.layer],
|
||||||
|
@ -909,11 +913,3 @@ void lovrGraphicsSetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t he
|
||||||
state.layers[state.layer].viewport[2] = width;
|
state.layers[state.layer].viewport[2] = width;
|
||||||
state.layers[state.layer].viewport[3] = height;
|
state.layers[state.layer].viewport[3] = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
Material* lovrGraphicsGetDefaultMaterial() {
|
|
||||||
if (!state.defaultMaterial) {
|
|
||||||
state.defaultMaterial = lovrMaterialCreate(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return state.defaultMaterial;
|
|
||||||
}
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ typedef struct {
|
||||||
mat4 transform;
|
mat4 transform;
|
||||||
DefaultShader shader;
|
DefaultShader shader;
|
||||||
Material* material;
|
Material* material;
|
||||||
|
Texture* textures[MAX_MATERIAL_TEXTURES];
|
||||||
Mesh* mesh;
|
Mesh* mesh;
|
||||||
MeshDrawMode mode;
|
MeshDrawMode mode;
|
||||||
struct {
|
struct {
|
||||||
|
@ -139,4 +140,3 @@ void lovrGraphicsPushPipeline();
|
||||||
void lovrGraphicsPopPipeline();
|
void lovrGraphicsPopPipeline();
|
||||||
void lovrGraphicsSetCamera(mat4 projection, mat4 view);
|
void lovrGraphicsSetCamera(mat4 projection, mat4 view);
|
||||||
void lovrGraphicsSetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
|
void lovrGraphicsSetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
|
||||||
Material* lovrGraphicsGetDefaultMaterial();
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ static void renderNode(Model* model, int nodeIndex, int instances) {
|
||||||
lovrGraphicsDraw(&(GraphicsDraw) {
|
lovrGraphicsDraw(&(GraphicsDraw) {
|
||||||
.transform = model->nodeTransforms[nodeIndex],
|
.transform = model->nodeTransforms[nodeIndex],
|
||||||
.mesh = model->mesh,
|
.mesh = model->mesh,
|
||||||
|
.material = lovrMeshGetMaterial(model->mesh),
|
||||||
.instances = instances
|
.instances = instances
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue