Skip to content

Commit

Permalink
Merge remote-tracking branch 'snacchus/gfx-rdp' into gfx-rdp
Browse files Browse the repository at this point in the history
  • Loading branch information
rasky committed Jun 18, 2023
2 parents 1a8c46b + 03c6259 commit a915aec
Show file tree
Hide file tree
Showing 24 changed files with 748 additions and 458 deletions.
20 changes: 20 additions & 0 deletions examples/gldemo/camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CAMERA_H
#define CAMERA_H

typedef struct {
float distance;
float rotation;
} camera_t;

void camera_transform(const camera_t *camera)
{
// Set the camera transform
glLoadIdentity();
gluLookAt(
0, -camera->distance, -camera->distance,
0, 0, 0,
0, 1, 0);
glRotatef(camera->rotation, 0, 1, 0);
}

#endif
29 changes: 26 additions & 3 deletions examples/gldemo/cube.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CUBE_H
#define CUBE_H

#include <libdragon.h>
#include <GL/gl.h>
#include "vertex.h"

Expand Down Expand Up @@ -59,9 +60,6 @@ void setup_cube()

void draw_cube()
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
Expand All @@ -73,6 +71,31 @@ void draw_cube()
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex_t), (void*)(8*sizeof(float) + (void*)cube_vertices));

glDrawElements(GL_TRIANGLES, sizeof(cube_indices) / sizeof(uint16_t), GL_UNSIGNED_SHORT, cube_indices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}

void render_cube()
{
rdpq_debug_log_msg("Cube");
glPushMatrix();
glTranslatef(0,-1.f,0);

// Apply vertex color as material color.
// Because the cube has colors set per vertex, we can color each face seperately
glEnable(GL_COLOR_MATERIAL);

// Apply to ambient and diffuse material properties
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

draw_cube();

glDisable(GL_COLOR_MATERIAL);

glPopMatrix();
}

#endif
50 changes: 50 additions & 0 deletions examples/gldemo/decal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef DECAL_H
#define DECAL_H

#include <libdragon.h>
#include <GL/gl.h>

void draw_quad()
{
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0, 1, 0);
glTexCoord2f(0, 0);
glVertex3f(-0.5f, 0, -0.5f);
glTexCoord2f(0, 1);
glVertex3f(-0.5f, 0, 0.5f);
glTexCoord2f(1, 0);
glVertex3f(0.5f, 0, -0.5f);
glTexCoord2f(1, 1);
glVertex3f(0.5f, 0, 0.5f);
glEnd();
}

void render_decal()
{
rdpq_debug_log_msg("Decal");
glPushMatrix();
glTranslatef(0, 0, 6);
glRotatef(35, 0, 1, 0);
glScalef(3, 3, 3);

// Decals are drawn with the depth func set to GL_EQUAL. Note that glPolygonOffset is not supported on N64.
glDepthFunc(GL_EQUAL);

// Disable writing to depth buffer, because the depth value will be the same anyway
glDepthMask(GL_FALSE);

// Apply vertex color as material color.
// This time, we set one vertex color for the entire model.
glEnable(GL_COLOR_MATERIAL);
glColor4f(1.0f, 0.4f, 0.2f, 0.5f);

draw_quad();

glDisable(GL_COLOR_MATERIAL);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);

glPopMatrix();
}

#endif
140 changes: 34 additions & 106 deletions examples/gldemo/gldemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
#include <malloc.h>
#include <math.h>

#include "camera.h"
#include "cube.h"
#include "decal.h"
#include "sphere.h"
#include "plane.h"
#include "prim_test.h"
#include "skinned.h"

// Set this to 1 to enable rdpq debug output.
// The demo will only run for a single frame and stop.
#define DEBUG_RDP 0

static uint32_t animation = 3283;
static uint32_t texture_index = 0;
static float distance = -10.0f;
static float cam_rotate = 0.0f;
static camera_t camera;
static surface_t zbuffer;

static GLuint textures[4];
Expand Down Expand Up @@ -58,19 +60,11 @@ static const char *texture_path[4] = {

static sprite_t *sprites[4];

void load_texture(GLenum target, sprite_t *sprite)
{
for (uint32_t i = 0; i < 7; i++)
{
surface_t surf = sprite_get_lod_pixels(sprite, i);
if (!surf.buffer) break;

glTexImageN64(target, i, &surf);
}
}

void setup()
{
camera.distance = -10.0f;
camera.rotation = 0.0f;

zbuffer = surface_alloc(FMT_RGBA16, display_get_width(), display_get_height());

for (uint32_t i = 0; i < 4; i++)
Expand All @@ -86,10 +80,6 @@ void setup()
setup_plane();
make_plane_mesh();

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_NORMALIZE);

float aspect_ratio = (float)display_get_width() / (float)display_get_height();
float near_plane = 1.0f;
float far_plane = 50.0f;
Expand Down Expand Up @@ -134,28 +124,23 @@ void setup()
{
glBindTexture(GL_TEXTURE_2D, textures[i]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);

load_texture(GL_TEXTURE_2D, sprites[i]);
glSpriteTextureN64(GL_TEXTURE_2D, sprites[i], &(rdpq_texparms_t){.s.repeats = REPEAT_INFINITE, .t.repeats = REPEAT_INFINITE});
}
}

