opengl_x11

Playing with OpenGL
git clone git://bsandro.tech/opengl_x11
Log | Files | Refs

commit 3e182313eb72b66426a814c3d1d8725165a26f83
parent db78243437c28ef7ea818f63f3a11acd6c68c6be
Author: bsandro <email@bsandro.tech>
Date:   Mon,  6 Oct 2025 23:05:22 +0300

still leaking memory

Diffstat:
MMakefile | 2+-
Mmain1.c | 100++++++++++++++++++++++++++++++++++++++++++-------------------------------------
2 files changed, 54 insertions(+), 48 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ all: - ${CC} main1.c -std=gnu23 -Wpedantic -Os -s -fomit-frame-pointer -ffast-math -fno-stack-protector -march=native -fwrapv -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables -fno-unwind-tables -fmerge-all-constants -fno-ident -lGL -lGLU -lX11 -Wl,--gc-sections -o test1 + ${CC} main1.c -std=gnu23 -Wpedantic -Os -s -fomit-frame-pointer -ffast-math -fno-stack-protector -march=native -fwrapv -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables -fno-unwind-tables -fmerge-all-constants -fno-ident -lm -lGL -lGLU -lX11 -Wl,--gc-sections -o test1 min: all strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag ./test1 diff --git a/main1.c b/main1.c @@ -3,6 +3,7 @@ #include <string.h> #include <time.h> #include <unistd.h> +#include <math.h> #include <X11/X.h> #include <X11/Xlib.h> #define GL_GLEXT_PROTOTYPES 1 @@ -25,6 +26,8 @@ static const double current_ts() { return (double)ts.tv_sec + (double)ts.tv_nsec/1e9; } +static double t_started; + static const GLchar *vertexSrc = " \n\ #version 420 \n\ in vec3 position; \n\ @@ -33,14 +36,14 @@ void main() { \n\ } \n\ "; -static const GLchar *fragmentSrc = " \n\ -#version 420 \n\ -uniform double iTime; \n\ -uniform float alpha; \n\ -out vec4 outColor; \n\ -void main() { \n\ - outColor = vec4(sin(float(iTime)), 0.5, 0.3, sin(float(iTime)));\n\ -} \n\ +static const GLchar *fragmentSrc = " \n\ +#version 420 \n\ +uniform double iTime; \n\ +out vec4 outColor; \n\ +void main() { \n\ + float c = abs(sin(float(iTime))); \n\ + outColor = vec4(c, 0.5, 0.3, 1.0); \n\ +} \n\ "; static GLuint mkShader(GLenum type, const GLchar *src) { @@ -67,48 +70,13 @@ static GLuint linkProgram(GLuint vertex, GLuint fragment) { return shaderProgram; } -void DrawThing() { - glClearColor(0.1f, 0.1f, 0.15f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - GLfloat vertices[] = { - 0.01f, 0.01f, 0.0f, - 0.4f, 0.0f, 0.0f, - 0.0f, 0.4f, 0.0f, - - -0.01f, -0.01f, 0.0f, - -0.4f, 0.0f, 0.0f, - 0.0f, -0.4f, 0.0f - }; - - GLuint vbo; - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - GLuint vertex = mkShader(GL_VERTEX_SHADER, vertexSrc); - GLuint fragment = mkShader(GL_FRAGMENT_SHADER, fragmentSrc); - GLuint shaderPrg = linkProgram(vertex, fragment); - - GLfloat iTime = glGetUniformLocation(shaderPrg, "iTime"); - glUniform1d(iTime, current_ts()); - printf("%f\n", current_ts()); - - glUseProgram(shaderPrg); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)0); - glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)/sizeof(GLfloat)/3); - glDisableVertexAttribArray(0); - glUseProgram(0); +void DrawThing(GLuint shaderPrg) { + } void __attribute__((constructor)) start() { printf("start()\n"); + t_started = current_ts(); } void __attribute((destructor)) end() { @@ -141,6 +109,30 @@ int main(int argc, char *argv[]) { XStoreName(dpy, win, "OPENGL"); GLXContext ctx = glXCreateContext(dpy, vi, NULL, GL_TRUE); glXMakeCurrent(dpy, win, ctx); + + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + static const GLfloat vertices[] = { + 0.01f, 0.01f, 0.0f, + 0.4f, 0.0f, 0.0f, + 0.0f, 0.4f, 0.0f, + + -0.01f, -0.01f, 0.0f, + -0.4f, 0.0f, 0.0f, + 0.0f, -0.4f, 0.0f + }; + + GLuint vbo; + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + GLuint vertex = mkShader(GL_VERTEX_SHADER, vertexSrc); + GLuint fragment = mkShader(GL_FRAGMENT_SHADER, fragmentSrc); + GLuint shaderPrg = linkProgram(vertex, fragment); + while (1) { while (XPending(dpy)) { XEvent xev; @@ -163,7 +155,21 @@ int main(int argc, char *argv[]) { } } - DrawThing(); + glClearColor(0.1f, 0.1f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + GLfloat iTime = glGetUniformLocation(shaderPrg, "iTime"); + glUniform1d(iTime, current_ts()-t_started); + //printf("%f\n", fabsf(sinf(current_ts()-t_started))); + + glUseProgram(shaderPrg); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)0); + glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)/sizeof(GLfloat)/3); + //glDisableVertexAttribArray(0); + //glUseProgram(0); + glXSwapBuffers(dpy, win); usleep(1000*16); }