Skip to content

Commit 5f3205d

Browse files
committed
examples: make the cube_tex example work on the console
This is a bit primitive, because we are hardcoding the texture stage and other TEV variables (we'll improve this later), but it does the job.
1 parent d667dcb commit 5f3205d

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

examples/sdl2/opengl20/cube_tex.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ POSSIBILITY OF SUCH DAMAGE.
3535

3636
#include "textures.h"
3737

38+
#if defined(__wii__) || defined(__gamecube__)
39+
#include "opengx_shaders.h"
40+
#endif
41+
3842
#include <SDL.h>
3943
#include <SDL_opengl.h>
4044
#include <stdio.h>
@@ -105,6 +109,10 @@ process_event(SDL_Event *event)
105109

106110
int main(int argc, char **argv)
107111
{
112+
#if defined(__wii__) || defined(__gamecube__)
113+
setup_opengx_shaders();
114+
#endif
115+
108116
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) != 0) {
109117
SDL_Log("SDL init error: %s", SDL_GetError());
110118
return EXIT_FAILURE;

examples/sdl2/opengl20/opengx_shaders.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,46 @@ static void gl2gears_setup_draw(GLuint program, const OgxDrawData *draw_data,
6767
GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
6868
}
6969

70+
typedef struct {
71+
GLint mvp_loc;
72+
GLint tex_sampler_loc;
73+
} CubeTexData;
74+
75+
static void cube_tex_setup_draw(GLuint program, const OgxDrawData *draw_data,
76+
void *user_data)
77+
{
78+
CubeTexData *data = user_data;
79+
float m[16];
80+
glGetUniformfv(program, data->mvp_loc, m);
81+
GLint texture_unit;
82+
glGetUniformiv(program, data->tex_sampler_loc, &texture_unit);
83+
ogx_set_mvp_matrix(m);
84+
85+
uint8_t tex_map = GX_TEXMAP0;
86+
uint8_t tex_coord = GX_TEXCOORD0;
87+
uint8_t input_coordinates = GX_TG_TEX0;
88+
uint8_t stage = GX_TEVSTAGE0;
89+
GXTexObj *texture;
90+
texture = ogx_shader_get_texobj(texture_unit);
91+
GX_LoadTexObj(texture, tex_map);
92+
GX_SetNumChans(1);
93+
GX_SetChanCtrl(GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX,
94+
0, GX_DF_CLAMP, GX_AF_NONE);
95+
96+
// In data: c: Texture Color b: raster value, Operation: b*c
97+
GX_SetTevColorIn(stage, GX_CC_ZERO, GX_CC_RASC, GX_CC_TEXC, GX_CC_CPREV);
98+
GX_SetTevAlphaIn(stage, GX_CA_ZERO, GX_CA_RASA, GX_CA_TEXA, GX_CA_APREV);
99+
GX_SetTevColorOp(stage, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE,
100+
GX_TEVPREV);
101+
GX_SetTevAlphaOp(stage, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE,
102+
GX_TEVPREV);
103+
GX_SetTexCoordGen(tex_coord, GX_TG_MTX2x4, input_coordinates, GX_IDENTITY);
104+
105+
GX_SetNumTevStages(1);
106+
GX_SetTevOrder(stage, tex_coord, tex_map, GX_COLOR0A0);
107+
GX_SetNumTexGens(1);
108+
}
109+
70110
static bool shader_compile(GLuint shader)
71111
{
72112
uint32_t source_hash = ogx_shader_get_source_hash(shader);
@@ -82,6 +122,15 @@ static bool shader_compile(GLuint shader)
82122
ogx_shader_add_attributes(shader, 2,
83123
"position", GL_FLOAT_VEC3, GX_VA_POS,
84124
"normal", GL_FLOAT_VEC3, GX_VA_NRM);
125+
} else if (source_hash == 0x53560768) {
126+
/* cube_tex.cpp vertex shader */
127+
ogx_shader_add_uniforms(shader, 2,
128+
"MVP", GL_FLOAT_MAT4,
129+
"myTextureSampler", GL_SAMPLER_2D);
130+
ogx_shader_add_attributes(shader, 3,
131+
"vertexPosition_modelspace", GL_FLOAT_VEC3, GX_VA_POS,
132+
"vertexUV", GL_FLOAT_VEC2, GX_VA_TEX0,
133+
"vertexColor", GL_FLOAT_VEC4, GX_VA_CLR0);
85134
}
86135
}
87136

@@ -99,11 +148,18 @@ static GLenum link_program(GLuint program)
99148
data->light_pos_loc = glGetUniformLocation(program, "LightSourcePosition");
100149
ogx_shader_program_set_user_data(program, data, free);
101150
ogx_shader_program_set_setup_draw_cb(program, gl2gears_setup_draw);
151+
} else if (vertex_shader_hash == 0x53560768) {
152+
/* cube_tex.cpp vertex shader */
153+
CubeTexData *data = calloc(1, sizeof(CubeTexData));
154+
data->mvp_loc = glGetUniformLocation(program, "MVP");
155+
data->tex_sampler_loc = glGetUniformLocation(program, "myTextureSampler");
156+
ogx_shader_program_set_user_data(program, data, free);
157+
ogx_shader_program_set_setup_draw_cb(program, cube_tex_setup_draw);
102158
}
103159
return GL_NO_ERROR;
104160
}
105161

106-
const OgxProgramProcessor s_processor = {
162+
static const OgxProgramProcessor s_processor = {
107163
.compile_shader = shader_compile,
108164
.link_program = link_program,
109165
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*****************************************************************************
2+
Copyright (c) 2024 Alberto Mardegan ([email protected])
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions
6+
are met:
7+
1. Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
3. Neither the name of copyright holders nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19+
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
20+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
POSSIBILITY OF SUCH DAMAGE.
27+
*****************************************************************************/
28+
29+
#ifndef OPENGX_SHADERS_H
30+
#define OPENGX_SHADERS_H
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
void setup_opengx_shaders(void);
37+
38+
#ifdef __cplusplus
39+
} // extern C
40+
#endif
41+
42+
#endif /* OPENGX_SHADERS_H */

0 commit comments

Comments
 (0)