From 8cc9e84ccabc4aee2c5b694ca574a47992daf609 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Wed, 1 Aug 2018 11:08:39 +0100 Subject: [PATCH] Add a (broken) terrain sample --- samples/Makefile | 1 + samples/terrain/Makefile | 29 ++++++ samples/terrain/main.c | 142 ++++++++++++++++++++++++++++ samples/terrain/romdisk/PLACEHOLDER | 0 4 files changed, 172 insertions(+) create mode 100644 samples/terrain/Makefile create mode 100644 samples/terrain/main.c create mode 100644 samples/terrain/romdisk/PLACEHOLDER diff --git a/samples/Makefile b/samples/Makefile index 668e46d..daa72a0 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -16,3 +16,4 @@ all: $(KOS_MAKE) -C zclip all $(KOS_MAKE) -C zclip_triangle all $(KOS_MAKE) -C zclip_trianglestrip all + $(KOS_MAKE) -C terrain all diff --git a/samples/terrain/Makefile b/samples/terrain/Makefile new file mode 100644 index 0000000..9f21591 --- /dev/null +++ b/samples/terrain/Makefile @@ -0,0 +1,29 @@ +TARGET = terrain.elf +OBJS = main.o + +all: rm-elf $(TARGET) + +include $(KOS_BASE)/Makefile.rules + +clean: + -rm -f $(TARGET) $(OBJS) romdisk.* + +rm-elf: + -rm -f $(TARGET) romdisk.* + +$(TARGET): $(OBJS) romdisk.o + $(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \ + $(OBJS) romdisk.o $(OBJEXTRA) -lm -lkosutils $(KOS_LIBS) + +romdisk.img: + $(KOS_GENROMFS) -f romdisk.img -d romdisk -v + +romdisk.o: romdisk.img + $(KOS_BASE)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o + +run: $(TARGET) + $(KOS_LOADER) $(TARGET) + +dist: + rm -f $(OBJS) romdisk.o romdisk.img + $(KOS_STRIP) $(TARGET) diff --git a/samples/terrain/main.c b/samples/terrain/main.c new file mode 100644 index 0000000..06ef4eb --- /dev/null +++ b/samples/terrain/main.c @@ -0,0 +1,142 @@ +#include +#include + +#include "gl.h" +#include "glu.h" +#include "glkos.h" + +#define TERRAIN_SIZE 250 +#define TERRAIN_SCALE 1.0f +#define TERRAIN_HEIGHT_SCALE 1.0f + +typedef struct { + float x, y, z; +} Vec3; + +Vec3 vertices[TERRAIN_SIZE * TERRAIN_SIZE]; +unsigned int indices[TERRAIN_SIZE * TERRAIN_SIZE * 2]; +Vec3 normals[TERRAIN_SIZE * TERRAIN_SIZE]; + +void GenerateTerrain() { + const float OFFSET = (-TERRAIN_SIZE * TERRAIN_SCALE) / 2.0f; + + unsigned int j = 0; + unsigned int z, x; + for(z = 0; z < TERRAIN_SIZE; ++z) { + for(x = 0; x < TERRAIN_SIZE; ++x) { + unsigned int i = (z * TERRAIN_SIZE) + x; + + vertices[i].x = (x * TERRAIN_SCALE) + OFFSET; + vertices[i].z = (z * TERRAIN_SCALE) + OFFSET; + vertices[i].y = (float)rand() / (float)(RAND_MAX / TERRAIN_HEIGHT_SCALE); + + normals[i].x = normals[i].z = 0.0f; + normals[i].y = 1.0f; + + if(z > 0) { + indices[j++] = (i - TERRAIN_SIZE); + indices[j++] = i; + } + } + } +} + +/* A general OpenGL initialization function. Sets all of the initial parameters. */ +void InitGL(int Width, int Height) // We call this right after our OpenGL window is created. +{ + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black + glClearDepth(1.0); // Enables Clearing Of The Depth Buffer + glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do + glEnable(GL_DEPTH_TEST); // Enables Depth Testing + glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); // Reset The Projection Matrix + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window + + glMatrixMode(GL_MODELVIEW); + + GenerateTerrain(); + + /* glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glDisable(GL_BLEND); + + GLfloat mat_diffuse[] = {0.0, 1.0, 0.0, 1.0}; + GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; + GLfloat mat_shininess[] = { 50.0 }; + GLfloat light_position[] = { 1.0, -1.0, 0.0, 0.0 }; + + glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_diffuse); + glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); + glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); + glLightfv(GL_LIGHT0, GL_POSITION, light_position); */ + + fprintf(stderr, "Generated terrain\n"); +} + +/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */ +void ReSizeGLScene(int Width, int Height) +{ + if (Height == 0) // Prevent A Divide By Zero If The Window Is Too Small + Height = 1; + + glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); + glMatrixMode(GL_MODELVIEW); +} + + +/* The main drawing function. */ +void DrawGLScene() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer + glLoadIdentity(); // Reset The View + + glDisable(GL_CULL_FACE); + + glTranslatef(0.0f, -1.0f, 0.0f); + + static float x = 0.0f; + x += 1.0f; + + glRotatef(x, 0, 1.0f, 0); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + glNormalPointer(GL_FLOAT, 0, normals); + glVertexPointer(3, GL_FLOAT, 0, &vertices[0].x); + + unsigned int z; + for(z = 0; z < TERRAIN_SIZE; ++z) { + glDrawElements( + GL_TRIANGLE_STRIP, + TERRAIN_SIZE * 2, + GL_UNSIGNED_INT, + indices + (z * (TERRAIN_SIZE * 2)) + ); + } + + // swap buffers to display, since we're double buffered. + glKosSwapBuffers(); +} + +int main(int argc, char **argv) +{ + glKosInit(); + + InitGL(640, 480); + ReSizeGLScene(640, 480); + + while(1) { + DrawGLScene(); + } + + return 0; +} diff --git a/samples/terrain/romdisk/PLACEHOLDER b/samples/terrain/romdisk/PLACEHOLDER new file mode 100644 index 0000000..e69de29