--- /dev/null
+
+
+/* -------------------------------------------------------------------------- */
+
+#include <kernelapi.h>
+#include <ni.h>
+
+#include <math.h>
+
+/* ------------------------------------------------------------- */
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "X11/Xutil.h"
+
+#include <EGL/egl.h>
+#include <GLES2/gl2.h>
+
+#define WINDOW_WIDTH 640
+#define WINDOW_HEIGHT 480
+
+#define POS_ARRAY 0
+#define NORMAL_ARRAY 1
+#define TEXCOORD_ARRAY 2
+
+#define TEX_SIZE 128
+
+/* ------------------------------------------------------------- */
+
+Window x11Window = 0;
+Display* x11Display = 0;
+long x11Screen = 0;
+XVisualInfo* x11Visual = 0;
+Colormap x11Colormap = 0;
+
+EGLDisplay eglDisplay = 0;
+EGLConfig eglConfig = 0;
+EGLSurface eglSurface = 0;
+EGLContext eglContext = 0;
+
+/* ------------------------------------------------------------- */
+
+/* -------------------------------------------------------------------------- */
+
+#define GLfloat float
+static GLfloat xco= 0;
+static GLfloat yco= 1;
+static GLfloat zco= -35;
+static GLfloat view_rotx=0.0, view_roty=0.0, view_rotz=0.0;
+static int shift=0;
+
+/* -------------------------------------------------------------------------- */
+
+static int handles_resource(char* name);
+static void sync_resource(ni_resource* res);
+static void init_gl(void);
+static void make_world(void);
+static void reshape(int width, int height);
+static void draw(void);
+static void key(unsigned char k, int down);
+static void plane(void* n);
+static void double_cube(void* s);
+static void cube(GLfloat size, int outside);
+static int set_object(char* n,
+ GLfloat x, GLfloat y, GLfloat z, GLfloat angle,
+ void (*listFn)(void*),
+ void* listFnArgs );
+
+/* -------------------------------------------------------------------------- */
+
+EXPORT int mid_module_loaded(void)
+{
+ ni_register_driver("mid", handles_resource, sync_resource);
+
+ init_gl();
+ make_world();
+
+ k_gl_register_reshape(reshape);
+ k_gl_register_draw(draw);
+ k_gl_register_key(key);
+
+ k_log_out("MID Driver initialised");
+
+ testloop();
+
+ return 1;
+}
+
+EXPORT int mid_module_event(void* data)
+{
+ k_log_out("MID got event: %p", data);
+ ni_event* evt=data;
+ ni_event_delete(evt);
+ return 1;
+}
+
+/* -------------------------------------------------------------------------- */
+
+int handles_resource(char* name)
+{
+ return 0;
+}
+
+void sync_resource(ni_resource* res)
+{
+}
+
+/* -------------------------------------------------------------------------- */
+
+void init_gl(void)
+{
+}
+
+void make_world(void)
+{
+}
+
+void reshape(int width, int height)
+{
+}
+
+void draw(void)
+{
+}
+
+#define SHIFT 0
+void key(unsigned char k, int down)
+{
+ if(k==SHIFT && down){ shift=1; return; }
+ if(k==SHIFT && !down){ shift=0; return; }
+ if(!down) return;
+
+ if(shift) k-=('a'-'A');
+
+ float speed=0.25;
+ switch (k) {
+ case 'H':
+ xco-=speed*(float)sin((view_roty-90)*3.14/180);
+ zco+=speed*(float)cos((view_roty-90)*3.14/180);
+ if(xco< -35) xco= -35;
+ if(xco> 35) xco= 35;
+ if(zco< -35) zco= -35;
+ if(zco> 35) zco= 35;
+ break;
+ case 'L':
+ xco+=speed*(float)sin((view_roty-90)*3.14/180);
+ zco-=speed*(float)cos((view_roty-90)*3.14/180);
+ if(xco< -35) xco= -35;
+ if(xco> 35) xco= 35;
+ if(zco< -35) zco= -35;
+ if(zco> 35) zco= 35;
+ break;
+ case 'i':
+ xco-=speed*(float)sin(view_roty*3.14/180);
+ zco+=speed*(float)cos(view_roty*3.14/180);
+ if(xco< -35) xco= -35;
+ if(xco> 35) xco= 35;
+ if(zco< -35) zco= -35;
+ if(zco> 35) zco= 35;
+ break;
+ case 'o':
+ xco+=speed*(float)sin(view_roty*3.14/180);
+ zco-=speed*(float)cos(view_roty*3.14/180);
+ if(xco< -35) xco= -35;
+ if(xco> 35) xco= 35;
+ if(zco< -35) zco= -35;
+ if(zco> 35) zco= 35;
+ break;
+ case 'j':
+ yco-=speed;
+ if(yco< 0.2) yco= 0.2;
+ break;
+ case 'k':
+ yco+=speed;
+ break;
+ case 'l':
+ view_roty += speed*20;
+ break;
+ case 'h':
+ view_roty -= speed*20;
+ break;
+/*
+ case 'J':
+ view_rotx += 2.0;
+ break;
+ case 'K':
+ view_rotx -= 2.0;
+ break;
+ case 'z':
+ view_rotz += 2.0;
+ break;
+ case 'Z':
+ view_rotz -= 2.0;
+ break;
+*/
+ default:
+ return;
+ }
+ draw();
+}
+
+/* -------------------------------------------------------------------------- */
+/* ------------------------------------------------------------- */
+
+void cleanupAndExit(int code)
+{
+ eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+ eglTerminate(eglDisplay);
+
+ if(x11Window) XDestroyWindow(x11Display, x11Window);
+ if(x11Colormap) XFreeColormap( x11Display, x11Colormap);
+ if(x11Display) XCloseDisplay( x11Display);
+
+ exit(code);
+}
+
+/* ------------------------------------------------------------- */
+
+void getX11Display(int windowwidth, int windowheight)
+{
+ x11Display = XOpenDisplay(0);
+
+ if(!x11Display) {
+ printf("Error: Unable to open X display\n");
+ cleanupAndExit(-1);
+ }
+
+ x11Screen = XDefaultScreen(x11Display);
+ Window rootWindow = RootWindow(x11Display, x11Screen);
+ int depth = DefaultDepth(x11Display, x11Screen);
+ x11Visual = malloc(sizeof(XVisualInfo));
+ XMatchVisualInfo(x11Display, x11Screen, depth, TrueColor, x11Visual);
+
+ if(!x11Visual) {
+ printf("Error: Unable to acquire visual\n");
+ cleanupAndExit(-1);
+ }
+
+ x11Colormap = XCreateColormap(x11Display, rootWindow, x11Visual->visual, AllocNone);
+ XSetWindowAttributes XSWA;
+ XSWA.colormap = x11Colormap;
+ XSWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
+ unsigned int cwmask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
+
+ x11Window = XCreateWindow(x11Display, RootWindow(x11Display, x11Screen), 0, 0, windowwidth, windowheight,
+ 0, CopyFromParent, InputOutput, CopyFromParent, cwmask, &XSWA);
+ XMapWindow(x11Display, x11Window);
+ XFlush(x11Display);
+}
+
+int isEGLError(char* where)
+{
+ EGLint err = eglGetError();
+ if(err != EGL_SUCCESS) {
+ printf("EGL failed at %s (%d).\n", where, err);
+ return 1;
+ }
+ return 0;
+}
+
+void setUpEGL(void)
+{
+ eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);
+
+ EGLint iMajorVersion, iMinorVersion;
+ if(!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion)) {
+ printf("Error: eglInitialize() failed.\n");
+ cleanupAndExit( -1);
+ }
+
+ EGLint pi32ConfigAttribs[5];
+ pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
+ pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
+ pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
+ pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
+ pi32ConfigAttribs[4] = EGL_NONE;
+
+ EGLint pi32ContextAttribs[3];
+ pi32ContextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
+ pi32ContextAttribs[1] = 2;
+ pi32ContextAttribs[2] = EGL_NONE;
+
+ int iConfigs;
+ if(!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1)) {
+ printf("Error: eglChooseConfig() failed.\n");
+ cleanupAndExit( -1);
+ }
+
+ eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);
+
+ if(isEGLError("eglCreateWindowSurface")) cleanupAndExit( -1);
+
+ eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, pi32ContextAttribs);
+
+ if(isEGLError("eglCreateContext")) cleanupAndExit( -1);
+
+ eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
+
+ if(isEGLError("eglMakeCurrent")) cleanupAndExit( -1);
+}
+
+/* ------------------------------------------------------------- */
+
+GLuint isShaderError(GLuint thing)
+{
+ GLint isShader = glIsShader(thing);
+
+ GLint ok;
+ if(isShader){
+ glGetShaderiv(thing, GL_COMPILE_STATUS, &ok);
+ }else{
+ glGetProgramiv(thing, GL_LINK_STATUS, &ok);
+ }
+ if(ok) return 0;
+
+ GLint infoLen=0;
+ if(isShader){
+ glGetShaderiv(thing, GL_INFO_LOG_LENGTH, &infoLen);
+ }else{
+ glGetProgramiv(thing, GL_INFO_LOG_LENGTH, &infoLen);
+ }
+ if(infoLen){
+ char* infoLog = malloc(sizeof(char)*infoLen);
+ if(isShader){
+ glGetShaderInfoLog(thing, infoLen, NULL, infoLog);
+ }else{
+ glGetProgramInfoLog(thing, infoLen, NULL, infoLog);
+ }
+ printf("%s: %s\n", isShader? "Shader compile": "Program link", infoLog);
+ free(infoLog);
+ }
+ return 1;
+}
+
+char* file2string(const char *path)
+{
+ FILE *fd;
+ long len, r;
+ char *str;
+
+ if(!(fd=fopen(path, "r"))) {
+ fprintf(stderr, "Can't open file '%s' for reading\n", path);
+ return NULL;
+ }
+
+ fseek(fd, 0, SEEK_END);
+ len = ftell(fd);
+ fseek(fd, 0, SEEK_SET);
+
+ if(!(str=malloc(len*sizeof(char)))) {
+ fprintf(stderr, "Can't malloc space for '%s'\n", path);
+ return NULL;
+ }
+
+ r = fread(str, sizeof(char), len, fd);
+
+ str[r-1] = '\0';
+
+ fclose(fd);
+
+ return str;
+}
+
+/* ------------------------------------------------------------- */
+
+GLuint program;
+GLuint texture;
+GLuint vbo;
+unsigned int numberOfVertices;
+unsigned int posStep;
+unsigned int normStep;
+unsigned int tcStep;
+unsigned int stride;
+float angle=0.0;
+float viewAngle = 0.0;
+
+GLuint useTheProgram()
+{
+ const char *vsSource = file2string("tnl.vert");
+ const char *fsSource = file2string("tnl.frag");
+
+ GLuint vs = glCreateShader(GL_VERTEX_SHADER);
+ glShaderSource(vs, 1, &vsSource, NULL);
+ glCompileShader(vs);
+ if(isShaderError(vs)) return 0;
+
+ GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
+ glShaderSource(fs, 1, &fsSource, NULL);
+ glCompileShader(fs);
+ if(isShaderError(fs)) return 0;
+
+ free((void*)vsSource);
+ free((void*)fsSource);
+
+ program = glCreateProgram();
+ glAttachShader(program, vs);
+ glAttachShader(program, fs);
+
+ glBindAttribLocation(program, POS_ARRAY, "vertPos");
+ glBindAttribLocation(program, NORMAL_ARRAY, "vertNormal"); /* vertNormal and vertTexCoord don't need to be bound here.. ? */
+ glBindAttribLocation(program, TEXCOORD_ARRAY, "vertTexCoord");
+
+ glLinkProgram(program);
+ if(isShaderError(program)) return 0;
+
+ glUseProgram(program);
+
+ glUniform3f(glGetUniformLocation(program, "frameLightDirection"), 1.0, 0.0, 1.0);
+ glUniform1i(glGetUniformLocation(program, "texture"), 0); /* 0 ?? */
+
+ return 1;
+}
+
+void setUpTnL(){
+
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+
+ GLuint* td = malloc(sizeof(GLuint)*TEX_SIZE*TEX_SIZE);
+ int i,j;
+ for(i=0; i<TEX_SIZE; i++)
+ for(j=0; j<TEX_SIZE; j++) {
+ GLuint col = (255L<<24) + ((255L-j*2)<<16) + ((255L-i)<<8) + (255L-i*2);
+ if ( ((i*j)/8) % 2 ) col = (GLuint) (255L<<24) + (255L<<16) + (0L<<8) + (255L);
+ td[j*TEX_SIZE+i] = col;
+ }
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, td);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+ free(td);
+
+ GLfloat trivertices[] = {
+ -0.4f,-0.4f,0.4f, // Pos
+ 0.0f,0.0f,1.0f, // Normal
+ 0.0f,0.0f, // TexCoord
+
+ 0.4f,-0.0f,0.0f, // Pos
+ 0.0f,0.0f,1.0f, // Normal
+ 1.0f,0.0f, // TexCoord
+
+ 0.0f,0.4f,0.4f, // Pos
+ 0.5f,0.5f,0.5f, // Normal
+ 0.5f,1.0f, // TexCoord
+
+ 0.4f,0.0f,0.4f, // Pos
+ 0.5f,0.5f,0.5f, // Normal
+ 0.5f,0.0f, // TexCoord
+
+ 0.4f,0.4f,0.4f, // Pos
+ 0.0f,0.0f,1.0f, // Normal
+ 0.0f,0.0f, // TexCoord
+
+ };
+
+ numberOfVertices = 4;
+ posStep = 3 * sizeof(GLfloat);
+ normStep = 3 * sizeof(GLfloat);
+ tcStep = 2 * sizeof(GLfloat);
+ stride = posStep + normStep + tcStep;
+
+ glGenBuffers(1, &vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferData(GL_ARRAY_BUFFER, numberOfVertices * stride, trivertices, GL_STATIC_DRAW);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+}
+
+int drawStuff(int width, int height)
+{
+ glViewport(0, 0, width, height);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ float TransRotScaleVerticeMatrix[] = {
+ cos(angle), 0, sin(angle), 0,
+ 0, 1, 0, 0,
+ -sin(angle), 0, cos(angle), 0,
+ 0, 0, 0, 1
+ };
+
+ float ModelViewProjectionMatrix[] = {
+ cos(viewAngle), 0, sin(viewAngle), 0,
+ 0, 1, 0, 0,
+ -sin(viewAngle), 0, cos(viewAngle), 0,
+ 0, 0, 0, 1
+ };
+
+ float TransRotScaleNormalMatrix[] = {
+ cos(angle), 0, sin(angle),
+ 0, 1, 0,
+ -sin(angle), 0, cos(angle)
+ };
+
+ glUniformMatrix4fv(glGetUniformLocation(program, "frameTRSV"), 1, GL_FALSE, TransRotScaleVerticeMatrix);
+ glUniformMatrix4fv(glGetUniformLocation(program, "frameMVP"), 1, GL_FALSE, ModelViewProjectionMatrix);
+ glUniformMatrix3fv(glGetUniformLocation(program, "frameTRSN"), 1, GL_FALSE, TransRotScaleNormalMatrix);
+
+
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+
+ glEnableVertexAttribArray(POS_ARRAY);
+ glVertexAttribPointer(POS_ARRAY, 3, GL_FLOAT, GL_FALSE, stride, 0);
+
+ glEnableVertexAttribArray(NORMAL_ARRAY);
+ glVertexAttribPointer(NORMAL_ARRAY, 3, GL_FLOAT, GL_FALSE, stride, (void*)posStep );
+
+ glEnableVertexAttribArray(TEXCOORD_ARRAY);
+ glVertexAttribPointer(TEXCOORD_ARRAY, 2, GL_FLOAT, GL_FALSE, stride, (void*)(posStep + normStep) );
+
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, numberOfVertices);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+ eglSwapBuffers(eglDisplay, eglSurface);
+
+ angle += .001f;
+
+ return !isEGLError("drawStuff");
+}
+
+void deleteStuff(){
+ glDeleteTextures(1, &texture);
+ glDeleteBuffers(1, &vbo);
+ glDeleteProgram(program);
+/* glDeleteShader();
+ glDeleteShader();*/
+}
+
+/* ------------------------------------------------------------- */
+
+void testloop()
+{
+ printf("Test OpenGL ES 2.0\n---------------------------\n");
+
+ getX11Display(WINDOW_WIDTH, WINDOW_HEIGHT);
+
+ setUpEGL();
+
+ glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
+
+ if(!useTheProgram()) cleanupAndExit(-1);
+ setUpTnL();
+
+ int done = 0;
+ while(!done){
+
+ if(!drawStuff(WINDOW_WIDTH, WINDOW_HEIGHT)) cleanupAndExit(-1);
+
+ int nm = XPending(x11Display);
+ int m;
+ for(m=0; m< nm; m++) {
+
+ XEvent event;
+ XNextEvent(x11Display, &event);
+ switch(event.type){
+ case ButtonPress:
+ done = 1;
+ break;
+ case KeyPress:
+ if(event.xkey.keycode == 113) viewAngle += 0.1;
+ if(event.xkey.keycode == 114) viewAngle -= 0.1;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ cleanupAndExit(0);
+}
+
+/* ------------------------------------------------------------- */
+
+
+