citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 8a265c4667a1101b8b6476e5959c3aa3889e2ad1
parent 0dd49fc6169fff94e0b78e674ece100969845492
Author: Kyle Milz <kyle@0x30.net>
Date:   Sun, 18 Dec 2016 15:58:32 -0700

src: integrate gltest with gl

Diffstat:
Msrc/gl_testmain.cc | 293++++++++++++++++++++++++-------------------------------------------------------
1 file changed, 89 insertions(+), 204 deletions(-)

diff --git a/src/gl_testmain.cc b/src/gl_testmain.cc @@ -1,171 +1,70 @@ -/* - * Demo of off-screen Mesa rendering - * - * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions. - * - * If you want to render BIG images you'll probably have to increase - * MAX_WIDTH and MAX_Height in src/config.h. - * - * This program is in the public domain. - * - * Brian Paul - * - * PPM output provided by Joerg Schmalzl. - * ASCII PPM output added by Brian Paul. - * - * Usage: osdemo [filename] - */ - - -#include <math.h> +// +// osdemo.c originally from Brian Paul, public domain. +// PPM output provided by Joerg Schmalzl. +// ASCII PPM output added by Brian Paul. +// +#include <err.h> +#include <GL/glew.h> +#define GLAPI extern +#include <GL/osmesa.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "GL/osmesa.h" - -#ifdef __APPLE__ -# include <OpenGL/gl.h> -# include <OpenGL/glu.h> -#else -# include <GL/gl.h> -# include <GL/glu.h> -#endif -#ifndef GLAPIENTRY -#define GLAPIENTRY -#endif +#include "gl_buffer.h" +#include "gl_font.h" +#include "gl_procfile.h" +#include "gl_view.h" +#include "process_dir.h" -// #define SAVE_TARGA +#define SAVE_TARGA static int Width = 400; static int Height = 400; static void -Sphere(float radius, int slices, int stacks) -{ - GLUquadric *q = gluNewQuadric(); - gluQuadricNormals(q, GLU_SMOOTH); - gluSphere(q, radius, slices, stacks); - gluDeleteQuadric(q); -} - - -static void -Cone(float base, float height, int slices, int stacks) -{ - GLUquadric *q = gluNewQuadric(); - gluQuadricDrawStyle(q, GLU_FILL); - gluQuadricNormals(q, GLU_SMOOTH); - gluCylinder(q, base, 0.0, height, slices, stacks); - gluDeleteQuadric(q); -} - - -static void -Torus(float innerRadius, float outerRadius, int sides, int rings) -{ - /* from GLUT... */ - int i, j; - GLfloat theta, phi, theta1; - GLfloat cosTheta, sinTheta; - GLfloat cosTheta1, sinTheta1; - const GLfloat ringDelta = 2.0 * M_PI / rings; - const GLfloat sideDelta = 2.0 * M_PI / sides; - - theta = 0.0; - cosTheta = 1.0; - sinTheta = 0.0; - for (i = rings - 1; i >= 0; i--) { - theta1 = theta + ringDelta; - cosTheta1 = cos(theta1); - sinTheta1 = sin(theta1); - glBegin(GL_QUAD_STRIP); - phi = 0.0; - for (j = sides; j >= 0; j--) { - GLfloat cosPhi, sinPhi, dist; - - phi += sideDelta; - cosPhi = cos(phi); - sinPhi = sin(phi); - dist = outerRadius + innerRadius * cosPhi; - - glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi); - glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, innerRadius * sinPhi); - glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi); - glVertex3f(cosTheta * dist, -sinTheta * dist, innerRadius * sinPhi); - } - glEnd(); - theta = theta1; - cosTheta = cosTheta1; - sinTheta = sinTheta1; - } -} - - -static void render_image(void) { - GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; - GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; - GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; - GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; - GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 }; - GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 }; - GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 }; - + ProcessDir m_pdir; + std::vector<GlProcessFile> drawables; - glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); - glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); - glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); - glLightfv(GL_LIGHT0, GL_POSITION, light_position); + demo_glstate_t *st = demo_glstate_create(); + GlBuffer text_buffer; - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_DEPTH_TEST); + View *static_vu = new View(st); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0); - glMatrixMode(GL_MODELVIEW); + demo_font_t *font = demo_font_create(demo_glstate_get_atlas(st)); - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + static_vu->setup(); - glPushMatrix(); - glRotatef(20.0, 1.0, 0.0, 0.0); + glyphy_point_t top_left = { 0, 0 }; + text_buffer.move_to(&top_left); + text_buffer.add_text("waiting...", font, 1); - glPushMatrix(); - glTranslatef(-0.75, 0.5, 0.0); - glRotatef(90.0, 1.0, 0.0, 0.0); - glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat ); - Torus(0.275, 0.85, 20, 20); - glPopMatrix(); + for (std::string &file_name : m_pdir.scan()) + drawables.emplace_back(file_name, font); - glPushMatrix(); - glTranslatef(-0.75, -0.5, 0.0); - glRotatef(270.0, 1.0, 0.0, 0.0); - glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat ); - Cone(1.0, 2.0, 16, 1); - glPopMatrix(); + glyphy_extents_t extents; + for (auto &i : drawables) { + glyphy_extents_t t = i.get_extents(); + extents.max_x = std::max(extents.max_x, t.max_x); + extents.max_y = std::max(extents.max_y, t.max_y); + extents.min_x = std::min(extents.min_x, t.min_x); + extents.min_y = std::min(extents.min_y, t.min_y); + } - glPushMatrix(); - glTranslatef(0.75, 0.0, -1.0); - glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat ); - Sphere(1.0, 20, 20); - glPopMatrix(); + // Set up view transforms + static_vu->display(extents); - glPopMatrix(); + text_buffer.draw(); - /* This is very important!!! - * Make sure buffered commands are finished!!! - */ - glFinish(); + for (auto &i : drawables) + i.display(); } - #ifdef SAVE_TARGA - static void write_targa(const char *filename, const GLubyte *buffer, int width, int height) { @@ -204,9 +103,7 @@ write_targa(const char *filename, const GLubyte *buffer, int width, int height) } } } - #else - static void write_ppm(const char *filename, const GLubyte *buffer, int width, int height) { @@ -251,84 +148,72 @@ write_ppm(const char *filename, const GLubyte *buffer, int width, int height) fclose(f); } } - -#endif - - +#endif // SAVE_TARGA int main(int argc, char *argv[]) { - OSMesaContext ctx; - void *buffer; - char *filename = NULL; - - if (argc < 2) { - fprintf(stderr, "Usage:\n"); - fprintf(stderr, " osdemo filename [width height]\n"); - return 0; - } - - filename = argv[1]; - if (argc == 4) { - Width = atoi(argv[2]); - Height = atoi(argv[3]); - } - - /* Create an RGBA-mode context */ + OSMesaContext ctx; + void *buffer; + char *filename = NULL; + + if (argc < 2) { + fprintf(stderr, "Usage:\n"); + fprintf(stderr, " %s filename [width height]\n", argv[0]); + return 0; + } + + filename = argv[1]; + if (argc == 4) { + Width = atoi(argv[2]); + Height = atoi(argv[3]); + } + + // Create an RGBA-mode context #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 - /* specify Z, stencil, accum sizes */ - ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL ); + // specify Z, stencil, accum sizes + ctx = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, NULL); #else - ctx = OSMesaCreateContext( OSMESA_RGBA, NULL ); + ctx = OSMesaCreateContext(OSMESA_RGBA, NULL); #endif - if (!ctx) { - printf("OSMesaCreateContext failed!\n"); - return 0; - } + if (!ctx) + errx(1, "OSMesaCreateContext failed!"); - /* Allocate the image buffer */ - buffer = malloc( Width * Height * 4 * sizeof(GLubyte) ); - if (!buffer) { - printf("Alloc image buffer failed!\n"); - return 0; - } + // Allocate the image buffer + if ((buffer = malloc(Width * Height * 4 * sizeof(GLubyte))) == NULL) + errx(1, "Alloc image buffer failed!"); - /* Bind the buffer to the context and make it current */ - if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height )) { - printf("OSMesaMakeCurrent failed!\n"); - return 0; - } + // Bind the buffer to the context and make it current + if (!OSMesaMakeCurrent(ctx, buffer, GL_UNSIGNED_BYTE, Width, Height)) + errx(1, "OSMesaMakeCurrent failed!"); + int z, s, a; + glGetIntegerv(GL_DEPTH_BITS, &z); + glGetIntegerv(GL_STENCIL_BITS, &s); + glGetIntegerv(GL_ACCUM_RED_BITS, &a); + printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); - { - int z, s, a; - glGetIntegerv(GL_DEPTH_BITS, &z); - glGetIntegerv(GL_STENCIL_BITS, &s); - glGetIntegerv(GL_ACCUM_RED_BITS, &a); - printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); - } + GLenum glew_status = glewInit(); + if (GLEW_OK != glew_status) + errx(1, "%s", glewGetErrorString(glew_status)); + if (!glewIsSupported("GL_VERSION_2_0")) + errx(1, "No support for OpenGL 2.0 found"); - render_image(); + render_image(); - if (filename != NULL) { + if (filename != NULL) #ifdef SAVE_TARGA - write_targa(filename, buffer, Width, Height); + write_targa(filename, static_cast<const GLubyte *>(buffer), Width, Height); #else - write_ppm(filename, static_cast<const GLubyte *>(buffer), Width, Height); + write_ppm(filename, static_cast<const GLubyte *>(buffer), Width, Height); #endif - } - else { - printf("Specify a filename if you want to make an image file\n"); - } - - printf("all done\n"); + else + printf("Specify a filename if you want to make an image file\n"); - /* free the image buffer */ - free( buffer ); + printf("all done\n"); - /* destroy the context */ - OSMesaDestroyContext( ctx ); + free(buffer); + OSMesaDestroyContext( ctx ); - return 0; + return 0; }