#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
// Helper to compile shaders
GLuint compileShader(GLenum type, const char* source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
int success;
char infoLog[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, infoLog);
fprintf(stderr, "Shader Compilation Error: %s\n", infoLog);
}
return shader;
}
int main() {
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
// Configure for a transparent window
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Glass Window Demo", NULL, NULL);
if (!window) {
fprintf(stderr, "Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Vertex Shader: Simple quad
const char* vSrc = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"out vec2 TexCoord;\n"
"void main() {\n"
" gl_Position = vec4(aPos, 1.0);\n"
" TexCoord = aPos.xy * 0.5 + 0.5;\n"
"}\0";
// Fragment Shader: Glass effect with tint and subtle gradient
const char* fSrc = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"void main() {\n"
" // Subtle blue tint for glass\n"
" vec3 glassTint = vec3(0.6, 0.8, 0.95);\n"
" // Add a slight 'sheen' or reflection gradient\n"
" float sheen = pow(TexCoord.x + TexCoord.y, 2.0) * 0.1;\n"
" vec3 finalColor = glassTint + vec3(sheen);\n"
" // Low alpha makes it see-through\n"
" FragColor = vec4(finalColor, 0.2);\n"
"}\0";
GLuint vShader = compileShader(GL_VERTEX_SHADER, vSrc);
GLuint fShader = compileShader(GL_FRAGMENT_SHADER, fSrc);
GLuint program = glCreateProgram();
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
// Quad covering the screen
float vertices[] = {
-1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
unsigned int indices[] = { 0, 1, 2, 0, 2, 3 };
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
printf("OpenGL Glass Window Running...\n");
printf("The window should be transparent with a slight blue tint.\n");
while (!glfwWindowShouldClose(window)) {
// Clear with 0 alpha to ensure background transparency
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteProgram(program);
glfwTerminate();
return 0;
}