Rebase old "3D_test" branch onto master

This commit is contained in:
Crayon2000 2024-03-28 21:49:12 -04:00
parent 28f105044b
commit f343f7effb
7 changed files with 1729 additions and 0 deletions

1405
GRRLIB/GRRLIB/GRRLIB_3Dobj.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -161,6 +161,71 @@ typedef struct GRRLIB_Font {
bool kerning; /**< true whenever a face object contains kerning data that can be accessed with FT_Get_Kerning. */
} GRRLIB_ttfFont;
//------------------------------------------------------------------------------
/**
* Structure that defines a material in a model.
*/
typedef struct _GRRLIB_Material {
char* name; /**< Name of material (newmtl).*/
u8 diffuse[3]; /**< Diffuse component (Kd). */
u8 ambient[3]; /**< Ambient component (Ka). */
u8 specular[3]; /**< Specular component (Ks). */
u8 alpha; /**< Transparency of the material (d or Tr). */
GRRLIB_texImg* diffusetex; /**< Diffuse texture map (map_Kd). */
GRRLIB_texImg* ambienttex; /**< Ambient texture map (map_Ka). */
GRRLIB_texImg* speculartex; /**< Specular texture map (map_Ks). */
f32 shininess; /**< Specular exponent (Ns).*/
} GRRLIB_Material;
/**
* Structure that defines a group in a model.
*/
typedef struct _GRRLIB_Group {
char* name; /**< Name of this group. */
u32 numtriangles; /**< Number of triangles in this group. */
u32* triangles; /**< Array of triangle indices. */
u32 material; /**< Index to material for group. */
struct _GRRLIB_Group* next; /**< Pointer to next group in model. */
} GRRLIB_Group;
/**
* Structure that defines a triangle in a model.
*/
typedef struct {
u32 vindices[3]; /**< Array of triangle vertex indices. */
u32 nindices[3]; /**< Array of triangle normal indices. */
u32 tindices[3]; /**< Array of triangle texcoord indices.*/
u32 findex; /**< Index of triangle facet normal. */
} GRRLIB_Triangle;
/**
* Structure that defines a model.
*/
typedef struct {
char* pathname; /**< Path to this model. */
char* mtllibname; /**< Name of the material library. */
u32 numvertices; /**< Number of vertices in model. */
f32* vertices; /**< Array of vertices. */
u32 numnormals; /**< Number of normals in model. */
f32* normals; /**< Array of normals. */
u32 numtexcoords; /**< Number of texcoords in model. */
f32* texcoords; /**< Array of texture coordinates. */
u32 numfacetnorms; /**< Number of facetnorms in model. */
f32* facetnorms; /**< Array of facetnorms. */
u32 numtriangles; /**< Number of triangles in model. */
GRRLIB_Triangle* triangles; /**< Array of triangles. */
u32 nummaterials; /**< Number of materials in model. */
GRRLIB_Material* materials; /**< Array of materials. */
u32 numgroups; /**< Number of groups in model. */
GRRLIB_Group* groups; /**< Linked list of groups. */
guVector position; /**< Position of the model. */
} GRRLIB_Model;
//==============================================================================
// Allow general access to screen and frame information
//==============================================================================

View file

