change scene graph syntax

This commit is contained in:
Luca Conte 2024-06-19 17:34:39 +02:00
parent 70c611d0d6
commit 5926b21cd4
3 changed files with 53 additions and 37 deletions

View File

@ -35,7 +35,7 @@ name myCrate1
# Position des Objekts # Position des Objekts
# relativ zur Position des Parent Elements # relativ zur Position des Parent Elements
position 0.0 0.0 2.0 translate 0.0 0.0 2.0
# Skalierung des Objekts # Skalierung des Objekts
# relativ zur Skalierung des Parent Elements # relativ zur Skalierung des Parent Elements
@ -43,16 +43,16 @@ scale 1.0 1.0 1.0
# rotationen # rotationen
rotationX 0.0 rotateX 0.0
rotationY 1.5 rotateY 1.5
rotationZ 0.0 rotateZ 0.0
obj 1 obj 1
use 0 use 0
name myCrate2 name myCrate2
position 0.0 1.0 0.0 translate 0.0 1.0 0.0
scale 0.5 0.5 0.5 scale 0.5 0.5 0.5
# Definition des Parent Objekts # Definition des Parent Objekts
parent 0 parent 0
@ -62,7 +62,7 @@ obj 2
use 1 use 1
name Sonne name Sonne
#texture ../texture/pb.png #texture ../texture/pb.png
position 0.0 0.0 0.0 translate 0.0 0.0 0.0
scale 2.0 2.0 2.0 scale 2.0 2.0 2.0
@ -70,7 +70,7 @@ obj 3
use 1 use 1
name Erde name Erde
#texture ../texture/earth/day.png #texture ../texture/earth/day.png
position 4.0 0.0 0.0 translate 4.0 0.0 0.0
scale 1.0 1.0 1.0 scale 1.0 1.0 1.0
parent 2 parent 2
@ -79,6 +79,6 @@ obj 4
use 1 use 1
name Mond name Mond
#texture ../texture/checkerboard.png #texture ../texture/checkerboard.png
position 2.0 0.0 0.0 translate 2.0 0.0 0.0
scale 0.5 0.5 0.5 scale 0.5 0.5 0.5
parent 3 parent 3

View File

