#include <assert.h>
#include <math.h>
#include "obj.h"
-
-#define MAXSTR 1024
-#define OPT_CLAMP 1
+#include "tga.h"
#ifndef CONF_NO_GL
+#ifdef __APPLE__
+# include <OpenGL/gl3.h>
+#else
+# include <GL/glew.h>
+#endif
+
#ifdef OBJ_INDEX_IS_INT
typedef GL_UNSIGNED_INT GL_INDEX_T;
#else
#endif
#endif
+#define MAXSTR 1024
+#define OPT_CLAMP 1
+
static void invalidate(struct obj *);
/*----------------------------------------------------------------------------*/
/*============================================================================*/
-#pragma pack(push, 1)
-struct tga_head
-{
- unsigned char id_length;
- unsigned char color_map_type;
- unsigned char image_type;
- unsigned short color_map_offset;
- unsigned short color_map_length;
- unsigned char color_map_size;
- unsigned short image_x_origin;
- unsigned short image_y_origin;
- unsigned short image_width;
- unsigned short image_height;
- unsigned char image_depth;
- unsigned char image_descriptor;
-};
-#pragma pack(pop)
-
-void *read_tga(const char *filename, int *w, int *h, int *d)
-{
- struct tga_head head;
- FILE *stream;
-
- if ((stream = fopen(filename, "rb")))
- {
- if (fread(&head, sizeof (struct tga_head), 1, stream) == 1)
- {
- if (head.image_type == 2)
- {
- *w = (int) head.image_width;
- *h = (int) head.image_height;
- *d = (int) head.image_depth;
-
- if (fseek(stream, head.id_length, SEEK_CUR) == 0)
- {
- size_t s = (*d) / 8;
- size_t n = (*w) * (*h);
- void *p;
-
- if ((p = calloc(n, s)))
- {
- if (fread(p, s, n, stream) == n)
- {
- fclose(stream);
- return p;
- }
- }
- }
- }
- }
- fclose(stream);
- }
- return 0;
-}
-
+#ifdef CONF_NO_GL
+struct array *obj_load_image(const char *filename)
+#else
unsigned int obj_load_image(const char *filename)
+#endif
{
+#ifdef CONF_NO_GL
+ struct array *o = NULL;
+#else
unsigned int o = 0;
+#endif
-#ifndef CONF_NO_GL
if (filename)
{
- int w;
- int h;
- int d;
- void *p;
-
/* Read the image data from the named file to a new pixel buffer. */
+ struct tga *tga = tga_read(filename);
- if ((p = read_tga(filename, &w, &h, &d)))
- {
- /* Create an OpenGL texture object using these pixels. */
-
- glGenTextures(1, &o);
- glBindTexture(GL_TEXTURE_2D, o);
-
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
-
- if (d == 32)
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
- GL_BGRA, GL_UNSIGNED_BYTE, p);
- if (d == 24)
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
- GL_BGR, GL_UNSIGNED_BYTE, p);
-
- glGenerateMipmap(GL_TEXTURE_2D);
-
- /* Discard the unnecessary pixel buffer. */
+#ifdef CONF_NO_GL
+ /* We really do not need the TGA header, since coordinates are based */
+ /* on the data, rather than using the anchor position from header. */
+ o = tga->data;
+ tga->data = NULL;
+#else
+ int h = (int)tga->data->dim[0];
+ int w = (int)tga->data->dim[1];
+ int d = (int)tga->data->dim[2];
+ void *p = array_index0(tga->data);
+
+ /* Create an OpenGL texture object using these pixels. */
+ glGenTextures(1, &o);
+ glBindTexture(GL_TEXTURE_2D, o);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+
+ if (d == 4)
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
+ GL_BGRA, GL_UNSIGNED_BYTE, p);
+ if (d == 3)
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0,
+ GL_BGR, GL_UNSIGNED_BYTE, p);
+
+ glGenerateMipmap(GL_TEXTURE_2D);
+#endif
- free(p);
- }
+ /* Discard the unnecessary pixel buffer. */
+ tga_free(tga);
}
-#endif
return o;
}
{
assert_prop(O, mi, ki);
-#ifndef CONF_NO_GL
if (O->mv[mi].kv[ki].map)
+#ifdef CONF_NO_GL
+ array_free(O->mv[mi].kv[ki].map); // note: may be NULL
+#else
glDeleteTextures(1, &O->mv[mi].kv[ki].map);
#endif
return O->mv[mi].name;
}
+#ifdef CONF_NO_GL
+struct array *obj_get_mtrl_map(const struct obj *O, int mi, int ki)
+#else
unsigned int obj_get_mtrl_map(const struct obj *O, int mi, int ki)
+#endif
{
assert_prop(O, mi, ki);
return O->mv[mi].kv[ki].map;
#ifndef UTIL3D_OBJ_H
#define UTIL3D_OBJ_H
-#ifndef CONF_NO_GL
-#ifdef __APPLE__
-# include <OpenGL/gl3.h>
-#else
-# include <GL/glew.h>
-#endif
-#endif
-
/*============================================================================*/
#define OBJ_INDEX_IS_INT
{
char *str;
int opt;
+#ifdef CONF_NO_GL
+ struct array *map;
+#else
unsigned int map;
+#endif
float c[4];
float o[3];
/*----------------------------------------------------------------------------*/
const char *obj_get_mtrl_name(const struct obj *, int);
+#ifdef CONF_NO_GL
+struct array *obj_get_mtrl_map (const struct obj *, int, int);
+#else
unsigned int obj_get_mtrl_map (const struct obj *, int, int);
+#endif
unsigned int obj_get_mtrl_opt (const struct obj *, int, int);
void obj_get_mtrl_c (const struct obj *, int, int, float *);
void obj_get_mtrl_o (const struct obj *, int, int, float *);
/*----------------------------------------------------------------------------*/
+#ifdef CONF_NO_GL
+struct array *obj_load_image(const char *);
+#else
unsigned int obj_load_image(const char *);
+#endif
void obj_mini(struct obj *);
void obj_norm(struct obj *);