opengl_x11

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

commit 178b30516d7089192ab60973d7b2ab5a2edea450
parent 2e3cf4e48f6aec3834e75675b2fd7847beabdc3b
Author: bsandro <email@bsandro.tech>
Date:   Mon,  1 Dec 2025 01:40:46 +0200

crawling square (changing vertex buffer every frame)

Diffstat:
Mobj2d.c | 27++++++++++++++++++---------
1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/obj2d.c b/obj2d.c @@ -12,6 +12,9 @@ #include <GL/glx.h> #include <GL/glu.h> +#define SIZE_W 800 +#define SIZE_H 800 + #define LEN(x) (sizeof(x)/sizeof(x[0])) #define GL_CHECK(x) \ @@ -32,11 +35,10 @@ static const double current_ts() { static double t_started; -//@todo move shaders to separate files static const GLchar *vertexSrc = R"( #version 420 -layout(location=0) in vec2 position; -layout(location=1) in vec3 color; +in vec2 position; +in vec3 color; out vec3 dotColor; void main() { gl_Position = vec4(position, 0.0, 1.0); @@ -73,7 +75,11 @@ uniform double iTime; in vec3 fColor; out vec4 outColor; void main() { - outColor = vec4(fColor, 1.0); + vec3 color = fColor; + if (gl_FragCoord.x < 300) { + color.g = 1.0f; + } + outColor = vec4(color, 1.0); } )"; @@ -96,8 +102,8 @@ static GLuint linkProgram(GLuint vertex, GLuint geometry, GLuint fragment) { glAttachShader(shaderProgram, vertex); glAttachShader(shaderProgram, geometry); glAttachShader(shaderProgram, fragment); - glBindAttribLocation(shaderProgram, 0, "position"); - glBindAttribLocation(shaderProgram, 1, "color"); + //glBindAttribLocation(shaderProgram, 0, "position"); + //glBindAttribLocation(shaderProgram, 1, "color"); glLinkProgram(shaderProgram); return shaderProgram; } @@ -136,7 +142,7 @@ int main(int argc, char *argv[]) { 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); + win = XCreateWindow(dpy, root, 0, 0, SIZE_W, SIZE_H, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa); XMapWindow(dpy, win); XStoreName(dpy, win, "OPENGL"); GLXContext ctx = glXCreateContext(dpy, vi, NULL, GL_TRUE); @@ -156,7 +162,7 @@ int main(int argc, char *argv[]) { glBindVertexArray(vao); // x,y, color(r,g,b) - static const GLfloat vertices[][5] = { + GLfloat vertices[][5] = { {-0.3, -0.5, 1.f, 0.f, 0.f}, { 0.6, -0.1, 0.f, 1.f, 0.f}, { 0.4, 0.4, 0.f, 0.f, 1.f}, @@ -166,7 +172,7 @@ int main(int argc, char *argv[]) { GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW); GLuint vertex = mkShader(GL_VERTEX_SHADER, vertexSrc); GLuint geometry = mkShader(GL_GEOMETRY_SHADER, geometrySrc); @@ -208,6 +214,9 @@ int main(int argc, char *argv[]) { glViewport(0, 0, ce->width, ce->height); } } +vertices[0][0] += 0.0003f; +glNamedBufferData(vbo, sizeof(vertices), vertices, GL_DYNAMIC_DRAW); + glClearColor(0.1f, 0.1f, 0.15f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);