-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the sky experiments code, and remove M_PI
- Loading branch information
1 parent
31036aa
commit 3428f2c
Showing
5 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters