From 1b4c2367d6ee9a886949eaa64feec929cf8e387d Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Wed, 6 Jun 2012 07:59:35 +0100 Subject: [PATCH] libc3: remerged Submodules are a bit sensitive, so I'm remerging this and will use 'git subtree' instead to split libc3 development Signed-off-by: Michel Pollet --- examples/shared/libc3/Makefile | 15 +- examples/shared/libc3/README.md | 46 ++- examples/shared/libc3/doc/libc3-flowchart.dot | 220 +++++++++++++ examples/shared/libc3/src/c3.c | 14 +- examples/shared/libc3/src/c3.h | 11 +- examples/shared/libc3/src/c3algebra.c | 16 +- examples/shared/libc3/src/c3algebra.h | 11 +- examples/shared/libc3/src/c3arcball.c | 8 +- examples/shared/libc3/src/c3arcball.h | 8 +- examples/shared/libc3/src/c3cairo.c | 8 +- examples/shared/libc3/src/c3cairo.h | 8 +- examples/shared/libc3/src/c3camera.c | 8 +- examples/shared/libc3/src/c3camera.h | 8 +- examples/shared/libc3/src/c3context.c | 8 +- examples/shared/libc3/src/c3context.h | 8 +- examples/shared/libc3/src/c3driver.h | 8 +- examples/shared/libc3/src/c3driver_context.h | 8 +- examples/shared/libc3/src/c3driver_geometry.h | 8 +- examples/shared/libc3/src/c3driver_object.h | 8 +- examples/shared/libc3/src/c3geometry.c | 90 +++++- examples/shared/libc3/src/c3geometry.h | 64 ++-- examples/shared/libc3/src/c3lines.c | 8 +- examples/shared/libc3/src/c3lines.h | 15 +- examples/shared/libc3/src/c3object.c | 8 +- examples/shared/libc3/src/c3object.h | 8 +- examples/shared/libc3/src/c3pixels.c | 8 +- examples/shared/libc3/src/c3pixels.h | 12 +- examples/shared/libc3/src/c3program.c | 20 +- examples/shared/libc3/src/c3program.h | 23 +- examples/shared/libc3/src/c3quaternion.c | 8 +- examples/shared/libc3/src/c3quaternion.h | 8 +- examples/shared/libc3/src/c3sphere.c | 67 ++++ examples/shared/libc3/src/c3sphere.h | 40 +++ examples/shared/libc3/src/c3stl.c | 9 +- examples/shared/libc3/src/c3stl.h | 12 +- examples/shared/libc3/src/c3texture.c | 8 +- examples/shared/libc3/src/c3texture.h | 14 +- examples/shared/libc3/src/c3transform.c | 8 +- examples/shared/libc3/src/c3transform.h | 8 +- examples/shared/libc3/src/c3types.h | 45 +++ examples/shared/libc3/src/c_utils.c | 8 +- examples/shared/libc3/srcgl/c3gl.c | 296 ++++++++++++++++++ examples/shared/libc3/srcgl/c3gl.h | 29 ++ 43 files changed, 1063 insertions(+), 182 deletions(-) create mode 100644 examples/shared/libc3/doc/libc3-flowchart.dot create mode 100644 examples/shared/libc3/src/c3sphere.c create mode 100644 examples/shared/libc3/src/c3sphere.h create mode 100644 examples/shared/libc3/src/c3types.h create mode 100644 examples/shared/libc3/srcgl/c3gl.c create mode 100644 examples/shared/libc3/srcgl/c3gl.h diff --git a/examples/shared/libc3/Makefile b/examples/shared/libc3/Makefile index 7f4320f..2c64f15 100644 --- a/examples/shared/libc3/Makefile +++ b/examples/shared/libc3/Makefile @@ -5,13 +5,18 @@ REVISION = 1 SHELL := ${shell which bash} IPATH += src +IPATH += srcgl VPATH += src +VPATH += srcgl OBJ = obj-${shell $(CC) -dumpmachine} C3SRC = ${wildcard src/*.c} C3OBJ = ${patsubst src/%,${OBJ}/%,${C3SRC:.c=.lo}} +C3GLSRC = ${wildcard srcgl/*.c} +C3GLOBJ = ${patsubst srcgl/%,${OBJ}/%,${C3GLSRC:.c=.lo}} + CC = clang PKGCONFIG = pkg-config INSTALL = install @@ -34,7 +39,7 @@ DESTDIR = /usr/local -include ${wildcard .make.options*} -all: ${OBJ} src/c3config.h ${OBJ}/libc3.la +all: ${OBJ} src/c3config.h ${OBJ}/libc3.la ${OBJ}/libc3gl.la ${OBJ}: mkdir -p ${OBJ} @@ -63,6 +68,14 @@ ${OBJ}/libc3.la: ${C3OBJ} -version-info 0:1:0 \ -rpath $(DESTDIR)/lib $(LDFLAGS) +${OBJ}/libc3gl.la: ${C3GLOBJ} + @echo LINK $@ + $(E)$(LIBTOOL) --mode=link --tag=CC \ + $(CC) $(CPPFLAGS) $(CFLAGS) \ + $^ -o $@ \ + -version-info 0:1:0 \ + -rpath $(DESTDIR)/lib $(LDFLAGS) + ${OBJ}/%.lo: src/c3config.h ${OBJ}/%.lo: %.c @echo CC $< diff --git a/examples/shared/libc3/README.md b/examples/shared/libc3/README.md index 31207ab..018ecb2 100644 --- a/examples/shared/libc3/README.md +++ b/examples/shared/libc3/README.md @@ -1,4 +1,4 @@ -libc3 - No frill 'scene' graph library in C +**libc3** - No frill 'scene' graph library in C ===== (C) 2012 Michel Pollet @@ -12,17 +12,48 @@ It's vaguely inspired by THREE.js funnily enough, because it allows you to hack around and quickly get stuff on screen with the minimal amount of effort. +Introduction +----------- +The general idea is that the library keeps track of geometry and stuff, but doesn't +do *any* opengl or related calls. Instead, it uses callbacks into code that will +take care of the rendering related tasks. + +So for example a c3pixels represents a texture, but a callback into the rendering +layer will be responsible to push the pixels to OpenGL, and store the object back +into the c3pixels for reference. + +Status +------- +The API is generally functional, but it's brand new. I try not to add bits that +I aren't needed, and I also don't add stuff that isn't tested. + +There is an ASCII STL file loader that works, and a few other bit of geometry related +helpers. + +It's currently used in one 'serious' project and also in my [3D printer simulator](https://github.com/buserror-uk/simavr/tree/master/examples/board_reprap), +as part of simavr. There you cal also find the "opengl renderer" set of callbacks, in the +near future, this layer will be part of a *libc3-gl* companion library. + +General Roadmap +--------------- +There is a [PDF Flowchart](https://github.com/buserror-uk/libc3/raw/master/doc/libc3-flowchart.pdf) +of how things are mostly organized as far as data structure goes, but the following is a +breakdown of the major components. + The API has various bits: * c3algebra: C derivative of an old C++ piece of code I had lying around and that has been present in my toolset for a long time. It gives you *vectors* (c3vec2, c3vec3, c3vec4) and *matrices* (c3mat3, c3mat4) with various tools to manipulate them. * c3quaternion: Quaternion implementation using c3algebra -* c3camera/c3arcball: camera manipulation, not perfect +* c3camera/c3arcball: camera manipulation bits The basic data structure is as follow: * *c3context*: - Mostly placeholder for now, hosts a "root" object, can reproject the - objects & geometry, and call the callbacks to draw them. + Hosts a "root" object, and a list of 'viewpoints' (ie either cameras, or lights). + it can reproject the objects & geometries, and call the callbacks to draw them. + + The context also keeps a list of *c3pixels* and *c3program* that are referenced + by the geometries. * *c3object*: * Has a list of (sub) c3objects * Has a list of c3transforms (ie matrices) @@ -33,11 +64,12 @@ The basic data structure is as follow: * *c3geometry*: * Has a 'type' (raw for simple vertices, texture, triangles etc) * Has a 'subtype' (mostly can be used to draw GL types) - * Has a 'material' (ie color, texture... to be completed) + * Has a 'material' (ie color, texture, a GPU program... to be completed) * Has a list of vertices + * Has a cached copy of a vertices when it has been 'projected' * Has a list of texture coordinates (optional) * Has a list of vertices colors (optional) - * Has a cached copy of a vertices when it has been 'projected' + * Had a list of vertices indexes (optional) * *c3transform*: Is just a sugar coated matrix, with an optional name. @@ -52,8 +84,6 @@ Also there are: * *c3cairo*: Placeholder for now, inherits from *c3texture* and will contain a cairo surface mapped to a GL texture. -* *c3pango*: - A text label, inherits from *c3cairo* Draw Drivers "Inheritance" ------------ diff --git a/examples/shared/libc3/doc/libc3-flowchart.dot b/examples/shared/libc3/doc/libc3-flowchart.dot new file mode 100644 index 0000000..4312255 --- /dev/null +++ b/examples/shared/libc3/doc/libc3-flowchart.dot @@ -0,0 +1,220 @@ +digraph g { +// label = "libc3\n - lightweight scene graph library - Data Structure Hierarchy"; + label = < + libc3 - + http://github.com/buserror-uk/libc3 + - Lightweight C Scene Graph Library - Data Structure Flowchart + >; + node [fontsize=24,shape=box]; + // concentrate=true; + + c3context [shape=none, label=< + + + +
c3context
+ Keeps track of a root c3object,
+ possible multiple views, and also stores
+ texture pixels and shader programs +
+ >]; + c3context_view [shape=none, label=< + + + +
c3context_view
+ Keep tracks of a scene point of view.
+ used for the eye(s), and the light(s) +
+ >]; + c3camera [shape=none, label=< + + + +
c3camera
+ Keeps an 'eye' point, a 'lookat'
+ point the related vectors, and a
+ transform matrix +
+ >]; + c3object [shape=none, label=< + + + +
c3object
+ Anchor point that holds optional
+ transform matrices, other sub-c3objects
+ and c3geometries
+
+ >]; + c3transform [shape=none, label=< + + + +
c3transform
+ Holds a matrix to be applies to
+ a c3object and it's descendants +
+ >]; + c3cairo [shape=none, label=< + + + +
c3cairo
+ Maps a Cairo image surface
+ to a c3texture - Optional. +
+ >]; + c3texture [shape=none, label=< + + + +
c3texture
+ Provides a geometry to render a
+ c3pixel (and anonymous texture) +
+ >]; + c3geometry [shape=none, label=< + + + +
c3geometry
+ Contains the real drawing data, vertices,
+ tex coordinates, normals and a material. +
+ >]; + c3pixels [shape=none, label=< + + + +
c3pixels
+ Basic description of a pixmap
+ to be loaded as a texture. +
+ >]; + c3program [shape=none, label=< + + + +
c3program
+ Holds a vertex/fragment/... program,
+ also parses them for uniform/parameters +
+ >]; + c3color [shape=none, label=< + + + +
c3color
+ Base RGBA float color +
+ >]; + c3vertex [shape=none, label=< + + + +
c3vertex
+ Array of X,Y,Z coordinates,
+ with associated buffer object +
+ >]; + c3normal [shape=none, label=< + + + +
c3normal
+ Array of normals with
+ associated buffer object +
+ >]; + c3tex [shape=none, label=< + + + +
c3tex
+ Array of texture coordinates
+ with associated buffer object +
+ >]; + c3indice [shape=none, label=< + + + +
c3indice
+ Array of indices into
+ the other arrays +
+ >]; + c3mat [shape=none, label=< + + + +
c3mat
+ Geometry material related data
+ ie color, texture, program. +
+ >]; + c3stl [shape=none, label=< + + + +
c3stl
+ Loads ASCII STL model files
+ and generates corresponding c3object. +
+ >]; + c3lines [shape=none, label=< + + + +
c3lines
+ Converts segments into a textured
+ triangle mesh for wide,
+ antialiasied lines +
+ >]; + c3sphere [shape=none, label=< + + + +
c3sphere
+ Creares geometry for a sphere
+ with texture coordinaes&normals +
+ >]; + c3context -> c3context_view; + c3context -> c3object [style=bold]; + c3context -> c3pixels [style=dashed]; + c3context -> c3program [style=dashed]; + + c3context_view -> c3camera; + + { rank = same; c3object; c3stl; } + { rank = same; c3geometry; c3lines; c3sphere; } + c3stl -> c3object [ weight=0.1, color="gray" ]; + c3lines -> c3geometry [ weight=0.1, color="gray" ]; + c3sphere -> c3geometry [ weight=0.1, color="gray" ]; + + c3texture -> c3geometry; + { node [style=dashed]; c3cairo; } + c3cairo -> c3texture; + + c3object -> c3geometry [style=bold]; + c3object -> c3texture; + c3object -> c3cairo; + c3object -> c3transform; + + c3object -> c3object; + + c3geometry -> c3vertex; + c3geometry -> c3normal ; + c3geometry -> c3tex; + c3geometry -> c3mat; + c3geometry -> c3indice; + + c3mat -> c3pixels; + c3mat -> c3program; + c3mat -> c3color; + + c3texture -> c3pixels; +}; diff --git a/examples/shared/libc3/src/c3.c b/examples/shared/libc3/src/c3.c index 38fcf83..4901765 100644 --- a/examples/shared/libc3/src/c3.c +++ b/examples/shared/libc3/src/c3.c @@ -3,22 +3,28 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include "c3.h" +#include "c3config.h" +const char * +c3version() +{ + return CONFIG_C3_VERSION; +} diff --git a/examples/shared/libc3/src/c3.h b/examples/shared/libc3/src/c3.h index 190ba23..d6ca68f 100644 --- a/examples/shared/libc3/src/c3.h +++ b/examples/shared/libc3/src/c3.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ @@ -29,4 +29,7 @@ #include "c3transform.h" #include "c3texture.h" +const char * +c3version(); + #endif /* __C3_H___ */ diff --git a/examples/shared/libc3/src/c3algebra.c b/examples/shared/libc3/src/c3algebra.c index b47525d..31406a5 100644 --- a/examples/shared/libc3/src/c3algebra.c +++ b/examples/shared/libc3/src/c3algebra.c @@ -6,20 +6,20 @@ Derivative and inspiration from original C++: Paul Rademacher & Jean-Francois DOUEG, - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include @@ -351,6 +351,14 @@ c3vec3 c3vec3_prod(const c3vec3 a, const c3vec3 b) return c3vec3f(a.n[VX]*b.n[VX], a.n[VY]*b.n[VY], a.n[VZ]*b.n[VZ]); } +c3vec3 c3vec3_polar(const c3vec3 a) +{ + c3f l = c3vec3_length(a); + c3f phi = atan2(a.y, a.x); + c3f theta = acos(a.z / l); + return c3vec3f(phi, theta, l); +} + /**************************************************************** * * * c3vec4 Member functions * diff --git a/examples/shared/libc3/src/c3algebra.h b/examples/shared/libc3/src/c3algebra.h index d2b71a1..b959a06 100644 --- a/examples/shared/libc3/src/c3algebra.h +++ b/examples/shared/libc3/src/c3algebra.h @@ -6,20 +6,20 @@ Derivative and inspiration from original C++: Paul Rademacher & Jean-Francois DOUEG, - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ @@ -87,7 +87,7 @@ c3vec2 c3vec2_max(const c3vec2 a, const c3vec2 b); c3vec2 c3vec2_prod(const c3vec2 a, const c3vec2 b); /* - * c3vec4 related + * c3vec3 related */ c3vec3 c3vec3_zero(); @@ -115,6 +115,7 @@ c3vec3 c3vec3_prod(const c3vec3 a, const c3vec3 b); c3vec3 c3vec3_cross(const c3vec3 a, const c3vec3 b); c3vec3 c3vec2_cross(const c3vec2 a, const c3vec2 b); +c3vec3 c3vec3_polar(const c3vec3 a); // returns phi, theta, length /* * c3vec4 related diff --git a/examples/shared/libc3/src/c3arcball.c b/examples/shared/libc3/src/c3arcball.c index f801651..35fe442 100644 --- a/examples/shared/libc3/src/c3arcball.c +++ b/examples/shared/libc3/src/c3arcball.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include diff --git a/examples/shared/libc3/src/c3arcball.h b/examples/shared/libc3/src/c3arcball.h index d61c815..d7309f1 100644 --- a/examples/shared/libc3/src/c3arcball.h +++ b/examples/shared/libc3/src/c3arcball.h @@ -6,20 +6,20 @@ Feb 1998, Paul Rademacher (rademach@cs.unc.edu) Oct 2003, Nigel Stewart - GLUI Code Cleaning - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ /* diff --git a/examples/shared/libc3/src/c3cairo.c b/examples/shared/libc3/src/c3cairo.c index a2b1655..2ab449b 100644 --- a/examples/shared/libc3/src/c3cairo.c +++ b/examples/shared/libc3/src/c3cairo.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3cairo.h b/examples/shared/libc3/src/c3cairo.h index 75d64b6..04a53c8 100644 --- a/examples/shared/libc3/src/c3cairo.h +++ b/examples/shared/libc3/src/c3cairo.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3camera.c b/examples/shared/libc3/src/c3camera.c index 64abd60..be3070d 100644 --- a/examples/shared/libc3/src/c3camera.c +++ b/examples/shared/libc3/src/c3camera.c @@ -4,20 +4,20 @@ Copyright 2008-2012 Michel Pollet Copyright (c) 1998 Paul Rademacher - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include diff --git a/examples/shared/libc3/src/c3camera.h b/examples/shared/libc3/src/c3camera.h index 9342e58..1a719db 100644 --- a/examples/shared/libc3/src/c3camera.h +++ b/examples/shared/libc3/src/c3camera.h @@ -4,20 +4,20 @@ Copyright 2008-2012 Michel Pollet Copyright (c) 1998 Paul Rademacher - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #ifndef __C3VIEW_H___ diff --git a/examples/shared/libc3/src/c3context.c b/examples/shared/libc3/src/c3context.c index 351e631..79508e6 100644 --- a/examples/shared/libc3/src/c3context.c +++ b/examples/shared/libc3/src/c3context.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include diff --git a/examples/shared/libc3/src/c3context.h b/examples/shared/libc3/src/c3context.h index 91ea4f1..93514cf 100644 --- a/examples/shared/libc3/src/c3context.h +++ b/examples/shared/libc3/src/c3context.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3driver.h b/examples/shared/libc3/src/c3driver.h index 901c32d..b48a4c7 100644 --- a/examples/shared/libc3/src/c3driver.h +++ b/examples/shared/libc3/src/c3driver.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3driver_context.h b/examples/shared/libc3/src/c3driver_context.h index c3cff59..64d2449 100644 --- a/examples/shared/libc3/src/c3driver_context.h +++ b/examples/shared/libc3/src/c3driver_context.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3driver_geometry.h b/examples/shared/libc3/src/c3driver_geometry.h index fa23eb5..f8edddc 100644 --- a/examples/shared/libc3/src/c3driver_geometry.h +++ b/examples/shared/libc3/src/c3driver_geometry.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3driver_object.h b/examples/shared/libc3/src/c3driver_object.h index a809439..ed1ea27 100644 --- a/examples/shared/libc3/src/c3driver_object.h +++ b/examples/shared/libc3/src/c3driver_object.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3geometry.c b/examples/shared/libc3/src/c3geometry.c index 8de3ead..a10eda5 100644 --- a/examples/shared/libc3/src/c3geometry.c +++ b/examples/shared/libc3/src/c3geometry.c @@ -3,23 +3,25 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ +#include +#include #include "c3object.h" #include "c3context.h" #include "c3driver_geometry.h" @@ -165,4 +167,84 @@ c3geometry_draw( C3_DRIVER(g, draw); } +void +c3geometry_factor( + c3geometry_p g, + c3f tolerance, + c3f normaltolerance) +{ + printf("%s Start geometry has %d vertices and %d indexes\n", __func__, + g->vertice.count, g->indices.count); + printf("%s Start geometry has %d normals and %d tex\n", __func__, + g->normals.count, g->textures.count); + + c3f tolerance2 = tolerance * tolerance; + + int in_index = g->indices.count; + int vcount = in_index ? in_index : g->vertice.count; + int input = 0; + int output = 0; + g->indices.count = 0; + while (input < vcount) { + int current = in_index ? g->indices.e[input] : input; + c3vec3 v = g->vertice.e[current]; + c3vec3 n = g->normals.count ? g->normals.e[current] : c3vec3f(0,0,0); + c3vec3 np = c3vec3_polar(n); // normal in polar coord + + int oi = -1; + for (int ci = 0; ci < output && oi == -1; ci++) + if (c3vec3_length2(c3vec3_sub(g->vertice.e[ci], v)) < tolerance2) { + if (g->normals.count) { + c3vec3 nc = g->normals.e[ci]; + c3vec3 pc = c3vec3_polar(nc); + + c3vec3 d = c3vec3_sub(np, pc); + while (d.n[0] <= -M_PI) d.n[0] += (2*M_PI); + while (d.n[1] <= -M_PI) d.n[1] += (2*M_PI); + + if (fabs(d.n[0]) < normaltolerance && + fabs(d.n[1]) < normaltolerance) { + oi = ci; + // replace the compared normal with the 'merged' one + // that should hopefully trim it to the right direction + // somehow. Not perfect obviously + g->normals.e[ci] = c3vec3_add(n, nc); + } + } else + oi = ci; + } + if (oi == -1) { + oi = output; + g->vertice.e[output] = g->vertice.e[current]; + if (g->textures.count) + g->textures.e[output] = g->textures.e[current]; + if (g->normals.count) + g->normals.e[output] = n; + if (g->colorf.count) + g->colorf.e[output] = g->colorf.e[current]; + output++; + } + c3indices_array_add(&g->indices, oi); + input++; + } + g->vertice.count = output; + c3vertex_array_realloc(&g->vertice, output); + if (g->textures.count) { + g->textures.count = output; + c3tex_array_realloc(&g->textures, output); + } + if (g->normals.count) { + g->normals.count = output; + c3vertex_array_realloc(&g->normals, output); + for (int ni = 0; ni < output; ni++) + g->normals.e[ni] = c3vec3_normalize(g->normals.e[ni]); + } + if (g->colorf.count) { + g->colorf.count = output; + c3colorf_array_realloc(&g->colorf, output); + } + g->dirty = 1; + printf("%s end geometry has %d vertices and %d indexes\n", __func__, + g->vertice.count, g->indices.count); +} diff --git a/examples/shared/libc3/src/c3geometry.h b/examples/shared/libc3/src/c3geometry.h index 355654f..e7d0183 100644 --- a/examples/shared/libc3/src/c3geometry.h +++ b/examples/shared/libc3/src/c3geometry.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ /* @@ -29,24 +29,22 @@ #ifndef __C3GEOMETRY_H___ #define __C3GEOMETRY_H___ -#include "c3algebra.h" +#include "c3types.h" #include "c_utils.h" -typedef c3vec3 c3vertex, *c3vertex_p; -typedef c3vec4 c3colorf, *c3colorf_p; -typedef c3vec2 c3tex, *c3tex_p; struct c3object_t; struct c3pixels_t; struct c3program_t; -DECLARE_C_ARRAY(c3vertex, c3vertex_array, 16, uint32_t bid); -DECLARE_C_ARRAY(c3tex, c3tex_array, 16, uint32_t bid); -DECLARE_C_ARRAY(c3colorf, c3colorf_array, 16, uint32_t bid); +DECLARE_C_ARRAY(c3vertex_t, c3vertex_array, 16, c3apiobject_t bid); +DECLARE_C_ARRAY(c3tex_t, c3tex_array, 16, c3apiobject_t bid); +DECLARE_C_ARRAY(c3colorf_t, c3colorf_array, 16, c3apiobject_t bid); +DECLARE_C_ARRAY(c3index_t, c3indices_array, 16, c3apiobject_t bid); -//! Geometry material. TODO: Beef up. Add vertex/fragment programs.. +//! Geometry material. typedef struct c3material_t { - c3colorf color; + c3colorf_t color; struct c3pixels_t * texture; struct c3program_t * program; struct { @@ -54,17 +52,13 @@ typedef struct c3material_t { } blend; } c3material_t; -//! Bounding box. TODO: Move to a separate file? -typedef struct c3bbox_t { - c3vec3 min, max; -} c3bbox_t; +#define C3_TYPE(_a,_b,_c,_d) \ + (((uint32_t)(_a)<<24)|((uint32_t)(_b)<<16)|((uint32_t)(_c)<<8)|(_d)) //! Generic geometry type enum { - C3_RAW_TYPE = 0, - C3_LINES_TYPE, - C3_TRIANGLE_TYPE, - C3_TEXTURE_TYPE, + C3_RAW_TYPE = C3_TYPE('r','a','w','g'), + C3_TRIANGLE_TYPE = C3_TYPE('t','r','i','a'), }; /*! @@ -73,9 +67,9 @@ enum { * contains, like "texture", and the subtype can be used to store the * real format of the vertices. like GL_LINES etc */ -typedef union c3geometry_type_t { - struct { uint32_t type : 16, subtype : 16; }; - uint32_t value; +typedef struct c3geometry_type_t { + uint32_t type; + c3apiobject_t subtype; } c3geometry_type_t; /*! @@ -97,6 +91,7 @@ typedef struct c3geometry_t { c3tex_array_t textures; c3colorf_array_t colorf; c3vertex_array_t normals; + c3indices_array_t indices; // projected version of the vertice c3vertex_array_t projected; @@ -152,6 +147,22 @@ void c3geometry_draw( c3geometry_p g ); +/* + * if not present, create an index array, and collapses + * the vertex array by removing vertices that are within + * 'tolerance' (as a distance between vertices considered equals). + * If the normals exists, they are also compared + * to normaltolerance (in radian) and if both position and normals + * are within tolerance, the vertices are collapsed and the normals + * are averaged. + * This code allows smooth rendering of STL files generated by + * CAD programs that generate only triangle normals. + */ +void +c3geometry_factor( + c3geometry_p g, + c3f tolerance, + c3f normaltolerance); //! allocate (if not there) and return a custom driver for this geometry /*! @@ -170,12 +181,13 @@ IMPLEMENT_C_ARRAY(c3geometry_array); IMPLEMENT_C_ARRAY(c3vertex_array); IMPLEMENT_C_ARRAY(c3tex_array); IMPLEMENT_C_ARRAY(c3colorf_array); +IMPLEMENT_C_ARRAY(c3indices_array); static inline c3geometry_type_t -c3geometry_type(int type, int subtype) +c3geometry_type(uint32_t type, int subtype) { c3geometry_type_t r;// = { .type = type, .subtype = subtype }; // older gcc <4.6 doesn't like this - r.type = type; r.subtype = subtype; + r.type = type; r.subtype = (c3apiobject_t)subtype; return r; } diff --git a/examples/shared/libc3/src/c3lines.c b/examples/shared/libc3/src/c3lines.c index 9db9047..a6b85ee 100644 --- a/examples/shared/libc3/src/c3lines.c +++ b/examples/shared/libc3/src/c3lines.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3lines.h b/examples/shared/libc3/src/c3lines.h index 7befe47..4ee8b34 100644 --- a/examples/shared/libc3/src/c3lines.h +++ b/examples/shared/libc3/src/c3lines.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ @@ -25,11 +25,14 @@ #include "c3geometry.h" +enum { + C3_LINES_TYPE = C3_TYPE('l','i','n','e'), +}; /* * Takes an array of points A,B and split it into 'fat' lines around * lineWidth, generates an array of triangles and an array of corresponding - * texture cordinates. Can also do a projection at the same time - * TODO: Add array indexes + * texture coordinates. Can also do a projection at the same time + * TODO: Add array indices */ void c3lines_prepare( diff --git a/examples/shared/libc3/src/c3object.c b/examples/shared/libc3/src/c3object.c index 52cbab3..04d17f1 100644 --- a/examples/shared/libc3/src/c3object.c +++ b/examples/shared/libc3/src/c3object.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3object.h b/examples/shared/libc3/src/c3object.h index 78122fa..57f16da 100644 --- a/examples/shared/libc3/src/c3object.h +++ b/examples/shared/libc3/src/c3object.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3pixels.c b/examples/shared/libc3/src/c3pixels.c index cdaef7e..63cf99e 100644 --- a/examples/shared/libc3/src/c3pixels.c +++ b/examples/shared/libc3/src/c3pixels.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include diff --git a/examples/shared/libc3/src/c3pixels.h b/examples/shared/libc3/src/c3pixels.h index 1952123..386f323 100644 --- a/examples/shared/libc3/src/c3pixels.h +++ b/examples/shared/libc3/src/c3pixels.h @@ -3,27 +3,27 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #ifndef __C3PIXELS_H___ #define __C3PIXELS_H___ -#include +#include "c3types.h" #include "c_utils.h" //! for format hint @@ -51,7 +51,7 @@ typedef struct c3pixels_t { }; uint32_t flags; }; - uint32_t texture; + c3apiobject_t texture; int refCount; // TODO: Implement reference counting ? } c3pixels_t, *c3pixels_p; diff --git a/examples/shared/libc3/src/c3program.c b/examples/shared/libc3/src/c3program.c index e7c7998..5e2edf4 100644 --- a/examples/shared/libc3/src/c3program.c +++ b/examples/shared/libc3/src/c3program.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include @@ -66,6 +66,17 @@ c3program_purge( c3shader_array_free(&p->shaders); } +c3program_param_p +c3program_locate_param( + c3program_p p, + const char * name ) +{ + for (int pi = 0; pi < p->params.count; pi++) + if (!strcmp(p->params.e[pi].name->str, name)) + return &p->params.e[pi]; + return NULL; +} + int c3program_load_shader( c3program_p p, @@ -138,6 +149,7 @@ c3program_load_shader( .program = p, }; c3program_param_array_add(&p->params, pa); + if (p->verbose) printf("%s %s: new parameter '%s' '%s'\n", __func__, p->name->str, unitype, uniname); } else diff --git a/examples/shared/libc3/src/c3program.h b/examples/shared/libc3/src/c3program.h index dfc6060..626d12b 100644 --- a/examples/shared/libc3/src/c3program.h +++ b/examples/shared/libc3/src/c3program.h @@ -3,30 +3,31 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #ifndef __C3PROGRAM_H___ #define __C3PROGRAM_H___ +#include "c3types.h" #include "c_utils.h" typedef struct c3shader_t { - uint32_t sid; // shader id + c3apiobject_t sid; // shader id uint32_t type; str_p name; str_p shader; @@ -36,7 +37,7 @@ DECLARE_C_ARRAY(c3shader_t, c3shader_array, 4); typedef struct c3program_param_t { struct c3program_t * program; - int32_t pid; // parameter id + c3apiobject_t pid; // parameter id str_p type; str_p name; } c3program_param_t, *c3program_param_p; @@ -44,11 +45,12 @@ typedef struct c3program_param_t { DECLARE_C_ARRAY(c3program_param_t, c3program_param_array, 4); typedef struct c3program_t { - uint32_t pid; // program id + c3apiobject_t pid; // program id + int verbose : 1; str_p name; c3shader_array_t shaders; c3program_param_array_t params; - str_p log; + str_p log; // if an error occurs } c3program_t, *c3program_p; DECLARE_C_ARRAY(c3program_p, c3program_array, 4); @@ -80,6 +82,11 @@ c3program_load_shader( const char * filename, uint16_t flags); +c3program_param_p +c3program_locate_param( + c3program_p p, + const char * name ); + IMPLEMENT_C_ARRAY(c3program_param_array); IMPLEMENT_C_ARRAY(c3shader_array); IMPLEMENT_C_ARRAY(c3program_array); diff --git a/examples/shared/libc3/src/c3quaternion.c b/examples/shared/libc3/src/c3quaternion.c index 10dd468..894604e 100644 --- a/examples/shared/libc3/src/c3quaternion.c +++ b/examples/shared/libc3/src/c3quaternion.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include diff --git a/examples/shared/libc3/src/c3quaternion.h b/examples/shared/libc3/src/c3quaternion.h index 92bf35d..89020ea 100644 --- a/examples/shared/libc3/src/c3quaternion.h +++ b/examples/shared/libc3/src/c3quaternion.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3sphere.c b/examples/shared/libc3/src/c3sphere.c new file mode 100644 index 0000000..5a8db4c --- /dev/null +++ b/examples/shared/libc3/src/c3sphere.c @@ -0,0 +1,67 @@ +/* + c3sphere.c + + Copyright 2008-2012 Michel Pollet + + This file is part of libc3. + + libc3 is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libc3 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libc3. If not, see . + */ + + +#include +#include "c3geometry.h" +#include "c3sphere.h" + +c3geometry_p +c3sphere_uv( + struct c3object_t * parent, + c3vec3 center, + c3f radius, + int rings, + int sectors ) +{ + float const R = 1. / (float) (rings - 1); + float const S = 1. / (float) (sectors - 1); + + c3geometry_p g = c3geometry_new(c3geometry_type(C3_SPHERE_TYPE, 0), parent); + + c3vertex_array_realloc(&g->vertice, rings * sectors); + c3vertex_array_realloc(&g->normals, rings * sectors); + c3tex_array_realloc(&g->textures, rings * sectors); + c3indices_array_realloc(&g->indices, rings * sectors * 6); + + for (int r = 0; r < rings; r++) + for (int s = 0; s < sectors; s++) { + float const y = sin(-M_PI_2 + M_PI * r * R); + float const x = cos(2 * M_PI * s * S) * sin(M_PI * r * R); + float const z = sin(2 * M_PI * s * S) * sin(M_PI * r * R); + + c3tex_array_add(&g->textures, c3vec2f(s * S, r * R)); + c3vertex_array_add(&g->vertice, + c3vec3_add(center, c3vec3f(x * radius, y * radius, z * radius))); + c3vertex_array_add(&g->normals, c3vec3_normalize(c3vec3f(x, y, z))); + } + + for (int r = 0; r < rings - 1; r++) + for (int s = 0; s < sectors - 1; s++) { + uint16_t i[6] = { + r * sectors + (s + 1), r * sectors + s, (r + 1) * sectors + (s + 1), + (r + 1) * sectors + (s + 1), r * sectors + s, (r + 1) * sectors + s, + }; + c3indices_array_insert(&g->indices, g->indices.count, i, 6); + } + return g; +} + diff --git a/examples/shared/libc3/src/c3sphere.h b/examples/shared/libc3/src/c3sphere.h new file mode 100644 index 0000000..84c1811 --- /dev/null +++ b/examples/shared/libc3/src/c3sphere.h @@ -0,0 +1,40 @@ +/* + c3sphere.h + + Copyright 2008-2012 Michel Pollet + + This file is part of libc3. + + libc3 is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libc3 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libc3. If not, see . + */ + + +#ifndef __C3SPHERE_H___ +#define __C3SPHERE_H___ + +#include "c3algebra.h" + +enum { + C3_SPHERE_TYPE = C3_TYPE('s','p','h','e'), +}; + +struct c3geometry_t * +c3sphere_uv( + struct c3object_t * parent, + c3vec3 center, + c3f radius, + int rings, + int sectors ); + +#endif /* __C3SPHERE_H___ */ diff --git a/examples/shared/libc3/src/c3stl.c b/examples/shared/libc3/src/c3stl.c index c42709b..9183f6d 100644 --- a/examples/shared/libc3/src/c3stl.c +++ b/examples/shared/libc3/src/c3stl.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #include @@ -97,6 +97,7 @@ c3stl_load( if (!strncmp(keyword, "facet ", 6)) { c3vec3 normal; _c3stl_read_vertex(keyword + 6, &normal); + normal = c3vec3_normalize(normal); c3vertex_array_add(¤t_g->normals, normal); c3vertex_array_add(¤t_g->normals, normal); c3vertex_array_add(¤t_g->normals, normal); diff --git a/examples/shared/libc3/src/c3stl.h b/examples/shared/libc3/src/c3stl.h index d8bd250..8f562cd 100644 --- a/examples/shared/libc3/src/c3stl.h +++ b/examples/shared/libc3/src/c3stl.h @@ -3,26 +3,30 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ #ifndef __C3STL_H___ #define __C3STL_H___ +/* + * Loads an ASCII (TODO: Load STL Binary?) STL file as a c3object with + * a set of c3geometries with the triangles + */ struct c3object_t * c3stl_load( const char * filename, diff --git a/examples/shared/libc3/src/c3texture.c b/examples/shared/libc3/src/c3texture.c index 78a8d20..27ae649 100644 --- a/examples/shared/libc3/src/c3texture.c +++ b/examples/shared/libc3/src/c3texture.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3texture.h b/examples/shared/libc3/src/c3texture.h index 1a3af62..ecaae3e 100644 --- a/examples/shared/libc3/src/c3texture.h +++ b/examples/shared/libc3/src/c3texture.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ @@ -26,10 +26,12 @@ #include "c3geometry.h" #include "c3pixels.h" +enum { + C3_TEXTURE_TYPE = C3_TYPE('t','e','x','u'), +}; + typedef struct c3texture_t { c3geometry_t geometry; -// c3pixels_t pixels; -// int normalized : 1; // use 0.. 1 texture coordinates c3vec2 size; // quad size } c3texture_t, *c3texture_p; diff --git a/examples/shared/libc3/src/c3transform.c b/examples/shared/libc3/src/c3transform.c index a453ad7..de442aa 100644 --- a/examples/shared/libc3/src/c3transform.c +++ b/examples/shared/libc3/src/c3transform.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3transform.h b/examples/shared/libc3/src/c3transform.h index 63e8f38..b6b4289 100644 --- a/examples/shared/libc3/src/c3transform.h +++ b/examples/shared/libc3/src/c3transform.h @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/src/c3types.h b/examples/shared/libc3/src/c3types.h new file mode 100644 index 0000000..0c0c8ca --- /dev/null +++ b/examples/shared/libc3/src/c3types.h @@ -0,0 +1,45 @@ +/* + c3types.h + + Copyright 2008-2012 Michel Pollet + + This file is part of libc3. + + libc3 is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libc3 is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libc3. If not, see . + */ + + +#ifndef __C3TYPES_H___ +#define __C3TYPES_H___ + +#include +#include "c3algebra.h" + +typedef c3vec3 c3vertex_t, *c3vertex_p; +typedef c3vec4 c3colorf_t, *c3colorf_p; +typedef c3vec2 c3tex_t, *c3tex_p; +typedef uint16_t c3index_t, *c3index_p; + +/* this type is used to store an API object (texture id etc + * it is made to force a cast in most cases as OpenGL uses integers + * for object ids + */ +typedef void * c3apiobject_t; + +//! Bounding box +typedef struct c3bbox_t { + c3vec3 min, max; +} c3bbox_t; + +#endif /* __C3TYPES_H___ */ diff --git a/examples/shared/libc3/src/c_utils.c b/examples/shared/libc3/src/c_utils.c index e3bb673..0a086de 100644 --- a/examples/shared/libc3/src/c_utils.c +++ b/examples/shared/libc3/src/c_utils.c @@ -3,20 +3,20 @@ Copyright 2008-2012 Michel Pollet - This file is part of simavr. + This file is part of libc3. - simavr is free software: you can redistribute it and/or modify + libc3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - simavr is distributed in the hope that it will be useful, + libc3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with simavr. If not, see . + along with libc3. If not, see . */ diff --git a/examples/shared/libc3/srcgl/c3gl.c b/examples/shared/libc3/srcgl/c3gl.c new file mode 100644 index 0000000..889633f --- /dev/null +++ b/examples/shared/libc3/srcgl/c3gl.c @@ -0,0 +1,296 @@ +/* + c3gl.c + + Copyright 2008-2012 Michel Pollet + + This file is part of simavr. + + simavr is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + simavr is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with simavr. If not, see . + */ + +#if __APPLE__ +#define GL_GLEXT_PROTOTYPES +#include +#include +#include +#else +#define GL_GLEXT_PROTOTYPES +#include +#include +#include +#endif +#include + + +#include "c3.h" +#include "c3lines.h" +#include "c3sphere.h" +#include "c3program.h" + +#include "c3driver_context.h" + +#include "c3gl.h" + +#define GLCHECK(_w) {_w; dumpError(#_w);} + +static int dumpError(const char * what) +{ + GLenum e; + int count = 0; + while ((e = glGetError()) != GL_NO_ERROR) { + printf("%s: %s\n", what, gluErrorString(e)); + count++; + } + return count; +} + +static void +_c3_load_program( + c3program_p p) +{ + if (!p || p->pid || p->log) + return; + + if (p->verbose) + printf("%s loading %s\n", __func__, p->name->str); + for (int si = 0; si < p->shaders.count && !p->log; si++) { + c3shader_p s = &p->shaders.e[si]; + + if (p->verbose) + printf("%s compiling shader %s\n", __func__, s->name->str); + + s->sid = (c3apiobject_t)glCreateShader(s->type); + const GLchar * pgm = s->shader->str; + glShaderSource((GLuint)s->sid, 1, &pgm, NULL); + + glCompileShader((GLuint)s->sid); + + GLint status; + glGetShaderiv((GLuint)s->sid, GL_COMPILE_STATUS, &status); + + if (status != GL_FALSE) + continue; + + GLint infoLogLength; + glGetShaderiv((GLuint)s->sid, GL_INFO_LOG_LENGTH, &infoLogLength); + + p->log = str_alloc(infoLogLength); + glGetShaderInfoLog((GLuint)s->sid, infoLogLength, NULL, p->log->str); + + fprintf(stderr, "%s compile %s: %s\n", __func__, s->name->str, p->log->str); + break; + } + if (p->log) + return; + p->pid = (c3apiobject_t)glCreateProgram(); + + for (int si = 0; si < p->shaders.count && !p->log; si++) { + c3shader_p s = &p->shaders.e[si]; + + glAttachShader((GLuint)p->pid, (GLuint)s->sid); + } + glLinkProgram((GLuint)p->pid); + + GLint status; + glGetProgramiv((GLuint)p->pid, GL_LINK_STATUS, &status); + + for (int si = 0; si < p->shaders.count && !p->log; si++) { + c3shader_p s = &p->shaders.e[si]; + + glDetachShader((GLuint)p->pid, (GLuint)s->sid); + glDeleteShader((GLuint)s->sid); + s->sid = 0; + } + + if (status == GL_FALSE) { + GLint infoLogLength; + glGetProgramiv((GLuint)p->pid, GL_INFO_LOG_LENGTH, &infoLogLength); + + p->log = str_alloc(infoLogLength); + + glGetProgramInfoLog((GLuint)p->pid, infoLogLength, NULL, p->log->str); + fprintf(stderr, "%s link %s: %s\n", __func__, p->name->str, p->log->str); + + goto error; + } + for (int pi = 0; pi < p->params.count; pi++) { + c3program_param_p pa = &p->params.e[pi]; + pa->pid = (c3apiobject_t)glGetUniformLocation((GLuint)p->pid, pa->name->str); + if (p->verbose) + printf("%s %s load parameter '%s'\n", __func__, p->name->str, pa->name->str); + if (pa->pid == (c3apiobject_t)-1) { + fprintf(stderr, "%s %s: parameter '%s' not found\n", + __func__, p->name->str, pa->name->str); + } + } + + c3program_purge(p); + return; +error: + c3program_purge(p); + if (p->pid) + glDeleteProgram((GLuint)p->pid); + p->pid = 0; +} + +static void +_c3_load_pixels( + c3pixels_p pix) +{ + GLuint mode = pix->normalize ? GL_TEXTURE_2D : GL_TEXTURE_RECTANGLE_ARB; + if (!pix->texture) { + if (pix->trace) + printf("%s Creating texture %s %dx%d\n", + __func__, pix->name ? pix->name->str : "", pix->w, pix->h); + pix->dirty = 1; + GLuint texID = 0; + GLCHECK(glEnable(mode)); + + glGenTextures(1, &texID); +// glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, +// GL_MODULATE); //set texture environment parameters +// dumpError("glTexEnvf"); + + glPixelStorei(GL_UNPACK_ROW_LENGTH, pix->row / pix->psize); + GLCHECK(glTexParameteri(mode, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + GLCHECK(glTexParameteri(mode, GL_TEXTURE_MIN_FILTER, + pix->normalize ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR)); + GLCHECK(glTexParameteri(mode, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)); + GLCHECK(glTexParameteri(mode, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)); + if (pix->normalize) + GLCHECK(glTexParameteri(mode, GL_GENERATE_MIPMAP, GL_TRUE)); + #if 1 + GLfloat fLargest; + glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest); + //printf("fLargest = %f\n", fLargest); + GLCHECK(glTexParameterf(mode, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest)); + #endif + if (pix->normalize) + GLCHECK(glGenerateMipmap(mode)); + + pix->texture = (c3apiobject_t)texID; + pix->dirty = 1; + } + if (pix->dirty) { + pix->dirty = 0; + GLCHECK(glBindTexture(mode, (GLuint)pix->texture)); + glTexImage2D(mode, 0, + pix->format == C3PIXEL_A ? GL_ALPHA16 : GL_RGBA8, + pix->w, pix->h, 0, + pix->format == C3PIXEL_A ? GL_ALPHA : GL_BGRA, + GL_UNSIGNED_BYTE, + pix->base); + dumpError("glTexImage2D"); + if (pix->normalize) + GLCHECK(glGenerateMipmap(mode)); + } +} + +static void +_c3_geometry_project( + c3context_p c, + const struct c3driver_context_t * d, + c3geometry_p g, + c3mat4p m) +{ + if (g->mat.texture) + _c3_load_pixels(g->mat.texture); + if (g->mat.program) + _c3_load_program(g->mat.program); + + switch(g->type.type) { + case C3_SPHERE_TYPE: + case C3_TRIANGLE_TYPE: + case C3_LINES_TYPE: + g->type.subtype = (c3apiobject_t)GL_TRIANGLES; + break; + case C3_TEXTURE_TYPE: { + if (g->mat.texture) + g->type.subtype = (c3apiobject_t)GL_TRIANGLE_FAN; + } break; + default: + break; + } +} + +/* + * Thid id the meta function that draws a c3geometry. It looks for normals, + * indices, textures and so on and call the glDrawArrays + */ +static void +_c3_geometry_draw( + c3context_p c, + const struct c3driver_context_t *d, + c3geometry_p g ) +{ + glColor4fv(g->mat.color.n); + dumpError("glColor"); +// glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, g->mat.color.n); + glVertexPointer(3, GL_FLOAT, 0, + g->projected.count ? g->projected.e : g->vertice.e); + glEnableClientState(GL_VERTEX_ARRAY); + dumpError("GL_VERTEX_ARRAY"); + glDisable(GL_TEXTURE_2D); + if (g->mat.texture) { + GLuint mode = g->mat.texture->normalize ? + GL_TEXTURE_2D : GL_TEXTURE_RECTANGLE_ARB; + glEnable(mode); + if (g->mat.texture->trace) + printf("%s uses texture %s (%d tex)\n", + __func__, g->mat.texture->name->str, g->textures.count); + // printf("tex mode %d texture %d\n", g->mat.mode, g->mat.texture); + dumpError("glEnable texture"); + glBindTexture(mode, (GLuint)g->mat.texture->texture); + dumpError("glBindTexture"); + glTexCoordPointer(2, GL_FLOAT, 0, g->textures.e); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + dumpError("GL_TEXTURE_COORD_ARRAY"); + } + if (g->mat.program) { + GLCHECK(glUseProgram((GLuint)g->mat.program->pid)); + } + if (g->normals.count) { + GLCHECK(glNormalPointer(GL_FLOAT, 0, g->normals.e)); + glEnableClientState(GL_NORMAL_ARRAY); + } + if (g->indices.count) { + // GLCHECK(glIndexPointer(GL_UNSIGNED_SHORT, 0, g->indices.e)); + // glEnableClientState(GL_INDEX_ARRAY); + GLCHECK(glDrawElements((GLuint)g->type.subtype, + g->indices.count, GL_UNSIGNED_SHORT, + g->indices.e)); + // glDisableClientState(GL_INDEX_ARRAY); + } else { + glDrawArrays((GLuint)g->type.subtype, 0, + g->projected.count ? g->projected.count : g->vertice.count); + } + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + if (g->mat.texture) + glDisable(g->mat.texture->normalize ? GL_TEXTURE_2D : GL_TEXTURE_RECTANGLE_ARB); + if (g->mat.program) + glUseProgram(0); +} + +const c3driver_context_t c3context_driver = { + .geometry_project = _c3_geometry_project, + .geometry_draw = _c3_geometry_draw, +}; + +const struct c3driver_context_t * +c3gl_getdriver() +{ + return &c3context_driver; +} diff --git a/examples/shared/libc3/srcgl/c3gl.h b/examples/shared/libc3/srcgl/c3gl.h new file mode 100644 index 0000000..9d999c9 --- /dev/null +++ b/examples/shared/libc3/srcgl/c3gl.h @@ -0,0 +1,29 @@ +/* + c3gl.h + + Copyright 2008-2012 Michel Pollet + + This file is part of simavr. + + simavr is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + simavr is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with simavr. If not, see . + */ + + +#ifndef __C3GL_H___ +#define __C3GL_H___ + +const struct c3driver_context_t * +c3gl_getdriver(); + +#endif /* __C3GL_H___ */ -- 2.39.5