summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2007-10-16 22:40:46 +0000
committerDimitri Sokolyuk <demon@dim13.org>2007-10-16 22:40:46 +0000
commit2632e257f7b30e93dd6a410f73cdfd8c7c0c5801 (patch)
treec58e9a080e3197304d5acbba1b90d245c0f3c27e
parent78a546af1e00b4051066f01ad9e6cffbaf8cd7de (diff)
Stern X11 version
-rw-r--r--Makefile4
-rw-r--r--stern.c247
2 files changed, 120 insertions, 131 deletions
diff --git a/Makefile b/Makefile
index 1040c07..cabb4ae 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,9 @@
PROG= stern
-#PROG= simple-drawing
NOMAN=
COPTS+= -I/usr/X11R6/include
LDFLAGS+= -L/usr/X11R6/lib
LDADD+= -lX11 -lm
-#LDADD+=-lGL
-#LDADD+=-lGLU
-#LDADD+=-lglut
. include <bsd.prog.mk>
diff --git a/stern.c b/stern.c
index e866f17..e104fc3 100644
--- a/stern.c
+++ b/stern.c
@@ -1,7 +1,10 @@
-// STERN.BAS 29.04.1996
-// STERN.CPP 11.02.2000
-// STERN.C 11.07.2003
-// stern.c 07.02.2004
+/* $Id$
+ * stern.c 25.12.2006
+ * stern.c 07.02.2004
+ * STERN.C 11.07.2003
+ * STERN.CPP 11.02.2000
+ * STERN.BAS 29.04.1996
+ */
#include <X11/Xlib.h>
#include <unistd.h>
@@ -9,45 +12,55 @@
#include <stdio.h>
#include <math.h>
-void Calc3D(void);
-void DrawObject(void);
-void InitProgram(void);
+int Calc3D(int *, int *, int *);
+void DrawObject(int);
void MainLoop(void);
-void Rotation(void);
-
-#define SLEEP 50000
-#define WIDTH 320
-#define HIGHT 320
-#define X_POS 0
-#define Y_POS 0
-#define BORDER 2
-#define PI 3.1415926
-#define Lines 72
-/* Lines 36
+#define Usleep 10000
+#define Width 800
+#define Height 600
+#define Pi 3.1415926
+
+#define SECOND
+#if defined(FIRST)
+#define D 500
+#define Lines 24
static int World[Lines][3] = {
- -60, 40, 20, -20, 40, 20,
- -20, 40, 20, -20, 0, 20,
- -20, 0, 20, 60, 0, 20,
- 60, 0, 20, 60,-40, 20,
- 60,-40, 20, -60,-40, 20,
- -60,-40, 20, -60, 40, 20,
-
- -60, 40,-20, -20, 40,-20,
- -20, 40,-20, -20, 0,-20,
- -20, 0,-20, 60, 0,-20,
- 60, 0,-20, 60,-40,-20,
- 60,-40,-20, -60,-40,-20,
- -60,-40,-20, -60, 40,-20,
-
- -60, 40, 20, -60, 40,-20,
- -20, 40, 20, -20, 40,-20,
- -20, 0, 20, -20, 0,-20,
- 60, 0, 20, 60, 0,-20,
- 60,-40, 20, 60,-40,-20,
- -60,-40, 20, -60,-40,-20};
-*/
-/* Lines 72 */
+ 50, 50, 50, -50, 50, 50, 50,-50, 50, -50,-50, 50,
+ 50, 50,-50, -50, 50,-50, 50,-50,-50, -50,-50,-50,
+ 50, 50, 50, 50,-50, 50, -50, 50, 50, -50,-50, 50,
+ 50, 50,-50, 50,-50,-50, -50, 50,-50, -50,-50,-50,
+ 50, 50, 50, 50, 50,-50, 50,-50, 50, 50,-50,-50,
+ -50,-50, 50, -50,-50,-50, -50, 50, 50, -50, 50,-50
+};
+#elif defined(SECOND)
+#define D 200
+#define Lines 36
+static int World[Lines][3] = {
+ -60, 40, 20, -20, 40, 20,
+ -20, 40, 20, -20, 0, 20,
+ -20, 0, 20, 60, 0, 20,
+ 60, 0, 20, 60,-40, 20,
+ 60,-40, 20, -60,-40, 20,
+ -60,-40, 20, -60, 40, 20,
+
+ -60, 40,-20, -20, 40,-20,
+ -20, 40,-20, -20, 0,-20,
+ -20, 0,-20, 60, 0,-20,
+ 60, 0,-20, 60,-40,-20,
+ 60,-40,-20, -60,-40,-20,
+ -60,-40,-20, -60, 40,-20,
+
+ -60, 40, 20, -60, 40,-20,
+ -20, 40, 20, -20, 40,-20,
+ -20, 0, 20, -20, 0,-20,
+ 60, 0, 20, 60, 0,-20,
+ 60,-40, 20, 60,-40,-20,
+ -60,-40, 20, -60,-40,-20
+};
+#else
+#define D 300
+#define Lines 72
static int World[Lines][3] = {
90, 0, 0, 10, 10, 10, 90, 0, 0, 10, 10,-10,
90, 0, 0, 10,-10,-10, 90, 0, 0, 10,-10, 10,
@@ -66,119 +79,99 @@ static int World[Lines][3] = {
10, 10, 10, 10,-10, 10, -10, 10, 10, -10,-10, 10,
10, 10,-10, 10,-10,-10, -10, 10,-10, -10,-10,-10,
10, 10, 10, 10, 10,-10, 10,-10, 10, 10,-10,-10,
- -10,-10, 10, -10,-10,-10, -10, 10, 10, -10, 10,-10};
-
-
-float X, Y, Z, sX, sY, Xa, Ya, Za, sXs, sYs, D, Page, Xmid, Ymid;
-float R, R1, R2, R3, Sr1, Sr2, Sr3, Cr1, Cr2, Cr3, mX, mY, mZ;
+ -10,-10, 10, -10,-10,-10, -10, 10, 10, -10, 10,-10
+};
+#endif
+
Display *display;
Window win;
GC gc;
-XGCValues values;
+/* XGCValues values; */
int main(void)
{
- InitProgram();
- MainLoop();
- XCloseDisplay(display);
- exit(0);
-}
-
-
-void InitProgram(void)
-{
int screen;
+ int black, white;
+ int r = 0;
- display = XOpenDisplay(getenv("DISPLAY"));
+ display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
- win = XCreateSimpleWindow(display, RootWindow(display, screen),
- X_POS, Y_POS, WIDTH, HIGHT, BORDER,
- BlackPixel(display, screen),
- WhitePixel(display, screen));
+ black = BlackPixel(display, screen);
+ white = WhitePixel(display, screen);
+ win = XCreateSimpleWindow(display, DefaultRootWindow(display),
+ 0, 0, Width, Height, 0, black, white);
XMapWindow(display, win);
- XFlush(display);
- gc = XCreateGC(display, win, 0, &values);
- XSetForeground(display, gc, BlackPixel(display, screen));
- XSetBackground(display, gc, WhitePixel(display, screen));
- XSetLineAttributes(display, gc, 1, LineSolid, CapButt, JoinBevel);
- XSetFillStyle(display, gc, FillSolid);
- XSync(display, False);
+ gc = XCreateGC(display, win, 0, NULL);
+
+ XSetForeground(display, gc, black);
+ XSetBackground(display, gc, white);
- Xmid = WIDTH / 2;
- Ymid = HIGHT / 2;
+ XSetLineAttributes(display, gc, 2, LineSolid, CapButt, JoinBevel);
+ XSetFillStyle(display, gc, FillSolid);
+ XSelectInput(display, win, StructureNotifyMask);
- D = 1200; /* View point and rotation values */
- mZ = -850;
- mX = -5;
- R1 = 0;
- R2 = 0;
- R3 = 0.3;
-}
+ for (;;) {
+ XEvent event;
+ XNextEvent(display, &event);
+ if (event.type == MapNotify)
+ break;
+ }
-void MainLoop(void)
-{
- int k;
- while(1)
- {
- R += 0.1;
- if(R > (2 * PI)) R = 0;
- R1 = R;
- R2 = R;
- R3 = R;
+ for (;;) {
+ if (r >= 360)
+ r = 0;
XClearWindow(display, win);
- DrawObject();
+ DrawObject(++r);
XFlush(display);
- usleep(SLEEP);
+
+ usleep(Usleep);
}
+
+ XCloseDisplay(display);
+
+ return 0;
}
-void DrawObject(void)
+float Sr, Cr;
+
+void DrawObject(int r)
{
+ float R;
int i;
+ int x1, y1, x2, y2;
- Rotation();
- for(i=0; i<Lines; i+=2)
- {
- X = World[i][0];
- Y = World[i][1];
- Z = World[i][2];
- Calc3D();
- sXs = sX;
- sYs = sY;
-
- X = World[i+1][0];
- Y = World[i+1][1];
- Z = World[i+1][2];
- Calc3D();
-
- XDrawLine(display, win, gc, sXs, sYs, sX, sY);
+ R = Pi * r / 180;
+
+ Sr = sin(R);
+ Cr = cos(R);
+
+ for(i=0; i<Lines; i+=2) {
+ Calc3D(World[i], &x1, &y1);
+ Calc3D(World[i+1], &x2, &y2);
+ XDrawLine(display, win, gc, x1, y1, x2, y2);
}
}
-void Calc3D(void)
+int
+Calc3D(int *w, int *x, int *y)
{
- X = -1 * X;
- Xa = Cr1 * X - Sr1 * Z;
- Za = Sr1 * X + Cr1 * Z;
- X = Cr2 * Xa + Sr2 * Y;
- Ya = Cr2 * Y - Sr2 * Xa;
- Z = Cr3 * Za - Sr3 * Ya;
- Y = Sr3 * Za + Cr3 * Ya;
- X = X + mX;
- Y = Y + mY;
- Z = Z + mZ;
- sX = Xmid + D * X / Z;
- sY = Ymid + D * Y / Z;
-}
+ float X, Y, Z, Xa, Ya, Za;
-void Rotation(void)
-{
- Sr1 = sin(R1);
- Sr2 = sin(R2);
- Sr3 = sin(R3);
- Cr1 = cos(R1);
- Cr2 = cos(R2);
- Cr3 = cos(R3);
+ X = w[0];
+ Y = w[1];
+ Z = w[2];
+
+ Ya = Y * Cr + Z * Sr;
+ Za = Z * Cr - Y * Sr;
+ Xa = X * Cr + Za * Sr;
+ Z = Za * Cr - X * Sr;
+ X = Xa * Cr + Ya * Sr;
+ Y = Ya * Cr - Xa * Sr;
+
+ *x = 2 * X * (Y + D) / D + Width / 2;
+ *y = 2 * Z * (Y + D) / D + Height / 2;
+
+ return Y < 0 ? 1 : 0;
}