⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object.c

📁 allows the user to select files containing objects and draw them using one of the available project
💻 C
字号:
#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "object.h"

/* Buffer size for reading numbers */
#define MAX_BUFF 40

/*
 * Creates an object.
 */
OBJECT_3D *ObjectCreate (int cCols, int cRows)
{
  OBJECT_3D *pObject;
  int x;

  assert (cCols >= 0 || cRows >= 0);
  
  
  pObject = (OBJECT_3D *) malloc(sizeof(OBJECT_3D));
  if (pObject == NULL)
    return NULL;
  
  pObject->ppPoints = (POINT_3D **) malloc(sizeof(POINT_3D) * cCols);
  if (pObject->ppPoints == NULL) {
    free (pObject);
    return NULL;
  }

  pObject->cCols = cCols;
  pObject->cRows = cRows;
  
  /* Initializes the pointers */
  for (x = 0; x < cCols; x++)
    pObject->ppPoints [x] = (POINT_3D *) malloc (sizeof(POINT_3D) * cRows);
  
  return pObject;
}

/*
 * Multiplies the object position by the matrix,
 * returning a new copy.
 */
POBJECT_3D ObjectMultiply (const POBJECT_3D pObject, const MATRIX_4x4 pMatrix)
{
  POBJECT_3D pNewObject;
  int x, y;
  
  assert (pMatrix != NULL && pObject != NULL);

  pNewObject = ObjectCreate (pObject->cCols, pObject->cRows);   
  if (pNewObject == NULL)
    return NULL;

  for (x = 0; x < pObject->cCols; x++)
    for (y = 0; y < pObject->cRows; y++) {
      MatrixMultiplyPoint (pMatrix, &pObject->ppPoints[x][y], &pNewObject->ppPoints[x][y]);

      pNewObject->ppPoints[x][y].x /= pNewObject->ppPoints[x][y].w;
      pNewObject->ppPoints[x][y].y /= pNewObject->ppPoints[x][y].w;
      pNewObject->ppPoints[x][y].z /= pNewObject->ppPoints[x][y].w;
      pNewObject->ppPoints[x][y].w = 1;
    }

  return pNewObject;
}

/*
 *  Loads an 3D object from a file.
 *  Returns NULL on error.
 */
OBJECT_3D *ObjectLoad(const char *pszFileName)
{
  FILE * fd;
  int cCols, cRows, nConnect;
  OBJECT_3D *pObject;
  char szBuff [MAX_BUFF];
  int x, y;

  assert (pszFileName != NULL);
  
  fd = fopen (pszFileName, "r");
  if (fd == NULL)
    return NULL;

  fscanf (fd, "%d\n",&cRows);
  fscanf (fd, "%d\n",&cCols);
  fscanf (fd, "%d\n",&nConnect);
  pObject = (OBJECT_3D *) malloc(sizeof(OBJECT_3D));
  if (pObject == NULL) {
    fclose (fd);
    return NULL;
  }

  pObject->cCols = cCols + nConnect;
  pObject->cRows = cRows;
  
  pObject->ppPoints = (POINT_3D **) malloc(sizeof(POINT_3D) * pObject->cCols);
  if (pObject->ppPoints == NULL) {
    free (pObject);
    fclose (fd);
    return NULL;
  }

  
  /* Initializes the pointers */
  for (x = 0; x < pObject->cCols; x++)
    pObject->ppPoints [x] = (POINT_3D *) malloc (sizeof(POINT_3D) * cRows);
  

  for (y = 0; y < cRows; y++)
    for (x = 0; x < cCols; x++) {
      fscanf (fd, "%s", szBuff);
      pObject->ppPoints [x][y].x = atof (szBuff);
    
      fscanf (fd, "%s", szBuff);
      pObject->ppPoints [x][y].y = atof (szBuff);

      fscanf (fd, "%s", szBuff);
      pObject->ppPoints [x][y].z = atof (szBuff);

      pObject->ppPoints [x][y].w = 1;
    }

  if (nConnect == 1)
   for (y = 0; y < cRows; y++)
     pObject->ppPoints [cCols][y] = pObject->ppPoints [0][y];
    

  fclose (fd);
  return pObject;
}


/*
 * Destroys the object.
 */
void ObjectFree (OBJECT_3D *pObject)
{
  int x;

  assert (pObject != NULL);

  for (x = 0; x < pObject->cCols; x++)
    free (pObject->ppPoints [x]);

  free (pObject);
}


/*
 *  Shows the object definition.
 */
void ObjectPrint (const OBJECT_3D *pObject, const char *pszFileName)
{
  int x, y;
  FILE *fd;

  assert (pObject != NULL && pszFileName != NULL);

  fd = fopen (pszFileName, "wt");
  if (fd == NULL)
    return;

  fprintf (fd, "%d\n", pObject->cRows);
  fprintf (fd, "%d\n", pObject->cCols);
  
  for (y = 0; y < pObject->cRows; y++)
    for (x = 0; x < pObject->cCols; x++)
      fprintf (fd, "%.12g %.12g %.12g\n", pObject->ppPoints [x][y].x, 
                                          pObject->ppPoints [x][y].y,
                                          pObject->ppPoints [x][y].z);

  fclose (fd);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -