Browse Source

Port to SDL2

master
JD Cantrell 3 years ago
parent
commit
ed6604940d
4 changed files with 333 additions and 1 deletions
  1. 1
    0
      .gitignore
  2. 8
    0
      Makefile
  3. 11
    1
      README.md
  4. 313
    0
      src/bsplines.c

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
bsplines

+ 8
- 0
Makefile View File

@@ -0,0 +1,8 @@
SDL_CFLAGS := $(shell sdl2-config --cflags)
SDL_LDFLAGS := $(shell sdl2-config --libs)
GL_LDFLAGS := "-lGL"

all: bsplines

bsplines: src/bsplines.c
gcc $(SDL_CFLAGS) $(SDL_LDFLAGS) $(GL_LDFLAGS) src/bsplines.c -o bsplines

+ 11
- 1
README.md View File

@@ -1,3 +1,13 @@
This was my first attempt and contributing to any open source software.
I made a graphics assignment into an XscreenSave hack. Unfortunately, it
I made a graphics assignment into an XscreenSaver hack. Unfortunately, it
was not accepted but I still liked it quite a bit.

Dependencies:
* SDL2-devel

Build:

```
make
./bsplines
```

+ 313
- 0
src/bsplines.c View File

@@ -0,0 +1,313 @@
#include <stdio.h>
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

#define randomNumber while ((randomNum = (random()/(float)RAND_MAX))<=.0001) {}

struct lineList {
float *x,*y,*z,*dx,*dy,*dz;
float r1,g1,b1;
float rinc,binc,ginc;
int numberOfPoints;
struct lineList* previous;
struct lineList* next;
};

int width, height;
struct lineList* head = NULL;
struct lineList* tail = NULL;

/*blending poly tables*/
float* poly1;
float* poly2;
float* poly3;
float* poly4;
float texOffset;
GLuint texture;

int steps = 30;
int size = 5;
int speed = 10;

/*************************************************************/
/* adds a new b-spline loop
* first 6 parameters are rgb values for colors
* last parameter is the number of control points for b-spline drawing
*/
void addLoop(
float nr1,float ng1,float nb1,float nr2,float ng2,float nb2,int points) {
int t=0,a=0;
float temp[points+5];
struct lineList* new;
new = malloc(sizeof(struct lineList));

/*we add 5 extra points to redraw the fist 4 to get a nice smooth loop*/
float *newx = malloc(sizeof(temp));
float *newy = malloc(sizeof(temp));
float *newz = malloc(sizeof(temp));
float *newdx = malloc(sizeof(temp));
float *newdy = malloc(sizeof(temp));
float *newdz = malloc(sizeof(temp));

/* add an array of new points */
for (t=0;t<points;t++) {
float randomNum=0;
randomNumber/* macro defined above*/
*(newx+t)=randomNum*2-1;
randomNumber
*(newy+t)=randomNum*2-1;
randomNumber
*(newz+t)=randomNum*1;
randomNumber
*(newdx+t)= (randomNum*2-1)/(10*speed);
randomNumber
*(newdy+t)= (randomNum*2-1)/(10*speed);
randomNumber
*(newdz+t)= (randomNum*2-1)/(10*speed);
}
/*add the last few points to make a loop */

for (t=t;t<points+5;t++) {
*(newx+t)=*(newx+a);
*(newy+t)=*(newy+a);
*(newz+t)=*(newz+a);
*(newdx+t)= *(newdx+a);
*(newdy+t)= *(newdy+a);
*(newdz+t)= *(newdz+a);
a++;
}

/*add to the linelist struct*/
new->x=newx;
new->y=newy;
new->z=newz;
new->dx=newdx;
new->dy=newdy;
new->dz=newdz;

/* set colors */
new->r1=nr1;
new->g1=ng1;
new->b1=nb1;

/*calculate color blending increments */
new->rinc = (nr2-nr1)/(.5*(points)*steps);
new->ginc = (ng2-ng1)/(.5*(points)*steps);
new->binc = (nb2-nb1)/(.5*(points)*steps);

new->numberOfPoints=points+4;

/*set links */
new->previous=tail;
new->next=NULL;
if (head==NULL) {head=new;}
if (tail!=NULL) {tail->next=new;}
tail=new;
}

void init(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glFrustum(-1.0,1.0,-1.0,1.0,-5.0,10.0);
glMatrixMode(GL_MODELVIEW);
glClearColor (0.0, 0.0, 0.0, 0.0);
int curves = 4;

/*enable smoothing */
if (1) {
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
}

/*set sizes*/
glLineWidth(size);
glPointSize(size);

/*init blending poly tables*/

poly1 = malloc(sizeof(float) * (steps + 5));
poly2 = malloc(sizeof(float) * (steps + 5));
poly3 = malloc(sizeof(float) * (steps + 5));
poly4 = malloc(sizeof(float) * (steps + 5));

/*these are our blending polynomials. they make a neat graph where at any
*given point all four add up to one */
int p = 0;
for (float s=0; s <= 1 + 3 * (1.0 / steps); s+= 1.0 / steps) {
*(poly1+p)=(1.0/6)*((1-s)*(1-s)*(1-s));
*(poly2+p)=(1.0/6)*((s+2)*(1-s)*(1-s)+(s+1)*(1-s)*(2-s)+s*(2-s)*(2-s));
*(poly3+p)=(1.0/6)*((s+1)*(s+1)*(1-s)+s*(s+1)*(2-s)+s*s*(3-s));
*(poly4+p)=(1.0/6)*s*s*s;
p++;
}
// if (dotexture) {
// texOffset = 13.0/width;
// glGenTextures(1, &texture);
// LoadTexture(mi, particle_xpm);
// }
/*add lines - dirty but it works*/
int controlPoints=10;
addLoop(0.0,1.0,1.0,0.0,0.0,1.0,controlPoints);
if (curves>=2) {addLoop(1.0,1.0,0.0,1.0,0.0,0.0,controlPoints);}
if (curves>=3) {addLoop(1.0,1.0,1.0,0.0,1.0,0.0,controlPoints);}
if (curves>=4) {addLoop(1.0,1.0,1.0,0.45,0.0,0.45,controlPoints);}
}



