opengl_x11

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

commit 610172bf4c37ce9222ef7dd739a848779e193298
parent 175a629e9c96b4d79e6e9693386ccc9a95665e88
Author: bsandro <email@bsandro.tech>
Date:   Mon, 20 Oct 2025 22:06:29 +0300

rename files

Diffstat:
Dmain1.c | 199-------------------------------------------------------------------------------
Rmain3.c -> micro.c | 0
Rmain2.c -> shaders.c | 0
Rmain2.c -> tesselating.c | 0
4 files changed, 0 insertions(+), 199 deletions(-)

diff --git a/main1.c b/main1.c @@ -1,199 +0,0 @@ -/* basic vertex+fragment shaders */ -#include <stdio.h> -#include <stdlib.h> -#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 -#include <GL/gl.h> -#include <GL/glx.h> -#include <GL/glu.h> - -Display *dpy; -Window root; -GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; -XVisualInfo *vi; -XSetWindowAttributes swa; -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 double t_started; - -//@todo move shaders to separate files -static const GLchar *vertexSrc = " \n\ -#version 420 \n\ -in vec2 position; \n\ -in vec3 inColor; \n\ -out vec3 color; \n\ -void main() { \n\ - gl_Position = vec4(position, 0.0, 1.0); \n\ - color = inColor; \n\ -} \n\ -"; - -static const GLchar *fragmentSrc = " \n\ -#version 420 \n\ -uniform double iTime; \n\ -in vec3 color; \n\ -out vec4 outColor; \n\ -void main() { \n\ - float c1 = abs(sin(float(iTime))); \n\ - float c2 = abs(cos(float(iTime))); \n\ - outColor = vec4(color+c1-c2, 1.0); \n\ -} \n\ -"; - -static GLuint mkShader(GLenum type, const GLchar *src) { - GLuint shader = glCreateShader(type); - glShaderSource(shader, 1, &src, NULL); - glCompileShader(shader); - GLint ok = 0; - glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); - if (!ok) { - static char buf[512] = {0}; - glGetShaderInfoLog(shader, sizeof(buf)-1, NULL, buf); - fprintf(stderr, "Shader %d compile error: %s\n", (int)type, buf); - } - return shader; -} - -static GLuint linkProgram(GLuint vertex, GLuint fragment) { - GLuint shaderProgram = glCreateProgram(); - glAttachShader(shaderProgram, vertex); - glAttachShader(shaderProgram, fragment); - glBindAttribLocation(shaderProgram, 0, "position"); - glBindFragDataLocation(shaderProgram, 0, "outColor"); - glLinkProgram(shaderProgram); - return shaderProgram; -} - -void __attribute__((constructor)) start() { - printf("start()\n"); - t_started = current_ts(); -} - -void __attribute((destructor)) end() { - printf("end()\n"); -} - -int main(int argc, char *argv[]) { - dpy = XOpenDisplay(NULL); - - if (dpy == NULL) { - printf("\n\tcannot connect to X server\n\n"); - exit(0); - } - - root = DefaultRootWindow(dpy); - vi = glXChooseVisual(dpy, 0, att); - - if (vi == NULL) { - printf("\n\tno appropriate visual found\n\n"); - exit(0); - } else { - printf("\n\tvisual %p selected\n", (void *)vi->visualid); /* %p creates hexadecimal output like in glxinfo */ - } - - Colormap cmap = XCreateColormap(dpy, root, vi->visual, AllocNone); - swa.colormap = cmap; - swa.event_mask = ExposureMask | KeyPressMask; - win = XCreateWindow(dpy, root, 0, 0, 500, 500, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa); - XMapWindow(dpy, win); - XStoreName(dpy, win, "OPENGL"); - GLXContext ctx = glXCreateContext(dpy, vi, NULL, GL_TRUE); - glXMakeCurrent(dpy, win, ctx); - - GLuint vao; - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - // [x, y, r, g, b] - static const GLfloat vertices[] = { - 0.01f, 0.01f, 1.0f, 0.0f, 0.0f, - 0.8f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.8f, 0.0f, 0.0f, 1.0f, - - -0.01f, -0.01f, 0.0f, 0.0f, 1.0f, - -0.8f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, -0.8f, 1.0f, 0.0f, 0.0f, - - 0.3f, 0.3f, 1.0f, 0.0f, 0.0f, - 0.3f, 0.6f, 1.0f, 0.0f, 0.0f, - 0.6f, 0.3f, 1.0f, 0.0f, 0.0f, - 0.6f, 0.6f, 1.0f, 0.0f, 0.0f - }; - - static const GLuint elements[] = { - 0, 1, 5, - 3, 2, 4, - 6, 7, 8, - 7, 8, 9 - }; - - GLuint vbo; - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - GLuint ebo; - glGenBuffers(1, &ebo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, 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; - 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); - } - } - - 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); - - GLint position = glGetAttribLocation(shaderPrg, "position"); - glEnableVertexAttribArray(position); - glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), 0); - GLint color = glGetAttribLocation(shaderPrg, "inColor"); - glEnableVertexAttribArray(color); - glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void *)(2*sizeof(GLfloat))); - glDrawElements(GL_TRIANGLES, sizeof(elements), GL_UNSIGNED_INT, 0); - - glXSwapBuffers(dpy, win); - usleep(1000*16); - } -} diff --git a/main3.c b/micro.c diff --git a/main2.c b/shaders.c diff --git a/main2.c b/tesselating.c