From 3428f2c1bb5a352ac4b22250d2f1cfbd39824e8c Mon Sep 17 00:00:00 2001 From: byoung Date: Sat, 30 Sep 2023 18:36:29 -0600 Subject: [PATCH] add the sky experiments code, and remove M_PI --- experiments/sky.c | 63 ++++++++++++++++++++++++++++++++++++++++ experiments/sky.frag | 53 ++++++++++++++++++++++++++++++++++ experiments/sky.vert | 10 +++++++ experiments/sky2.frag | 67 +++++++++++++++++++++++++++++++++++++++++++ tools/mapc.c | 4 +-- 5 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 experiments/sky.c create mode 100644 experiments/sky.frag create mode 100644 experiments/sky.vert create mode 100644 experiments/sky2.frag diff --git a/experiments/sky.c b/experiments/sky.c new file mode 100644 index 0000000..997d4e5 --- /dev/null +++ b/experiments/sky.c @@ -0,0 +1,63 @@ +#include +#include + +// 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; +} diff --git a/experiments/sky.frag b/experiments/sky.frag new file mode 100644 index 0000000..ccc5305 --- /dev/null +++ b/experiments/sky.frag @@ -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); +} diff --git a/experiments/sky.vert b/experiments/sky.vert new file mode 100644 index 0000000..5fdcf56 --- /dev/null +++ b/experiments/sky.vert @@ -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; +} diff --git a/experiments/sky2.frag b/experiments/sky2.frag new file mode 100644 index 0000000..2fcbfab --- /dev/null +++ b/experiments/sky2.frag @@ -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); +} diff --git a/tools/mapc.c b/tools/mapc.c index 5e59a87..3fe57fd 100644 --- a/tools/mapc.c +++ b/tools/mapc.c @@ -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] = { @@ -1213,4 +1213,4 @@ int main(int argc, char * argv[]) { cgltf_free(data); return 0; -} \ No newline at end of file +}