primer commit
This commit is contained in:
219
src/main.cpp
Normal file
219
src/main.cpp
Normal file
@@ -0,0 +1,219 @@
|
||||
// src/main.cpp
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
// Vertex shader: genera posiciones de pantalla y una coordenada uv en [0,1]
|
||||
static const char *vertexShaderSrc = R"glsl(
|
||||
#version 330 core
|
||||
layout(location = 0) in vec2 aPos;
|
||||
out vec2 vUV;
|
||||
void main() {
|
||||
vUV = aPos * 0.5 + 0.5;
|
||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||
}
|
||||
)glsl";
|
||||
|
||||
// Fragment shader: adapta tu shader de Shadertoy
|
||||
static const char *fragmentShaderSrc = R"glsl(
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
in vec2 vUV;
|
||||
uniform vec2 iResolution;
|
||||
uniform float iTime;
|
||||
|
||||
vec3 palette( float t) {
|
||||
vec3 a = vec3(1.0, 0.5, 0.5);
|
||||
vec3 b = vec3(1.0, 0.5, 0.5);
|
||||
vec3 c = vec3(1.0, 1.0, 1.0);
|
||||
vec3 d = vec3(0.263, 0.416, 0.557);
|
||||
return a + b * cos( 6.28318 * (c * t * d) );
|
||||
}
|
||||
|
||||
void mainImage( out vec4 fragColor, in vec2 fragCoord )
|
||||
{
|
||||
vec2 uv = (fragCoord * 2.0 - iResolution.xy) / iResolution.y;
|
||||
float d = length(uv);
|
||||
vec3 col = palette(d);
|
||||
d = sin(d * 8.0 + iTime) / 8.0;
|
||||
d = abs(d);
|
||||
d = 0.02 / d;
|
||||
col *= d;
|
||||
fragColor = vec4(col, 1.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 fragCoordPixels = vUV * iResolution;
|
||||
vec4 outColor;
|
||||
mainImage(outColor, fragCoordPixels);
|
||||
FragColor = outColor;
|
||||
}
|
||||
)glsl";
|
||||
|
||||
// helpers
|
||||
static GLuint compileShader(GLenum type, const char *src)
|
||||
{
|
||||
GLuint s = glCreateShader(type);
|
||||
glShaderSource(s, 1, &src, nullptr);
|
||||
glCompileShader(s);
|
||||
GLint ok = 0;
|
||||
glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
|
||||
if (!ok)
|
||||
{
|
||||
GLint len = 0;
|
||||
glGetShaderiv(s, GL_INFO_LOG_LENGTH, &len);
|
||||
std::string log(len, ' ');
|
||||
glGetShaderInfoLog(s, len, nullptr, &log[0]);
|
||||
std::cerr << "Shader compile error: " << log << '\n';
|
||||
glDeleteShader(s);
|
||||
return 0;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static GLuint linkProgram(GLuint vs, GLuint fs)
|
||||
{
|
||||
GLuint p = glCreateProgram();
|
||||
glAttachShader(p, vs);
|
||||
glAttachShader(p, fs);
|
||||
glLinkProgram(p);
|
||||
GLint ok = 0;
|
||||
glGetProgramiv(p, GL_LINK_STATUS, &ok);
|
||||
if (!ok)
|
||||
{
|
||||
GLint len = 0;
|
||||
glGetProgramiv(p, GL_INFO_LOG_LENGTH, &len);
|
||||
std::string log(len, ' ');
|
||||
glGetProgramInfoLog(p, len, nullptr, &log[0]);
|
||||
std::cerr << "Program link error: " << log << '\n';
|
||||
glDeleteProgram(p);
|
||||
return 0;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
std::cerr << "SDL_Init error: " << SDL_GetError() << '\n';
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Pedir contexto OpenGL 3.3 Core
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
|
||||
int winW = 800, winH = 800;
|
||||
// SDL3: SDL_CreateWindow signature es (title, w, h, flags)
|
||||
SDL_Window *window = SDL_CreateWindow("Shadertoy SDL3 + OpenGL",
|
||||
winW, winH,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||
if (!window)
|
||||
{
|
||||
std::cerr << "SDL_CreateWindow error: " << SDL_GetError() << '\n';
|
||||
SDL_Quit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
||||
if (!glContext)
|
||||
{
|
||||
std::cerr << "SDL_GL_CreateContext error: " << SDL_GetError() << '\n';
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
|
||||
{
|
||||
std::cerr << "Failed to initialize GL loader\n";
|
||||
SDL_GL_DestroyContext(glContext);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSrc);
|
||||
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSrc);
|
||||
if (!vs || !fs)
|
||||
return -1;
|
||||
GLuint program = linkProgram(vs, fs);
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
if (!program)
|
||||
return -1;
|
||||
|
||||
float quadVertices[] = {
|
||||
-1.0f,
|
||||
-1.0f,
|
||||
1.0f,
|
||||
-1.0f,
|
||||
-1.0f,
|
||||
1.0f,
|
||||
1.0f,
|
||||
1.0f,
|
||||
};
|
||||
|
||||
GLuint vao = 0, vbo = 0;
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
GLint locRes = glGetUniformLocation(program, "iResolution");
|
||||
GLint locTime = glGetUniformLocation(program, "iTime");
|
||||
|
||||
bool running = true;
|
||||
Uint32 startTicks = SDL_GetTicks();
|
||||
|
||||
while (running)
|
||||
{
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e))
|
||||
{
|
||||
if (e.type == SDL_EVENT_QUIT)
|
||||
running = false;
|
||||
if (e.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED)
|
||||
running = false;
|
||||
}
|
||||
|
||||
int w, h;
|
||||
SDL_GetWindowSize(window, &w, &h);
|
||||
glViewport(0, 0, w, h);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(program);
|
||||
if (locRes >= 0)
|
||||
glUniform2f(locRes, float(w), float(h));
|
||||
if (locTime >= 0)
|
||||
{
|
||||
float t = (SDL_GetTicks() - startTicks) / 1000.0f;
|
||||
glUniform1f(locTime, t);
|
||||
}
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
glBindVertexArray(0);
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
SDL_Delay(1);
|
||||
}
|
||||
|
||||
glDeleteBuffers(1, &vbo);
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
glDeleteProgram(program);
|
||||
|
||||
SDL_GL_DestroyContext(glContext);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user