opengl_x11

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

commit ad95809dbab825b09804b2a1ceceb1ad09ba0e3d
parent c2aa6601fedb944bfdbfef3d356626cafe9db466
Author: bsandro <email@bsandro.tech>
Date:   Sun, 19 Oct 2025 14:13:09 +0300

tesselation

Diffstat:
Mmain2.c | 130++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
1 file changed, 85 insertions(+), 45 deletions(-)

diff --git a/main2.c b/main2.c @@ -12,7 +12,9 @@ #include <GL/glx.h> #include <GL/glu.h> -static const GLint attr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; +#ifndef CALLBACK +#define CALLBACK void (*)(void) +#endif static const double current_ts() { struct timespec ts; @@ -22,6 +24,41 @@ 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\ @@ -85,7 +122,7 @@ int main(int argc, char *argv[]) { Window win; XWindowAttributes gwa; XEvent xev; - + GLint attr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; if (dpy == NULL) { printf("\n\tcannot connect to X server\n\n"); @@ -111,42 +148,29 @@ int main(int argc, char *argv[]) { 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); + //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] = { {-0.5,0.9,0}, {0,-0.5,0}, {0.5,0.9,0}, {0,0.3,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 vertex = mkShader(GL_VERTEX_SHADER, vertexSrc); GLuint fragment = mkShader(GL_FRAGMENT_SHADER, fragmentSrc); @@ -174,6 +198,17 @@ int main(int argc, char *argv[]) { } } + // 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); @@ -183,13 +218,18 @@ int main(int argc, char *argv[]) { 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); + 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); glXSwapBuffers(dpy, win); usleep(1000*16);