Permalink
Browse files

setup basic project, from stripping out A5, time to start work...

  • Loading branch information...
bettsmatt committed Oct 6, 2012
1 parent 76f2c03 commit 2f6b6a42713683cb8cb5b238038d009bb3b62910
Showing with 1,566 additions and 0 deletions.
  1. BIN BetaFX
  2. +488 −0 G308_Geometry.cpp
  3. +87 −0 G308_Geometry.h
  4. +176 −0 G308_ImageLoader.cpp
  5. +41 −0 G308_ImageLoader.h
  6. +13 −0 Makefile
  7. +94 −0 define.h
  8. +371 −0 main.cpp
  9. +235 −0 quaternion.cpp
  10. +61 −0 quaternion.h
View
BIN BetaFX
Binary file not shown.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,87 @@
+//---------------------------------------------------------------------------
+//
+// Copyright (c) 2012 Taehyun Rhee
+//
+// Edited by Daniel Atkins
+//
+// This software is provided 'as-is' for assignment of COMP308
+// in ECS, Victoria University of Wellington,
+// without any express or implied warranty.
+// In no event will the authors be held liable for any
+// damages arising from the use of this software.
+//
+// The contents of this file may not be copied or duplicated in any form
+// without the prior permission of its owner.
+//
+//----------------------------------------------------------------------------
+
+#pragma once
+
+#include "define.h"
+#include <GL/glut.h>
+
+typedef struct v3 {
+ double x;
+ double y;
+ double z;
+} v3;
+
+class G308_Geometry
+{
+private:
+ // Array for Geometry
+ G308_Point* m_pVertexArray; // Vertex Array
+ G308_Normal* m_pNormalArray; // Normal Array
+ G308_Triangle* m_pTriangles; // Triangle Array
+ G308_UVcoord* m_pUVArray; // Texture Coordinate Array
+
+ // Data for Geoemetry
+ int m_nNumPoint;
+ int m_nNumUV;
+ int m_nNumNormal;
+ int m_nNumPolygon;
+
+ int mode; // Which mode to display
+
+ // Data for Rendering
+ int m_glGeomListPoly; // Display List for Polygon
+ int m_glGeomListWire; // Display List for Wireframe
+
+
+
+ bool hasTexture;
+ GLuint textureID;
+
+ bool hasCubemap;
+
+ GLfloat* mat_specular;
+ GLfloat* mat_shininess;
+ GLfloat* mat_diffuse;
+ GLfloat* mat_ambient;
+
+public:
+
+ float worldRot;
+ v3* pos;
+
+ G308_Geometry(void);
+ ~G308_Geometry(void);
+
+ void ReadOBJ(const char* filename);
+ void ReadTexture(const char* filename);
+
+ void CreateGLPolyGeometry(); // [Assignment5] Create GL Display List for Polygon Geometry, using textures!
+ void CreateGLWireGeometry(); // Already written for you, this time.
+
+ void RenderGeometry(); // mode : G308_SHADE_POLYGON, G308_SHADE_WIREFRAME
+ void toggleMode(); //Switch between showing filled polygons and wireframes
+
+ void SetPos(v3* newPosition); // Set the position
+ void setTexture (GLuint); // Set the Texture
+ void setMaterial(GLfloat*, GLfloat*, GLfloat*, GLfloat*); // Material
+
+ void SetCubemap (); // Set cubema
+
+ void EnableCubemap (); // Enable cubemap
+ void DisableCubemap (); // Disable cubemap
+};
View
@@ -0,0 +1,176 @@
+//---------------------------------------------------------------------------
+//
+// Copyright (c) 2012 Daniel Atkins
+//
+// This software is provided 'as-is' for assignment of COMP308
+// in ECS, Victoria University of Wellington,
+// without any express or implied warranty.
+// In no event will the authors be held liable for any
+// damages arising from the use of this software.
+//
+// The contents of this file may not be copied or duplicated in any form
+// without the prior permission of its owner.
+//
+//----------------------------------------------------------------------------
+
+#include "G308_ImageLoader.h"
+
+/*
+ * Magical JPEG loader. You probably shouldn't edit this.
+ */
+int loadTextureFromJPEG(char* filename, TextureInfo *info) {
+
+ //Gonna need a file pointer.
+ FILE *fd;
+
+ //Init the structs required by libjpeg
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+
+ //This will keep track of where we are in the file
+ unsigned char * line;
+
+ //Redirect stderr so things aren't messy
+ cinfo.err = jpeg_std_error(&jerr);
+
+ //Init the decompress struct
+ jpeg_create_decompress(&cinfo);
+
+ //Try to open the file. Better code would return an error value,
+ //but I'm just making it exit instead.
+ if (0 == (fd = fopen(filename, "rb"))) {
+ printf("Error opening file");
+ exit(1);
+ }
+ //Self evident.
+ info->filename = filename;
+ //Point libjpeg at the file
+ jpeg_stdio_src(&cinfo, fd);
+ //Read in the JPEG header
+ jpeg_read_header(&cinfo, TRUE);
+ //Used to keep track of offset in 1-D pixel array
+ unsigned int size = cinfo.image_width;
+
+ //Populate the texture-info struct.
+ //If it's not GL_RGB, then you have a really strange JPEG.
+ info->height = cinfo.image_height;
+ info->width = cinfo.image_width;
+ info->format = GL_RGB;
+ //If your JPEG somehow has alpha, you're gonna have a bad time.
+ info->hasAlpha = false;
+
+ //Assign an array for pixels
+ unsigned char* image = (unsigned char*) malloc(sizeof(char) * 3 * cinfo.image_height * cinfo.image_width);
+
+ //Begin magic.
+ jpeg_start_decompress(&cinfo);
+ while (cinfo.output_scanline < cinfo.output_height) {
+ line = image
+ + (3 * size) * cinfo.output_scanline;
+ jpeg_read_scanlines(&cinfo, &line, 1);
+ }
+ jpeg_finish_decompress(&cinfo);
+ jpeg_destroy_decompress(&cinfo);
+
+ //Allocate an array for the pixels. Why two? You'll see.
+ info->textureData = (unsigned char*) malloc(sizeof(char) * 3 * cinfo.image_height * cinfo.image_width);
+
+ //Copy the pixels from image to textureData in reverse row order. Because GL.
+ for(int y = info->height-1; y >= 0; y--){
+ for(int x = 0; x < info->width*3; x++){
+ info->textureData[(info->height-y-1)*(info->width*3)+x] = image[y*info->width*3+x];
+ }
+ }
+ free(image);
+
+ return 0;
+}
+
+/*
+ * Magical PNG loader. You probably shouldn't edit this.
+ */
+int loadTextureFromPNG(char* filename , TextureInfo* t){
+
+ //Required structs and file pointers.
+ png_structp png_ptr;
+ png_infop info_ptr;
+ unsigned int sig_read = 0;
+ FILE *fp;
+
+ //Again, this should return an error code instead of exiting,
+ //but for this assignment, it should work anyway.
+ if ((fp = fopen(filename, "rb")) == NULL){
+ printf("Error opening %s\n",filename);
+ exit(1);
+ }
+
+ //Magic, do not touch.
+ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+ if (png_ptr == NULL) {
+ printf("LibPNG did something terrible!\n");
+ fclose(fp);
+ exit(1);
+ }
+
+ //Allocate the struct we're going to read the data into.
+ info_ptr = png_create_info_struct(png_ptr);
+ if (info_ptr == NULL) {
+ printf("LibPNG did something terrible!\n");
+ fclose(fp);
+ png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
+ exit(1);
+ }
+
+ //Error handling magic. Do not touch.
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ printf("LibPNG did something terrible!\n");
+ png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
+ fclose(fp);
+ exit(1);
+ }
+
+ //Point libPNG to the right place
+ png_init_io(png_ptr, fp);
+ png_set_sig_bytes(png_ptr, sig_read);
+
+ //Yet more magic you shouldn't touch. We -probably- have enough memory to read it all in one go.
+ png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, png_voidp_NULL);
+
+ //Start setting values in the textureinfo struct
+ //Note that PNGs -can- have alpha.
+ t->width = png_get_image_width(png_ptr,info_ptr);
+ t->height = png_get_image_height(png_ptr,info_ptr);
+ switch (png_get_color_type(png_ptr,info_ptr)) {
+ case PNG_COLOR_TYPE_RGBA:
+ t->hasAlpha = true;
+ break;
+ case PNG_COLOR_TYPE_RGB:
+ t->hasAlpha = false;
+ break;
+ default:
+ printf("Invalid PNG.\n");
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+ fclose(fp);
+ exit(1);
+ }
+ //How long is a row?
+ unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
+ //Allocate some space!
+ t->textureData = (unsigned char*) malloc(row_bytes * t->height);
+
+ //Read the image data into the texture array, in reverse row order. Because GL.
+ png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
+ for (int i = 0; i < t->height; i++) {
+ memcpy(t->textureData+(row_bytes * (t->height-1-i)), row_pointers[i], row_bytes);
+ }
+
+ //Clean up
+ png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
+ fclose(fp);
+
+ return 0;
+}
+
+
+
View
@@ -0,0 +1,41 @@
+/*
+ * 308_ImageLoader.h
+ *
+ * Created on: Sep 17, 2012
+ * Author: atkinsdani1
+ */
+
+#pragma once
+
+#include <GL/glut.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <jpeglib.h>
+#include <jerror.h>
+#include <png.h>
+
+//Magic defines for libPNG because they removed them from the library
+#define png_infopp_NULL (png_infopp)NULL
+#define int_p_NULL (int*)NULL
+#define png_voidp_NULL NULL
+
+/*
+ * The TextureInfo struct. This is where we keep all the texture info when we load a file.
+ * The actual image is stored as an array of unsigned characters (aka unsigned bytes).
+ * It's three bytes per pixel -- one for each color channel, so this array will have a length
+ * of width*height*3. Note that it is ONE dimensional!
+ */
+typedef struct texInfo {
+
+ char *filename;
+ unsigned char* textureData;
+ int format;
+ int width;
+ int height;
+ bool hasAlpha;
+
+} TextureInfo;
+
+int loadTextureFromJPEG(char*,TextureInfo*);
+
+int loadTextureFromPNG(char*, TextureInfo*);
View
@@ -0,0 +1,13 @@
+CC = g++
+LPATH = -L/usr/pkg/lib
+LDPATH = -Wl,-R/usr/pkg/lib
+CFLAGS=-g -Wall
+LIBS=-lGL -lglut -lGLU -ljpeg -lpng15 -lm
+IPATH= -I/usr/pkg/include
+
+all:
+ $(CC) $(CFLAGS) -o BetaFX *.cpp $(IPATH) $(LIBS) $(LDPATH) $(LPATH)
+
+clean :
+ rm -rf *.o
+ rm TextureDemo
Oops, something went wrong.

0 comments on commit 2f6b6a4

Please sign in to comment.