Skip to content

Commit 1ae69d4

Browse files
mardyWinterMute
authored andcommitted
examples: make the gl2gears example work with opengx
1 parent 0d80c01 commit 1ae69d4

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

examples/sdl2/opengl20/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(EXTRA_FILES)
22
set(EXTRA_LIBS)
33
if(CMAKE_SYSTEM_NAME MATCHES "NintendoWii|NintendoGameCube")
4+
list(APPEND EXTRA_FILES opengx_shaders.c)
45
list(APPEND EXTRA_LIBS opengx)
56
else()
67
list(APPEND EXTRA_LIBS PkgConfig::OPENGL)

examples/sdl2/opengl20/gl2gears.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#include <stdio.h>
5252
#include <string.h>
5353
#include <SDL.h>
54-
#include <SDL_opengles2.h>
54+
#include <SDL_opengl.h>
5555

5656
#ifndef _MSC_VER
5757
#include <unistd.h>
@@ -115,7 +115,7 @@ static SDL_GLContext gl_context = NULL;
115115
/** Should the main loop be running? */
116116
static SDL_bool app_running = SDL_TRUE;
117117

118-
#ifndef __GLIBC__
118+
#if !defined(__GLIBC__) && !defined(__NEWLIB__)
119119
/**
120120
* sincos() for platforms that do not support it.
121121
*/
@@ -698,6 +698,10 @@ gears_init(void)
698698
const char *p;
699699
char msg[512];
700700

701+
#if defined(__wii__) || defined(__gamecube__)
702+
setup_opengx_shaders();
703+
#endif
704+
701705
glEnable(GL_CULL_FACE);
702706
glEnable(GL_DEPTH_TEST);
703707

@@ -785,10 +789,10 @@ main(int argc, char *argv[])
785789
window = SDL_CreateWindowFrom(native_window);
786790
} else {
787791
Uint32 window_flags = SDL_WINDOW_OPENGL;
788-
#if __IPHONEOS__ || __ANDROID__ || __WINRT__
792+
#if __IPHONEOS__ || __ANDROID__ || __WINRT__ || __wii__
789793
window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
790794
#endif
791-
window = SDL_CreateWindow("es2gears", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 300, 300, window_flags);
795+
window = SDL_CreateWindow("es2gears", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, window_flags);
792796
}
793797

794798
if (window == NULL) {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#define GL_GLEXT_PROTOTYPES
2+
#include "opengx.h"
3+
4+
#include <SDL_opengl.h>
5+
#include <gccore.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
static inline GXColor gxcol_new_fv(const float *components)
10+
{
11+
GXColor c = {
12+
(u8)(components[0] * 255.0f),
13+
(u8)(components[1] * 255.0f),
14+
(u8)(components[2] * 255.0f),
15+
(u8)(components[3] * 255.0f)
16+
};
17+
return c;
18+
}
19+
20+
typedef struct {
21+
GLint mvp_loc;
22+
GLint normal_matrix_loc;
23+
GLint mat_color_loc;
24+
GLint light_pos_loc;
25+
} Gl2GearsData;
26+
27+
static void gl2gears_setup_draw(GLuint program, const OgxDrawData *draw_data,
28+
void *user_data)
29+
{
30+
Gl2GearsData *data = user_data;
31+
float m[16];
32+
glGetUniformfv(program, data->mvp_loc, m);
33+
float normal_matrix[16];
34+
glGetUniformfv(program, data->normal_matrix_loc, normal_matrix);
35+
float colorf[4];
36+
glGetUniformfv(program, data->mat_color_loc, colorf);
37+
float light_dir[4];
38+
glGetUniformfv(program, data->light_pos_loc, light_dir);
39+
ogx_set_mvp_matrix(m);
40+
41+
Mtx normalm;
42+
ogx_matrix_gl_to_mtx(normal_matrix, normalm);
43+
GX_LoadNrmMtxImm(normalm, GX_PNMTX0);
44+
45+
GXLightObj light;
46+
// Increase distance to simulate a directional light
47+
float light_pos[3] = {
48+
light_dir[0] * 100000,
49+
light_dir[1] * 100000,
50+
light_dir[2] * 100000,
51+
};
52+
GX_InitLightPosv(&light, light_pos);
53+
GXColor white = { 255, 255, 255, 255 };
54+
GX_InitLightColor(&light, white);
55+
GX_InitLightAttn(&light, 1, 0, 0, 1, 0, 0); // no attenuation
56+
GX_LoadLightObj(&light, 1);
57+
58+
59+
GXColor mat_color = gxcol_new_fv(colorf);
60+
GX_SetNumChans(1);
61+
GX_SetChanMatColor(GX_COLOR0A0, mat_color);
62+
GX_SetChanCtrl(GX_COLOR0A0, GX_ENABLE, GX_SRC_REG, GX_SRC_REG,
63+
1, GX_DF_CLAMP, GX_AF_NONE);
64+
65+
GX_SetNumTevStages(1);
66+
GX_SetNumTexGens(0);
67+
GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
68+
}
69+
70+
static bool shader_compile(GLuint shader)
71+
{
72+
uint32_t source_hash = ogx_shader_get_source_hash(shader);
73+
74+
fprintf(stderr, "%s:%d shader %x hash %08x\n", __FILE__, __LINE__, shader, source_hash);
75+
if (source_hash == 0x5b32d27f) {
76+
/* gl2gears.c vertex shader */
77+
ogx_shader_add_uniforms(shader, 4,
78+
"ModelViewProjectionMatrix", GL_FLOAT_MAT4,
79+
"NormalMatrix", GL_FLOAT_MAT4,
80+
"LightSourcePosition", GL_FLOAT_VEC4,
81+
"MaterialColor", GL_FLOAT_VEC4);
82+
ogx_shader_add_attributes(shader, 2,
83+
"position", GL_FLOAT_VEC3, GX_VA_POS,
84+
"normal", GL_FLOAT_VEC3, GX_VA_NRM);
85+
}
86+
}
87+
88+
static GLenum link_program(GLuint program)
89+
{
90+
GLuint shaders[2];
91+
int count = 0;
92+
glGetAttachedShaders(program, 2, &count, shaders);
93+
uint32_t vertex_shader_hash = ogx_shader_get_source_hash(shaders[0]);
94+
if (vertex_shader_hash == 0x5b32d27f) { /* gl2gears */
95+
Gl2GearsData *data = calloc(1, sizeof(Gl2GearsData));
96+
data->mvp_loc = glGetUniformLocation(program, "ModelViewProjectionMatrix");
97+
data->normal_matrix_loc = glGetUniformLocation(program, "NormalMatrix");
98+
data->mat_color_loc = glGetUniformLocation(program, "MaterialColor");
99+
data->light_pos_loc = glGetUniformLocation(program, "LightSourcePosition");
100+
ogx_shader_program_set_user_data(program, data, free);
101+
ogx_shader_program_set_setup_draw_cb(program, gl2gears_setup_draw);
102+
}
103+
return GL_NO_ERROR;
104+
}
105+
106+
const OgxProgramProcessor s_processor = {
107+
.compile_shader = shader_compile,
108+
.link_program = link_program,
109+
};
110+
111+
void setup_opengx_shaders()
112+
{
113+
ogx_shader_register_program_processor(&s_processor);
114+
}

0 commit comments

Comments
 (0)