examples: Add shadow volume example

This commit is contained in:
Antonio Niño Díaz 2024-03-06 21:07:51 +00:00
parent 227bffddc5
commit 5a2b722128
7 changed files with 526 additions and 0 deletions

View File

@ -0,0 +1,220 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
include $(DEVKITARM)/ds_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
# DATA is a list of directories containing binary files embedded using bin2o
# GRAPHICS is a list of directories containing image files to be converted with grit
# AUDIO is a list of directories containing audio to be converted by maxmod
# ICON is the image used to create the game icon, leave blank to use default rule
# NITRO is a directory that will be accessible via NitroFS
#---------------------------------------------------------------------------------
TARGET := $(shell basename $(CURDIR))
BUILD := build
SOURCES := source
INCLUDES := include
DATA := data
GRAPHICS := graphics
AUDIO :=
ICON :=
# specify a directory which contains the nitro filesystem
# this is relative to the Makefile
NITRO :=
# These set the information text in the nds file
GAME_TITLE := $(shell basename $(CURDIR))
GAME_SUBTITLE1 := Nitro Engine example
GAME_SUBTITLE2 := github.com/AntonioND/nitro-engine
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -marm -mthumb-interwork -march=armv5te -mtune=arm946e-s
CFLAGS := -g -Wall -O3\
$(ARCH) $(INCLUDE) -DARM9
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project (order is important)
#---------------------------------------------------------------------------------
LIBS := -lNE -lfat -lnds9
# automatigically add libraries for NitroFS
ifneq ($(strip $(NITRO)),)
LIBS := -lfilesystem -lfat $(LIBS)
endif
# automagically add maxmod library
ifneq ($(strip $(AUDIO)),)
LIBS := -lmm9 $(LIBS)
endif
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(LIBNDS) $(PORTLIBS) $(DEVKITPRO)/nitro-engine
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(CURDIR)/$(subst /,,$(dir $(ICON)))\
$(foreach dir,$(SOURCES),$(CURDIR)/$(dir))\
$(foreach dir,$(DATA),$(CURDIR)/$(dir))\
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
PNGFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
# prepare NitroFS directory
ifneq ($(strip $(NITRO)),)
export NITRO_FILES := $(CURDIR)/$(NITRO)
endif
# get audio list for maxmod
ifneq ($(strip $(AUDIO)),)
export MODFILES := $(foreach dir,$(notdir $(wildcard $(AUDIO)/*.*)),$(CURDIR)/$(AUDIO)/$(dir))
# place the soundbank file in NitroFS if using it
ifneq ($(strip $(NITRO)),)
export SOUNDBANK := $(NITRO_FILES)/soundbank.bin
# otherwise, needs to be loaded from memory
else
export SOUNDBANK := soundbank.bin
BINFILES += $(SOUNDBANK)
endif
endif
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(PNGFILES:.png=.o) $(OFILES_BIN) $(OFILES_SOURCES)
export HFILES := $(PNGFILES:.png=.h) $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE := $(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir))\
$(foreach dir,$(LIBDIRS),-I$(dir)/include)\
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.bmp)
ifneq (,$(findstring $(TARGET).bmp,$(icons)))
export GAME_ICON := $(CURDIR)/$(TARGET).bmp
else
ifneq (,$(findstring icon.bmp,$(icons)))
export GAME_ICON := $(CURDIR)/icon.bmp
endif
endif
else
ifeq ($(suffix $(ICON)), .grf)
export GAME_ICON := $(CURDIR)/$(ICON)
else
export GAME_ICON := $(CURDIR)/$(BUILD)/$(notdir $(basename $(ICON))).grf
endif
endif
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
$(BUILD):
@mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(SOUNDBANK)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).nds: $(OUTPUT).elf $(GAME_ICON)
$(OUTPUT).elf: $(OFILES)
# source files depend on generated headers
$(OFILES_SOURCES) : $(HFILES)
# need to build soundbank first
$(OFILES): $(SOUNDBANK)
#---------------------------------------------------------------------------------
# rule to build solution from music files
#---------------------------------------------------------------------------------
$(SOUNDBANK) : $(MODFILES)
#---------------------------------------------------------------------------------
mmutil $^ -d -o$@ -hsoundbank.h
#---------------------------------------------------------------------------------
%.bin.o %_bin.h : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
#---------------------------------------------------------------------------------
# This rule creates assembly source files using grit
# grit takes an image file and a .grit describing how the file is to be processed
# add additional rules like this for each image extension
# you use in the graphics folders
#---------------------------------------------------------------------------------
%.s %.h: %.png %.grit
#---------------------------------------------------------------------------------
grit $< -fts -o$*
#---------------------------------------------------------------------------------
# Convert non-GRF game icon to GRF if needed
#---------------------------------------------------------------------------------
$(GAME_ICON): $(notdir $(ICON))
#---------------------------------------------------------------------------------
@echo convert $(notdir $<)
@grit $< -g -gt -gB4 -gT FF00FF -m! -p -pe 16 -fh! -ftr
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

View File

@ -0,0 +1,7 @@
# This is a minimal makefile only used for the examples. If you want a makefile
# for your project, take one from the templates inside examples/templates.
BINDIRS := data
GFXDIRS := graphics
include ../../Makefile.example.blocksds

View File

@ -0,0 +1,20 @@
#!/bin/sh
NITRO_ENGINE=$DEVKITPRO/nitro-engine
ASSETS=$NITRO_ENGINE/examples/assets
TOOLS=$NITRO_ENGINE/tools
OBJ2DL=$TOOLS/obj2dl/obj2dl.py
rm -rf data
mkdir -p data
python3 $OBJ2DL \
--input $ASSETS/teapot.obj \
--output data/teapot.bin \
--texture 256 256 \
--scale 0.1
python3 $OBJ2DL \
--input $ASSETS/cube.obj \
--output data/cube.bin \
--texture 256 256 \

Binary file not shown.

View File

@ -0,0 +1,2 @@
# 16-bit palette, no transparency
-gx -gb -gB16 -gT!

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

View File

@ -0,0 +1,277 @@
// SPDX-License-Identifier: CC0-1.0
//
// SPDX-FileContributor: Antonio Niño Díaz, 2024
//
// This file is part of Nitro Engine
#include <NEMain.h>
#include "teapot_bin.h"
#include "teapot.h"
typedef struct {
NE_Camera *Camera;
NE_Model *Teapot;
NE_Material *Material;
bool draw_edges;
} SceneData;
void DrawFloor(void)
{
NE_PolyNormal(0, -0.97, 0);
NE_PolyBegin(GL_QUAD);
NE_PolyTexCoord(0, 0);
NE_PolyVertex(-10, 0, -10);
NE_PolyTexCoord(0, 256);
NE_PolyVertex(-10, 0, 10);
NE_PolyTexCoord(256, 256);
NE_PolyVertex(10, 0, 10);
NE_PolyTexCoord(256, 0);
NE_PolyVertex(10, 0, -10);
NE_PolyEnd();
}
void DrawLid(void)
{
NE_PolyNormal(0, -0.97, 0);
NE_PolyBegin(GL_QUAD);
NE_PolyTexCoord(0, 0);
NE_PolyVertex(-0.75, 3, -0.75);
NE_PolyTexCoord(0, 256);
NE_PolyVertex(-0.75, 3, 0.75);
NE_PolyTexCoord(256, 256);
NE_PolyVertex( 0.75, 3, 0.75);
NE_PolyTexCoord(256, 0);
NE_PolyVertex( 0.75, 3, -0.75);
NE_PolyEnd();
}
void DrawShadowVolume(void)
{
// Lid
NE_PolyBegin(GL_QUAD);
NE_PolyVertex(-0.75, 3, -0.75);
NE_PolyVertex(-0.75, 3, 0.75);
NE_PolyVertex( 0.75, 3, 0.75);
NE_PolyVertex( 0.75, 3, -0.75);
NE_PolyEnd();
// Walls
NE_PolyBegin(GL_QUAD_STRIP);
NE_PolyVertex(-0.75, 3, -0.75);
NE_PolyVertex(-0.75, 0, -0.75);
NE_PolyVertex(-0.75, 3, 0.75);
NE_PolyVertex(-0.75, 0, 0.75);
NE_PolyVertex( 0.75, 3, 0.75);
NE_PolyVertex( 0.75, 0, 0.75);
NE_PolyVertex( 0.75, 3, -0.75);
NE_PolyVertex( 0.75, 0, -0.75);
NE_PolyVertex(-0.75, 3, -0.75);
NE_PolyVertex(-0.75, 0, -0.75);
NE_PolyEnd();
// Bottom
NE_PolyBegin(GL_QUAD);
NE_PolyVertex(-0.75, 0, -0.75);
NE_PolyVertex(-0.75, 0, 0.75);
NE_PolyVertex( 0.75, 0, 0.75);
NE_PolyVertex( 0.75, 0, -0.75);
NE_PolyEnd();
}
void Draw3DSceneBright(void *arg)
{
NE_LightSet(0, NE_White, 0, -0.97, -0.0);
SceneData *Scene = arg;
// Set camera
NE_CameraUse(Scene->Camera);
// Set polygon format for regular models
NE_PolyFormat(31, 0, NE_LIGHT_0, NE_CULL_BACK, NE_MODULATION);
// Draw regular models
NE_ModelDraw(Scene->Teapot);
NE_MaterialUse(Scene->Material);
DrawFloor();
DrawLid();
// Draw shadow volume as a black volume (shadow)
NE_MaterialUse(NULL);
NE_PolyColor(NE_Black);
if (Scene->draw_edges)
{
// Draw the shadow volume in wireframe mode to see where it is
NE_PolyFormat(0, 0, 0, NE_CULL_NONE, NE_MODULATION);
DrawShadowVolume();
}
NE_PolyFormat(1, 0, 0, NE_CULL_NONE, NE_SHADOW_POLYGONS);
DrawShadowVolume();
NE_PolyFormat(20, 63, 0, NE_CULL_NONE, NE_SHADOW_POLYGONS);
DrawShadowVolume();
}
void Draw3DSceneDark(void *arg)
{
NE_LightSet(0, RGB15(8, 8, 8), 0, -0.97, -0.0);
SceneData *Scene = arg;
// Set camera
NE_CameraUse(Scene->Camera);
// Set polygon format for regular models
NE_PolyFormat(31, 0, NE_LIGHT_0, NE_CULL_BACK, NE_MODULATION);
// Draw regular models
NE_ModelDraw(Scene->Teapot);
NE_MaterialUse(Scene->Material);
DrawFloor();
DrawLid();
// Draw shadow volume as a yellow volume (light)
NE_MaterialUse(NULL);
NE_PolyColor(NE_Yellow);
if (Scene->draw_edges)
{
// Draw the shadow volume in wireframe mode to see where it is
NE_PolyFormat(0, 0, 0, NE_CULL_NONE, NE_MODULATION);
DrawShadowVolume();
}
NE_PolyFormat(1, 0, 0, NE_CULL_NONE, NE_SHADOW_POLYGONS);
DrawShadowVolume();
NE_PolyFormat(20, 63, 0, NE_CULL_NONE, NE_SHADOW_POLYGONS);
DrawShadowVolume();
}
int main(int argc, char *argv[])
{
SceneData Scene = { 0 };
// This is needed for special screen effects
irqEnable(IRQ_HBLANK);
irqSet(IRQ_VBLANK, NE_VBLFunc);
irqSet(IRQ_VBLANK, NE_HBLFunc);
// Init console and Nitro Engine
NE_InitDual3D();
NE_InitConsole();
// Setup camera
Scene.Camera = NE_CameraCreate();
NE_CameraSet(Scene.Camera,
0, 3.25, -3.25,
0, 1.25, 0,
0, 1, 0);
// Load teapot and texture
{
Scene.Teapot = NE_ModelCreate(NE_Static);
Scene.Material = NE_MaterialCreate();
// Load meshe from RAM and assign it to a model
NE_ModelLoadStaticMesh(Scene.Teapot, teapot_bin);
// Load teapot texture from RAM and assign it to a material
NE_MaterialTexLoad(Scene.Material, NE_RGB5, 256, 256,
NE_TEXGEN_TEXCOORD | NE_TEXTURE_WRAP_S | NE_TEXTURE_WRAP_T,
teapotBitmap);
// Assign material to the model
NE_ModelSetMaterial(Scene.Teapot, Scene.Material);
// Set some properties to the material
NE_MaterialSetProperties(Scene.Material,
RGB15(24, 24, 24), // Diffuse
RGB15(8, 8, 8), // Ambient
RGB15(0, 0, 0), // Specular
RGB15(0, 0, 0), // Emission
false, false); // Vertex color, use shininess table
// Set initial position of the object
NE_ModelSetCoordI(Scene.Teapot,
floattof32(0), floattof32(1.5), floattof32(0));
}
printf("\x1b[0;0H"
"ABXY: Rotate\n"
"Pad: Move\n"
"SELECT: Show edges of shadow\n"
"START: Exit to loader\n");
while (1)
{
NE_WaitForVBL(0);
// Refresh keys
scanKeys();
uint32_t keys = keysHeld();
// Move model using the pad
if (keys & KEY_UP)
NE_ModelTranslate(Scene.Teapot, 0, 0, 0.05);
if (keys & KEY_DOWN)
NE_ModelTranslate(Scene.Teapot, 0, 0, -0.05);
if (keys & KEY_RIGHT)
NE_ModelTranslate(Scene.Teapot, -0.05, 0, 0);
if (keys & KEY_LEFT)
NE_ModelTranslate(Scene.Teapot, 0.05, 0, 0);
// Rotate model using the pad
if (keys & KEY_Y)
NE_ModelRotate(Scene.Teapot, 0, 0, 2);
if (keys & KEY_B)
NE_ModelRotate(Scene.Teapot, 0, 0, -2);
if (keys & KEY_X)
NE_ModelRotate(Scene.Teapot, 0, 2, 0);
if (keys & KEY_A)
NE_ModelRotate(Scene.Teapot, 0, -2, 0);
if (keys & KEY_SELECT)
Scene.draw_edges = true;
else
Scene.draw_edges = false;
if (keys & KEY_START)
break;
// Draw Scene
NE_ProcessDualArg(Draw3DSceneBright, Draw3DSceneDark, &Scene, &Scene);
}
return 0;
}