diff --git a/GL/lighting.c b/GL/lighting.c index 2aa6188..6283350 100644 --- a/GL/lighting.c +++ b/GL/lighting.c @@ -388,7 +388,9 @@ void _glCalculateLighting(EyeSpaceData* ES, Vertex* vertex) { VPpli[2] /= VPpliL; GLfloat ndotVPpli; + vec3f_dot(n[0], n[1], n[2], VPpli[0], VPpli[1], VPpli[2], ndotVPpli); + ndotVPpli = (ndotVPpli < 0) ? 0 : ndotVPpli; const GLfloat k0 = light->constant_attenuation; const GLfloat k1 = light->linear_attenuation; @@ -425,4 +427,3 @@ void _glCalculateLighting(EyeSpaceData* ES, Vertex* vertex) { vertex->bgra[B8IDX] = (GLubyte)(fminf(final[2] * 255.0f, 255.0f)); vertex->bgra[A8IDX] = (GLubyte)(fminf(final[3] * 255.0f, 255.0f)); } - diff --git a/samples/Makefile b/samples/Makefile index 7fc4b0f..b565d58 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -29,3 +29,4 @@ all: $(KOS_MAKE) -C polygon_offset all $(KOS_MAKE) -C blend_test all $(KOS_MAKE) -C mipmap all + $(KOS_MAKE) -C lights all diff --git a/samples/lights/Makefile b/samples/lights/Makefile new file mode 100644 index 0000000..b268753 --- /dev/null +++ b/samples/lights/Makefile @@ -0,0 +1,29 @@ +TARGET = lights.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/lights/main.c b/samples/lights/main.c new file mode 100644 index 0000000..e02d85a --- /dev/null +++ b/samples/lights/main.c @@ -0,0 +1,276 @@ +#include + +#include "gl.h" +#include "glu.h" +#include "glkos.h" + +extern uint8 romdisk[]; +KOS_INIT_ROMDISK(romdisk); + + +float xrot, yrot, zrot; + + +int texture[1]; + + +struct Image { + unsigned long sizeX; + unsigned long sizeY; + char *data; +}; +typedef struct Image Image; + + + +int ImageLoad(char *filename, Image *image) { + FILE *file; + unsigned long size; + unsigned long i; + unsigned short int planes; + unsigned short int bpp; + char temp; + + + if ((file = fopen(filename, "rb"))==NULL) + { + printf("File Not Found : %s\n",filename); + return 0; + } + + + fseek(file, 18, SEEK_CUR); + + + if ((i = fread(&image->sizeX, 4, 1, file)) != 1) { + printf("Error reading width from %s.\n", filename); + return 0; + } + printf("Width of %s: %lu\n", filename, image->sizeX); + + + if ((i = fread(&image->sizeY, 4, 1, file)) != 1) { + printf("Error reading height from %s.\n", filename); + return 0; + } + printf("Height of %s: %lu\n", filename, image->sizeY); + + + size = image->sizeX * image->sizeY * 3; + + + if ((fread(&planes, 2, 1, file)) != 1) { + printf("Error reading planes from %s.\n", filename); + return 0; + } + if (planes != 1) { + printf("Planes from %s is not 1: %u\n", filename, planes); + return 0; + } + + + if ((i = fread(&bpp, 2, 1, file)) != 1) { + printf("Error reading bpp from %s.\n", filename); + return 0; + } + if (bpp != 24) { + printf("Bpp from %s is not 24: %u\n", filename, bpp); + return 0; + } + + + fseek(file, 24, SEEK_CUR); + + + image->data = (char *) malloc(size); + if (image->data == NULL) { + printf("Error allocating memory for color-corrected image data"); + return 0; + } + + if ((i = fread(image->data, size, 1, file)) != 1) { + printf(stderr, "Error reading image data from %s.\n", filename); + return 0; + } + + for (i=0;idata[i]; + image->data[i] = image->data[i+2]; + image->data[i+2] = temp; + } + + + return 1; +} + + +void LoadGLTextures() { + + Image *image1; + + + image1 = (Image *) malloc(sizeof(Image)); + if (image1 == NULL) { + printf("Error allocating space for image"); + exit(0); + } + + if (!ImageLoad("/rd/NeHe.bmp", image1)) { + exit(1); + } + + glGenTextures(1, &texture[0]); + glBindTexture(GL_TEXTURE_2D, texture[0]); + + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + + + + glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data); + + glGenerateMipmapEXT(GL_TEXTURE_2D); +}; + + +void InitGL(int Width, int Height) +{ + LoadGLTextures(); + glEnable(GL_TEXTURE_2D); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClearDepth(1.0); + glDepthFunc(GL_LESS); + glEnable(GL_DEPTH_TEST); + glShadeModel(GL_SMOOTH); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); + + glMatrixMode(GL_MODELVIEW); + + glEnable(GL_COLOR_MATERIAL); + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + + glClearColor(0.5, 0.5, 0.5, 0.5); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + // Create light components + GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; + GLfloat diffuseLight[] = { 1.0f, 0.0f, 0.0, 1.0f }; + GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; + GLfloat position[] = { -1.5f, -1.0f, 0.0f, 0.0f }; + + // Assign created components to GL_LIGHT0 + glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); + glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); + glLightfv(GL_LIGHT0, GL_POSITION, position); + + glDisable(GL_TEXTURE_2D); +} + + +void ReSizeGLScene(int Width, int Height) +{ + if (Height == 0) + Height = 1; + + glViewport(0, 0, Width, Height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); + glMatrixMode(GL_MODELVIEW); +} + +void DrawCube(float x, float z) { + glPushMatrix(); + glTranslatef(x, 0, z); + glColor4f(1, 1, 1, 1); + + glBegin(GL_QUADS); + + + glNormal3f(0, 0, 1); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + + + glNormal3f(0, 0, -1); + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + + + glNormal3f(0, 1, 0); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + + glNormal3f(0, -1, 0); + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + + glNormal3f(1, 0, 0); + glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); + + glNormal3f(-1, 0, 0); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); + + glEnd(); + + glPopMatrix(); +} + +void DrawGLScene() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + + + glTranslatef(0.0f, 0.0f, -55.0f); + glRotatef(45, 1, 0, 0); + glTranslatef(0, 0, -30); + + glBindTexture(GL_TEXTURE_2D, texture[0]); + + int x, z; + for(z = -100; z < 100; z += 10) { + for(x = -100; x < 100; x += 10) { + DrawCube(x, z); + } + } + + glKosSwapBuffers(); +} + +int main(int argc, char **argv) +{ + glKosInit(); + + InitGL(640, 480); + ReSizeGLScene(640, 480); + + while(1) { + DrawGLScene(); + } + + return 0; +} diff --git a/samples/lights/romdisk/NeHe.bmp b/samples/lights/romdisk/NeHe.bmp new file mode 100644 index 0000000..6b3db10 Binary files /dev/null and b/samples/lights/romdisk/NeHe.bmp differ diff --git a/samples/lights/romdisk/PLACEHOLDER b/samples/lights/romdisk/PLACEHOLDER new file mode 100644 index 0000000..e69de29