@ -183,5 +183,17 @@ void GRRLIB_PrintfTTFW(int x, int y, GRRLIB_ttfFont *myFont, const wchar_t *stri
u32 GRRLIB_WidthTTF(GRRLIB_ttfFont *myFont, const char *, unsigned int);
u32 GRRLIB_WidthTTFW(GRRLIB_ttfFont *myFont, const wchar_t *, unsigned int);
//------------------------------------------------------------------------------
// GRRLIB_3Dobj.c - 3D obj file functions for GRRLIB
void GRRLIB_Draw3dObj(GRRLIB_Model* model);
GRRLIB_Model* GRRLIB_ReadOBJ(char* filename);
GRRLIB_Model* GRRLIB_ReadOBJMem(const char *buffer, u32 size);
void GRRLIB_DeleteObj(GRRLIB_Model* model);
void GRRLIB_VertexNormals(GRRLIB_Model* model, f32 angle);
void GRRLIB_FacetNormals(GRRLIB_Model* model);
void GRRLIB_LinearTexture(GRRLIB_Model* model);
void GRRLIB_SpheremapTexture(GRRLIB_Model* model);
u32 GRRLIB_FindMaterial(GRRLIB_Model* model, char* name);
#endif // __GRRLIB_FNLIB_H__
/** @} */ // end of group

146
examples/3D_obj/Makefile Normal file
View file

@ -0,0 +1,146 @@
#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif
include $(DEVKITPPC)/wii_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
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES :=
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS = $(CFLAGS)
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lgrrlib -lfreetype -lpngu -lpng -ljpeg -lz -lfat
LIBS += -lwiiuse
#LIBS += -lmodplay -lasnd
LIBS += -lbte -logc -lm
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS :=
#---------------------------------------------------------------------------------
# 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 := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
export LD := $(CC)
else
export LD := $(CXX)
endif
export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
$(sFILES:.s=.o) $(SFILES:.S=.o)
#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) \
-I$(LIBOGC_INC)
#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
-L$(LIBOGC_LIB)
export OUTPUT := $(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
#---------------------------------------------------------------------------------
run:
wiiload $(TARGET).dol
#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)
#---------------------------------------------------------------------------------
# This rule links in binary data with the .jpg extension
#---------------------------------------------------------------------------------
%.jpg.o : %.jpg
#---------------------------------------------------------------------------------
@echo $(notdir $<)
$(bin2o)
#---------------------------------------------------------------------------------
# This rule links in binary data with the .png extension
#---------------------------------------------------------------------------------
%.png.o : %.png
#---------------------------------------------------------------------------------
@echo $(notdir $<)
$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

Binary file not shown.

After

Width:  |  Height:  |  Size: 932 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 KiB

View file

@ -0,0 +1,101 @@
/*===========================================
Loading obj file example.
============================================*/
#include <grrlib.h>
#include <stdlib.h>
#include <wiiuse/wpad.h>
// Graphics
#include "texture_jpg.h"
#include "font_png.h"
int main() {
f32 modelRotX = 0.0f;
f32 modelRotY = 0.0f;
f32 modelRotZ = 0.0f;
f32 camZ = 50.0f;
u8 Amb = 0x00;
f32 zlight = 0.0f;
GRRLIB_Model* model;
const char strCtl1[] = "DPAD TO ROTATE MODEL";
const char strCtl2[] = "PLUS/MINUS TO ZOOM MODEL";
const char strCredit[] = "BY NONAMENO/CRAYON FROM GRRLIB TEAM";
GRRLIB_Init();
WPAD_Init();
GRRLIB_texImg *tex_font = GRRLIB_LoadTexture(font_png);
GRRLIB_InitTileSet(tex_font, 16, 16, 32);
GRRLIB_texImg *tex_obj = GRRLIB_LoadTextureJPGEx(texture_jpg, texture_jpg_size);
GRRLIB_SetBackgroundColour(0x30, 0x30, 0x30, 0xFF);
model = GRRLIB_ReadOBJ("sd:/data/head_chord.obj");
if(model->numnormals == 0) {
GRRLIB_FacetNormals(model);
GRRLIB_VertexNormals(model, 90.0);
}
if(model->numtexcoords == 0) {
GRRLIB_LinearTexture(model);
}
while(1) {
GRRLIB_Camera3dSettings(0.0f,0.0f,camZ, 0,1,0, 0,0,0);
GRRLIB_3dMode(0.1, 1000, 45, model->numtexcoords, model->numnormals);
if(model->numtexcoords) {
GRRLIB_SetTexture(tex_obj, 0);
}
GRRLIB_ObjectView(0, 0, 0, modelRotX, modelRotY, modelRotZ, 1, 1, 1);
GRRLIB_SetLightAmbient(RGBA(Amb,Amb,Amb,0xFF));
GRRLIB_SetLightDiff(0, (guVector){-6, 0, zlight}, 20.0f, 1.0f, 0xFFFFFFFF);
GRRLIB_SetLightDiff(1, (guVector){ 6, 0, zlight}, 20.0f, 1.0f, 0xFFFFFFFF);
GRRLIB_SetLightDiff(2, (guVector){ 0,-6, zlight}, 20.0f, 1.0f, 0xFFFFFFFF);
GRRLIB_Draw3dObj(model);
GRRLIB_SetLightOff();
GRRLIB_2dMode();
GRRLIB_Printf(rmode->fbWidth/2.0 - (tex_font->tilew*strlen(strCtl1))/2.0, 20,
tex_font, 0xFFFFFF33, 1, strCtl1);
GRRLIB_Printf(rmode->fbWidth/2.0 - (tex_font->tilew*strlen(strCtl2))/2.0, 52,
tex_font, 0xFFFFFF33, 1, strCtl2);
GRRLIB_Printf(rmode->fbWidth/2.0 - (tex_font->tilew*strlen(strCredit))/2.0,
rmode->efbHeight - tex_font->tileh - 7,
tex_font, 0xFFFFFF33, 1, strCredit);
GRRLIB_Render();
WPAD_ScanPads();
if(WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) break;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_RIGHT) modelRotY++;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_LEFT) modelRotY--;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_DOWN) modelRotX++;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_UP) modelRotX--;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_PLUS) camZ -= 0.3f;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_MINUS) camZ += 0.3f;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_A) zlight += 0.4f;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_B) zlight -= 0.4f;
if(WPAD_ButtonsHeld(0) & WPAD_BUTTON_1 && WPAD_ButtonsHeld(0) & WPAD_BUTTON_2) {
WPAD_Rumble(0, true); // Rumble on
GRRLIB_ScrShot("sd:/grrlib_3d.png");
WPAD_Rumble(0, false); // Rumble off
}
}
GRRLIB_Exit(); // Be a good boy, clear the memory allocated by GRRLIB
GRRLIB_DeleteObj(model);
GRRLIB_FreeTexture(tex_obj);
GRRLIB_FreeTexture(tex_font);
exit(0);
}