Permalink
Browse files

Working on example classes, they're not general purpose but demonstra…

…te simple evaluators, VBO's, and NURBS surfaces.
  • Loading branch information...
1 parent 2d1c294 commit 7f8b5af1a1f80d7322b217513a794a3bf0e1a118 @jzrake committed Apr 20, 2012
Showing with 165 additions and 15 deletions.
  1. +161 −11 src/luview.cpp
  2. +4 −4 test/load.lua
View
@@ -288,12 +288,8 @@ class BoundingBox : public DrawableObject
-class RegularGrid2D : public DrawableObject
+class ExampleSimpleEvaluator : public DrawableObject
{
-private:
- // double Lx, Ly;
- // int Nx, Ny;
-
public:
void draw_local()
{
@@ -315,7 +311,7 @@ class RegularGrid2D : public DrawableObject
const double x = Lx0 + i*dx;
const double y = Ly0 + j*dy;
- const double z = (x*x + y*y) - 0.5;
+ const double z = x*x + y*y;
const int m = i*sx + j*sy;
surfdata[3*m + 0] = x;
@@ -324,21 +320,173 @@ class RegularGrid2D : public DrawableObject
}
}
-
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
- glMap2f(GL_MAP2_VERTEX_3, Lx0, Lx1, 3*sx, Nx, Ly0, Ly1, 3*sy, Ny, surfdata);
- glMapGrid2f(Nx, Lx0, Lx1, Ny, Ly0, Ly1);
- glEvalMesh2(GL_LINE, 0, Nx, 0, Ny);
+ for (int i=1; i<Nx-1; ++i) {
+ for (int j=1; j<Ny-1; ++j) {
+
+ const double x = Lx0 + i*dx;
+ const double y = Ly0 + j*dy;
+ const GLfloat *s = &surfdata[3*((i-1)*sx + (j-1)*sy)];
+
+ glMap2f(GL_MAP2_VERTEX_3, x-dx, x+dx, 3*sx, 3, y-dy, y+dy, 3*sy, 3, s);
+ glMapGrid2f(4, x-dx/2, x+dx/2, 4, y-dy/2, y+dy/2);
+ glEvalMesh2(GL_LINE, 0, 4, 0, 4);
+ }
+ }
glDisable(GL_MAP2_VERTEX_3);
glDisable(GL_AUTO_NORMAL);
glDisable(GL_NORMALIZE);
free(surfdata);
+ }
+} ;
+
+
+class ExampleSimpleVBO : public DrawableObject
+{
+public:
+ void draw_local()
+ {
+ GLuint vbo,ibo;
+ GLfloat verts[8][3] = {{0.0, 0.0, 0.0},
+ {0.0, 0.0, 0.1},
+ {0.0, 0.1, 0.0},
+ {0.0, 0.1, 0.1},
+ {0.1, 0.0, 0.0},
+ {0.1, 0.0, 0.1},
+ {0.1, 0.1, 0.0},
+ {0.1, 0.1, 0.1}};
+ GLubyte ind[8] = {0,3,6,9,12,15,18,21};
+
+ glGenBuffers(1, &vbo);
+ glGenBuffers(1, &ibo);
+
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
+
+ glBufferData(GL_ARRAY_BUFFER, 24*sizeof(GLfloat), verts, GL_STATIC_DRAW);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, 8*sizeof(GLubyte), ind,
+ GL_STATIC_DRAW);
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glVertexPointer(3, GL_FLOAT, sizeof(GLfloat), 0);
+ glDrawElements(GL_LINE_STRIP, 8, GL_UNSIGNED_BYTE, 0);
+ glDisableClientState(GL_VERTEX_ARRAY);
+
+ glDeleteBuffers(1, &vbo);
+ glDeleteBuffers(1, &ibo);
+ }
+} ;
+
+
+class ExampleSimpleNURBS : public DrawableObject
+{
+private:
+ GLfloat ctlpoints[4][4][3];
+ int showPoints;
+ GLUnurbsObj *theNurb;
+
+public:
+ ExampleSimpleNURBS() : showPoints(1)
+ {
+ // init_surface();
+ }
+ ~ExampleSimpleNURBS()
+ {
+ // delete NURB
+ }
+
+private:
+ void init_surface()
+ {
+ int u, v;
+ for (u = 0; u < 4; u++) {
+ for (v = 0; v < 4; v++) {
+ ctlpoints[u][v][0] = 2.0*((GLfloat)u - 1.5);
+ ctlpoints[u][v][1] = 2.0*((GLfloat)v - 1.5);
+
+ if ( (u == 1 || u == 2) && (v == 1 || v == 2))
+ ctlpoints[u][v][2] = 3.0;
+ else
+ ctlpoints[u][v][2] = -3.0;
+ }
+ }
+ }
+
+ void init()
+ {
+ GLfloat mat_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
+ GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat mat_shininess[] = { 100.0 };
+
+ glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
+ glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
+ glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
+
+ glEnable(GL_LIGHTING);
+ glEnable(GL_LIGHT0);
+ glEnable(GL_AUTO_NORMAL);
+ glEnable(GL_NORMALIZE);
+
+ init_surface();
+
+ theNurb = gluNewNurbsRenderer();
+ gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
+ gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
+ gluNurbsCallback(theNurb, GLU_ERROR,
+ (GLvoid (*)()) nurbsError);
+ }
+
+ void draw_local()
+ {
+ GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
+ int i, j;
+ init();
+
+ glRotatef(-90.0, 1.0, 0.0, 0.0);
+ glScalef(0.1, 0.1, 0.1);
+
+ gluBeginSurface(theNurb);
+ gluNurbsSurface(theNurb,
+ 8, knots, 8, knots,
+ 4 * 3, 3, &ctlpoints[0][0][0],
+ 4, 4, GL_MAP2_VERTEX_3);
+ gluEndSurface(theNurb);
+
+ if (showPoints) {
+ glPointSize(5.0);
+ glDisable(GL_LIGHTING);
+ glColor3f(1.0, 1.0, 0.0);
+ glBegin(GL_POINTS);
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 4; j++) {
+ glVertex3f(ctlpoints[i][j][0],
+ ctlpoints[i][j][1], ctlpoints[i][j][2]);
+ }
+ }
+ glEnd();
+ glEnable(GL_LIGHTING);
+ }
+
+ glDisable(GL_LIGHTING);
+ glDisable(GL_LIGHT0);
+ glDisable(GL_AUTO_NORMAL);
+ glDisable(GL_NORMALIZE);
+
+ gluDeleteNurbsRenderer(theNurb);
+ }
+
+ static void nurbsError(GLenum errorCode)
+ {
+ const GLubyte *estring;
+ estring = gluErrorString(errorCode);
+ fprintf (stderr, "Nurbs Error: %s\n", estring);
+ exit (0);
}
} ;
@@ -353,7 +501,9 @@ extern "C" int luaopen_luview(lua_State *L)
LuaCppObject::Init(L);
LuaCppObject::Register<Window>(L);
LuaCppObject::Register<BoundingBox>(L);
- LuaCppObject::Register<RegularGrid2D>(L);
+ LuaCppObject::Register<ExampleSimpleEvaluator>(L);
+ LuaCppObject::Register<ExampleSimpleVBO>(L);
+ LuaCppObject::Register<ExampleSimpleNURBS>(L);
return 1;
}
View
@@ -6,18 +6,18 @@ local luview = require 'luview'
local window = luview.Window()
local bounding_box = luview.BoundingBox()
local little_box = luview.BoundingBox()
-local grid = luview:RegularGrid2D()
+local grid = luview.ExampleSimpleNURBS()
bounding_box:set_linewidth(2.5)
bounding_box:set_color(0.8, 0.9, 0.3)
little_box:set_scale(0.2, 0.5, 0.5)
little_box:set_orientation(0.0, 20.0, 0.0)
-grid:set_color(1,1,1)
+grid:set_color(1.0, 0.5, 0.0)
+grid:set_linewidth(1.0)
+grid:set_position(0.0, 0.0, -0.5)
window:set_position(0.0, 0.0, -3.0)
window:set_color(0.1, 0.05, 0.1)
window:render_scene({bounding_box, little_box, grid})
---window:render_scene({grid})
-

0 comments on commit 7f8b5af

Please sign in to comment.