Merge pull request #43 from h4570/feature/ambient-color

Renderer: added setAmbientLight() and setWorldColor()
This commit is contained in:
Michał Mostowik
2020-12-07 23:50:41 +01:00
committed by GitHub
8 changed files with 54 additions and 22 deletions
+5 -3
View File
@@ -17,6 +17,7 @@
#include <draw_primitives.h>
#include <math3d.h>
#include <packet2.h>
#include "./light.hpp"
#include "../models/mesh.hpp"
#include "../models/math/matrix.hpp"
#include "../models/screen_settings.hpp"
@@ -29,17 +30,18 @@ class GifSender
{
public:
GifSender(u32 t_packetSize, ScreenSettings *t_screen);
GifSender(u32 t_packetSize, ScreenSettings *t_screen, Light *t_light);
~GifSender();
void initPacket(u8 context);
void addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer);
void addClear(zbuffer_t *t_zBuffer);
void addClear(zbuffer_t *t_zBuffer, color_t *t_rgb);
void sendPacket();
void sendClear(zbuffer_t *t_zBuffer);
void sendClear(zbuffer_t *t_zBuffer, color_t *t_rgb);
static void sendTexture(Texture &texture, texbuffer_t *t_texBuffer);
private:
Light *light;
xyz_t *xyz;
color_t *rgbaq;
texel_t *st;
+6 -2
View File
@@ -23,10 +23,14 @@ public:
Light();
~Light();
static u16 getLightsCount(u32 t_bulbsCount);
static void calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_lightsCount, Vector3 t_objPosition);
u16 getLightsCount(u32 t_bulbsCount);
/** Values from 0.0000F to 1.0000F */
void setAmbientLight(const Vector3 &t_rgb);
void calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int *t_lightTypes, LightBulb *t_bulbs, u32 t_lightsCount, Vector3 t_objPosition);
private:
Vector3 ambientLight;
};
#endif
+12 -1
View File
@@ -16,6 +16,7 @@
#include <packet2.h>
#include "gif_sender.hpp"
#include "vif_sender.hpp"
#include "light.hpp"
#include "../models/math/plane.hpp"
#include "../models/sprite.hpp"
#include "../models/screen_settings.hpp"
@@ -102,11 +103,19 @@ public:
void setCameraDefinitions(Matrix *t_worldView, Vector3 *t_cameraPos, Plane *t_planes);
void setWorldColor(const color_t &t_rgb);
void endFrame(float fps);
TextureRepository *getTextureRepository() { return &textureRepo; };
void setAmbientLight(const Vector3 &t_rgb) { light.setAmbientLight(t_rgb); }
TextureRepository *getTextureRepository()
{
return &textureRepo;
};
private:
// We have some GCC bug here. Just try to reorder declarations. For example move worldColor up - game will crash.
void changeTexture(Texture *t_tex);
u32 lastTextureId;
texbuffer_t textureBuffer;
@@ -117,12 +126,14 @@ private:
void beginFrameIfNeeded();
u8 isFrameEmpty;
Matrix perspective;
Light light;
RenderData renderData;
TextureRepository textureRepo;
ScreenSettings *screen;
GifSender *gifSender;
VifSender *vifSender;
packet2_t *flipPacket;
color_t worldColor;
void allocateBuffers(float t_screenW, float t_screenH);
void initDrawingEnv(float t_screenW, float t_screenH);
void setPrim();
+3 -1
View File
@@ -14,6 +14,7 @@
#include <tamtypes.h>
#include <packet2_utils.h>
#include "../models/render_data.hpp"
#include "./light.hpp"
#include "../models/light_bulb.hpp"
#include "../models/mesh.hpp"
#include "../models/math/matrix.hpp"
@@ -24,7 +25,7 @@ class VifSender
{
public:
VifSender();
VifSender(Light *t_light);
~VifSender();
// TODO refactor
@@ -32,6 +33,7 @@ public:
void sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation);
private:
Light *light;
void uploadMicroProgram();
void setDoubleBuffer();
void drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *textureBuffer);
+8 -8
View File
@@ -12,7 +12,6 @@
#include "../include/utils/math.hpp"
#include "../include/utils/debug.hpp"
#include "../include/modules/light.hpp"
#include <packet2_chain.h>
#include <kernel.h>
#include <dma.h>
@@ -29,9 +28,10 @@
/** Initializes vars and creates data transfer packets
* @param packetSize Size of data packet, should be increased when more data will be rendered
*/
GifSender::GifSender(u32 t_packetSize, ScreenSettings *t_screen) : screen(t_screen)
GifSender::GifSender(u32 t_packetSize, ScreenSettings *t_screen, Light *t_light) : screen(t_screen)
{
PRINT_LOG("Initializing GifSender");
light = t_light;
packetSize = t_packetSize;
packets[0] = packet2_create(t_packetSize, P2_TYPE_NORMAL, P2_MODE_CHAIN, false);
packets[1] = packet2_create(t_packetSize, P2_TYPE_NORMAL, P2_MODE_CHAIN, false);
@@ -76,7 +76,7 @@ void GifSender::sendTexture(Texture &texture, texbuffer_t *t_texBuffer)
packet2_free(packet2);
}
void GifSender::sendClear(zbuffer_t *t_zBuffer)
void GifSender::sendClear(zbuffer_t *t_zBuffer, color_t *t_rgb)
{
packet2_t *packet2 = packet2_create(36, P2_TYPE_NORMAL, P2_MODE_CHAIN, false);
packet2_chain_open_end(packet2, 0, 0);
@@ -84,7 +84,7 @@ void GifSender::sendClear(zbuffer_t *t_zBuffer)
packet2_update(packet2, draw_clear(packet2->next, 0,
2048.0F - (screen->width / 2), 2048.0F - (screen->height / 2),
screen->width, screen->height,
0x10, 0x10, 0x10));
t_rgb->r, t_rgb->g, t_rgb->b));
packet2_update(packet2, draw_enable_tests(packet2->next, 0, t_zBuffer));
packet2_update(packet2, draw_finish(packet2->next));
packet2_chain_close_tag(packet2);
@@ -118,14 +118,14 @@ void GifSender::sendPacket()
}
/** Adds clear screen to current packet */
void GifSender::addClear(zbuffer_t *t_zBuffer)
void GifSender::addClear(zbuffer_t *t_zBuffer, color_t *t_rgb)
{
packet2_chain_open_cnt(currentPacket, 0, 0, 0);
packet2_update(currentPacket, draw_disable_tests(currentPacket->next, 0, t_zBuffer));
packet2_update(currentPacket, draw_clear(currentPacket->next, 0,
2048.0F - (screen->width / 2), 2048.0F - (screen->height / 2),
screen->width, screen->height,
0x10, 0x10, 0x10));
t_rgb->r, t_rgb->g, t_rgb->b));
packet2_update(currentPacket, draw_enable_tests(currentPacket->next, 0, t_zBuffer));
packet2_chain_close_tag(currentPacket);
}
@@ -180,7 +180,7 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount
if (SHOULD_BE_LIGHTED)
{
const u16 lightsCount = Light::getLightsCount(t_bulbsCount);
const u16 lightsCount = light->getLightsCount(t_bulbsCount);
VECTOR *lightDirections = new VECTOR[lightsCount];
VECTOR *lightColors = new VECTOR[lightsCount];
int *lightTypes = new int[lightsCount];
@@ -188,7 +188,7 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount
VECTOR *lights = new VECTOR[vertexCount];
calculate_normals(normals, vertexCount, normals, localLight);
Light::calculateLight(lightDirections, lightColors, lightTypes, t_bulbs, lightsCount, t_mesh.position);
light->calculateLight(lightDirections, lightColors, lightTypes, t_bulbs, lightsCount, t_mesh.position);
calculate_lights(lights, vertexCount, normals, lightDirections, lightColors, lightTypes, lightsCount);
+5 -3
View File
@@ -26,6 +26,8 @@ const u8 ADDITIONAL_LIGHTS = 1; // Ambient
const float LIGHT_MAX = 255.0F;
const float LIGHT_DIS_MOD = 4.0F;
void Light::setAmbientLight(const Vector3 &t_rgb) { ambientLight.set(t_rgb); }
u16 Light::getLightsCount(u32 t_bulbsCount) { return t_bulbsCount + ADDITIONAL_LIGHTS; }
/** Calculates lighting. Used in gifSender in object 3D calculations */
@@ -40,9 +42,9 @@ void Light::calculateLight(VECTOR *t_lightDirections, VECTOR *t_lightColors, int
t_lightDirections[0][2] = 0.0F;
t_lightDirections[0][3] = 1.0F;
t_lightColors[0][0] = 0.0F;
t_lightColors[0][1] = 0.0F;
t_lightColors[0][2] = 0.0F;
t_lightColors[0][0] = ambientLight.x;
t_lightColors[0][1] = ambientLight.y;
t_lightColors[0][2] = ambientLight.z;
t_lightColors[0][3] = 1.0F;
// ---
+13 -3
View File
@@ -44,9 +44,12 @@ Renderer::Renderer(u32 t_packetSize, ScreenSettings *t_screen)
allocateBuffers(t_screen->width, t_screen->height);
initDrawingEnv(t_screen->width, t_screen->height);
setPrim();
worldColor.r = 0x10;
worldColor.g = 0x10;
worldColor.b = 0x10;
screen = t_screen;
gifSender = new GifSender(t_packetSize, t_screen);
vifSender = new VifSender();
gifSender = new GifSender(t_packetSize, t_screen, &light);
vifSender = new VifSender(&light);
perspective.setPerspective(*t_screen);
renderData.perspective = &perspective;
PRINT_LOG("Renderer initialized!");
@@ -187,6 +190,13 @@ void Renderer::setPrim()
PRINT_LOG("Prim set!");
}
void Renderer::setWorldColor(const color_t &t_rgb)
{
worldColor.r = t_rgb.r;
worldColor.g = t_rgb.g;
worldColor.b = t_rgb.b;
}
/** Defines and allocates framebuffers and zbuffer */
void Renderer::allocateBuffers(float t_screenW, float t_screenH)
{
@@ -305,7 +315,7 @@ void Renderer::beginFrameIfNeeded()
if (isFrameEmpty)
{
isFrameEmpty = false;
gifSender->sendClear(&zBuffer);
gifSender->sendClear(&zBuffer, &worldColor);
}
}
+2 -1
View File
@@ -29,8 +29,9 @@ extern u32 VU1Draw3D_CodeStart __attribute__((section(".vudata")));
extern u32 VU1Draw3D_CodeEnd __attribute__((section(".vudata")));
//
VifSender::VifSender()
VifSender::VifSender(Light *t_light)
{
light = t_light;
PRINT_LOG("Initializing VifSender");
PRINT_LOG("VifSender initialized!");
dma_channel_initialize(DMA_CHANNEL_VIF1, NULL, 0);