Skip to content
Snippets Groups Projects
Commit 6c5a5c29 authored by Taddeüs Kroes's avatar Taddeüs Kroes
Browse files

- Added graphics assignment 9.

parent ec5c92d4
No related branches found
No related tags found
No related merge requests found
Showing
with 963 additions and 0 deletions
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
CC=gcc
WARNING_FLAGS=-Wall -Wextra -Werror-implicit-function-declaration -Wshadow -Wstrict-prototypes -pedantic-errors
CFLAGS=-g -O2 -std=c99 $(WARNING_FLAGS)
LDFLAGS=-g -lGL -lglut -lGLU
.c.o:
$(CC) -c $(CFLAGS) $<
all: main
main: main.o marching_tetrahedra.o volume.o
$(CC) $(LDFLAGS) -o main main.o marching_tetrahedra.o volume.o
clean:
rm -f *.o
rm -f main
volume.o : v3math.h volume.h
volume.o : volume.h volume.c
v3math.o : v3math.h
marching_tetrahedra.o : marching_tetrahedra.h marching_tetrahedra.c
marching_tetrahedra.o : v3math.h volume.h marching_tetrahedra.h
main.o : marching_tetrahedra.h v3math.h volume.h main.c
File added
This diff is collapsed.
/* Computer Graphics, Assignment, Volume rendering with cubes/points/isosurface
*
* Student name ....
* Student email ...
* Collegekaart ....
* Date ............
* Comments ........
*
* (always fill in these fields before submitting!!)
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "marching_tetrahedra.h"
/* Compute a linearly interpolated position where an isosurface cuts
an edge between two vertices (p1 and p2), each with their own
scalar value (v1 and v2) */
static vec3
interpolate_points(unsigned char isovalue, vec3 p1, vec3 p2,
unsigned char v1, unsigned char v2)
{
/* Initially, simply return the midpoint between p1 and p2.
So no real interpolation is done yet */
return v3_add(v3_multiply(p1, 0.5), v3_multiply(p2, 0.5));
}
/* Using the given iso-value generate triangles for the tetrahedron
defined by corner vertices v0, v1, v2, v3 of cell c.
Store the resulting triangles in the "triangles" array.
Return the number of triangles created (either 0, 1, or 2).
Note: the output array "triangles" should have space for at least
2 triangles.
*/
static int
generate_tetrahedron_triangles(triangle *triangles, unsigned char isovalue,
cell c, int v0, int v1, int v2, int v3)
{
unsigned char *v = c.value;
vec3 *p = triangles[0].p;
int bitsystem = 0;
if(c.value[v0] > isovalue) bitsystem += 1;
if(c.value[v1] > isovalue) bitsystem += 2;
if(c.value[v2] > isovalue) bitsystem += 4;
if(c.value[v3] > isovalue) bitsystem += 8;
switch( bitsystem )
{
case 1: case 14: // 0001
p[0] = interpolate_points(isovalue, c.p[v0], c.p[v1], v[v0], v[v1]);
p[1] = interpolate_points(isovalue, c.p[v0], c.p[v2], v[v0], v[v2]);
p[2] = interpolate_points(isovalue, c.p[v0], c.p[v3], v[v0], v[v3]);
return 1;
case 2: case 13: // 0010
p[0] = interpolate_points(isovalue, c.p[v1], c.p[v0], v[v1], v[v0]);
p[1] = interpolate_points(isovalue, c.p[v1], c.p[v3], v[v1], v[v3]);
p[2] = interpolate_points(isovalue, c.p[v1], c.p[v2], v[v1], v[v2]);
return 1;
case 4: case 11: // 0100
p[0] = interpolate_points(isovalue, c.p[v2], c.p[v0], v[v2], v[v0]);
p[1] = interpolate_points(isovalue, c.p[v2], c.p[v1], v[v2], v[v1]);
p[2] = interpolate_points(isovalue, c.p[v2], c.p[v3], v[v2], v[v3]);
return 1;
case 8: case 7: // 1000
p[0] = interpolate_points(isovalue, c.p[v3], c.p[v0], v[v3], v[v0]);
p[1] = interpolate_points(isovalue, c.p[v3], c.p[v2], v[v3], v[v2]);
p[2] = interpolate_points(isovalue, c.p[v3], c.p[v1], v[v3], v[v1]);
return 1;
case 5: case 10: // 0101
p[0] = interpolate_points(isovalue, c.p[v0], c.p[v1], v[v0], v[v1]);
p[1] = interpolate_points(isovalue, c.p[v2], c.p[v3], v[v2], v[v3]);
p[2] = interpolate_points(isovalue, c.p[v0], c.p[v3], v[v0], v[v3]);
p = triangles[1].p;
p[0] = interpolate_points(isovalue, c.p[v0], c.p[v1], v[v0], v[v1]);
p[1] = interpolate_points(isovalue, c.p[v1], c.p[v2], v[v1], v[v2]);
p[2] = interpolate_points(isovalue, c.p[v2], c.p[v3], v[v2], v[v3]);
return 2;
case 3: case 12: // 0011
p[0] = interpolate_points(isovalue, c.p[v0], c.p[v3], v[v0], v[v3]);
p[1] = interpolate_points(isovalue, c.p[v0], c.p[v2], v[v0], v[v2]);
p[2] = interpolate_points(isovalue, c.p[v1], c.p[v3], v[v1], v[v3]);
p = triangles[1].p;
p[0] = interpolate_points(isovalue, c.p[v1], c.p[v3], v[v1], v[v3]);
p[2] = interpolate_points(isovalue, c.p[v1], c.p[v2], v[v1], v[v2]);
p[1] = interpolate_points(isovalue, c.p[v0], c.p[v2], v[v0], v[v2]);
return 2;
case 6: case 9: // 0110
p[0] = interpolate_points(isovalue, c.p[v0], c.p[v1], v[v0], v[v1]);
p[2] = interpolate_points(isovalue, c.p[v1], c.p[v3], v[v1], v[v3]);
p[1] = interpolate_points(isovalue, c.p[v2], c.p[v3], v[v2], v[v3]);
p = triangles[1].p;
p[0] = interpolate_points(isovalue, c.p[v0], c.p[v1], v[v0], v[v1]);
p[1] = interpolate_points(isovalue, c.p[v0], c.p[v2], v[v0], v[v2]);
p[2] = interpolate_points(isovalue, c.p[v2], c.p[v3], v[v2], v[v3]);
return 2;
default: // 0000
return 0;
}
}
/* Generate triangles for a single cell for the given iso-value. This function
should produce at most 6 * 2 triangles (for which the "triangles" array
should have enough space).
Use calls to generate_tetrahedron_triangles().
Return the number of triangles produced
*/
int
generate_cell_triangles(triangle *triangles, cell c, unsigned char isovalue)
{
const int points[24] = {0,1,3,7,0,2,6,7,0,1,5,7,0,2,3,7,0,4,5,7,0,4,6,7};
int i, tri_count = 0;
for( i = 0; i < 21; i += 4 )
{
tri_count += generate_tetrahedron_triangles(triangles + tri_count,
isovalue, c, points[i], points[i+1], points[i+2], points[i+3]);
}
return tri_count;
}
#ifndef MARCHING_TETRAHEDRA_H
#define MARCHING_TETRAHEDRA_H
#include "v3math.h"
#include "volume.h"
typedef struct
{
vec3 p[3];
vec3 n[3];
}
triangle;
int generate_cell_triangles(triangle *triangles, cell c, unsigned char isovalue);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment