From 635576972024319c15141645d3304db8cd1d1e19 Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Mon, 8 Aug 2022 13:50:34 -0400 Subject: basic bezier path system added --- gl_calls.cpp | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 gl_calls.cpp (limited to 'gl_calls.cpp') diff --git a/gl_calls.cpp b/gl_calls.cpp new file mode 100644 index 0000000..85ce0b3 --- /dev/null +++ b/gl_calls.cpp @@ -0,0 +1,170 @@ +const char *vertexShaderSource = "#version 330 core\n" +"layout (location = 0) in vec3 aPos;\n" +"layout (location = 1) in vec2 aTexCoord;\n" +"out vec2 TexCoord;\n" +"void main()\n" +"{\n" +" gl_Position = vec4(aPos, 1.0);\n" +" TexCoord = aTexCoord;\n" +"}\0"; + +#if 0 +const char *fragmentShaderSource = "#version 330 core\n" +"out vec4 FragColor;\n" +"in vec2 TexCoord;\n" +"uniform float Start;\n" +"uniform float Mid;\n" +"uniform float End;\n" +"uniform vec4 StartCol;\n" +"uniform vec4 MidCol;\n" +"uniform vec4 EndCol;\n" +"uniform sampler2D ourTexture;\n" +"void main()\n" +"{\n" +"vec4 OutCol = texture(ourTexture, TexCoord);\n" +// individual channels +"vec4 ColorI = pow(OutCol, MidCol);\n" +"vec4 ValI = 1.0f / (EndCol - StartCol) * (ColorI - StartCol);\n" +// global channel +"vec4 ColorG = pow(ValI, vec4(Mid));\n" +"vec4 ValG = 1.0f / (End - Start) * (ColorG - Start);\n" +"FragColor = clamp(ValG, 0.0f, 1.0f);\n" +"}\0"; +#else +const char *fragmentShaderSource = "#version 330 core\n" +"uniform float Radius;\n" +"uniform vec2 Direction;\n" +"uniform sampler2D ourTexture;\n" +"out vec4 FragColor;\n" +"in vec2 TexCoord;\n" +"\n" +"vec4 blur(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {\n" +" vec4 color = vec4(0.0f);\n" +" float Omega = Radius / 3;\n" +" float Divisor = 2*Omega*Omega;\n" +" float A2 = 1.0f / (Omega * sqrt(2*3.141592));\n" +" for (float Span = -round(Radius); Span < round(Radius); Span++) {\n" +" float Dividend = -Span * Span;\n" +" float Multiplier = A2 * exp(Dividend/Divisor);\n" +" vec2 Dir = Span*direction;\n" +" color += texture2D(image, uv + (Dir / resolution)) * Multiplier;\n" +" }\n" +" return color;\n" +"}\n" +"void main(void) {\n" +" gl_FragColor = blur(ourTexture, TexCoord, vec2(1280, 720), Direction);\n" +"}\0"; +#endif + + +static void TestGL_InitShader() { + TGL.VertexShader = glCreateShader(GL_VERTEX_SHADER); + + // We have to compile our shaders before executing them. + glShaderSource(TGL.VertexShader, 1, &vertexShaderSource, NULL); + glCompileShader(TGL.VertexShader); + + int success; + char infoLog[512]; + glGetShaderiv(TGL.VertexShader, GL_COMPILE_STATUS, &success); + + if(!success) + { + glGetShaderInfoLog(TGL.VertexShader, 512, NULL, infoLog); + printf("Vertex shader fail:\n %s", infoLog); + } + + TGL.FragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + + glShaderSource(TGL.FragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(TGL.FragmentShader); + + glGetShaderiv(TGL.FragmentShader, GL_COMPILE_STATUS, &success); + + if(!success) + { + glGetShaderInfoLog(TGL.FragmentShader, 512, NULL, infoLog); + printf("Fragment shader fail:\n %s", infoLog); + } + + // Shader programs link both types of shaders together. + TGL.ShaderProgram = glCreateProgram(); + + glAttachShader(TGL.ShaderProgram, TGL.VertexShader); + glAttachShader(TGL.ShaderProgram, TGL.FragmentShader); + glLinkProgram(TGL.ShaderProgram); + + glGetProgramiv(TGL.ShaderProgram, GL_LINK_STATUS, &success); + if(!success) { + glGetProgramInfoLog(TGL.ShaderProgram, 512, NULL, infoLog); + printf("Shader linkage fail:\n %s", infoLog); + } + + // The shaders are no longer needed by anything once they're linked. + glDeleteShader(TGL.VertexShader); + glDeleteShader(TGL.FragmentShader); + +} + +static void TestGL_InitVerts() { + + float GLVertices[] = { + 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, + -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, + -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, + }; + unsigned int GLIndices[] = { + 0, 1, 3, + 1, 2, 3 + }; + + // Indices! + glGenBuffers(1, &TGL.EBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, TGL.EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLIndices), GLIndices, + GL_STATIC_DRAW); + glGenBuffers(1, &TGL.VBO); + + // Our vertices need to be stored in this buffer. + glBindBuffer(GL_ARRAY_BUFFER, TGL.VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(GLVertices), GLVertices, GL_STATIC_DRAW); + + // position attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + // texture coordinate (note the last parameter's offset) + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); +} + +void +TestGL_InitTexture(gl_effect_layer *Test, void *Data, uint16 Width, uint16 Height) +{ + glGenTextures(1, &Test->Texture); + glBindTexture(GL_TEXTURE_2D, Test->Texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glViewport(0, 0, Width, Height); + + glGenFramebuffers(1, &Test->FBO); + glGenRenderbuffers(1, &Test->RBO); + + glBindRenderbuffer(GL_RENDERBUFFER, Test->RBO); + glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, Width, Height); + + glBindFramebuffer(GL_FRAMEBUFFER, Test->FBO); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, Test->RBO); + + // Non-POT textures with RGB instead of RGBA doesn't seem to work, but + // since I don't plan on storing bitmaps that way (AVX2 is much more + // efficient with RGBA) it doesn't really matter. + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA, + GL_UNSIGNED_BYTE, Data); + // glGenerateMipmap(GL_TEXTURE_2D); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} -- cgit v1.2.3