Browse Source

Initial texenv implementation

pull/33/head
Ian Chamberlain 1 year ago
parent
commit
0e02f90442
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 5
      citro3d-sys/bindgen.sh
  2. 25
      citro3d/examples/triangle.rs
  3. 1
      citro3d/src/lib.rs
  4. 99
      citro3d/src/texenv.rs

5
citro3d-sys/bindgen.sh

@ -1,5 +0,0 @@
#!/usr/bin/env bash
set -euxo pipefail
cargo run --package bindgen-citro3d > src/bindings.rs

25
citro3d/examples/triangle.rs

@ -6,6 +6,7 @@
use citro3d::macros::include_shader; use citro3d::macros::include_shader;
use citro3d::math::{AspectRatio, ClipPlanes, Matrix4, Projection, StereoDisplacement}; use citro3d::math::{AspectRatio, ClipPlanes, Matrix4, Projection, StereoDisplacement};
use citro3d::render::ClearFlags; use citro3d::render::ClearFlags;
use citro3d::texenv::{self, CombineFunc, TexEnv};
use citro3d::{attrib, buffer, render, shader}; use citro3d::{attrib, buffer, render, shader};
use ctru::prelude::*; use ctru::prelude::*;
use ctru::services::gfx::{RawFrameBuffer, Screen, TopScreen3D}; use ctru::services::gfx::{RawFrameBuffer, Screen, TopScreen3D};
@ -88,7 +89,11 @@ fn main() {
let mut buf_info = buffer::Info::new(); let mut buf_info = buffer::Info::new();
let (attr_info, vbo_idx) = prepare_vbos(&mut buf_info, &vbo_data); let (attr_info, vbo_idx) = prepare_vbos(&mut buf_info, &vbo_data);
scene_init(); // Configure the first fragment shading substage to just pass through the vertex color
// See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight
TexEnv::get(&mut instance, texenv::Id(0))
.src(texenv::Mode::BOTH, texenv::Source::PrimaryColor, None, None)
.func(texenv::Mode::BOTH, CombineFunc::Replace);
let projection_uniform_idx = program.get_uniform("projection").unwrap(); let projection_uniform_idx = program.get_uniform("projection").unwrap();
@ -191,21 +196,3 @@ fn calculate_projections() -> Projections {
center, center,
} }
} }
fn scene_init() {
// Load the vertex shader, create a shader program and bind it
unsafe {
// Configure the first fragment shading substage to just pass through the vertex color
// See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight
let env = citro3d_sys::C3D_GetTexEnv(0);
citro3d_sys::C3D_TexEnvInit(env);
citro3d_sys::C3D_TexEnvSrc(
env,
citro3d_sys::C3D_Both,
ctru_sys::GPU_PRIMARY_COLOR,
0,
0,
);
citro3d_sys::C3D_TexEnvFunc(env, citro3d_sys::C3D_Both, ctru_sys::GPU_REPLACE);
}
}

1
citro3d/src/lib.rs

@ -15,6 +15,7 @@ pub mod error;
pub mod math; pub mod math;
pub mod render; pub mod render;
pub mod shader; pub mod shader;
pub mod texenv;
pub mod uniform; pub mod uniform;
pub use error::{Error, Result}; pub use error::{Error, Result};

99
citro3d/src/texenv.rs

@ -0,0 +1,99 @@
//! Texture environment support. See `<c3d/texenv.h>` for more information.
use bitflags::bitflags;
use crate::Instance;
#[doc(alias = "C3D_TexEnv")]
pub struct TexEnv<'a> {
raw: *mut citro3d_sys::C3D_TexEnv,
_instance: &'a mut Instance,
}
impl<'a> TexEnv<'a> {
#[doc(alias = "C3D_TexEnvInit")]
pub fn set(&self, _instance: &mut Instance, id: Id) {
unsafe {
// SAFETY: pointee is only copied from, not modified
citro3d_sys::C3D_SetTexEnv(id.0, self.raw);
}
}
pub fn get(instance: &'a mut Instance, id: Id) -> Self {
unsafe {
Self {
raw: citro3d_sys::C3D_GetTexEnv(id.0),
_instance: instance,
}
}
}
pub fn src(
&mut self,
mode: Mode,
s1: Source,
s2: Option<Source>,
s3: Option<Source>,
) -> &mut Self {
unsafe {
citro3d_sys::C3D_TexEnvSrc(
self.raw,
mode.bits(),
s1 as _,
s2.unwrap_or(Source::PrimaryColor) as _,
s3.unwrap_or(Source::PrimaryColor) as _,
)
}
self
}
pub fn func(&mut self, mode: Mode, func: CombineFunc) -> &mut Self {
unsafe {
citro3d_sys::C3D_TexEnvFunc(self.raw, mode.bits(), func as _);
}
self
}
}
bitflags! {
#[doc(alias = "C3D_TexEnvMode")]
pub struct Mode: citro3d_sys::C3D_TexEnvMode {
const RGB = citro3d_sys::C3D_RGB;
const ALPHA = citro3d_sys::C3D_Alpha;
const BOTH = citro3d_sys::C3D_Both;
}
}
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
#[doc(alias = "GPU_TEVSRC")]
pub enum Source {
PrimaryColor = ctru_sys::GPU_PRIMARY_COLOR,
FragmentPrimaryColor = ctru_sys::GPU_FRAGMENT_PRIMARY_COLOR,
FragmentSecondaryColor = ctru_sys::GPU_FRAGMENT_SECONDARY_COLOR,
Texture0 = ctru_sys::GPU_TEXTURE0,
Texture1 = ctru_sys::GPU_TEXTURE1,
Texture2 = ctru_sys::GPU_TEXTURE2,
Texture3 = ctru_sys::GPU_TEXTURE3,
PreviousBuffer = ctru_sys::GPU_PREVIOUS_BUFFER,
Constant = ctru_sys::GPU_CONSTANT,
Previous = ctru_sys::GPU_PREVIOUS,
}
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
#[doc(alias = "GPU_COMBINEFUNC")]
pub enum CombineFunc {
Replace = ctru_sys::GPU_REPLACE,
Modulate = ctru_sys::GPU_MODULATE,
Add = ctru_sys::GPU_ADD,
AddSigned = ctru_sys::GPU_ADD_SIGNED,
Interpolate = ctru_sys::GPU_INTERPOLATE,
Subtract = ctru_sys::GPU_SUBTRACT,
Dot3Rgb = ctru_sys::GPU_DOT3_RGB,
Dot3Rgba = ctru_sys::GPU_DOT3_RGBA,
}
#[derive(Copy, Clone, Debug)]
pub struct Id(/* TODO maybe non pub? idk */ pub libc::c_int);
Loading…
Cancel
Save