testdyngles is exactly what it sounds like -- a version of testdyngl that uses OpenGL ES calls instead of OpenGL. Was necessary to create because glOrtho is called glOrthof in OpenGL ES, and OpenGL ES doesn't have glBegin() type semantics for specifying geometry. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 15 Aug 2008 00:52:52 +0000
branchgsoc2008_iphone
changeset 24292c55b72ba46e
parent 2428 826210ef2c10
child 2430 166821fa1591
testdyngles is exactly what it sounds like -- a version of testdyngl that uses OpenGL ES calls instead of OpenGL. Was necessary to create because glOrtho is called glOrthof in OpenGL ES, and OpenGL ES doesn't have glBegin() type semantics for specifying geometry.
test/testdyngles.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testdyngles.c	Fri Aug 15 00:52:52 2008 +0000
     1.3 @@ -0,0 +1,211 @@
     1.4 +/*
     1.5 + * Small SDL example to demonstrate dynamically loading 
     1.6 + * OpenGL lib and functions
     1.7 + *
     1.8 + * (FYI it was supposed to look like snow in the wind or something...)
     1.9 + *
    1.10 + * Compile with :
    1.11 + * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
    1.12 + *
    1.13 + * You can specify a different OpenGL lib on the command line, i.e. :
    1.14 + * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
    1.15 + * or
    1.16 + * ./testdyngl  /usr/lib/libGL.so.1.0.4496
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#include <stdio.h>
    1.21 +#include <stdlib.h>
    1.22 +
    1.23 +#include "SDL.h"
    1.24 +
    1.25 +#ifdef __IPHONEOS__
    1.26 +#define HAVE_OPENGLES
    1.27 +#endif
    1.28 +
    1.29 +#ifdef HAVE_OPENGLES
    1.30 +
    1.31 +#include "SDL_opengles.h"
    1.32 +
    1.33 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.34 +static void
    1.35 +quit(int rc)
    1.36 +{
    1.37 +    SDL_Quit();
    1.38 +    exit(rc);
    1.39 +}
    1.40 +
    1.41 +void *
    1.42 +get_funcaddr(const char *p)
    1.43 +{
    1.44 +    void *f = SDL_GL_GetProcAddress(p);
    1.45 +    if (f) {
    1.46 +        return f;
    1.47 +    } else {
    1.48 +        printf("Unable to get function pointer for %s\n", p);
    1.49 +        quit(1);
    1.50 +    }
    1.51 +    return NULL;
    1.52 +}
    1.53 +
    1.54 +typedef struct
    1.55 +{
    1.56 +    //void (APIENTRY * glBegin) (GLenum);
    1.57 +    //void (APIENTRY * glEnd) ();
    1.58 +    //void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
    1.59 +	
    1.60 +	void (APIENTRY *glEnableClientState)(GLenum array);
    1.61 +	void (APIENTRY *glVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
    1.62 +	void (APIENTRY *glDrawArrays)(GLenum mode, GLint first, GLsizei count);
    1.63 +
    1.64 +	
    1.65 +    void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
    1.66 +    void (APIENTRY * glClear) (GLbitfield);
    1.67 +    void (APIENTRY * glDisable) (GLenum);
    1.68 +    void (APIENTRY * glEnable) (GLenum);
    1.69 +    void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
    1.70 +    void (APIENTRY * glPointSize) (GLfloat);
    1.71 +    void (APIENTRY * glHint) (GLenum, GLenum);
    1.72 +    void (APIENTRY * glBlendFunc) (GLenum, GLenum);
    1.73 +    void (APIENTRY * glMatrixMode) (GLenum);
    1.74 +    void (APIENTRY * glLoadIdentity) ();
    1.75 +    void (APIENTRY * glOrthof) (GLfloat, GLfloat, GLfloat, GLfloat,
    1.76 +                               GLfloat, GLfloat);
    1.77 +    void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
    1.78 +    void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
    1.79 +    void (APIENTRY * glFogf) (GLenum, GLfloat);
    1.80 +}
    1.81 +glfuncs;
    1.82 +
    1.83 +void
    1.84 +init_glfuncs(glfuncs * f)
    1.85 +{
    1.86 +	f->glEnableClientState = get_funcaddr("glEnableClientState");
    1.87 +	f->glVertexPointer = get_funcaddr("glVertexPointer");
    1.88 +	f->glDrawArrays = get_funcaddr("glDrawArrays");
    1.89 +	f->glClearColor = get_funcaddr("glClearColor");
    1.90 +    f->glClear = get_funcaddr("glClear");
    1.91 +    f->glDisable = get_funcaddr("glDisable");
    1.92 +    f->glEnable = get_funcaddr("glEnable");
    1.93 +    f->glColor4ub = get_funcaddr("glColor4ub");
    1.94 +    f->glPointSize = get_funcaddr("glPointSize");
    1.95 +    f->glHint = get_funcaddr("glHint");
    1.96 +    f->glBlendFunc = get_funcaddr("glBlendFunc");
    1.97 +    f->glMatrixMode = get_funcaddr("glMatrixMode");
    1.98 +    f->glLoadIdentity = get_funcaddr("glLoadIdentity");
    1.99 +    f->glOrthof = get_funcaddr("glOrthof");
   1.100 +    f->glRotatef = get_funcaddr("glRotatef");
   1.101 +    f->glViewport = get_funcaddr("glViewport");
   1.102 +    f->glFogf = get_funcaddr("glFogf");
   1.103 +}
   1.104 +
   1.105 +#define NB_PIXELS 1000
   1.106 +
   1.107 +int
   1.108 +main(int argc, char *argv[])
   1.109 +{
   1.110 +    glfuncs f;
   1.111 +    int i;
   1.112 +    SDL_Event event;
   1.113 +    int done = 0;
   1.114 +    GLfloat pixels[NB_PIXELS * 3];
   1.115 +    const char *gl_library = NULL;      /* Use the default GL library */
   1.116 +
   1.117 +	int video_w, video_h;
   1.118 +	
   1.119 +	/* you may want to change these according to the platform */
   1.120 +	video_w = 320; 
   1.121 +	video_h = 480;
   1.122 +	
   1.123 +    if (argv[1]) {
   1.124 +        gl_library = argv[1];
   1.125 +    }
   1.126 +
   1.127 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.128 +        printf("Unable to init SDL : %s\n", SDL_GetError());
   1.129 +        return (1);
   1.130 +    }
   1.131 +	
   1.132 +    if (SDL_GL_LoadLibrary(gl_library) < 0) {
   1.133 +        printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
   1.134 +        quit(1);
   1.135 +    }
   1.136 +
   1.137 +    if (SDL_SetVideoMode(video_h, video_w, 0, SDL_OPENGL) == NULL) {
   1.138 +        printf("Unable to open video mode : %s\n", SDL_GetError());
   1.139 +        quit(1);
   1.140 +    }
   1.141 +
   1.142 +    /* Set the window manager title bar */
   1.143 +    SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
   1.144 +
   1.145 +    init_glfuncs(&f);
   1.146 +
   1.147 +    for (i = 0; i < NB_PIXELS; i++) {
   1.148 +        pixels[3 * i] = rand() % 250 - 125;
   1.149 +        pixels[3 * i + 1] = rand() % 250 - 125;
   1.150 +        pixels[3 * i + 2] = rand() % 250 - 125;
   1.151 +    }
   1.152 +
   1.153 +    f.glViewport(0, 0, video_w, video_h);
   1.154 +
   1.155 +    f.glMatrixMode(GL_PROJECTION);
   1.156 +    f.glLoadIdentity();
   1.157 +    f.glOrthof(-100, 100, -100, 100, -500, 500);
   1.158 +
   1.159 +    f.glMatrixMode(GL_MODELVIEW);
   1.160 +    f.glLoadIdentity();
   1.161 +
   1.162 +    f.glEnable(GL_DEPTH_TEST);
   1.163 +    f.glDisable(GL_TEXTURE_2D);
   1.164 +    f.glEnable(GL_BLEND);
   1.165 +    f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.166 +
   1.167 +    f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   1.168 +    f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.169 +
   1.170 +    f.glEnable(GL_POINT_SMOOTH);
   1.171 +    f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
   1.172 +    f.glPointSize(5.0f);
   1.173 +    f.glEnable(GL_FOG);
   1.174 +    f.glFogf(GL_FOG_START, -500);
   1.175 +    f.glFogf(GL_FOG_END, 500);
   1.176 +    f.glFogf(GL_FOG_DENSITY, 0.005);
   1.177 +
   1.178 +    do {
   1.179 +        f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.180 +
   1.181 +        f.glRotatef(2.0, 1.0, 1.0, 1.0);
   1.182 +        f.glRotatef(1.0, 0.0, 1.0, 1.0);
   1.183 +
   1.184 +        f.glColor4ub(255, 255, 255, 255);
   1.185 +		
   1.186 +		f.glEnableClientState(GL_VERTEX_ARRAY);
   1.187 +		f.glVertexPointer(3, GL_FLOAT, 0, pixels);
   1.188 +		f.glDrawArrays(GL_POINTS, 0, NB_PIXELS);
   1.189 +		
   1.190 +        SDL_GL_SwapBuffers();
   1.191 +
   1.192 +        while (SDL_PollEvent(&event)) {
   1.193 +            if (event.type == SDL_KEYDOWN)
   1.194 +                done = 1;
   1.195 +        }
   1.196 +
   1.197 +        SDL_Delay(20);
   1.198 +    }
   1.199 +    while (!done);
   1.200 +
   1.201 +    SDL_Quit();
   1.202 +    return 0;
   1.203 +}
   1.204 +
   1.205 +#else /* HAVE_OPENGLES */
   1.206 +
   1.207 +int
   1.208 +main(int argc, char *argv[])
   1.209 +{
   1.210 +    printf("No OpenGL support on this system\n");
   1.211 +    return 1;
   1.212 +}
   1.213 +
   1.214 +#endif /* HAVE_OPENGLES */