opengl_x11

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

commit db78243437c28ef7ea818f63f3a11acd6c68c6be
parent dd3672d3ac6aec861233f3ad77f65525cc0a26ee
Author: bsandro <email@bsandro.tech>
Date:   Mon,  6 Oct 2025 21:58:25 +0300

leaking mem left and right

Diffstat:
MMakefile | 2+-
Mmain1.c | 98+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
2 files changed, 57 insertions(+), 43 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ all: - ${CC} main1.c -std=c99 -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 -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 @@ -1,6 +1,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> +#include <unistd.h> #include <X11/X.h> #include <X11/Xlib.h> #define GL_GLEXT_PROTOTYPES 1 @@ -17,8 +19,14 @@ Window win; XWindowAttributes gwa; XEvent xev; +static const double current_ts() { + struct timespec ts; + timespec_get(&ts, TIME_UTC); + return (double)ts.tv_sec + (double)ts.tv_nsec/1e9; +} + static const GLchar *vertexSrc = " \n\ -#version 150 core \n\ +#version 420 \n\ in vec3 position; \n\ void main() { \n\ gl_Position = vec4(position, 1.0); \n\ @@ -26,10 +34,12 @@ void main() { \n\ "; static const GLchar *fragmentSrc = " \n\ -#version 150 core \n\ +#version 420 \n\ +uniform double iTime; \n\ +uniform float alpha; \n\ out vec4 outColor; \n\ void main() { \n\ - outColor = vec4(0.7, 0.5, 0.3, 1.0);\n\ + outColor = vec4(sin(float(iTime)), 0.5, 0.3, sin(float(iTime)));\n\ } \n\ "; @@ -61,33 +71,18 @@ void DrawThing() { glClearColor(0.1f, 0.1f, 0.15f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - /*glBegin(GL_TRIANGLES); - // Vertex data - glColor3f(1.0f, 0.0f, 0.0f); - glVertex3f(-0.3f, 0.0f, 0.0f); - glColor3f(0.0f, 1.0f, 0.0f); - glVertex3f(0.3f, 0.1f, 0.0f); - glColor3f(1.0f, 0.0f, 1.0f); - glVertex3f(0.0f, 0.8f, 0.0f); - glEnd(); - - glBegin(GL_POLYGON); - glColor3f(0.8f, 0.0f, 0.0f); - glVertex3f(0.5f, 0.5f, 0.5f); - glVertex3f(0.1f, 0.1f, 0.1f); - glVertex3f(-0.1f, -0.1f, 0.1f); - glVertex3f(0.4f, 0.0f, 0.0f); - glVertex3f(-0.4f, 0.0f, 0.3f); - glEnd();*/ - GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLfloat vertices[] = { - 0.0f, 0.0f, 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, + + -0.01f, -0.01f, 0.0f, + -0.4f, 0.0f, 0.0f, + 0.0f, -0.4f, 0.0f }; GLuint vbo; @@ -98,15 +93,28 @@ void DrawThing() { 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, 3); + glDrawArrays(GL_TRIANGLES, 0, sizeof(vertices)/sizeof(GLfloat)/3); glDisableVertexAttribArray(0); glUseProgram(0); } +void __attribute__((constructor)) start() { + printf("start()\n"); +} + +void __attribute((destructor)) end() { + printf("end()\n"); +} + int main(int argc, char *argv[]) { dpy = XOpenDisplay(NULL); @@ -133,24 +141,30 @@ int main(int argc, char *argv[]) { XStoreName(dpy, win, "OPENGL"); GLXContext ctx = glXCreateContext(dpy, vi, NULL, GL_TRUE); glXMakeCurrent(dpy, win, ctx); - float p = .0f; while (1) { - // while (XPending(dpy)) ??? - XNextEvent(dpy, &xev); - if (xev.type == Expose) { - XGetWindowAttributes(dpy, win, &gwa); - glViewport(0, 0, gwa.width, gwa.height); - DrawThing(); - glXSwapBuffers(dpy, win); - } else if (xev.type == KeyPress) { - printf("btn:%d\n", xev.xbutton.button); - if (xev.xbutton.button==24||xev.xbutton.button==9) { - glXMakeCurrent(dpy, None, NULL); - glXDestroyContext(dpy, ctx); - XDestroyWindow(dpy, win); - XCloseDisplay(dpy); - exit(0); + while (XPending(dpy)) { + XEvent xev; + XNextEvent(dpy, &xev); + if (xev.type == Expose) { + XGetWindowAttributes(dpy, win, &gwa); + glViewport(0, 0, gwa.width, gwa.height); + } else if (xev.type == KeyPress) { + printf("btn:%d\n", xev.xbutton.button); + if (xev.xbutton.button==24||xev.xbutton.button==9) { + glXMakeCurrent(dpy, None, NULL); + glXDestroyContext(dpy, ctx); + XDestroyWindow(dpy, win); + XCloseDisplay(dpy); + exit(0); + } + } else if (xev.type == ConfigureNotify) { + XConfigureEvent *ce = (XConfigureEvent *)&xev; + glViewport(0, 0, ce->width, ce->height); } } + + DrawThing(); + glXSwapBuffers(dpy, win); + usleep(1000*16); } }