/*************************************************************/
void draw() {
float r, g, b, py, px, pz, rinc, binc, ginc, px1=0.0,py1=0.0,pz1=0.0;
float *x, *y, *dx, *dy,*z,*dz;
int numPoint=0, points;
struct lineList* currentLine;
int t,s;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/*cycle through each curve*/
for(currentLine=head;currentLine!=NULL;currentLine=currentLine->next) {
numPoint=0;/*current point number*/
px1=-1;/* first point unset*/

x=currentLine->x;
y=currentLine->y;
z=currentLine->z;
dx=currentLine->dx;
dy=currentLine->dy;
dz=currentLine->dz;
rinc = currentLine->rinc;
ginc = currentLine->ginc;
binc = currentLine->binc;
r=currentLine->r1;
g=currentLine->g1;
b=currentLine->b1;
points = currentLine->numberOfPoints;

/*begin drawing curve*/

//if (!dotexture) {
if (1) {glBegin(GL_LINE_STRIP);}
else {glBegin(GL_POINTS);}
//}
for (t = 0; t < points - 4;t++) {
for (s = 0; s < steps; s++) {

/*find x,y,z pointer math for no reason*/
px = ((*x)*(*(poly1+s)) + *(x+1)*(*(poly2+s)) + *(x+2)*(*(poly3+s))
+ *(x+3)*(*(poly4+s)));
py = ((*y)*(*(poly1+s)) + *(y+1)*(*(poly2+s)) + *(y+2)*(*(poly3+s))
+ *(y+3)*(*(poly4+s)));
pz = ((*z)*(*(poly1+s)) + *(z+1)*(*(poly2+s)) + *(z+2)*(*(poly3+s))
+ *(z+3)*(*(poly4+s)));

/*inc colors*/
if (((float)numPoint/points)<.3) {r+=rinc;b+=binc;g+=ginc;}
else {r-=rinc;g-=ginc;b-=binc;}

/*glColor3f(r,g,b);*/
glColor4f(r,g,b,(pz+1)/2);
if (px1==-1) {px1=px;py1=py;pz1=pz;}

// if (dotexture) {
// glBegin(GL_TRIANGLE_STRIP);
// glTexCoord2d(1, 1);
// glVertex3f(px + texOffset, py + texOffset, pz);
// glTexCoord2d(0, 1);
// glVertex3f(px - texOffset, py + texOffset, pz);
// glTexCoord2d(1, 0);
// glVertex3f(px + texOffset, py - texOffset, pz);
// glTexCoord2d(0, 0);
// glVertex3f(px - texOffset, py - texOffset, pz);
// glEnd();
// }
// else
// {
glVertex3f(px,py,pz);
// }


}

/*update points */
*x+=*dx,*y+=*dy,*z+=*dz;
if (*x>1||*x<-1) *dx=-*dx;
if (*y>1||*y<-1) *dy=-*dy;
if (*z>1||*z<-1) *dz=-*dz;
numPoint++,x++,dx++,y++,z++,dz++,dy++;
}
// if (!dotexture) {
glVertex3f(px1,py1,pz1);
glEnd();
// }

/*update the last few points */
for (t=0; t<4;t++) {
*x+=*dx,*y+=*dy,*z+=*dz;
if (*x>1||*x<-1) *dx=-*dx;
if (*y>1||*y<-1) *dy=-*dy;
if (*z>1||*z<-1) *dz=-*dz;
x++,dx++,y++,dy++,z++,dz++;
}
}
//glFinish();
}

// With help from https://github.com/icebreaker/zeecraft/blob/master/src/main.c
// And https://github.com/Twinklebear/TwinklebearDev-Lessons
int main(){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
SDL_Log("SDL_Init Error: %s\n", SDL_GetError());
return 1;
}

SDL_Window *window = SDL_CreateWindow("bsplines", 100, 100, 1280, 960, SDL_WINDOW_OPENGL);
if (window == NULL){
SDL_Log("SDL_CreateWindow Error: %s\n", SDL_GetError());
return 1;
}

SDL_GLContext context = SDL_GL_CreateContext(window);
if (context == 0){
SDL_Log("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}

SDL_GL_SetSwapInterval(1);
SDL_Log("GL_VENDER: %s", glGetString(GL_RENDERER));

init(1280, 960);
int running = 1;
unsigned int lastTime = 0;
while (running) {
SDL_Event event;
unsigned int now = SDL_GetTicks(), dt = now - lastTime;
lastTime = now;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
break;
}
}

//update curves
draw();
SDL_GL_SwapWindow(window);
}


//Clean up our objects and quit
SDL_DestroyWindow(window);
SDL_Quit();

return 0;
}



Loading…
Cancel
Save