ref: 5127d1b424617d41c24be5f682950cdf39dade0e
parent: 6a9a327e4154cf1c911baf47939c11bd907c6687
author: Jacob Moody <moody@posixcafe.org>
date: Wed Feb 22 21:32:21 EST 2023
plan9 initial port
--- a/SDL_Examples/gears.c
+++ b/SDL_Examples/gears.c
@@ -10,6 +10,8 @@
#include <stdlib.h>
#include <string.h>
+#include <draw.h>
+
#include "../include/GL/gl.h"
#include "../include/zbuffer.h"
@@ -21,7 +23,6 @@
#else
typedef unsigned char uchar;
#endif
-#include <SDL/SDL.h>
int noSDL = 0;
#ifndef M_PI
#define M_PI 3.14159265
@@ -189,7 +190,7 @@
static GLint gear1, gear2, gear3;
static GLfloat angle = 0.0;
-void draw() {
+void geardraw(void) {
angle += 2.0;
glPushMatrix();
glRotatef(view_rotx, 1.0, 0.0, 0.0);
@@ -217,7 +218,7 @@
glPopMatrix();
}
-void initScene() {
+void initScene(void) {
// static GLfloat pos[4] = {0.408248290463863, 0.408248290463863, 0.816496580927726, 0.0 }; //Light at infinity.
static GLfloat pos[4] = {5, 5, 10, 0.0}; // Light at infinity.
// static GLfloat pos[4] = {5, 5, -10, 0.0}; // Light at infinity.
@@ -307,95 +308,17 @@
larg = argv[i];
}
}
- if (!noSDL) {
-#ifdef PLAY_MUSIC
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
-#else
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
-#endif
- fprintf(stderr, "ERROR: cannot initialize SDL video.\n");
- return 1;
- }
- } else if (SDL_Init(0) < 0)
- fprintf(stderr, "ERROR: cannot initialize SDL without video.\n");
-#ifdef PLAY_MUSIC
- if (!noSDL)
- ainit(0);
-#endif
- SDL_Surface* screen = NULL;
- if (!noSDL)
- if ((screen = SDL_SetVideoMode(winSizeX, winSizeY, TGL_FEATURE_RENDER_BITS, SDL_SWSURFACE)) == 0) {
- fprintf(stderr, "ERROR: Video mode set failed.\n");
- return 1;
- }
- if (!noSDL) {
- printf("\nRMASK IS %u", screen->format->Rmask);
- printf("\nGMASK IS %u", screen->format->Gmask);
- printf("\nBMASK IS %u", screen->format->Bmask);
- printf("\nAMASK IS %u", screen->format->Amask);
+ if (initdraw(nil, nil, "gears") < 0) {
+ sysfatal("%r");
}
-#if TGL_FEATURE_RENDER_BITS == 32
- if (!noSDL)
- if (screen->format->Rmask != 0x00FF0000 || screen->format->Gmask != 0x0000FF00 || screen->format->Bmask != 0x000000FF) {
- needsRGBAFix = 1;
- printf("\nYour screen is using an RGBA output different than this library expects.");
- printf("\nYou should consider using the 16 bit version for optimal performance");
- }
-#endif
- if (!noSDL) {
- printf("\nRSHIFT IS %u", screen->format->Rshift);
- printf("\nGSHIFT IS %u", screen->format->Gshift);
- printf("\nBSHIFT IS %u", screen->format->Bshift);
- printf("\nASHIFT IS %u\n", screen->format->Ashift);
- }
- fflush(stdout);
-#ifdef PLAY_MUSIC
+ winSizeX = Dx(screen->r);
+ winSizeY = Dy(screen->r);
- track* myTrack = NULL;
- if (!noSDL)
- myTrack = lmus("WWGW.mp3");
- if (!noSDL)
- mplay(myTrack, -1, 1000);
-#endif
- if (!noSDL)
- SDL_ShowCursor(SDL_DISABLE);
- if (!noSDL)
- SDL_WM_SetCaption(argv[0], 0);
-
// initialize TinyGL:
// unsigned int pitch;
// int mode;
- if (!noSDL)
- switch (screen->format->BitsPerPixel) {
- case 8:
- fprintf(stderr, "ERROR: Palettes are currently not supported.\n");
- fprintf(stderr, "\nUnsupported by maintainer!!!");
- return 1;
- case 16:
- // pitch = screen->pitch;
- // fprintf(stderr,"\nUnsupported by maintainer!!!");
- // mode = ZB_MODE_5R6G5B;
- // return 1;
- break;
- case 24:
- // pitch = ( screen->pitch * 2 ) / 3;
- fprintf(stderr, "\nUnsupported by maintainer!!!");
- // mode = ZB_MODE_RGB24;
- return 1;
- break;
- case 32:
- // pitch = screen->pitch / 2;
- // mode = ZB_MODE_RGBA;
- break;
- default:
- return 1;
- break;
- }
ZBuffer* frameBuffer = NULL;
- if (TGL_FEATURE_RENDER_BITS == 32)
- frameBuffer = ZB_open(winSizeX, winSizeY, ZB_MODE_RGBA, 0);
- else
- frameBuffer = ZB_open(winSizeX, winSizeY, ZB_MODE_5R6G5B, 0);
+ frameBuffer = ZB_open(winSizeX, winSizeY, ZB_MODE_RGBA, 0);
if (!frameBuffer) {
printf("\nZB_open failed!");
exit(1);
@@ -403,11 +326,11 @@
glInit(frameBuffer);
// Print version info
- printf("\nVersion string:\n%s", glGetString(GL_VERSION));
- printf("\nVendor string:\n%s", glGetString(GL_VENDOR));
- printf("\nRenderer string:\n%s", glGetString(GL_RENDERER));
- printf("\nExtensions string:\n%s", glGetString(GL_EXTENSIONS));
- printf("\nLicense string:\n%s", glGetString(GL_LICENSE));
+ //printf("\nVersion string:\n%s", glGetString(GL_VERSION));
+ //printf("\nVendor string:\n%s", glGetString(GL_VENDOR));
+ //printf("\nRenderer string:\n%s", glGetString(GL_RENDERER));
+ //printf("\nExtensions string:\n%s", glGetString(GL_EXTENSIONS));
+ //printf("\nLicense string:\n%s", glGetString(GL_LICENSE));
// initialize GL:
glClearColor(0.0, 0.0, 0.0, 0.0);
glViewport(0, 0, winSizeX, winSizeY);
@@ -448,7 +371,7 @@
glSetEnableSpecular(GL_FALSE);
// variables for timing:
unsigned int frames = 0;
- unsigned int tNow = SDL_GetTicks();
+ unsigned long long tNow = 0;
unsigned int tLastFps = tNow;
// main loop:
@@ -457,10 +380,11 @@
while (isRunning) {
++frames;
// Depending on SDL to give us ticks even without a window open...
- tNow = SDL_GetTicks();
+ tNow = 0;
// test = TEST_fastInvSqrt(tNow);
// printf("\n%f",test);
// do event handling:
+ /*
if (!noSDL) {
SDL_Event evt;
while (SDL_PollEvent(&evt))
@@ -491,9 +415,10 @@
break;
}
}
+ */
// draw scene:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- draw();
+ geardraw();
if (dotext) {
glDrawText((unsigned char*)"RED text", 0, 0, 0xFF0000);
@@ -505,49 +430,17 @@
0, 48, 0xFF);
}
// swap buffers:
- if (!noSDL)
- if (SDL_MUSTLOCK(screen) && (SDL_LockSurface(screen) < 0)) {
- fprintf(stderr, "SDL ERROR: Can't lock screen: %s\n", SDL_GetError());
- return 1;
- }
- // Quickly convert all pixels to the correct format
-#if TGL_FEATURE_RENDER_BITS == 32
- // for testing!
- if (needsRGBAFix)
- for (int i = 0; i < frameBuffer->xsize * frameBuffer->ysize; i++) {
-#define DATONE (frameBuffer->pbuf[i])
- DATONE = ((DATONE & 0x00FF0000)) << screen->format->Rshift | ((DATONE & 0x0000FF00) >> 8) << screen->format->Gshift |
- ((DATONE & 0x000000FF) >> 16) << screen->format->Bshift;
- }
-#endif
+ loadimage(screen, screen->r, (uchar*)frameBuffer->pbuf, winSizeX*winSizeY*4);
+ /*
if (!noSDL)
- ZB_copyFrameBuffer(frameBuffer, screen->pixels, screen->pitch);
- if (!noSDL)
- if (SDL_MUSTLOCK(screen))
- SDL_UnlockSurface(screen);
- if (!noSDL)
- SDL_Flip(screen);
- if (!noSDL)
- if (fps > 0)
- if ((1000 / fps) > (SDL_GetTicks() - tNow)) {
- SDL_Delay((1000 / fps) - (SDL_GetTicks() - tNow)); // Yay stable framerate!
- }
- // check for error conditions:
- if (!noSDL) {
- char* sdl_error = SDL_GetError();
- if (sdl_error[0] != '\0') {
- fprintf(stderr, "SDL ERROR: \"%s\"\n", sdl_error);
- SDL_ClearError();
- }
- }
- // update fps:
- if (tNow >= tLastFps + 5000) {
- printf("%i frames in %f secs, %f frames per second.\n", frames, (float)(tNow - tLastFps) * 0.001f,
- (float)frames * 1000.0f / (float)(tNow - tLastFps));
- tLastFps = tNow;
- frames = 0;
- }
+ ZB_copyFrameBuffer(frameBuffer, sdl_screen->pixels, sdl_screen->pitch);
+ if(SDL_UpdateTexture(texture, NULL, sdl_screen->pixels, sdl_screen->pitch) < 0)
+ sysfatal("%r\n");
+ if(SDL_RenderCopy(renderer, texture, NULL, NULL) < 0)
+ sysfatal("%r\n");
+ SDL_RenderPresent(renderer);
+ */
}
printf("%i frames in %f secs, %f frames per second.\n", frames, (float)(tNow - tLastFps) * 0.001f, (float)frames * 1000.0f / (float)(tNow - tLastFps));
// cleanup:
@@ -556,17 +449,5 @@
// glDeleteList(gear3);
ZB_close(frameBuffer);
glClose();
- if (!noSDL)
- if (SDL_WasInit(SDL_INIT_VIDEO))
- SDL_QuitSubSystem(SDL_INIT_VIDEO);
-#ifdef PLAY_MUSIC
- if (!noSDL)
- mhalt();
- if (!noSDL)
- Mix_FreeMusic(myTrack);
- if (!noSDL)
- acleanup();
-#endif
- SDL_Quit();
return 0;
}
--- /dev/null
+++ b/SDL_Examples/mkfile
@@ -1,0 +1,10 @@
+</$objtype/mkfile
+
+CFLAGS=-Fpw -I/sys/include/npe -I/sys/include/npe/SDL2 -I ../include -D__plan9__ -D__${objtype}__ -DTGL_FEATURE_32_BITS=1 -DTGL_FEATURE_RENDER_BITS=32
+LIB=../src/libgl.$O.a
+
+OFILES=\
+ gears.$O\
+
+
+</sys/src/cmd/mkone
--- a/include-demo/3dMath.h
+++ b/include-demo/3dMath.h
@@ -252,19 +252,19 @@
return sqrtf(a.d[0] * a.d[0] + a.d[1] * a.d[1] + a.d[2] * a.d[2] + a.d[3] * a.d[3]);
}
static inline vec3 multvec3( vec3 a, vec3 b){
- return (vec3){
- .d[0]=a.d[0]*b.d[0],
- .d[1]=a.d[1]*b.d[1],
- .d[2]=a.d[2]*b.d[2]
- };
+ vec3 out;
+ out.d[0] = a.d[0]*b.d[0];
+ out.d[1] = a.d[1]*b.d[1];
+ out.d[2] = a.d[2]*b.d[2];
+ return out;
}
static inline vec4 multvec4( vec4 a, vec4 b){
- return (vec4){
- .d[0]=a.d[0]*b.d[0],
- .d[1]=a.d[1]*b.d[1],
- .d[2]=a.d[2]*b.d[2],
- .d[3]=a.d[3]*b.d[3]
- };
+ vec4 out;
+ out.d[0] = a.d[0]*b.d[0];
+ out.d[1] = a.d[1]*b.d[1];
+ out.d[2] = a.d[2]*b.d[2];
+ out.d[3] = a.d[3]*b.d[3];
+ return out;
}
static inline vec3 clampvec3( vec3 a, vec3 min, vec3 max){
vec3 ret;
@@ -288,12 +288,12 @@
return a.d[0] * b.d[0] + a.d[1] * b.d[1] + a.d[2] * b.d[2] + a.d[3] * b.d[3];
}
static inline vec4 getrow( mat4 a, uint index){
- return (vec4){
- .d[0]=a.d[index],
- .d[1]=a.d[4+index],
- .d[2]=a.d[8+index],
- .d[3]=a.d[12+index]
- };
+ vec4 ret;
+ ret.d[0] = a.d[index];
+ ret.d[1] = a.d[4+index];
+ ret.d[2] = a.d[8+index];
+ ret.d[3] = a.d[12+index];
+ return ret;
}
static inline mat4 swapRowColumnMajor( mat4 in){
mat4 result;
@@ -311,12 +311,12 @@
}
static inline vec4 getcol( mat4 a, uint index){
- return (vec4){
- .d[0]=a.d[index*4],
- .d[1]=a.d[index*4+1],
- .d[2]=a.d[index*4+2],
- .d[3]=a.d[index*4+3]
- };
+ vec4 ret;
+ ret.d[0] = a.d[index*4];
+ ret.d[1] = a.d[index*4+1];
+ ret.d[2] = a.d[index*4+2];
+ ret.d[3] = a.d[index*4+3];
+ return ret;
}
static inline mat4 multm4( mat4 a, mat4 b){
mat4 ret;
@@ -366,11 +366,24 @@
static inline vec4 scalev4( f_ s, vec4 i){i.d[0] *= s; i.d[1] *= s; i.d[2] *= s;i.d[3] *= s; return i;}
static inline vec3 normalizev3( vec3 a){
- if(lengthv3(a)==0) return (vec3){.d[0]=0.0,.d[1]=0.0,.d[2]=1.0};
+ vec3 ret;
+ if(lengthv3(a)==0){
+ ret.d[0]=0.0;
+ ret.d[1]=0.0;
+ ret.d[2]=1.0;
+ return ret;
+ }
return scalev3(1.0/lengthv3(a), a);
}
static inline vec4 normalizev4( vec4 a){
- if(lengthv4(a)==0) return (vec4){.d[0]=0.0,.d[1]=0.0,.d[2]=1.0,.d[3]=0.0};
+ vec4 ret;
+ if(lengthv4(a)==0){
+ ret.d[0]=0.0;
+ ret.d[1]=0.0;
+ ret.d[2]=1.0;
+ ret.d[2]=0.0;
+ return ret;
+ }
return scalev4(1.0/lengthv4(a), a);
}
static inline vec3 addv3( vec3 aa, vec3 b){
@@ -390,10 +403,13 @@
static inline vec3 subv3( vec3 a, vec3 b){
return addv3(a,scalev3(-1,b));
}
-static inline mat4 identitymat4(){
- return scalemat4(
- (vec4){.d[0]=1.0,.d[1]=1.0,.d[2]=1.0,.d[3]=1.0}
- );
+static inline mat4 identitymat4(void){
+ vec4 v;
+ v.d[0]=1.0;
+ v.d[1]=1.0;
+ v.d[2]=1.0;
+ v.d[3]=1.0;
+ return scalemat4(v);
}
static inline mat4 translate( vec3 t){
mat4 tm = identitymat4();
@@ -414,19 +430,19 @@
);
}
static inline vec4 upv3( vec3 in, f_ w){
- return (vec4){
- .d[0]=in.d[0],
- .d[1]=in.d[1],
- .d[2]=in.d[2],
- .d[3]=w
- };
+ vec4 ret;
+ ret.d[0]=in.d[0];
+ ret.d[1]=in.d[1];
+ ret.d[2]=in.d[2];
+ ret.d[3]=w;
+ return ret;
}
static inline vec3 downv4( vec4 in){
- return (vec3){
- .d[0]=in.d[0],
- .d[1]=in.d[1],
- .d[2]=in.d[2]
- };
+ vec3 ret;
+ ret.d[0]=in.d[0];
+ ret.d[1]=in.d[1];
+ ret.d[2]=in.d[2];
+ return ret;
}
static inline mat4 lookAt( vec3 eye, vec3 at, vec3 up){
mat4 cw = identitymat4();
@@ -498,12 +514,11 @@
return 1;
}
static inline vec4 boxvbox( aabb b1, aabb b2){ /*Just points along the minimum separating axis, Nothing fancy.*/
- vec4 ret = (vec4){
- .d[0]=0,
- .d[1]=0,
- .d[2]=0,
- .d[3]=0
- };
+ vec4 ret;
+ ret.d[0]=0;
+ ret.d[1]=0;
+ ret.d[2]=0;
+ ret.d[3]=0;
vec3 sumextents = addv3(b1.e,b2.e);
vec3 b1c = downv4(b1.c);
vec3 b2c = downv4(b2.c);
@@ -530,12 +545,10 @@
ret.d[0] = axispen[0].d[0];
for(int i = 1; i < 6; i++){
if(fabs(axispen[i/3].d[i%3]) < fabs(ret.d[3])){
- ret = (vec4){
- .d[0]=0,
- .d[1]=0,
- .d[2]=0,
- .d[3]=(axispen[i/3].d[i%3])
- };
+ ret.d[0]=0;
+ ret.d[1]=0;
+ ret.d[2]=0;
+ ret.d[3]=(axispen[i/3].d[i%3]);
ret.d[i%3] = ret.d[3];
ret.d[3] = fabs(ret.d[3]);
}
@@ -570,14 +583,13 @@
return boxvbox(virt,box);
}
}
- else
- return (vec4){
- .d[0]=0,
- .d[1]=0,
- .d[2]=0,
- .d[3]=0
- };
-
+ else {
+ ret.d[0]=0;
+ ret.d[1]=0;
+ ret.d[2]=0;
+ ret.d[3]=0;
+ return ret;
+ }
}
--- /dev/null
+++ b/src/mkfile
@@ -1,0 +1,34 @@
+</$objtype/mkfile
+
+CFLAGS=-Fpw -I/sys/include/npe -I/sys/include/npe/SDL2 -I ../include -D__plan9__ -D__${objtype}__ -DTGL_FEATURE_32_BITS -DTGL_FEATURE_RENDER_BITS=32
+LIB=libgl.$O.a
+CLEANFILES=libgl.$O.a
+
+OFILES=api.$O\
+ list.$O\
+ vertex.$O\
+ init.$O\
+ matrix.$O\
+ texture.$O\
+ misc.$O\
+ clear.$O\
+ light.$O\
+ clip.$O\
+ select.$O\
+ get.$O\
+ zbuffer.$O\
+ zline.$O\
+ ztriangle.$O\
+ zmath.$O\
+ image_util.$O\
+ msghandling.$O\
+ arrays.$O\
+ specbuf.$O\
+ memory.$O\
+ ztext.$O\
+ zraster.$O\
+ accum.$O\
+ zpostprocess.$O\
+
+
+</sys/src/cmd/mksyslib
--- a/src/zmath.c
+++ b/src/zmath.c
@@ -21,23 +21,25 @@
else
a->m[i][j] = 0.0;
*/
- const M4 c = (M4){{
- {1, 0, 0, 0},
- {0, 1, 0, 0},
- {0, 0, 1, 0},
- {0, 0, 0, 1},
- }};
- *a = c;
+ const GLfloat m[4][4] = {
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1,
+ };
+ memmove(a->m, m, sizeof m);
}
GLint gl_M4_IsId(M4* a) {
-
- const M4 c = (M4){{
- {1, 0, 0, 0},
- {0, 1, 0, 0},
- {0, 0, 1, 0},
- {0, 0, 0, 1},
- }};
+
+ const GLfloat m[4][4] = {
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1,
+ };
+ M4 c;
+ memmove(c.m, m, sizeof m);
return (memcmp(a->m, c.m, 16 * sizeof(GLfloat)) == 0);
/*
for (i = 0; i < 4; i++)