@ -14,6 +14,22 @@
#define SCG_LINE_BUFFER_SIZE 1024 #define SCG_LINE_BUFFER_SIZE 1024
#define SCG_KEYWORD_BUFFER_SIZE 32 #define SCG_KEYWORD_BUFFER_SIZE 32
#define KEYWORD_DEFINE_MODEL "model"
#define KEYWORD_DEFINE_NODE "obj"
#define KEYWORD_DEFINE_OBJ_FILE "file"
#define KEYWORD_DEFINE_TEXTURE_FILE "texture"
#define KEYWORD_USE_MODEL "use"
#define KEYWORD_DEFINE_PARENT "parent"
#define KEYWORD_DEFINE_NAME "name"
#define KEYWORD_SCALE "scale"
#define KEYWORD_TRANSLATE "translate"
#define KEYWORD_ROTATE_X "rotateX"
#define KEYWORD_ROTATE_Y "rotateY"
#define KEYWORD_ROTATE_Z "rotateZ"
void (*renderFunction)(SceneNode*); void (*renderFunction)(SceneNode*);
void setNodeRenderFunction(void (*newRenderFunction)(SceneNode*)) { void setNodeRenderFunction(void (*newRenderFunction)(SceneNode*)) {
@ -141,9 +157,9 @@ SceneNode* loadSceneGraphFromFile(char* path) {
sscanf(buf, keywordBufferFormat, keyword); sscanf(buf, keywordBufferFormat, keyword);
// printf("%s\n", keyword); // printf("%s\n", keyword);
if (strcmp(keyword, "model") == 0) { if (strcmp(keyword, KEYWORD_DEFINE_MODEL) == 0) {
int modelId = 0; int modelId = 0;
sscanf(buf, "model %d", &modelId); sscanf(buf, KEYWORD_DEFINE_MODEL " %d", &modelId);
if (modelId > maxModelId) { if (modelId > maxModelId) {
maxModelId = modelId; maxModelId = modelId;
} }
@ -167,23 +183,23 @@ SceneNode* loadSceneGraphFromFile(char* path) {
if (buf[0] == '\r' || buf[0] == '\n' || buf[0] == '\0') continue; if (buf[0] == '\r' || buf[0] == '\n' || buf[0] == '\0') continue;
sscanf(buf, keywordBufferFormat, keyword); sscanf(buf, keywordBufferFormat, keyword);
if (strcmp(keyword, "model") == 0) { if (strcmp(keyword, KEYWORD_DEFINE_MODEL) == 0) {
sscanf(buf, "model %d", &currentModel); sscanf(buf, KEYWORD_DEFINE_MODEL " %d", &currentModel);
continue; continue;
} }
if (strcmp(keyword, "file") == 0) { if (strcmp(keyword, KEYWORD_DEFINE_OBJ_FILE) == 0) {
sscanf(buf, "file %s", filepathBuffer); sscanf(buf, KEYWORD_DEFINE_OBJ_FILE " %s", filepathBuffer);
models[currentModel].objectData = readSingleObjFile(filepathBuffer); models[currentModel].objectData = readSingleObjFile(filepathBuffer);
continue; continue;
} }
if (strcmp(keyword, "texture") == 0) { if (strcmp(keyword, KEYWORD_DEFINE_TEXTURE_FILE) == 0) {
sscanf(buf, "texture %s", filepathBuffer); sscanf(buf, KEYWORD_DEFINE_TEXTURE_FILE " %s", filepathBuffer);
loadTexture(filepathBuffer, &models[currentModel].texture); loadTexture(filepathBuffer, &models[currentModel].texture);
continue; continue;
} }
if (strcmp(keyword, "obj") == 0) { if (strcmp(keyword, KEYWORD_DEFINE_NODE) == 0) {
if (currentNode && !currentNodeHasParent) { if (currentNode && !currentNodeHasParent) {
addChild(root, currentNode); addChild(root, currentNode);
@ -193,7 +209,7 @@ SceneNode* loadSceneGraphFromFile(char* path) {
currentNodeHasParent = false; currentNodeHasParent = false;
int nodeId = 0; int nodeId = 0;
sscanf(buf, "obj %d", &nodeId); sscanf(buf, KEYWORD_DEFINE_NODE " %d", &nodeId);
if (findNode(nodeId, root)) { if (findNode(nodeId, root)) {
fprintf(stderr, "redeclaration of objet with id %d - line %d\n", nodeId, currentLine); fprintf(stderr, "redeclaration of objet with id %d - line %d\n", nodeId, currentLine);
return NULL; return NULL;
@ -201,19 +217,19 @@ SceneNode* loadSceneGraphFromFile(char* path) {
currentNode = createSceneNode(nodeId); currentNode = createSceneNode(nodeId);
continue; continue;
} }
if (strcmp(keyword, "name") == 0) { if (strcmp(keyword, KEYWORD_DEFINE_NAME) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected, can't assign name - line %d\n", currentLine); fprintf(stderr, "no node selected, can't assign name - line %d\n", currentLine);
return NULL; return NULL;
} }
sscanf(buf, "name %s", filepathBuffer); sscanf(buf, KEYWORD_DEFINE_NAME " %s", filepathBuffer);
char* name = (char*)malloc(strlen(filepathBuffer) * sizeof(char) + 1); char* name = (char*)malloc(strlen(filepathBuffer) * sizeof(char) + 1);
strcpy(name, filepathBuffer); strcpy(name, filepathBuffer);
currentNode->name = name; currentNode->name = name;
continue; continue;
} }
if (strcmp(keyword, "parent") == 0) { if (strcmp(keyword, KEYWORD_DEFINE_PARENT) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected, can't assign parent - line %d\n", currentLine); fprintf(stderr, "no node selected, can't assign parent - line %d\n", currentLine);
return NULL; return NULL;
@ -224,7 +240,7 @@ SceneNode* loadSceneGraphFromFile(char* path) {
} }
int parentId = 0; int parentId = 0;
sscanf(buf, "parent %d", &parentId); sscanf(buf, KEYWORD_DEFINE_PARENT " %d", &parentId);
SceneNode* parent = findNode(parentId, root); SceneNode* parent = findNode(parentId, root);
if (!parent) { if (!parent) {
@ -238,14 +254,14 @@ SceneNode* loadSceneGraphFromFile(char* path) {
continue; continue;
} }
if (strcmp(keyword, "use") == 0) { if (strcmp(keyword, KEYWORD_USE_MODEL) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected to assign model to - line %d\n", currentLine); fprintf(stderr, "no node selected to assign model to - line %d\n", currentLine);
return NULL; return NULL;
} }
int usedModel = 0; int usedModel = 0;
sscanf(buf, "use %d", &usedModel); sscanf(buf, KEYWORD_USE_MODEL " %d", &usedModel);
if (usedModel > maxModelId || usedModel < 0) { if (usedModel > maxModelId || usedModel < 0) {
fprintf(stderr, "model with id %d not found - line %d\n", usedModel, currentLine); fprintf(stderr, "model with id %d not found - line %d\n", usedModel, currentLine);
@ -255,51 +271,51 @@ SceneNode* loadSceneGraphFromFile(char* path) {
currentNode->model = &models[usedModel]; currentNode->model = &models[usedModel];
} }
if (strcmp(keyword, "position") == 0) { if (strcmp(keyword, KEYWORD_TRANSLATE) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected to assign position to - line %d\n", currentLine); fprintf(stderr, "no node selected to assign position to - line %d\n", currentLine);
return NULL; return NULL;
} }
vec3 translation; vec3 translation;
sscanf(buf, "position %f %f %f", &translation.x, &translation.y, &translation.z); sscanf(buf, KEYWORD_TRANSLATE " %f %f %f", &translation.x, &translation.y, &translation.z);
translate(&currentNode->transformation, &currentNode->transformation, &translation); translate(&currentNode->transformation, &currentNode->transformation, &translation);
} }
if (strcmp(keyword, "scale") == 0) { if (strcmp(keyword, KEYWORD_SCALE) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected to assign scale to - line %d\n", currentLine); fprintf(stderr, "no node selected to assign scale to - line %d\n", currentLine);
return NULL; return NULL;
} }
vec3 translation; vec3 translation;
sscanf(buf, "scale %f %f %f", &translation.x, &translation.y, &translation.z); sscanf(buf, KEYWORD_SCALE " %f %f %f", &translation.x, &translation.y, &translation.z);
scale(&currentNode->transformation, &currentNode->transformation, &translation); scale(&currentNode->transformation, &currentNode->transformation, &translation);
} }
if (strcmp(keyword, "rotationX") == 0) { if (strcmp(keyword, KEYWORD_ROTATE_X) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected to assign rotationX to - line %d\n", currentLine); fprintf(stderr, "no node selected to assign rotationX to - line %d\n", currentLine);
return NULL; return NULL;
} }
GLfloat angle; GLfloat angle;
sscanf(buf, "rotationX %f", &angle); sscanf(buf, KEYWORD_ROTATE_X " %f", &angle);
rotateX(&currentNode->transformation, &currentNode->transformation, angle); rotateX(&currentNode->transformation, &currentNode->transformation, angle);
} }
if (strcmp(keyword, "rotationY") == 0) { if (strcmp(keyword, KEYWORD_ROTATE_Y) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected to assign rotationY to - line %d\n", currentLine); fprintf(stderr, "no node selected to assign rotationY to - line %d\n", currentLine);
return NULL; return NULL;
} }
GLfloat angle; GLfloat angle;
sscanf(buf, "rotationY %f", &angle); sscanf(buf, KEYWORD_ROTATE_Y " %f", &angle);
rotateY(&currentNode->transformation, &currentNode->transformation, angle); rotateY(&currentNode->transformation, &currentNode->transformation, angle);
} }
if (strcmp(keyword, "rotationZ") == 0) { if (strcmp(keyword, KEYWORD_ROTATE_Z) == 0) {
if (!currentNode) { if (!currentNode) {
fprintf(stderr, "no node selected to assign rotationZ to - line %d\n", currentLine); fprintf(stderr, "no node selected to assign rotationZ to - line %d\n", currentLine);
return NULL; return NULL;
} }
GLfloat angle; GLfloat angle;
sscanf(buf, "rotationZ %f", &angle); sscanf(buf, KEYWORD_ROTATE_Z " %f", &angle);
rotateZ(&currentNode->transformation, &currentNode->transformation, angle); rotateZ(&currentNode->transformation, &currentNode->transformation, angle);
} }
} }

View File

@ -10,17 +10,17 @@ name box1
obj 1 obj 1
parent 0 parent 0
scale 0.2 0.8 0.2 scale 0.2 0.8 0.2
position 0.5 0.0 0.0 translate 0.5 0.0 0.0
name box2 name box2
obj 2 obj 2
parent 1 parent 1
scale 0.3 0.6 0.3 scale 0.3 0.6 0.3
position 0.5 0.0 0.0 translate 0.5 0.0 0.0
name box3 name box3
obj 3 obj 3
parent 2 parent 2
scale 0.4 0.4 0.4 scale 0.4 0.4 0.4
position 0.5 0.0 0.0 translate 0.5 0.0 0.0
name box4 name box4