Skip to content

Instantly share code, notes, and snippets.

@lmBored
Created October 3, 2025 15:25
Show Gist options
  • Select an option

  • Save lmBored/59853507a663381fd262c3bca76480a1 to your computer and use it in GitHub Desktop.

Select an option

Save lmBored/59853507a663381fd262c3bca76480a1 to your computer and use it in GitHub Desktop.
#version 330
//uniform sampler2D cubemap[1];
uniform sampler2D cubemap0;
uniform sampler2D cubemap1;
in vec3 R; // vector from eye to object reflected in the normal direction
in vec2 outTexCoord;
out vec4 FragColor;
// Given methods to create homogeneous matrices
mat3 Translate(vec2 t) { return mat3(vec3(1,0,0),vec3(0,1,0),vec3(t.x,t.y,1)); }
mat3 Scale(vec2 s) { return mat3(vec3(s.x,0,0),vec3(0,s.y,0),vec3(0,0,1)); }
mat3 Mat(vec2 v0, vec2 v1, vec2 t) { return mat3(vec3(v0,0),vec3(v1,0),vec3(t,1)); }
void main( void )
{ vec2 uv; // coodinates in [-1,1] on side of cube
vec2 st0, st1; // images after transformation of uv axis (1,0) and uv=(0,1), respectively.
// refer to figure in assignment text
vec2 offset; // offset to center of square in cube map in [0,8]x[0,6]
// TODO: Depending on the value of R.x, R.y, R.z
// compute st0, st1 and offset.
// Assign boolean expressions for all case and subcase
// variables, such that you cover all combinations (correspoonding to cube faces) for
// R.x, R.y, R.z
// NOTE: this can be done by 2 cases and 3 subcases (see below)
// but additional cases may be added (full points may not be given in this case)
// 3d vector h represents the cartesian coordnates
// of the intersection with a unit cube face
// TODO: compute h in each case and then compute uv using h
// HINT: look at preparatory exercise in the assignment text | total of 4 points
//bool case1,case2;
//bool subcase1, subcase2, subcase3;
// Determine which face of the cube the reflection vector R intersects
// Based on the largest absolute component of R
vec3 absR = abs(R);
float maxComponent = max(max(absR.x, absR.y), absR.z);
// Cases based on which axis has the largest component
bool case1 = (absR.x == maxComponent); // X-dominant (left/right faces)
bool case2 = (absR.y == maxComponent); // Y-dominant (top/bottom faces)
// case3 is Z-dominant (front/back faces)
// Subcases for positive/negative directions
bool subcase1 = (case1 && R.x > 0.0); // +X face (right)
bool subcase2 = (case2 && R.y > 0.0); // +Y face (top)
bool subcase3 = (!case1 && !case2 && R.z > 0.0); // +Z face (front)
// Compute intersection point h with the unit cube face
// and map to uv coordinates on that face
if (case1) {
vec3 h;
if (subcase1) {
// +X face (right): x = +1
float t = 1.0 / R.x;
h = R * t;
uv = vec2(h.z, h.y); // Map z,y to u,v for right face
st0 = vec2(1, 0); st1 = vec2(0, 1); offset = vec2(5, 3); // Right face position
} else {
// -X face (left): x = -1
float t = -1.0 / R.x;
h = R * t;
uv = vec2(-h.z, h.y); // Map z,y to u,v for left face
st0 = vec2(1, 0); st1 = vec2(0, 1); offset = vec2(1, 3); // Left face position
}
} else if (case2) {
vec3 h;
if (subcase2) {
// +Y face (top): y = +1
float t = 1.0 / R.y;
h = R * t;
uv = vec2(h.x, h.z); // Map x,z to u,v for top face
st0 = vec2(1, 0); st1 = vec2(0, 1); offset = vec2(3, 5); // Top face position
} else {
// -Y face (bottom): y = -1
float t = -1.0 / R.y;
h = R * t;
uv = vec2(h.x, -h.z); // Map x,z to u,v for bottom face
st0 = vec2(1, 0); st1 = vec2(0, 1); offset = vec2(3, 1); // Bottom face position
}
} else {
vec3 h;
if (subcase3) {
// +Z face (front): z = +1
float t = 1.0 / R.z;
h = R * t;
uv = vec2(-h.x, h.y); // Map x,y to u,v for front face
st0 = vec2(1, 0); st1 = vec2(0, 1); offset = vec2(7, 3); // Front face position
} else {
// -Z face (back): z = -1
float t = -1.0 / R.z;
h = R * t;
uv = vec2(h.x, h.y); // Map x,y to u,v for back face
st0 = vec2(1, 0); st1 = vec2(0, 1); offset = vec2(3, 3); // Back face position
}
}
// TODO: Create a 3x3 matrix m0, using Mat() function
// and st0, st1, offset which now represent column vectors in m0.; see below
// The matrix maps locations on a face of the cube from [-1,1]x[-1,1]
// to the appropriate cube map patch assuming coordinates range in [0,8]x[0,6] | total 3 points
// m0 transforms from face coordinates to cube map coordinates
mat3 m0 = Mat(st0, st1, offset);
// TODO: Create a 3x3 scaling matrix m1, using Scale() function,
// such that coordinates are scaled to [1,0]x[0,1]; see below | total 1 point
mat3 m1 = Scale(vec2(1.0/8.0, 1.0/6.0));
// Finally, compute etxture coordinates
vec3 st = m1*m0*vec3(uv,1);
FragColor = texture(cubemap1, st.xy/st.z);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment