#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 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;
+/* -------------------------------------------------------------------------- */
-/* ------------------------------------------------------------- */
+EGLNativeDisplayType eglX11Display;
+EGLNativeWindowType eglX11Window;
+EGLDisplay eglDisplay = 0;
+EGLConfig eglConfig = 0;
+EGLSurface eglSurface = 0;
+EGLContext eglContext = 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 );
+
+static void destroyEGL();
+static int getX11Display(int windowwidth, int windowheight);
+static int setUpEGL();
+static int useTheProgram();
+static int setUpTnL();
+static int drawStuff(int width, int height);
/* -------------------------------------------------------------------------- */
EXPORT int mid_module_loaded(void)
{
- ni_register_driver("mid", handles_resource, sync_resource);
-
- init_gl();
- make_world();
+ ni_register_driver("mid", handles_resource, sync_resource);
- k_gl_register_reshape(reshape);
- k_gl_register_draw(draw);
- k_gl_register_key(key);
+ init_gl();
- k_log_out("MID Driver initialised");
+ k_gl_register_reshape(reshape);
+ k_gl_register_draw(draw);
+ k_gl_register_key(key);
- testloop();
+ k_log_out("MID Driver initialised");
- return 1;
+ 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;
+ 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;
+ return 0;
}
void sync_resource(ni_resource* res)
void init_gl(void)
{
-}
-
-void make_world(void)
-{
+ if(!getX11Display(WINDOW_WIDTH, WINDOW_HEIGHT)) destroyEGL();
+ if(!setUpEGL()) destroyEGL();
+ if(!useTheProgram()) destroyEGL();
+ if(!setUpTnL()) destroyEGL();
}
void reshape(int width, int height)
void draw(void)
{
+ if(!drawStuff(WINDOW_WIDTH, WINDOW_HEIGHT)) destroyEGL();
}
#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;
+ if(event.xkey.keycode == 113) viewAngle += 0.1;
+ if(event.xkey.keycode == 114) viewAngle -= 0.1;
*/
- default:
- return;
- }
- draw();
+ 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)
+/* need a callback to call this */
+void destroyEGL()
{
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();
return 0;
}
-void setUpEGL(void)
+int setUpEGL()
{
- eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);
+ eglDisplay = eglGetDisplay(eglX11Display);
EGLint iMajorVersion, iMinorVersion;
if(!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion)) {
printf("Error: eglInitialize() failed.\n");
- cleanupAndExit( -1);
+ return 0;
}
EGLint pi32ConfigAttribs[5];
int iConfigs;
if(!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1)) {
printf("Error: eglChooseConfig() failed.\n");
- cleanupAndExit( -1);
+ return 0;
}
- eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);
-
- if(isEGLError("eglCreateWindowSurface")) cleanupAndExit( -1);
+ eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, eglX11Window, NULL);
+ if(isEGLError("eglCreateWindowSurface")) return 0;
eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, pi32ContextAttribs);
-
- if(isEGLError("eglCreateContext")) cleanupAndExit( -1);
+ if(isEGLError("eglCreateContext")) return 0;
eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
+ if(isEGLError("eglMakeCurrent")) return 0;
- if(isEGLError("eglMakeCurrent")) cleanupAndExit( -1);
+ return 1;
}
/* ------------------------------------------------------------- */
float angle=0.0;
float viewAngle = 0.0;
-GLuint useTheProgram()
+int useTheProgram()
{
const char *vsSource = file2string("tnl.vert");
const char *fsSource = file2string("tnl.frag");
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, NORMAL_ARRAY, "vertNormal");
+ /* vertNormal and vertTexCoord don't need to be bound here.. ? */
glBindAttribLocation(program, TEXCOORD_ARRAY, "vertTexCoord");
glLinkProgram(program);
return 1;
}
-void setUpTnL(){
+int setUpTnL()
+{
+ glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, numberOfVertices * stride, trivertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+ return 1;
}
int drawStuff(int width, int height)
/* ------------------------------------------------------------- */
-void testloop()
+#include "X11/Xutil.h"
+
+Window x11Window = 0;
+Display* x11Display = 0;
+long x11Screen = 0;
+XVisualInfo* x11Visual = 0;
+Colormap x11Colormap = 0;
+
+int getX11Display(int windowwidth, int windowheight)
{
- printf("Test OpenGL ES 2.0\n---------------------------\n");
+ x11Display = XOpenDisplay(0);
- getX11Display(WINDOW_WIDTH, WINDOW_HEIGHT);
+ if(!x11Display) {
+ printf("Error: Unable to open X display\n");
+ return 0;
+ }
- setUpEGL();
-
- glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
+ 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");
+ return 0;
+ }
+
+ 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);
- if(!useTheProgram()) cleanupAndExit(-1);
- setUpTnL();
+ eglX11Display = (EGLNativeDisplayType)x11Display;
+ eglX11Window = (EGLNativeWindowType) x11Window;
- int done = 0;
- while(!done){
+ return 1;
+}
- if(!drawStuff(WINDOW_WIDTH, WINDOW_HEIGHT)) cleanupAndExit(-1);
+void cleanupX11()
+{
+ if(x11Window) XDestroyWindow(x11Display, x11Window);
+ if(x11Colormap) XFreeColormap( x11Display, x11Colormap);
+ if(x11Display) XCloseDisplay( x11Display);
+}
+void forKernel()
+{
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);
}
/* ------------------------------------------------------------- */