void draw_quad()
void set_light_positions(float rotation)
{
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0, 1, 0);
glTexCoord2f(0, 0);
glVertex3f(-0.5f, 0, -0.5f);
glTexCoord2f(0, 1);
glVertex3f(-0.5f, 0, 0.5f);
glTexCoord2f(1, 0);
glVertex3f(0.5f, 0, -0.5f);
glTexCoord2f(1, 1);
glVertex3f(0.5f, 0, 0.5f);
glEnd();
glPushMatrix();
glRotatef(rotation*5.43f, 0, 1, 0);

for (uint32_t i = 0; i < 8; i++)
{
glLightfv(GL_LIGHT0 + i, GL_POSITION, light_pos[i]);
}
glPopMatrix();
}

void render()
Expand All @@ -170,90 +155,33 @@ void render()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
0, -distance, -distance,
0, 0, 0,
0, 1, 0);
glRotatef(cam_rotate, 0, 1, 0);
camera_transform(&camera);

float rotation = animation * 0.5f;

glPushMatrix();

glRotatef(rotation*5.43f, 0, 1, 0);

for (uint32_t i = 0; i < 8; i++)
{
glLightfv(GL_LIGHT0 + i, GL_POSITION, light_pos[i]);
}

glPopMatrix();

glBindTexture(GL_TEXTURE_2D, textures[texture_index]);
set_light_positions(rotation);

// Set some global render modes that we want to apply to all models
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);

glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glColor3f(1, 1, 1);
rdpq_debug_log_msg("Plane");
draw_plane();
glTranslatef(0,-1.f,0);
rdpq_debug_log_msg("Cube");
draw_cube();
glPopMatrix();

glPushMatrix();
glTranslatef(0, 0, 6);
glRotatef(35, 0, 1, 0);
glScalef(3, 3, 3);
glColor4f(1.0f, 0.4f, 0.2f, 0.5f);
glDepthFunc(GL_EQUAL);
glDepthMask(GL_FALSE);
rdpq_debug_log_msg("Decal");
draw_quad();
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glPopMatrix();

glDisable(GL_COLOR_MATERIAL);

glPushMatrix();
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

glRotatef(rotation*0.23f, 1, 0, 0);
glRotatef(rotation*0.98f, 0, 0, 1);
glRotatef(rotation*1.71f, 0, 1, 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[texture_index]);

render_plane();
render_decal();
render_cube();
render_skinned(&camera, animation);

glBindTexture(GL_TEXTURE_2D, textures[(texture_index + 1)%4]);

glCullFace(GL_FRONT);
rdpq_debug_log_msg("Sphere");
draw_sphere();
glCullFace(GL_BACK);

glPopMatrix();

glPushMatrix();

glTranslatef(0, 6, 0);
glRotatef(-rotation*2.46f, 0, 1, 0);
render_sphere(rotation);

glDisable(GL_TEXTURE_2D);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

rdpq_debug_log_msg("Primitives");
glColor4f(1, 1, 1, 0.4f);
prim_test();
render_primitives(rotation);

glEnable(GL_CULL_FACE);
glDisable(GL_BLEND);

glPopMatrix();
gl_context_end();

rdpq_detach_show();
Expand Down Expand Up @@ -347,8 +275,8 @@ int main()
float mag = x*x + y*y;

if (fabsf(mag) > 0.01f) {
distance += y * 0.2f;
cam_rotate = cam_rotate - x * 1.2f;
camera.distance += y * 0.2f;
camera.rotation = camera.rotation - x * 1.2f;
}

render();
Expand Down
8 changes: 8 additions & 0 deletions examples/gldemo/plane.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PLANE_H
#define PLANE_H

#include <libdragon.h>
#include <GL/gl.h>
#include <math.h>

Expand Down Expand Up @@ -111,4 +112,11 @@ void draw_plane()
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}

void render_plane()
{
rdpq_debug_log_msg("Plane");

draw_plane();
}

#endif
31 changes: 31 additions & 0 deletions examples/gldemo/prim_test.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PRIM_TEST_H
#define PRIM_TEST_H

#include <libdragon.h>
#include <GL/gl.h>

void points()
Expand Down Expand Up @@ -174,4 +175,34 @@ void prim_test()
glPopMatrix();
}

void render_primitives(float rotation)
{
rdpq_debug_log_msg("Primitives");
glPushMatrix();

glTranslatef(0, 6, 0);
glRotatef(-rotation*2.46f, 0, 1, 0);

// Configure alpha blending (transparency)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Set a constant alpha for all vertices
glColor4f(1, 1, 1, 0.4f);

// We want to see back faces as well
glDisable(GL_CULL_FACE);

// Transparent polygons should not write to the depth buffer
glDepthMask(GL_FALSE);

prim_test();

glDepthMask(GL_TRUE);
glEnable(GL_CULL_FACE);
glDisable(GL_BLEND);

glPopMatrix();
}

#endif
Loading

0 comments on commit a915aec

Please sign in to comment.