mirror of
https://github.com/bulletphysics/bullet3
synced 2024-12-12 12:50:08 +00:00
add wavefront loader
start adding various scenes to test gpu rigid body pipeline reserve more memory for shapes (concave triangle mesh can be huge) in GLInstancingRenderer fix a few crashes when 0 objects
This commit is contained in:
parent
fc5e2ad5ba
commit
0fa8eccac0
@ -44,7 +44,7 @@ class GLInstancingRenderer
|
||||
|
||||
|
||||
public:
|
||||
GLInstancingRenderer(int m_maxObjectCapacity, int maxShapeCapacityInBytes = 512*1024);
|
||||
GLInstancingRenderer(int m_maxObjectCapacity, int maxShapeCapacityInBytes = 10*1024*1024);
|
||||
virtual ~GLInstancingRenderer();
|
||||
|
||||
void init();
|
||||
|
@ -106,7 +106,7 @@
|
||||
include "../btgui/OpenGLTrueTypeFont"
|
||||
include "../btgui/OpenGLWindow"
|
||||
include "../demo/gpudemo"
|
||||
|
||||
include "../demo/ObjLoader"
|
||||
|
||||
|
||||
end
|
||||
|
205
demo/ObjLoader/list.cpp
Normal file
205
demo/ObjLoader/list.cpp
Normal file
@ -0,0 +1,205 @@
|
||||
#include "list.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// internal helper functions
|
||||
char list_is_full(list *listo)
|
||||
{
|
||||
return(listo->item_count == listo->current_max_size);
|
||||
}
|
||||
|
||||
void list_grow(list *old_listo)
|
||||
{
|
||||
int i;
|
||||
list new_listo;
|
||||
|
||||
list_make(&new_listo, old_listo->current_max_size*2, old_listo->growable++);
|
||||
|
||||
for(i=0; i<old_listo->current_max_size; i++)
|
||||
list_add_item(&new_listo, old_listo->items[i] , old_listo->names[i]);
|
||||
|
||||
list_free(old_listo);
|
||||
|
||||
//copy new structure to old list
|
||||
old_listo->names = new_listo.names;
|
||||
old_listo->items = new_listo.items;
|
||||
old_listo->item_count = new_listo.item_count;
|
||||
old_listo->current_max_size = new_listo.current_max_size;
|
||||
old_listo->growable = new_listo.growable;
|
||||
}
|
||||
//end helpers
|
||||
|
||||
void list_make(list *listo, int start_size, char growable)
|
||||
{
|
||||
listo->names = (char**) malloc(sizeof(char*) * start_size);
|
||||
listo->items = (void**) malloc(sizeof(void*) * start_size);
|
||||
listo->item_count = 0;
|
||||
listo->current_max_size = start_size;
|
||||
listo->growable = growable;
|
||||
}
|
||||
|
||||
int list_add_item(list *listo, void *item, char *name)
|
||||
{
|
||||
int name_length;
|
||||
char *new_name;
|
||||
|
||||
if( list_is_full(listo) )
|
||||
{
|
||||
if( listo->growable )
|
||||
list_grow(listo);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
listo->names[listo->item_count] = NULL;
|
||||
if(name != NULL)
|
||||
{
|
||||
name_length = strlen(name);
|
||||
new_name = (char*) malloc(sizeof(char) * name_length + 1);
|
||||
strncpy(new_name, name, name_length);
|
||||
listo->names[listo->item_count] = new_name;
|
||||
}
|
||||
|
||||
listo->items[listo->item_count] = item;
|
||||
listo->item_count++;
|
||||
|
||||
return listo->item_count-1;
|
||||
}
|
||||
|
||||
char* list_print_items(list *listo)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i < listo->item_count; i++)
|
||||
{
|
||||
printf("%s\n", listo->names[i]);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* list_get_index(list *listo, int indx)
|
||||
{
|
||||
if(indx < listo->item_count)
|
||||
return listo->items[indx];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* list_get_item(list *listo, void *item_to_find)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for(i=0; i < listo->item_count; i++)
|
||||
{
|
||||
if(listo->items[i] == item_to_find)
|
||||
return listo->items[i];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* list_get_name(list *listo, char *name_to_find)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for(i=0; i < listo->item_count; i++)
|
||||
{
|
||||
if(strncmp(listo->names[i], name_to_find, strlen(name_to_find)) == 0)
|
||||
return listo->items[i];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int list_find(list *listo, char *name_to_find)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for(i=0; i < listo->item_count; i++)
|
||||
{
|
||||
if(strncmp(listo->names[i], name_to_find, strlen(name_to_find)) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void list_delete_item(list *listo, void *item)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i < listo->item_count; i++)
|
||||
{
|
||||
if( listo->items[i] == item )
|
||||
list_delete_index(listo, i);
|
||||
}
|
||||
}
|
||||
|
||||
void list_delete_name(list *listo, char *name)
|
||||
{
|
||||
int i;
|
||||
//int j;
|
||||
//char remove = 0;
|
||||
|
||||
// int length_name = strlen(name);
|
||||
int item_name;
|
||||
|
||||
if(name == NULL)
|
||||
return;
|
||||
|
||||
for(i=0; i < listo->item_count; i++)
|
||||
{
|
||||
item_name = strlen(name);
|
||||
|
||||
if( name != NULL && (strncmp(listo->names[i], name, strlen(name)) == 0) )
|
||||
list_delete_index(listo, i);
|
||||
}
|
||||
}
|
||||
|
||||
void list_delete_index(list *listo, int indx)
|
||||
{
|
||||
int j;
|
||||
|
||||
//remove item
|
||||
if(listo->names[indx] != NULL)
|
||||
free(listo->names[indx]);
|
||||
|
||||
//restructure
|
||||
for(j=indx; j < listo->item_count-1; j++)
|
||||
{
|
||||
listo->names[j] = listo->names[j+1];
|
||||
listo->items[j] = listo->items[j+1];
|
||||
}
|
||||
|
||||
listo->item_count--;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void list_delete_all(list *listo)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=listo->item_count-1; i>=0; i--)
|
||||
list_delete_index(listo, i);
|
||||
}
|
||||
|
||||
void list_free(list *listo)
|
||||
{
|
||||
list_delete_all(listo);
|
||||
free(listo->names);
|
||||
free(listo->items);
|
||||
}
|
||||
|
||||
void list_print_list(list *listo)
|
||||
{
|
||||
int i;
|
||||
|
||||
printf("count: %i/%i\n", listo->item_count, listo->current_max_size);
|
||||
|
||||
for(i=0; i < listo->item_count; i++)
|
||||
{
|
||||
printf("list[%i]: %s\n", i, listo->names[i]);
|
||||
}
|
||||
}
|
29
demo/ObjLoader/list.h
Normal file
29
demo/ObjLoader/list.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef __LIST_H
|
||||
#define __LIST_H
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int item_count;
|
||||
int current_max_size;
|
||||
char growable;
|
||||
|
||||
void **items;
|
||||
char **names;
|
||||
} list;
|
||||
|
||||
void list_make(list *listo, int size, char growable);
|
||||
int list_add_item(list *listo, void *item, char *name);
|
||||
char* list_print_items(list *listo);
|
||||
void* list_get_name(list *listo, char *name);
|
||||
void* list_get_index(list *listo, int indx);
|
||||
void* list_get_item(list *listo, void *item_to_find);
|
||||
int list_find(list *listo, char *name_to_find);
|
||||
void list_delete_index(list *listo, int indx);
|
||||
void list_delete_name(list *listo, char *name);
|
||||
void list_delete_item(list *listo, void *item);
|
||||
void list_delete_all(list *listo);
|
||||
void list_print_list(list *listo);
|
||||
void list_free(list *listo);
|
||||
|
||||
void test_list();
|
||||
#endif
|
43
demo/ObjLoader/objLoader.cpp
Normal file
43
demo/ObjLoader/objLoader.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "objLoader.h"
|
||||
#include "obj_parser.h"
|
||||
|
||||
|
||||
int objLoader::load(char *filename)
|
||||
{
|
||||
int no_error = 1;
|
||||
no_error = parse_obj_scene(&data, filename);
|
||||
if(no_error)
|
||||
{
|
||||
this->vertexCount = data.vertex_count;
|
||||
this->normalCount = data.vertex_normal_count;
|
||||
this->textureCount = data.vertex_texture_count;
|
||||
|
||||
this->faceCount = data.face_count;
|
||||
this->sphereCount = data.sphere_count;
|
||||
this->planeCount = data.plane_count;
|
||||
|
||||
this->lightPointCount = data.light_point_count;
|
||||
this->lightDiscCount = data.light_disc_count;
|
||||
this->lightQuadCount = data.light_quad_count;
|
||||
|
||||
this->materialCount = data.material_count;
|
||||
|
||||
this->vertexList = data.vertex_list;
|
||||
this->normalList = data.vertex_normal_list;
|
||||
this->textureList = data.vertex_texture_list;
|
||||
|
||||
this->faceList = data.face_list;
|
||||
this->sphereList = data.sphere_list;
|
||||
this->planeList = data.plane_list;
|
||||
|
||||
this->lightPointList = data.light_point_list;
|
||||
this->lightDiscList = data.light_disc_list;
|
||||
this->lightQuadList = data.light_quad_list;
|
||||
|
||||
this->materialList = data.material_list;
|
||||
|
||||
this->camera = data.camera;
|
||||
}
|
||||
|
||||
return no_error;
|
||||
}
|
50
demo/ObjLoader/objLoader.h
Normal file
50
demo/ObjLoader/objLoader.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef OBJ_LOADER_H
|
||||
#define OBJ_LOADER_H
|
||||
|
||||
#include "obj_parser.h"
|
||||
|
||||
class objLoader
|
||||
{
|
||||
public:
|
||||
objLoader() {}
|
||||
~objLoader()
|
||||
{
|
||||
delete_obj_data(&data);
|
||||
}
|
||||
|
||||
int load(char *filename);
|
||||
|
||||
obj_vector **vertexList;
|
||||
obj_vector **normalList;
|
||||
obj_vector **textureList;
|
||||
|
||||
obj_face **faceList;
|
||||
obj_sphere **sphereList;
|
||||
obj_plane **planeList;
|
||||
|
||||
obj_light_point **lightPointList;
|
||||
obj_light_quad **lightQuadList;
|
||||
obj_light_disc **lightDiscList;
|
||||
|
||||
obj_material **materialList;
|
||||
|
||||
int vertexCount;
|
||||
int normalCount;
|
||||
int textureCount;
|
||||
|
||||
int faceCount;
|
||||
int sphereCount;
|
||||
int planeCount;
|
||||
|
||||
int lightPointCount;
|
||||
int lightQuadCount;
|
||||
int lightDiscCount;
|
||||
|
||||
int materialCount;
|
||||
|
||||
obj_camera *camera;
|
||||
private:
|
||||
obj_scene_data data;
|
||||
};
|
||||
|
||||
#endif
|
167
demo/ObjLoader/objTester.cpp
Normal file
167
demo/ObjLoader/objTester.cpp
Normal file
@ -0,0 +1,167 @@
|
||||
// Obj_loader.cpp : Defines the entry point for the console application.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include "objLoader.h"
|
||||
|
||||
void printVector(obj_vector *v)
|
||||
{
|
||||
printf("%.2f,", v->e[0] );
|
||||
printf("%.2f,", v->e[1] );
|
||||
printf("%.2f ", v->e[2] );
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
objLoader *objData = new objLoader();
|
||||
objData->load("test.obj");
|
||||
|
||||
printf("Number of vertices: %i\n", objData->vertexCount);
|
||||
printf("Number of vertex normals: %i\n", objData->normalCount);
|
||||
printf("Number of texture coordinates: %i\n", objData->textureCount);
|
||||
printf("\n");
|
||||
|
||||
printf("Number of faces: %i\n", objData->faceCount);
|
||||
for(int i=0; i<objData->faceCount; i++)
|
||||
{
|
||||
obj_face *o = objData->faceList[i];
|
||||
printf(" face ");
|
||||
for(int j=0; j<3; j++)
|
||||
{
|
||||
printVector(objData->vertexList[ o->vertex_index[j] ]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Number of spheres: %i\n", objData->sphereCount);
|
||||
for(int i=0; i<objData->sphereCount; i++)
|
||||
{
|
||||
obj_sphere *o = objData->sphereList[i];
|
||||
printf(" sphere ");
|
||||
printVector(objData->vertexList[ o->pos_index ]);
|
||||
printVector(objData->normalList[ o->up_normal_index ]);
|
||||
printVector(objData->normalList[ o->equator_normal_index ]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Number of planes: %i\n", objData->planeCount);
|
||||
for(int i=0; i<objData->planeCount; i++)
|
||||
{
|
||||
obj_plane *o = objData->planeList[i];
|
||||
printf(" plane ");
|
||||
printVector(objData->vertexList[ o->pos_index ]);
|
||||
printVector(objData->normalList[ o->normal_index]);
|
||||
printVector(objData->normalList[ o->rotation_normal_index]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Number of point lights: %i\n", objData->lightPointCount);
|
||||
for(int i=0; i<objData->lightPointCount; i++)
|
||||
{
|
||||
obj_light_point *o = objData->lightPointList[i];
|
||||
printf(" plight ");
|
||||
printVector(objData->vertexList[ o->pos_index ]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Number of disc lights: %i\n", objData->lightDiscCount);
|
||||
for(int i=0; i<objData->lightDiscCount; i++)
|
||||
{
|
||||
obj_light_disc *o = objData->lightDiscList[i];
|
||||
printf(" dlight ");
|
||||
printVector(objData->vertexList[ o->pos_index ]);
|
||||
printVector(objData->normalList[ o->normal_index ]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Number of quad lights: %i\n", objData->lightQuadCount);
|
||||
for(int i=0; i<objData->lightQuadCount; i++)
|
||||
{
|
||||
obj_light_quad *o = objData->lightQuadList[i];
|
||||
printf(" qlight ");
|
||||
printVector(objData->vertexList[ o->vertex_index[0] ]);
|
||||
printVector(objData->vertexList[ o->vertex_index[1] ]);
|
||||
printVector(objData->vertexList[ o->vertex_index[2] ]);
|
||||
printVector(objData->vertexList[ o->vertex_index[3] ]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
if(objData->camera != NULL)
|
||||
{
|
||||
printf("Found a camera\n");
|
||||
printf(" position: ");
|
||||
printVector(objData->vertexList[ objData->camera->camera_pos_index ]);
|
||||
printf("\n looking at: ");
|
||||
printVector(objData->vertexList[ objData->camera->camera_look_point_index ]);
|
||||
printf("\n up normal: ");
|
||||
printVector(objData->normalList[ objData->camera->camera_up_norm_index ]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
printf("Number of materials: %i\n", objData->materialCount);
|
||||
for(int i=0; i<objData->materialCount; i++)
|
||||
{
|
||||
obj_material *mtl = objData->materialList[i];
|
||||
printf(" name: %s", mtl->name);
|
||||
printf(" amb: %.2f ", mtl->amb[0]);
|
||||
printf("%.2f ", mtl->amb[1]);
|
||||
printf("%.2f\n", mtl->amb[2]);
|
||||
|
||||
printf(" diff: %.2f ", mtl->diff[0]);
|
||||
printf("%.2f ", mtl->diff[1]);
|
||||
printf("%.2f\n", mtl->diff[2]);
|
||||
|
||||
printf(" spec: %.2f ", mtl->spec[0]);
|
||||
printf("%.2f ", mtl->spec[1]);
|
||||
printf("%.2f\n", mtl->spec[2]);
|
||||
|
||||
printf(" reflect: %.2f\n", mtl->reflect);
|
||||
printf(" trans: %.2f\n", mtl->trans);
|
||||
printf(" glossy: %i\n", mtl->glossy);
|
||||
printf(" shiny: %i\n", mtl->shiny);
|
||||
printf(" refact: %.2f\n", mtl->refract_index);
|
||||
|
||||
printf(" texture: %s\n", mtl->texture_filename);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
//vertex, normal, and texture test
|
||||
if(objData->textureCount > 2 && objData->normalCount > 2 && objData->faceCount > 2)
|
||||
{
|
||||
printf("Detailed face data:\n");
|
||||
|
||||
for(int i=0; i<3; i++)
|
||||
{
|
||||
obj_face *o = objData->faceList[i];
|
||||
printf(" face ");
|
||||
for(int j=0; j<3; j++)
|
||||
{
|
||||
printf("%i/", o->vertex_index[j] );
|
||||
printf("%i/", o->texture_index[j] );
|
||||
printf("%i ", o->normal_index[j] );
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
564
demo/ObjLoader/obj_parser.cpp
Normal file
564
demo/ObjLoader/obj_parser.cpp
Normal file
@ -0,0 +1,564 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "obj_parser.h"
|
||||
#include "list.h"
|
||||
#include "string_extra.h"
|
||||
|
||||
#define WHITESPACE " \t\n\r"
|
||||
|
||||
void obj_free_half_list(list *listo)
|
||||
{
|
||||
list_delete_all(listo);
|
||||
free(listo->names);
|
||||
}
|
||||
|
||||
int obj_convert_to_list_index(int current_max, int index)
|
||||
{
|
||||
if(index == 0) //no index
|
||||
return -1;
|
||||
|
||||
if(index < 0) //relative to current list position
|
||||
return current_max + index;
|
||||
|
||||
return index - 1; //normal counting index
|
||||
}
|
||||
|
||||
void obj_convert_to_list_index_v(int current_max, int *indices)
|
||||
{
|
||||
for(int i=0; i<MAX_VERTEX_COUNT; i++)
|
||||
indices[i] = obj_convert_to_list_index(current_max, indices[i]);
|
||||
}
|
||||
|
||||
void obj_set_material_defaults(obj_material *mtl)
|
||||
{
|
||||
mtl->amb[0] = 0.2;
|
||||
mtl->amb[1] = 0.2;
|
||||
mtl->amb[2] = 0.2;
|
||||
mtl->diff[0] = 0.8;
|
||||
mtl->diff[1] = 0.8;
|
||||
mtl->diff[2] = 0.8;
|
||||
mtl->spec[0] = 1.0;
|
||||
mtl->spec[1] = 1.0;
|
||||
mtl->spec[2] = 1.0;
|
||||
mtl->reflect = 0.0;
|
||||
mtl->trans = 1;
|
||||
mtl->glossy = 98;
|
||||
mtl->shiny = 0;
|
||||
mtl->refract_index = 1;
|
||||
mtl->texture_filename[0] = '\0';
|
||||
}
|
||||
|
||||
int obj_parse_vertex_index(int *vertex_index, int *texture_index, int *normal_index)
|
||||
{
|
||||
char *temp_str;
|
||||
char *token;
|
||||
int vertex_count = 0;
|
||||
|
||||
|
||||
while( (token = strtok(NULL, WHITESPACE)) != NULL)
|
||||
{
|
||||
if(texture_index != NULL)
|
||||
texture_index[vertex_count] = 0;
|
||||
if(normal_index != NULL)
|
||||
normal_index[vertex_count] = 0;
|
||||
|
||||
vertex_index[vertex_count] = atoi( token );
|
||||
|
||||
if(contains(token, "//")) //normal only
|
||||
{
|
||||
temp_str = strchr(token, '/');
|
||||
temp_str++;
|
||||
normal_index[vertex_count] = atoi( ++temp_str );
|
||||
}
|
||||
else if(contains(token, "/"))
|
||||
{
|
||||
temp_str = strchr(token, '/');
|
||||
texture_index[vertex_count] = atoi( ++temp_str );
|
||||
|
||||
if(contains(temp_str, "/"))
|
||||
{
|
||||
temp_str = strchr(temp_str, '/');
|
||||
normal_index[vertex_count] = atoi( ++temp_str );
|
||||
}
|
||||
}
|
||||
|
||||
vertex_count++;
|
||||
}
|
||||
|
||||
return vertex_count;
|
||||
}
|
||||
|
||||
obj_face* obj_parse_face(obj_growable_scene_data *scene)
|
||||
{
|
||||
int vertex_count;
|
||||
obj_face *face = (obj_face*)malloc(sizeof(obj_face));
|
||||
|
||||
vertex_count = obj_parse_vertex_index(face->vertex_index, face->texture_index, face->normal_index);
|
||||
obj_convert_to_list_index_v(scene->vertex_list.item_count, face->vertex_index);
|
||||
obj_convert_to_list_index_v(scene->vertex_texture_list.item_count, face->texture_index);
|
||||
obj_convert_to_list_index_v(scene->vertex_normal_list.item_count, face->normal_index);
|
||||
face->vertex_count = vertex_count;
|
||||
|
||||
return face;
|
||||
}
|
||||
|
||||
obj_sphere* obj_parse_sphere(obj_growable_scene_data *scene)
|
||||
{
|
||||
int temp_indices[MAX_VERTEX_COUNT];
|
||||
|
||||
obj_sphere *obj = (obj_sphere*)malloc(sizeof(obj_sphere));
|
||||
obj_parse_vertex_index(temp_indices, obj->texture_index, NULL);
|
||||
obj_convert_to_list_index_v(scene->vertex_texture_list.item_count, obj->texture_index);
|
||||
obj->pos_index = obj_convert_to_list_index(scene->vertex_list.item_count, temp_indices[0]);
|
||||
obj->up_normal_index = obj_convert_to_list_index(scene->vertex_normal_list.item_count, temp_indices[1]);
|
||||
obj->equator_normal_index = obj_convert_to_list_index(scene->vertex_normal_list.item_count, temp_indices[2]);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj_plane* obj_parse_plane(obj_growable_scene_data *scene)
|
||||
{
|
||||
int temp_indices[MAX_VERTEX_COUNT];
|
||||
|
||||
obj_plane *obj = (obj_plane*)malloc(sizeof(obj_plane));
|
||||
obj_parse_vertex_index(temp_indices, obj->texture_index, NULL);
|
||||
obj_convert_to_list_index_v(scene->vertex_texture_list.item_count, obj->texture_index);
|
||||
obj->pos_index = obj_convert_to_list_index(scene->vertex_list.item_count, temp_indices[0]);
|
||||
obj->normal_index = obj_convert_to_list_index(scene->vertex_normal_list.item_count, temp_indices[1]);
|
||||
obj->rotation_normal_index = obj_convert_to_list_index(scene->vertex_normal_list.item_count, temp_indices[2]);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj_light_point* obj_parse_light_point(obj_growable_scene_data *scene)
|
||||
{
|
||||
obj_light_point *o= (obj_light_point*)malloc(sizeof(obj_light_point));
|
||||
o->pos_index = obj_convert_to_list_index(scene->vertex_list.item_count, atoi( strtok(NULL, WHITESPACE)) );
|
||||
return o;
|
||||
}
|
||||
|
||||
obj_light_quad* obj_parse_light_quad(obj_growable_scene_data *scene)
|
||||
{
|
||||
obj_light_quad *o = (obj_light_quad*)malloc(sizeof(obj_light_quad));
|
||||
obj_parse_vertex_index(o->vertex_index, NULL, NULL);
|
||||
obj_convert_to_list_index_v(scene->vertex_list.item_count, o->vertex_index);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
obj_light_disc* obj_parse_light_disc(obj_growable_scene_data *scene)
|
||||
{
|
||||
int temp_indices[MAX_VERTEX_COUNT];
|
||||
|
||||
obj_light_disc *obj = (obj_light_disc*)malloc(sizeof(obj_light_disc));
|
||||
obj_parse_vertex_index(temp_indices, NULL, NULL);
|
||||
obj->pos_index = obj_convert_to_list_index(scene->vertex_list.item_count, temp_indices[0]);
|
||||
obj->normal_index = obj_convert_to_list_index(scene->vertex_normal_list.item_count, temp_indices[1]);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj_vector* obj_parse_vector()
|
||||
{
|
||||
obj_vector *v = (obj_vector*)malloc(sizeof(obj_vector));
|
||||
v->e[0] = atof( strtok(NULL, WHITESPACE));
|
||||
v->e[1] = atof( strtok(NULL, WHITESPACE));
|
||||
v->e[2] = atof( strtok(NULL, WHITESPACE));
|
||||
return v;
|
||||
}
|
||||
|
||||
obj_vector* obj_parse_vector2()
|
||||
{
|
||||
obj_vector *v = (obj_vector*)malloc(sizeof(obj_vector));
|
||||
v->e[0] = atof( strtok(NULL, WHITESPACE));
|
||||
v->e[1] = atof( strtok(NULL, WHITESPACE));
|
||||
v->e[2] = 0.f;
|
||||
return v;
|
||||
}
|
||||
|
||||
void obj_parse_camera(obj_growable_scene_data *scene, obj_camera *camera)
|
||||
{
|
||||
int indices[3];
|
||||
obj_parse_vertex_index(indices, NULL, NULL);
|
||||
camera->camera_pos_index = obj_convert_to_list_index(scene->vertex_list.item_count, indices[0]);
|
||||
camera->camera_look_point_index = obj_convert_to_list_index(scene->vertex_list.item_count, indices[1]);
|
||||
camera->camera_up_norm_index = obj_convert_to_list_index(scene->vertex_normal_list.item_count, indices[2]);
|
||||
}
|
||||
|
||||
int obj_parse_mtl_file(char *filename, list *material_list)
|
||||
{
|
||||
int line_number = 0;
|
||||
char *current_token;
|
||||
char current_line[OBJ_LINE_SIZE];
|
||||
char material_open = 0;
|
||||
obj_material *current_mtl = NULL;
|
||||
FILE *mtl_file_stream;
|
||||
|
||||
// open scene
|
||||
mtl_file_stream = fopen( filename, "r");
|
||||
if(mtl_file_stream == 0)
|
||||
{
|
||||
fprintf(stderr, "Error reading file: %s\n", filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
list_make(material_list, 10, 1);
|
||||
|
||||
while( fgets(current_line, OBJ_LINE_SIZE, mtl_file_stream) )
|
||||
{
|
||||
current_token = strtok( current_line, " \t\n\r");
|
||||
line_number++;
|
||||
|
||||
//skip comments
|
||||
if( current_token == NULL || strequal(current_token, "//") || strequal(current_token, "#"))
|
||||
continue;
|
||||
|
||||
|
||||
//start material
|
||||
else if( strequal(current_token, "newmtl"))
|
||||
{
|
||||
material_open = 1;
|
||||
current_mtl = (obj_material*) malloc(sizeof(obj_material));
|
||||
obj_set_material_defaults(current_mtl);
|
||||
|
||||
// get the name
|
||||
strncpy(current_mtl->name, strtok(NULL, " \t"), MATERIAL_NAME_SIZE);
|
||||
list_add_item(material_list, current_mtl, current_mtl->name);
|
||||
}
|
||||
|
||||
//ambient
|
||||
else if( strequal(current_token, "Ka") && material_open)
|
||||
{
|
||||
current_mtl->amb[0] = atof( strtok(NULL, " \t"));
|
||||
current_mtl->amb[1] = atof( strtok(NULL, " \t"));
|
||||
current_mtl->amb[2] = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
|
||||
//diff
|
||||
else if( strequal(current_token, "Kd") && material_open)
|
||||
{
|
||||
current_mtl->diff[0] = atof( strtok(NULL, " \t"));
|
||||
current_mtl->diff[1] = atof( strtok(NULL, " \t"));
|
||||
current_mtl->diff[2] = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
|
||||
//specular
|
||||
else if( strequal(current_token, "Ks") && material_open)
|
||||
{
|
||||
current_mtl->spec[0] = atof( strtok(NULL, " \t"));
|
||||
current_mtl->spec[1] = atof( strtok(NULL, " \t"));
|
||||
current_mtl->spec[2] = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
//shiny
|
||||
else if( strequal(current_token, "Ns") && material_open)
|
||||
{
|
||||
current_mtl->shiny = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
//transparent
|
||||
else if( strequal(current_token, "d") && material_open)
|
||||
{
|
||||
current_mtl->trans = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
//reflection
|
||||
else if( strequal(current_token, "r") && material_open)
|
||||
{
|
||||
current_mtl->reflect = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
//glossy
|
||||
else if( strequal(current_token, "sharpness") && material_open)
|
||||
{
|
||||
current_mtl->glossy = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
//refract index
|
||||
else if( strequal(current_token, "Ni") && material_open)
|
||||
{
|
||||
current_mtl->refract_index = atof( strtok(NULL, " \t"));
|
||||
}
|
||||
// illumination type
|
||||
else if( strequal(current_token, "illum") && material_open)
|
||||
{
|
||||
}
|
||||
// texture map
|
||||
else if( strequal(current_token, "map_Ka") && material_open)
|
||||
{
|
||||
strncpy(current_mtl->texture_filename, strtok(NULL, " \t"), OBJ_FILENAME_LENGTH);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unknown command '%s' in material file %s at line %i:\n\t%s\n",
|
||||
current_token, filename, line_number, current_line);
|
||||
//return 0;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(mtl_file_stream);
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int obj_parse_obj_file(obj_growable_scene_data *growable_data, char *filename)
|
||||
{
|
||||
FILE* obj_file_stream;
|
||||
int current_material = -1;
|
||||
char *current_token = NULL;
|
||||
char current_line[OBJ_LINE_SIZE];
|
||||
int line_number = 0;
|
||||
// open scene
|
||||
obj_file_stream = fopen( filename, "r");
|
||||
if(obj_file_stream == 0)
|
||||
{
|
||||
fprintf(stderr, "Error reading file: %s\n", filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
extreme_dimensions[0].x = INFINITY; extreme_dimensions[0].y = INFINITY; extreme_dimensions[0].z = INFINITY;
|
||||
extreme_dimensions[1].x = -INFINITY; extreme_dimensions[1].y = -INFINITY; extreme_dimensions[1].z = -INFINITY;
|
||||
|
||||
if(v->x < extreme_dimensions[0].x) extreme_dimensions[0].x = v->x;
|
||||
if(v->x > extreme_dimensions[1].x) extreme_dimensions[1].x = v->x;
|
||||
if(v->y < extreme_dimensions[0].y) extreme_dimensions[0].y = v->y;
|
||||
if(v->y > extreme_dimensions[1].y) extreme_dimensions[1].y = v->y;
|
||||
if(v->z < extreme_dimensions[0].z) extreme_dimensions[0].z = v->z;
|
||||
if(v->z > extreme_dimensions[1].z) extreme_dimensions[1].z = v->z;*/
|
||||
|
||||
|
||||
//parser loop
|
||||
while( fgets(current_line, OBJ_LINE_SIZE, obj_file_stream) )
|
||||
{
|
||||
current_token = strtok( current_line, " \t\n\r");
|
||||
line_number++;
|
||||
|
||||
//skip comments
|
||||
if( current_token == NULL || current_token[0] == '#')
|
||||
continue;
|
||||
|
||||
//parse objects
|
||||
else if( strequal(current_token, "v") ) //process vertex
|
||||
{
|
||||
list_add_item(&growable_data->vertex_list, obj_parse_vector(), NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "vn") ) //process vertex normal
|
||||
{
|
||||
list_add_item(&growable_data->vertex_normal_list, obj_parse_vector(), NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "vt") ) //process vertex texture
|
||||
{
|
||||
list_add_item(&growable_data->vertex_texture_list, obj_parse_vector2(), NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "f") ) //process face
|
||||
{
|
||||
obj_face *face = obj_parse_face(growable_data);
|
||||
face->material_index = current_material;
|
||||
list_add_item(&growable_data->face_list, face, NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "sp") ) //process sphere
|
||||
{
|
||||
obj_sphere *sphr = obj_parse_sphere(growable_data);
|
||||
sphr->material_index = current_material;
|
||||
list_add_item(&growable_data->sphere_list, sphr, NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "pl") ) //process plane
|
||||
{
|
||||
obj_plane *pl = obj_parse_plane(growable_data);
|
||||
pl->material_index = current_material;
|
||||
list_add_item(&growable_data->plane_list, pl, NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "p") ) //process point
|
||||
{
|
||||
//make a small sphere to represent the point?
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "lp") ) //light point source
|
||||
{
|
||||
obj_light_point *o = obj_parse_light_point(growable_data);
|
||||
o->material_index = current_material;
|
||||
list_add_item(&growable_data->light_point_list, o, NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "ld") ) //process light disc
|
||||
{
|
||||
obj_light_disc *o = obj_parse_light_disc(growable_data);
|
||||
o->material_index = current_material;
|
||||
list_add_item(&growable_data->light_disc_list, o, NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "lq") ) //process light quad
|
||||
{
|
||||
obj_light_quad *o = obj_parse_light_quad(growable_data);
|
||||
o->material_index = current_material;
|
||||
list_add_item(&growable_data->light_quad_list, o, NULL);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "c") ) //camera
|
||||
{
|
||||
growable_data->camera = (obj_camera*) malloc(sizeof(obj_camera));
|
||||
obj_parse_camera(growable_data, growable_data->camera);
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "usemtl") ) // usemtl
|
||||
{
|
||||
current_material = list_find(&growable_data->material_list, strtok(NULL, WHITESPACE));
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "mtllib") ) // mtllib
|
||||
{
|
||||
strncpy(growable_data->material_filename, strtok(NULL, WHITESPACE), OBJ_FILENAME_LENGTH);
|
||||
obj_parse_mtl_file(growable_data->material_filename, &growable_data->material_list);
|
||||
continue;
|
||||
}
|
||||
|
||||
else if( strequal(current_token, "o") ) //object name
|
||||
{ }
|
||||
else if( strequal(current_token, "s") ) //smoothing
|
||||
{ }
|
||||
else if( strequal(current_token, "g") ) // group
|
||||
{ }
|
||||
|
||||
else
|
||||
{
|
||||
printf("Unknown command '%s' in scene code at line %i: \"%s\".\n",
|
||||
current_token, line_number, current_line);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(obj_file_stream);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void obj_init_temp_storage(obj_growable_scene_data *growable_data)
|
||||
{
|
||||
list_make(&growable_data->vertex_list, 10, 1);
|
||||
list_make(&growable_data->vertex_normal_list, 10, 1);
|
||||
list_make(&growable_data->vertex_texture_list, 10, 1);
|
||||
|
||||
list_make(&growable_data->face_list, 10, 1);
|
||||
list_make(&growable_data->sphere_list, 10, 1);
|
||||
list_make(&growable_data->plane_list, 10, 1);
|
||||
|
||||
list_make(&growable_data->light_point_list, 10, 1);
|
||||
list_make(&growable_data->light_quad_list, 10, 1);
|
||||
list_make(&growable_data->light_disc_list, 10, 1);
|
||||
|
||||
list_make(&growable_data->material_list, 10, 1);
|
||||
|
||||
growable_data->camera = NULL;
|
||||
}
|
||||
|
||||
void obj_free_temp_storage(obj_growable_scene_data *growable_data)
|
||||
{
|
||||
obj_free_half_list(&growable_data->vertex_list);
|
||||
obj_free_half_list(&growable_data->vertex_normal_list);
|
||||
obj_free_half_list(&growable_data->vertex_texture_list);
|
||||
|
||||
obj_free_half_list(&growable_data->face_list);
|
||||
obj_free_half_list(&growable_data->sphere_list);
|
||||
obj_free_half_list(&growable_data->plane_list);
|
||||
|
||||
obj_free_half_list(&growable_data->light_point_list);
|
||||
obj_free_half_list(&growable_data->light_quad_list);
|
||||
obj_free_half_list(&growable_data->light_disc_list);
|
||||
|
||||
obj_free_half_list(&growable_data->material_list);
|
||||
}
|
||||
|
||||
void delete_obj_data(obj_scene_data *data_out)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i<data_out->vertex_count; i++)
|
||||
free(data_out->vertex_list[i]);
|
||||
free(data_out->vertex_list);
|
||||
for(i=0; i<data_out->vertex_normal_count; i++)
|
||||
free(data_out->vertex_normal_list[i]);
|
||||
free(data_out->vertex_normal_list);
|
||||
for(i=0; i<data_out->vertex_texture_count; i++)
|
||||
free(data_out->vertex_texture_list[i]);
|
||||
free(data_out->vertex_texture_list);
|
||||
|
||||
for(i=0; i<data_out->face_count; i++)
|
||||
free(data_out->face_list[i]);
|
||||
free(data_out->face_list);
|
||||
for(i=0; i<data_out->sphere_count; i++)
|
||||
free(data_out->sphere_list[i]);
|
||||
free(data_out->sphere_list);
|
||||
for(i=0; i<data_out->plane_count; i++)
|
||||
free(data_out->plane_list[i]);
|
||||
free(data_out->plane_list);
|
||||
|
||||
for(i=0; i<data_out->light_point_count; i++)
|
||||
free(data_out->light_point_list[i]);
|
||||
free(data_out->light_point_list);
|
||||
for(i=0; i<data_out->light_disc_count; i++)
|
||||
free(data_out->light_disc_list[i]);
|
||||
free(data_out->light_disc_list);
|
||||
for(i=0; i<data_out->light_quad_count; i++)
|
||||
free(data_out->light_quad_list[i]);
|
||||
free(data_out->light_quad_list);
|
||||
|
||||
for(i=0; i<data_out->material_count; i++)
|
||||
free(data_out->material_list[i]);
|
||||
free(data_out->material_list);
|
||||
|
||||
free(data_out->camera);
|
||||
}
|
||||
|
||||
void obj_copy_to_out_storage(obj_scene_data *data_out, obj_growable_scene_data *growable_data)
|
||||
{
|
||||
data_out->vertex_count = growable_data->vertex_list.item_count;
|
||||
data_out->vertex_normal_count = growable_data->vertex_normal_list.item_count;
|
||||
data_out->vertex_texture_count = growable_data->vertex_texture_list.item_count;
|
||||
|
||||
data_out->face_count = growable_data->face_list.item_count;
|
||||
data_out->sphere_count = growable_data->sphere_list.item_count;
|
||||
data_out->plane_count = growable_data->plane_list.item_count;
|
||||
|
||||
data_out->light_point_count = growable_data->light_point_list.item_count;
|
||||
data_out->light_disc_count = growable_data->light_disc_list.item_count;
|
||||
data_out->light_quad_count = growable_data->light_quad_list.item_count;
|
||||
|
||||
data_out->material_count = growable_data->material_list.item_count;
|
||||
|
||||
data_out->vertex_list = (obj_vector**)growable_data->vertex_list.items;
|
||||
data_out->vertex_normal_list = (obj_vector**)growable_data->vertex_normal_list.items;
|
||||
data_out->vertex_texture_list = (obj_vector**)growable_data->vertex_texture_list.items;
|
||||
|
||||
data_out->face_list = (obj_face**)growable_data->face_list.items;
|
||||
data_out->sphere_list = (obj_sphere**)growable_data->sphere_list.items;
|
||||
data_out->plane_list = (obj_plane**)growable_data->plane_list.items;
|
||||
|
||||
data_out->light_point_list = (obj_light_point**)growable_data->light_point_list.items;
|
||||
data_out->light_disc_list = (obj_light_disc**)growable_data->light_disc_list.items;
|
||||
data_out->light_quad_list = (obj_light_quad**)growable_data->light_quad_list.items;
|
||||
|
||||
data_out->material_list = (obj_material**)growable_data->material_list.items;
|
||||
|
||||
data_out->camera = growable_data->camera;
|
||||
}
|
||||
|
||||
int parse_obj_scene(obj_scene_data *data_out, char *filename)
|
||||
{
|
||||
obj_growable_scene_data growable_data;
|
||||
|
||||
obj_init_temp_storage(&growable_data);
|
||||
if( obj_parse_obj_file(&growable_data, filename) == 0)
|
||||
return 0;
|
||||
|
||||
//print_vector(NORMAL, "Max bounds are: ", &growable_data->extreme_dimensions[1]);
|
||||
//print_vector(NORMAL, "Min bounds are: ", &growable_data->extreme_dimensions[0]);
|
||||
|
||||
obj_copy_to_out_storage(data_out, &growable_data);
|
||||
obj_free_temp_storage(&growable_data);
|
||||
return 1;
|
||||
}
|
||||
|
143
demo/ObjLoader/obj_parser.h
Normal file
143
demo/ObjLoader/obj_parser.h
Normal file
@ -0,0 +1,143 @@
|
||||
#ifndef OBJ_PARSER_H
|
||||
#define OBJ_PARSER_H
|
||||
|
||||
#include "list.h"
|
||||
|
||||
#define OBJ_FILENAME_LENGTH 500
|
||||
#define MATERIAL_NAME_SIZE 255
|
||||
#define OBJ_LINE_SIZE 500
|
||||
#define MAX_VERTEX_COUNT 4 //can only handle quads or triangles
|
||||
|
||||
typedef struct obj_face
|
||||
{
|
||||
int vertex_index[MAX_VERTEX_COUNT];
|
||||
int normal_index[MAX_VERTEX_COUNT];
|
||||
int texture_index[MAX_VERTEX_COUNT];
|
||||
int vertex_count;
|
||||
int material_index;
|
||||
};
|
||||
|
||||
typedef struct obj_sphere
|
||||
{
|
||||
int pos_index;
|
||||
int up_normal_index;
|
||||
int equator_normal_index;
|
||||
int texture_index[MAX_VERTEX_COUNT];
|
||||
int material_index;
|
||||
};
|
||||
|
||||
typedef struct obj_plane
|
||||
{
|
||||
int pos_index;
|
||||
int normal_index;
|
||||
int rotation_normal_index;
|
||||
int texture_index[MAX_VERTEX_COUNT];
|
||||
int material_index;
|
||||
};
|
||||
|
||||
typedef struct obj_vector
|
||||
{
|
||||
double e[3];
|
||||
};
|
||||
|
||||
typedef struct obj_material
|
||||
{
|
||||
char name[MATERIAL_NAME_SIZE];
|
||||
char texture_filename[OBJ_FILENAME_LENGTH];
|
||||
double amb[3];
|
||||
double diff[3];
|
||||
double spec[3];
|
||||
double reflect;
|
||||
double refract;
|
||||
double trans;
|
||||
double shiny;
|
||||
double glossy;
|
||||
double refract_index;
|
||||
};
|
||||
|
||||
typedef struct obj_camera
|
||||
{
|
||||
int camera_pos_index;
|
||||
int camera_look_point_index;
|
||||
int camera_up_norm_index;
|
||||
};
|
||||
|
||||
typedef struct obj_light_point
|
||||
{
|
||||
int pos_index;
|
||||
int material_index;
|
||||
};
|
||||
|
||||
typedef struct obj_light_disc
|
||||
{
|
||||
int pos_index;
|
||||
int normal_index;
|
||||
int material_index;
|
||||
};
|
||||
|
||||
typedef struct obj_light_quad
|
||||
{
|
||||
int vertex_index[MAX_VERTEX_COUNT];
|
||||
int material_index;
|
||||
};
|
||||
|
||||
typedef struct obj_growable_scene_data
|
||||
{
|
||||
// vector extreme_dimensions[2];
|
||||
char scene_filename[OBJ_FILENAME_LENGTH];
|
||||
char material_filename[OBJ_FILENAME_LENGTH];
|
||||
|
||||
list vertex_list;
|
||||
list vertex_normal_list;
|
||||
list vertex_texture_list;
|
||||
|
||||
list face_list;
|
||||
list sphere_list;
|
||||
list plane_list;
|
||||
|
||||
list light_point_list;
|
||||
list light_quad_list;
|
||||
list light_disc_list;
|
||||
|
||||
list material_list;
|
||||
|
||||
obj_camera *camera;
|
||||
};
|
||||
|
||||
typedef struct obj_scene_data
|
||||
{
|
||||
obj_vector **vertex_list;
|
||||
obj_vector **vertex_normal_list;
|
||||
obj_vector **vertex_texture_list;
|
||||
|
||||
obj_face **face_list;
|
||||
obj_sphere **sphere_list;
|
||||
obj_plane **plane_list;
|
||||
|
||||
obj_light_point **light_point_list;
|
||||
obj_light_quad **light_quad_list;
|
||||
obj_light_disc **light_disc_list;
|
||||
|
||||
obj_material **material_list;
|
||||
|
||||
int vertex_count;
|
||||
int vertex_normal_count;
|
||||
int vertex_texture_count;
|
||||
|
||||
int face_count;
|
||||
int sphere_count;
|
||||
int plane_count;
|
||||
|
||||
int light_point_count;
|
||||
int light_quad_count;
|
||||
int light_disc_count;
|
||||
|
||||
int material_count;
|
||||
|
||||
obj_camera *camera;
|
||||
};
|
||||
|
||||
int parse_obj_scene(obj_scene_data *data_out, char *filename);
|
||||
void delete_obj_data(obj_scene_data *data_out);
|
||||
|
||||
#endif
|
24
demo/ObjLoader/premake4.lua
Normal file
24
demo/ObjLoader/premake4.lua
Normal file
@ -0,0 +1,24 @@
|
||||
|
||||
project "wavefrontObjLoader"
|
||||
|
||||
language "C++"
|
||||
|
||||
kind "ConsoleApp"
|
||||
targetdir "../../bin"
|
||||
|
||||
includedirs {
|
||||
".",
|
||||
}
|
||||
|
||||
|
||||
files {
|
||||
"objTester.cpp",
|
||||
"string_extra.cpp",
|
||||
"string_extra.h",
|
||||
"objLoader.cpp",
|
||||
"objLoader.h",
|
||||
"obj_parser.cpp",
|
||||
"obj_parser.h",
|
||||
"list.cpp",
|
||||
"list.h"
|
||||
}
|
16
demo/ObjLoader/string_extra.cpp
Normal file
16
demo/ObjLoader/string_extra.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include "string_extra.h"
|
||||
#include <string.h>
|
||||
|
||||
char strequal(const char *s1, const char *s2)
|
||||
{
|
||||
if(strcmp(s1, s2) == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char contains(const char *haystack, const char *needle)
|
||||
{
|
||||
if(strstr(haystack, needle) == NULL)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
8
demo/ObjLoader/string_extra.h
Normal file
8
demo/ObjLoader/string_extra.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef STRING_EXTRA_H
|
||||
#define STRING_EXTRA_H
|
||||
|
||||
char strequal(const char *s1, const char *s2);
|
||||
char contains(const char *haystack, const char *needle);
|
||||
|
||||
|
||||
#endif
|
@ -26,6 +26,8 @@
|
||||
#include "ParticleDemo.h"
|
||||
#include "broadphase/PairBench.h"
|
||||
#include "rigidbody/GpuRigidBodyDemo.h"
|
||||
#include "rigidbody/ConcaveScene.h"
|
||||
#include "rigidbody/GpuConvexScene.h"
|
||||
|
||||
//#include "BroadphaseBenchmark.h"
|
||||
|
||||
@ -61,6 +63,9 @@ btAlignedObjectArray<const char*> demoNames;
|
||||
int selectedDemo = 0;
|
||||
GpuDemo::CreateFunc* allDemos[]=
|
||||
{
|
||||
ConcaveScene::MyCreateFunc,
|
||||
|
||||
GpuConvexScene::MyCreateFunc,
|
||||
GpuRigidBodyDemo::MyCreateFunc,
|
||||
|
||||
//BroadphaseBenchmark::CreateFunc,
|
||||
|
@ -31,6 +31,16 @@ function createProject(vendor)
|
||||
files {
|
||||
"**.cpp",
|
||||
"**.h",
|
||||
|
||||
"../ObjLoader/string_extra.cpp",
|
||||
"../ObjLoader/string_extra.h",
|
||||
"../ObjLoader/objLoader.cpp",
|
||||
"../ObjLoader/objLoader.h",
|
||||
"../ObjLoader/obj_parser.cpp",
|
||||
"../ObjLoader/obj_parser.h",
|
||||
"../ObjLoader/list.cpp",
|
||||
"../ObjLoader/list.h",
|
||||
|
||||
"../../src/BulletCommon/btAlignedAllocator.cpp",
|
||||
"../../src/BulletCommon/btAlignedAllocator.h",
|
||||
"../../src/BulletCommon/btQuickprof.cpp",
|
||||
|
264
demo/gpudemo/rigidbody/ConcaveScene.cpp
Normal file
264
demo/gpudemo/rigidbody/ConcaveScene.cpp
Normal file
@ -0,0 +1,264 @@
|
||||
#include "ConcaveScene.h"
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "BulletCommon/btQuickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "BulletCommon/btQuaternion.h"
|
||||
#include "OpenGLWindow/btgWindowInterface.h"
|
||||
#include "gpu_broadphase/host/btGpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "basic_initialize/btOpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "parallel_primitives/host/btLauncherCL.h"
|
||||
#include "gpu_rigidbody/host/btGpuRigidBodyPipeline.h"
|
||||
#include "gpu_rigidbody/host/btGpuNarrowPhase.h"
|
||||
#include "gpu_rigidbody/host/btConfig.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
#include"../../ObjLoader/objLoader.h"
|
||||
|
||||
|
||||
struct GraphicsVertex
|
||||
{
|
||||
float xyzw[4];
|
||||
float normal[3];
|
||||
float uv[2];
|
||||
};
|
||||
struct GraphicsShape
|
||||
{
|
||||
const float* m_vertices;
|
||||
int m_numvertices;
|
||||
const int* m_indices;
|
||||
int m_numIndices;
|
||||
float m_scaling[4];
|
||||
};
|
||||
|
||||
GraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj)
|
||||
{
|
||||
btAlignedObjectArray<GraphicsVertex>* vertices = new btAlignedObjectArray<GraphicsVertex>;
|
||||
{
|
||||
// int numVertices = obj->vertexCount;
|
||||
// int numIndices = 0;
|
||||
btAlignedObjectArray<int>* indicesPtr = new btAlignedObjectArray<int>;
|
||||
/*
|
||||
for (int v=0;v<obj->vertexCount;v++)
|
||||
{
|
||||
vtx.xyzw[0] = obj->vertexList[v]->e[0];
|
||||
vtx.xyzw[1] = obj->vertexList[v]->e[1];
|
||||
vtx.xyzw[2] = obj->vertexList[v]->e[2];
|
||||
btVector3 n(vtx.xyzw[0],vtx.xyzw[1],vtx.xyzw[2]);
|
||||
if (n.length2()>SIMD_EPSILON)
|
||||
{
|
||||
n.normalize();
|
||||
vtx.normal[0] = n[0];
|
||||
vtx.normal[1] = n[1];
|
||||
vtx.normal[2] = n[2];
|
||||
|
||||
} else
|
||||
{
|
||||
vtx.normal[0] = 0; //todo
|
||||
vtx.normal[1] = 1;
|
||||
vtx.normal[2] = 0;
|
||||
}
|
||||
vtx.uv[0] = 0.5f;vtx.uv[1] = 0.5f; //todo
|
||||
vertices->push_back(vtx);
|
||||
}
|
||||
*/
|
||||
|
||||
for (int f=0;f<obj->faceCount;f++)
|
||||
{
|
||||
obj_face* face = obj->faceList[f];
|
||||
//btVector3 normal(face.m_plane[0],face.m_plane[1],face.m_plane[2]);
|
||||
if (face->vertex_count>=3)
|
||||
{
|
||||
btVector3 normal(0,1,0);
|
||||
int vtxBaseIndex = vertices->size();
|
||||
|
||||
if (face->vertex_count<=4)
|
||||
{
|
||||
indicesPtr->push_back(vtxBaseIndex);
|
||||
indicesPtr->push_back(vtxBaseIndex+1);
|
||||
indicesPtr->push_back(vtxBaseIndex+2);
|
||||
|
||||
GraphicsVertex vtx0;
|
||||
vtx0.xyzw[0] = obj->vertexList[face->vertex_index[0]]->e[0];
|
||||
vtx0.xyzw[1] = obj->vertexList[face->vertex_index[0]]->e[1];
|
||||
vtx0.xyzw[2] = obj->vertexList[face->vertex_index[0]]->e[2];
|
||||
vtx0.xyzw[3] = 0.f;//obj->vertexList[face->vertex_index[0]]->e[2];
|
||||
|
||||
vtx0.uv[0] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[0];
|
||||
vtx0.uv[1] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[1];
|
||||
|
||||
GraphicsVertex vtx1;
|
||||
vtx1.xyzw[0] = obj->vertexList[face->vertex_index[1]]->e[0];
|
||||
vtx1.xyzw[1] = obj->vertexList[face->vertex_index[1]]->e[1];
|
||||
vtx1.xyzw[2] = obj->vertexList[face->vertex_index[1]]->e[2];
|
||||
vtx1.xyzw[3]= 0.f;
|
||||
vtx1.uv[0] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[0];
|
||||
vtx1.uv[1] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[1];
|
||||
|
||||
GraphicsVertex vtx2;
|
||||
vtx2.xyzw[0] = obj->vertexList[face->vertex_index[2]]->e[0];
|
||||
vtx2.xyzw[1] = obj->vertexList[face->vertex_index[2]]->e[1];
|
||||
vtx2.xyzw[2] = obj->vertexList[face->vertex_index[2]]->e[2];
|
||||
vtx2.xyzw[3] = 0.f;
|
||||
vtx2.uv[0] = 0.5f;obj->textureList[face->vertex_index[2]]->e[0];
|
||||
vtx2.uv[1] = 0.5f;obj->textureList[face->vertex_index[2]]->e[1];
|
||||
|
||||
|
||||
btVector3 v0(vtx0.xyzw[0],vtx0.xyzw[1],vtx0.xyzw[2]);
|
||||
btVector3 v1(vtx1.xyzw[0],vtx1.xyzw[1],vtx1.xyzw[2]);
|
||||
btVector3 v2(vtx2.xyzw[0],vtx2.xyzw[1],vtx2.xyzw[2]);
|
||||
|
||||
normal = (v1-v0).cross(v2-v0);
|
||||
normal.normalize();
|
||||
vtx0.normal[0] = normal[0];
|
||||
vtx0.normal[1] = normal[1];
|
||||
vtx0.normal[2] = normal[2];
|
||||
vtx1.normal[0] = normal[0];
|
||||
vtx1.normal[1] = normal[1];
|
||||
vtx1.normal[2] = normal[2];
|
||||
vtx2.normal[0] = normal[0];
|
||||
vtx2.normal[1] = normal[1];
|
||||
vtx2.normal[2] = normal[2];
|
||||
vertices->push_back(vtx0);
|
||||
vertices->push_back(vtx1);
|
||||
vertices->push_back(vtx2);
|
||||
}
|
||||
if (face->vertex_count==4)
|
||||
{
|
||||
|
||||
indicesPtr->push_back(vtxBaseIndex);
|
||||
indicesPtr->push_back(vtxBaseIndex+1);
|
||||
indicesPtr->push_back(vtxBaseIndex+2);
|
||||
indicesPtr->push_back(vtxBaseIndex+3);
|
||||
//
|
||||
GraphicsVertex vtx3;
|
||||
vtx3.xyzw[0] = obj->vertexList[face->vertex_index[3]]->e[0];
|
||||
vtx3.xyzw[1] = obj->vertexList[face->vertex_index[3]]->e[1];
|
||||
vtx3.xyzw[2] = obj->vertexList[face->vertex_index[3]]->e[2];
|
||||
vtx3.uv[0] = 0.5;
|
||||
vtx3.uv[1] = 0.5;
|
||||
|
||||
vtx3.normal[0] = normal[0];
|
||||
vtx3.normal[1] = normal[1];
|
||||
vtx3.normal[2] = normal[2];
|
||||
|
||||
vertices->push_back(vtx3);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GraphicsShape* gfxShape = new GraphicsShape;
|
||||
gfxShape->m_vertices = &vertices->at(0).xyzw[0];
|
||||
gfxShape->m_numvertices = vertices->size();
|
||||
gfxShape->m_indices = &indicesPtr->at(0);
|
||||
gfxShape->m_numIndices = indicesPtr->size();
|
||||
for (int i=0;i<4;i++)
|
||||
gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices
|
||||
return gfxShape;
|
||||
}
|
||||
}
|
||||
|
||||
void ConcaveScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
objLoader* objData = new objLoader();
|
||||
//char* fileName = "data/plane.obj";
|
||||
//char* fileName = "data/teddy.obj";//"plane.obj";
|
||||
char* fileName = "data/sponza_closed.obj";//"plane.obj";
|
||||
|
||||
FILE* f = 0;
|
||||
|
||||
char relativeFileName[1024];
|
||||
{
|
||||
const char* prefix[]={"../","../../","../../../","../../../../"};
|
||||
int numPrefixes = sizeof(prefix)/sizeof(char*);
|
||||
|
||||
for (int i=0;i<numPrefixes;i++)
|
||||
{
|
||||
|
||||
sprintf(relativeFileName,"%s%s",prefix[i],fileName);
|
||||
f = fopen(relativeFileName,"r");
|
||||
if (f)
|
||||
{
|
||||
fclose(f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (f)
|
||||
fclose(f);
|
||||
else
|
||||
return;
|
||||
|
||||
objData->load(relativeFileName);
|
||||
|
||||
GraphicsShape* shape = createGraphicsShapeFromWavefrontObj(objData);
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_vertices)/sizeof(int);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
|
||||
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(shape->m_vertices, shape->m_numvertices, shape->m_indices, shape->m_numIndices);
|
||||
btQuaternion orn(0,0,0,1);
|
||||
|
||||
btVector4 color(0,1,0,1.f);//0.5);
|
||||
btVector4 scaling(1,1,1,1);
|
||||
|
||||
{
|
||||
btVector3 position(0,0,0);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_vertices)/sizeof(int);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=10;
|
||||
float scaling[4] = {1,1,1,1};
|
||||
|
||||
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
|
||||
if (0)
|
||||
{
|
||||
for (int i=0;i<1;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<1;k++)
|
||||
{
|
||||
float mass = j==0? 0.f : 1.f;
|
||||
|
||||
btVector3 position(40+i*ci.gapX,j*ci.gapY,k*ci.gapZ);
|
||||
btQuaternion orn(1,0,0,0);
|
||||
|
||||
btVector4 color(0,1,0,1);
|
||||
btVector4 scaling(1,1,1,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
float camPos[4]={0,0,0,0};//65.5,4.5,65.5,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(16);
|
||||
|
||||
}
|
27
demo/gpudemo/rigidbody/ConcaveScene.h
Normal file
27
demo/gpudemo/rigidbody/ConcaveScene.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef CONCAVE_SCENE_H
|
||||
#define CONCAVE_SCENE_H
|
||||
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
|
||||
class ConcaveScene : public GpuRigidBodyDemo
|
||||
{
|
||||
public:
|
||||
|
||||
ConcaveScene(){}
|
||||
virtual ~ConcaveScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBConcave";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new ConcaveScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
#endif //CONCAVE_SCENE_H
|
61
demo/gpudemo/rigidbody/GpuConvexScene.cpp
Normal file
61
demo/gpudemo/rigidbody/GpuConvexScene.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include "GpuConvexScene.h"
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
#include "BulletCommon/btQuickprof.h"
|
||||
#include "OpenGLWindow/ShapeData.h"
|
||||
|
||||
#include "OpenGLWindow/GLInstancingRenderer.h"
|
||||
#include "BulletCommon/btQuaternion.h"
|
||||
#include "OpenGLWindow/btgWindowInterface.h"
|
||||
#include "gpu_broadphase/host/btGpuSapBroadphase.h"
|
||||
#include "../GpuDemoInternalData.h"
|
||||
#include "basic_initialize/btOpenCLUtils.h"
|
||||
#include "OpenGLWindow/OpenGLInclude.h"
|
||||
#include "OpenGLWindow/GLInstanceRendererInternalData.h"
|
||||
#include "parallel_primitives/host/btLauncherCL.h"
|
||||
#include "gpu_rigidbody/host/btGpuRigidBodyPipeline.h"
|
||||
#include "gpu_rigidbody/host/btGpuNarrowPhase.h"
|
||||
#include "gpu_rigidbody/host/btConfig.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
|
||||
void GpuConvexScene::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_vertices)/sizeof(int);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=10;
|
||||
float scaling[4] = {1,1,1,1};
|
||||
|
||||
int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
|
||||
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
{
|
||||
float mass = j==0? 0.f : 1.f;
|
||||
|
||||
btVector3 position(i*ci.gapX,j*ci.gapY,k*ci.gapZ);
|
||||
btQuaternion orn(1,0,0,0);
|
||||
|
||||
btVector4 color(0,1,0,1);
|
||||
btVector4 scaling(1,1,1,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float camPos[4]={65.5,4.5,65.5,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(90);
|
||||
|
||||
}
|
27
demo/gpudemo/rigidbody/GpuConvexScene.h
Normal file
27
demo/gpudemo/rigidbody/GpuConvexScene.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef GPU_CONVEX_SCENE_H
|
||||
#define GPU_CONVEX_SCENE_H
|
||||
|
||||
#include "GpuRigidBodyDemo.h"
|
||||
|
||||
class GpuConvexScene : public GpuRigidBodyDemo
|
||||
{
|
||||
public:
|
||||
|
||||
GpuConvexScene(){}
|
||||
virtual ~GpuConvexScene(){}
|
||||
virtual const char* getName()
|
||||
{
|
||||
return "GRBConvex";
|
||||
}
|
||||
|
||||
static GpuDemo* MyCreateFunc()
|
||||
{
|
||||
GpuDemo* demo = new GpuConvexScene;
|
||||
return demo;
|
||||
}
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
};
|
||||
|
||||
#endif //GPU_CONVEX_SCENE_H
|
@ -13,6 +13,7 @@
|
||||
#include "gpu_rigidbody/host/btGpuRigidBodyPipeline.h"
|
||||
#include "gpu_rigidbody/host/btGpuNarrowPhase.h"
|
||||
#include "gpu_rigidbody/host/btConfig.h"
|
||||
#include "GpuRigidBodyDemoInternalData.h"
|
||||
|
||||
static btKeyboardCallback oldCallback = 0;
|
||||
extern bool gReset;
|
||||
@ -46,26 +47,7 @@ __kernel void
|
||||
);
|
||||
|
||||
|
||||
struct GpuRigidBodyDemoInternalData
|
||||
{
|
||||
|
||||
cl_kernel m_copyTransformsToVBOKernel;
|
||||
|
||||
btOpenCLArray<btVector4>* m_instancePosOrnColor;
|
||||
|
||||
class btGpuRigidBodyPipeline* m_rigidBodyPipeline;
|
||||
|
||||
btGpuNarrowPhase* m_np;
|
||||
btGpuSapBroadphase* m_bp;
|
||||
|
||||
GpuRigidBodyDemoInternalData()
|
||||
:m_instancePosOrnColor(0),
|
||||
m_copyTransformsToVBOKernel(0), m_rigidBodyPipeline(0),
|
||||
m_np(0),
|
||||
m_bp(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
GpuRigidBodyDemo::GpuRigidBodyDemo()
|
||||
@ -97,11 +79,26 @@ static void PairKeyboardCallback(int key, int state)
|
||||
oldCallback(key,state);
|
||||
}
|
||||
|
||||
void GpuRigidBodyDemo::setupScene(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void GpuRigidBodyDemo::initPhysics(const ConstructionInfo& ci)
|
||||
{
|
||||
|
||||
if (ci.m_window)
|
||||
{
|
||||
m_window = ci.m_window;
|
||||
oldCallback = ci.m_window->getKeyboardCallback();
|
||||
ci.m_window->setKeyboardCallback(PairKeyboardCallback);
|
||||
|
||||
}
|
||||
|
||||
m_instancingRenderer = ci.m_instancingRenderer;
|
||||
|
||||
initCL(ci.preferredOpenCLDeviceIndex,ci.preferredOpenCLPlatformIndex);
|
||||
|
||||
if (m_clData->m_clContext)
|
||||
{
|
||||
int errNum=0;
|
||||
@ -117,62 +114,13 @@ void GpuRigidBodyDemo::initPhysics(const ConstructionInfo& ci)
|
||||
|
||||
m_data->m_rigidBodyPipeline = new btGpuRigidBodyPipeline(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue, np, bp);
|
||||
|
||||
int strideInBytes = 9*sizeof(float);
|
||||
int numVertices = sizeof(cube_vertices)/strideInBytes;
|
||||
int numIndices = sizeof(cube_vertices)/sizeof(int);
|
||||
//int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
|
||||
int group=1;
|
||||
int mask=1;
|
||||
int index=10;
|
||||
float scaling[4] = {1,1,1,1};
|
||||
|
||||
int colIndex = np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
|
||||
setupScene(ci);
|
||||
|
||||
|
||||
for (int i=0;i<ci.arraySizeX;i++)
|
||||
{
|
||||
for (int j=0;j<ci.arraySizeY;j++)
|
||||
{
|
||||
for (int k=0;k<ci.arraySizeZ;k++)
|
||||
{
|
||||
float mass = j==0? 0.f : 1.f;
|
||||
|
||||
btVector3 position(i*ci.gapX,j*ci.gapY,k*ci.gapZ);
|
||||
btQuaternion orn(1,0,0,0);
|
||||
|
||||
btVector4 color(0,1,0,1);
|
||||
btVector4 scaling(1,1,1,1);
|
||||
int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
|
||||
int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index);
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
np->writeAllBodiesToGpu();
|
||||
bp->writeAabbsToGpu();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (ci.m_window)
|
||||
{
|
||||
m_window = ci.m_window;
|
||||
oldCallback = ci.m_window->getKeyboardCallback();
|
||||
ci.m_window->setKeyboardCallback(PairKeyboardCallback);
|
||||
|
||||
}
|
||||
|
||||
m_instancingRenderer = ci.m_instancingRenderer;
|
||||
|
||||
|
||||
float camPos[4]={65.5,4.5,65.5,0};
|
||||
//float camPos[4]={1,12.5,1.5,0};
|
||||
m_instancingRenderer->setCameraTargetPosition(camPos);
|
||||
m_instancingRenderer->setCameraDistance(90);
|
||||
|
||||
m_instancingRenderer->writeTransforms();
|
||||
|
||||
@ -204,9 +152,10 @@ void GpuRigidBodyDemo::renderScene()
|
||||
void GpuRigidBodyDemo::clientMoveAndDisplay()
|
||||
{
|
||||
bool animate=true;
|
||||
int numObjects= m_instancingRenderer->getInternalData()->m_totalNumInstances;
|
||||
int numObjects= m_data->m_rigidBodyPipeline->getNumBodies();
|
||||
//m_instancingRenderer->getInternalData()->m_totalNumInstances;
|
||||
btVector4* positions = 0;
|
||||
if (animate)
|
||||
if (animate && numObjects)
|
||||
{
|
||||
GLuint vbo = m_instancingRenderer->getInternalData()->m_vbo;
|
||||
int arraySizeInBytes = numObjects * (3)*sizeof(btVector4);
|
||||
@ -222,9 +171,10 @@ void GpuRigidBodyDemo::clientMoveAndDisplay()
|
||||
m_data->m_instancePosOrnColor->copyFromHostPointer(positions,3*numObjects,0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
m_data->m_rigidBodyPipeline->stepSimulation(1./60.f);
|
||||
|
||||
if (numObjects)
|
||||
{
|
||||
int ciErrNum = 0;
|
||||
cl_mem bodies = m_data->m_rigidBodyPipeline->getBodyBuffer();
|
||||
@ -236,7 +186,7 @@ void GpuRigidBodyDemo::clientMoveAndDisplay()
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
}
|
||||
|
||||
if (animate)
|
||||
if (animate && numObjects)
|
||||
{
|
||||
GLint err = glGetError();
|
||||
assert(err==GL_NO_ERROR);
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
class GpuRigidBodyDemo : public GpuDemo
|
||||
{
|
||||
|
||||
protected:
|
||||
class GLInstancingRenderer* m_instancingRenderer;
|
||||
class btgWindowInterface* m_window;
|
||||
|
||||
@ -17,7 +17,11 @@ public:
|
||||
virtual ~GpuRigidBodyDemo();
|
||||
|
||||
virtual void initPhysics(const ConstructionInfo& ci);
|
||||
|
||||
|
||||
virtual void setupScene(const ConstructionInfo& ci);
|
||||
|
||||
virtual void destroyScene(){};
|
||||
|
||||
virtual void exitPhysics();
|
||||
|
||||
virtual const char* getName()
|
||||
@ -30,8 +34,6 @@ public:
|
||||
return demo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
virtual void renderScene();
|
||||
|
||||
virtual void clientMoveAndDisplay();
|
||||
|
30
demo/gpudemo/rigidbody/GpuRigidBodyDemoInternalData.h
Normal file
30
demo/gpudemo/rigidbody/GpuRigidBodyDemoInternalData.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef GPU_RIGIDBODY_INTERNAL_DATA_H
|
||||
#define GPU_RIGIDBODY_INTERNAL_DATA_H
|
||||
|
||||
#include "basic_initialize/btOpenCLUtils.h"
|
||||
#include "parallel_primitives/host/btOpenCLArray.h"
|
||||
#include "BulletCommon/btVector3.h"
|
||||
|
||||
struct GpuRigidBodyDemoInternalData
|
||||
{
|
||||
|
||||
cl_kernel m_copyTransformsToVBOKernel;
|
||||
|
||||
btOpenCLArray<btVector4>* m_instancePosOrnColor;
|
||||
|
||||
class btGpuRigidBodyPipeline* m_rigidBodyPipeline;
|
||||
|
||||
class btGpuNarrowPhase* m_np;
|
||||
class btGpuSapBroadphase* m_bp;
|
||||
|
||||
GpuRigidBodyDemoInternalData()
|
||||
:m_instancePosOrnColor(0),
|
||||
m_copyTransformsToVBOKernel(0), m_rigidBodyPipeline(0),
|
||||
m_np(0),
|
||||
m_bp(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
#endif//GPU_RIGIDBODY_INTERNAL_DATA_H
|
||||
|
@ -205,18 +205,21 @@ void btGpuSapBroadphase::calculateOverlappingPairs(bool forceHost)
|
||||
{
|
||||
{
|
||||
int numSmallAabbs = m_smallAabbsGPU.size();
|
||||
BT_PROFILE("copyAabbsKernelSmall");
|
||||
btBufferInfoCL bInfo[] = {
|
||||
btBufferInfoCL( m_allAabbsGPU.getBufferCL(), true ),
|
||||
btBufferInfoCL( m_smallAabbsGPU.getBufferCL()),
|
||||
};
|
||||
if (numSmallAabbs)
|
||||
{
|
||||
BT_PROFILE("copyAabbsKernelSmall");
|
||||
btBufferInfoCL bInfo[] = {
|
||||
btBufferInfoCL( m_allAabbsGPU.getBufferCL(), true ),
|
||||
btBufferInfoCL( m_smallAabbsGPU.getBufferCL()),
|
||||
};
|
||||
|
||||
btLauncherCL launcher(m_queue, m_copyAabbsKernel );
|
||||
launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(btBufferInfoCL) );
|
||||
launcher.setConst( numSmallAabbs );
|
||||
int num = numSmallAabbs;
|
||||
launcher.launch1D( num);
|
||||
clFinish(m_queue);
|
||||
btLauncherCL launcher(m_queue, m_copyAabbsKernel );
|
||||
launcher.setBuffers( bInfo, sizeof(bInfo)/sizeof(btBufferInfoCL) );
|
||||
launcher.setConst( numSmallAabbs );
|
||||
int num = numSmallAabbs;
|
||||
launcher.launch1D( num);
|
||||
clFinish(m_queue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -157,6 +157,9 @@ void btGpuRigidBodyPipeline::setupGpuAabbsFull()
|
||||
cl_int ciErrNum=0;
|
||||
|
||||
int numBodies = m_data->m_narrowphase->getNumBodiesGpu();
|
||||
if (!numBodies)
|
||||
return;
|
||||
|
||||
//__kernel void initializeGpuAabbsFull( const int numNodes, __global Body* gBodies,__global Collidable* collidables, __global btAABBCL* plocalShapeAABB, __global btAABBCL* pAABB)
|
||||
btLauncherCL launcher(m_data->m_queue,m_data->m_updateAabbsKernel);
|
||||
launcher.setConst(numBodies);
|
||||
|
Loading…
Reference in New Issue
Block a user