opengl_x11

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

commit 6e83362402a67dc708acc6fddcc2771af9cce4d7
parent e3e8705316acdc30fb75835207d448d276e9400e
Author: bsandro <email@bsandro.tech>
Date:   Mon, 20 Oct 2025 22:19:01 +0300

resurrected wrongly moved file

Diffstat:
Mshaders.c | 149+++++++++++++++++++++++++++++--------------------------------------------------
1 file changed, 55 insertions(+), 94 deletions(-)

diff --git a/shaders.c b/shaders.c @@ -1,4 +1,4 @@ -/* tesselation */ +/* basic vertex+fragment shaders */ #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -12,9 +12,14 @@ #include <GL/glx.h> #include <GL/glu.h> -#ifndef CALLBACK -#define CALLBACK void (*)(void) -#endif +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; @@ -24,41 +29,6 @@ static const double current_ts() { static double t_started; -static void tessCbBegin(GLenum type) { - glBegin(type); - printf("glBegin(%d)\n", type); -} - -static void tessCbVertex(void *data) { - const float scale = 0.2f; - const GLdouble *p = data; - glVertex3d(p[0]*scale, p[1]*scale, p[2]); - printf("glVertex3d(%.1f, %.1f, %.1f)\n", *p, *(p+1), *(p+2)); -} - -static void tessCbEnd() { - glEnd(); - printf("glEnd()\n"); -} - -static void tessCbError(GLenum errno) { - printf("tesselator error: %d\n", errno); -} - -static void tessCbCombine(GLdouble coords[3], GLdouble *data[4], GLfloat weight[4], void **outData) { - // let memory leak for now - GLdouble *out = malloc(sizeof(GLdouble)*7); // x,y,z,r,g,b,a - out[0] = coords[0]; - out[1] = coords[1]; - out[2] = coords[2]; - // color - out[3] = weight[0]*data[0][3]+weight[1]*data[1][3]+weight[2]*data[2][3]+weight[3]*data[3][3]; - out[4] = weight[0]*data[0][4]+weight[1]*data[1][4]+weight[2]*data[2][4]+weight[3]*data[3][4]; - out[5] = weight[0]*data[0][5]+weight[1]*data[1][5]+weight[2]*data[2][5]+weight[3]*data[3][5]; - out[6] = weight[0]*data[0][6]+weight[1]*data[1][6]+weight[2]*data[2][6]+weight[3]*data[3][6]; - *outData = out; -} - //@todo move shaders to separate files static const GLchar *vertexSrc = " \n\ #version 420 \n\ @@ -117,20 +87,15 @@ void __attribute((destructor)) end() { } int main(int argc, char *argv[]) { - Display *dpy = XOpenDisplay(NULL); - XSetWindowAttributes swa; - Window win; - XWindowAttributes gwa; - XEvent xev; - GLint attr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; + dpy = XOpenDisplay(NULL); if (dpy == NULL) { printf("\n\tcannot connect to X server\n\n"); exit(0); } - Window root = DefaultRootWindow(dpy); - XVisualInfo *vi = glXChooseVisual(dpy, 0, attr); + root = DefaultRootWindow(dpy); + vi = glXChooseVisual(dpy, 0, att); if (vi == NULL) { printf("\n\tno appropriate visual found\n\n"); @@ -148,29 +113,42 @@ int main(int argc, char *argv[]) { GLXContext ctx = glXCreateContext(dpy, vi, NULL, GL_TRUE); glXMakeCurrent(dpy, win, ctx); - //GLdouble vertices_poly[][3] = { {-2,3,0}, {-2,0,0}, {2,0,0}, { 2,3,0}, {-1,2,0}, {-1,1,0}, {1,1,0}, { 1,2,0} }; - //GLdouble vertices_poly[][3] = { {-1,2,0}, {0,0,0}, {1,2,0}, {0,1.5,0} }; - /*GLdouble vertices_poly[][3] = { - { -0.7, -0.5, 0.0 }, - { -0.3, 0.3, 0.0 }, - { 0.0, 0.6, 0.0 }, - { 0.4, 0.3, 0.0 }, - { 0.2, -0.4, 0.0 }, - { 0.7, -0.4, 0.0 }, - { -0.4, -0.7, 0.0 } - };*/ - GLdouble vertices_poly[][6] = { { 0.0, 3.0, 0, 1, 0, 0}, - {-1.0, 0.0, 0, 0, 1, 0}, - { 1.6, 1.9, 0, 1, 0, 1}, - {-1.6, 1.9, 0, 1, 1, 0}, - { 1.0, 0.0, 0, 0, 0, 1} }; - int vertices_num = sizeof(vertices_poly)/sizeof(vertices_poly[0]); - GLUtesselator *tess = gluNewTess(); - gluTessCallback(tess, GLU_TESS_BEGIN, (CALLBACK)tessCbBegin); - gluTessCallback(tess, GLU_TESS_END, (CALLBACK)tessCbEnd); - gluTessCallback(tess, GLU_TESS_VERTEX, (CALLBACK)tessCbVertex); - gluTessCallback(tess, GLU_TESS_ERROR, (CALLBACK)tessCbError); - gluTessCallback(tess, GLU_TESS_COMBINE, (CALLBACK)tessCbCombine); + 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); @@ -198,18 +176,6 @@ int main(int argc, char *argv[]) { } } - // this scaling doesn't work for tesselation !!! - - /*glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(-10, 10, -10, 10, -1.5, 1.5); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity();*/ - - //glMatrixMode(GL_MODELVIEW); - //glLoadIdentity(); - //glScaled(0.1, 0.1, 1.0); - glClearColor(0.1f, 0.1f, 0.15f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); @@ -219,18 +185,13 @@ int main(int argc, char *argv[]) { glUseProgram(shaderPrg); - GLuint list = glGenLists(1); - glNewList(list, GL_COMPILE); - gluTessBeginPolygon(tess, NULL); - gluTessBeginContour(tess); - for (int i=0;i<vertices_num;++i) { - gluTessVertex(tess, vertices_poly[i], vertices_poly[i]); - } - gluTessEndContour(tess); - gluTessEndPolygon(tess); - glEndList(); - - glCallList(list); + 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);