Skip to content

Commit

Permalink
add the sky experiments code, and remove M_PI
Browse files Browse the repository at this point in the history
  • Loading branch information
computermouth committed Oct 1, 2023
1 parent 31036aa commit 3428f2c
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 2 deletions.
63 changes: 63 additions & 0 deletions experiments/sky.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <EGL/egl.h>
#include <GLES3/gl3.h>

// Define your GLES3 window setup and initialization functions here

int main() {
// Initialize GLES3 and create a window
// (You need to implement this part, platform-dependent)

// Compile and link the GLSL shader program
GLuint shaderProgram = glCreateProgram();
GLuint fragmentShader;

// Compile the fragment shader (you need to provide shader source code)
fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);

// Attach the fragment shader to the program
glAttachShader(shaderProgram, fragmentShader);

// Link the program
glLinkProgram(shaderProgram);

// Check for linking errors (you should implement a function for this)
if (!checkLinkingStatus(shaderProgram)) {
// Handle linking errors
return -1;
}

// Access shader uniforms (you should implement functions for this)
GLint yawUniform = glGetUniformLocation(shaderProgram, "yaw");
GLint pitchUniform = glGetUniformLocation(shaderProgram, "pitch");
GLint cameraPositionUniform = glGetUniformLocation(shaderProgram, "cameraPosition");

// Set up the render loop
while (!windowShouldClose()) { // Implement your own exit condition
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Use the shader program
glUseProgram(shaderProgram);

// Set shader uniforms (yaw, pitch)
glUniform1f(yawUniform, cameraYaw);
glUniform1f(pitchUniform, cameraPitch);
glUniform3f(cameraPositionUniform, cameraPositionX, cameraPositionY, cameraPositionZ);

// Render your scene here using GLES3 commands

// Swap buffers
eglSwapBuffers(display, surface);
}

// Cleanup and release resources

// Destroy the shader program
glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);

// Destroy the GLES3 context and window
// (You need to implement this part, platform-dependent)

return 0;
}
53 changes: 53 additions & 0 deletions experiments/sky.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#version 300 es

precision highp float;

uniform vec3 cameraPosition;
uniform float yaw;
uniform float pitch;

const int numStars = 100;

in vec2 fragCoord;

out vec4 fragColor;

void main() {
// Calculate the normalized screen coordinates
vec2 normalizedCoord = fragCoord / vec2(800.0, 600.0); // Adjust the resolution as needed

// Create a fixed array of star positions (you can specify these)
vec2 starPositions[numStars];

// Assign fixed positions to stars (you can adjust these values)
for (int i = 0; i < numStars; i++) {
starPositions[i] = vec2(0.0, 0.0); // Set the positions of the stars here
}

// Transform star positions based on camera's yaw and pitch
mat2 transformationMatrix = mat2(
cos(yaw), -sin(yaw),
sin(yaw), cos(yaw)
) * mat2(
cos(pitch), -sin(pitch),
sin(pitch), cos(pitch)
);

for (int i = 0; i < numStars; i++) {
starPositions[i] = transformationMatrix * starPositions[i];
}

// Check if the current fragment is inside any of the star positions
bool isStar = false;
for (int i = 0; i < numStars; i++) {
if (length(normalizedCoord - (starPositions[i] * 0.5 + 0.5)) < 0.01) {
isStar = true;
break;
}
}

// Output color based on whether it's a star or not
vec3 color = isStar ? vec3(1.0) : vec3(0.0);

fragColor = vec4(color, 1.0);
}
10 changes: 10 additions & 0 deletions experiments/sky.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 300 es

in vec2 inPosition;

out vec2 fragCoord;

void main() {
gl_Position = vec4(inPosition, 0.0, 1.0);
fragCoord = inPosition;
}
67 changes: 67 additions & 0 deletions experiments/sky2.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#version 300 es

precision highp float;

uniform vec3 cameraPosition;
uniform float yaw;
uniform float pitch;

const int numStars = 100;

in vec2 fragCoord;

out vec4 fragColor;

// Function to generate evenly distributed points on the unit sphere
vec3 generateSpherePoint(float u, float v) {
float phi = 2.0 * 3.14159265359 * u;
float theta = 3.14159265359 * v;
float x = cos(phi) * sin(theta);
float y = sin(phi) * sin(theta);
float z = cos(theta);
return vec3(x, y, z);
}

void main() {
// Calculate the normalized screen coordinates
vec2 normalizedCoord = fragCoord / vec2(800.0, 600.0); // Adjust the resolution as needed

// Create an array of star positions
vec3 starPositions[numStars];

// Populate starPositions with evenly distributed points on the unit sphere
for (int i = 0; i < numStars; i++) {
float u = float(i) / float(numStars);
float v = fract(float(i) * 0.61803398875); // Golden ratio to make distribution more uniform
starPositions[i] = generateSpherePoint(u, v);
}

// Transform star positions based on camera's orientation
mat2 transformationMatrix = mat2(
cos(yaw), -sin(yaw),
sin(yaw), cos(yaw)
) * mat2(
cos(pitch), -sin(pitch),
sin(pitch), cos(pitch)
);

for (int i = 0; i < numStars; i++) {
starPositions[i] = transformationMatrix * starPositions[i];
}

// Check if the current fragment is inside any of the star positions
bool isStar = false;
for (int i = 0; i < numStars; i++) {
vec3 starDirection = normalize(starPositions[i]);
vec3 viewDirection = normalize(cameraPosition - starDirection);
if (acos(dot(viewDirection, starDirection)) < 0.01) {
isStar = true;
break;
}
}

// Output color based on whether it's a star or not
vec3 color = isStar ? vec3(1.0) : vec3(0.0);

fragColor = vec4(color, 1.0);
}
4 changes: 2 additions & 2 deletions tools/mapc.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ mapc_pos3_t group_meshes(cgltf_data * data) {
}

// 270 degrees in radians
#define DEG270 (3.0 * M_PI / 2.0)
#define DEG270 (3.0 * 3.14159 / 2.0)

mapc_fpos3_t rotate_on_x(mapc_fpos3_t fp_in){
float rot_matrix[3][3] = {
Expand Down Expand Up @@ -1213,4 +1213,4 @@ int main(int argc, char * argv[]) {
cgltf_free(data);

return 0;
}
}

0 comments on commit 3428f2c

Please sign in to comment.