Eigenized old sphere mesh code (used only for handling .cms mesh files now.)

sensor-dev
Chris Laurel 2009-07-23 23:19:26 +00:00
parent 198df1561d
commit c26ea667f2
10 changed files with 123 additions and 111 deletions

View File

@ -684,7 +684,7 @@ void InitializeForms()
float r = p.distanceFromOrigin();
if (r < 1)
{
float prob = (1 - r) * (fractalsum(Point3f(p.x + 5, p.y + 5, p.z + 5), 8) + 1) * 0.5f;
float prob = (1 - r) * (fractalsum(Vector3f(p.x + 5, p.y + 5, p.z + 5), 8) + 1) * 0.5f;
if (Mathf::frand() < prob)
{
b.position = Vector4f(p.x, p.y, p.z, 1.0f);

View File

@ -177,8 +177,8 @@ Geometry* GeometryInfo::load(const string& resolvedFilename)
struct NoiseMeshParameters
{
Vec3f size;
Vec3f offset;
Vector3f size;
Vector3f offset;
float featureHeight;
float octaves;
float slices;
@ -197,8 +197,8 @@ static float NoiseDisplacementFunc(float u, float v, void* info)
// assert(info != NULL);
NoiseMeshParameters* params = (NoiseMeshParameters*) info;
return fractalsum(Point3f(x, y, z) + params->offset,
params->octaves) * params->featureHeight;
Vector3f p = Vector3f(x, y, z) + params->offset;
return fractalsum(p, params->octaves) * params->featureHeight;
}
@ -247,8 +247,8 @@ Model* LoadCelestiaMesh(const string& filename)
NoiseMeshParameters params;
params.size = Vec3f(1, 1, 1);
params.offset = Vec3f(10, 10, 10);
params.size = Vector3f::Ones();
params.offset = Vector3f::Constant(10.0f);
params.featureHeight = 0.0f;
params.octaves = 1;
params.slices = 20;

View File

@ -15,6 +15,7 @@
#include "glext.h"
#include "vecgl.h"
using namespace Eigen;
using namespace std;
@ -137,13 +138,13 @@ RenderContext::getPointScale() const
void
RenderContext::setCameraOrientation(const Quatf& q)
RenderContext::setCameraOrientation(const Quaternionf& q)
{
cameraOrientation = q;
}
Quatf
Quaternionf
RenderContext::getCameraOrientation() const
{
return cameraOrientation;

View File

@ -10,14 +10,16 @@
#ifndef _CELENGINE_RENDCONTEXT_H_
#define _CELENGINE_RENDCONTEXT_H_
#include <celmath/quaternion.h>
#include "mesh.h"
#include "shadermanager.h"
#include <Eigen/Geometry>
class RenderContext
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
RenderContext(const Mesh::Material*);
RenderContext();
virtual ~RenderContext() {};
@ -45,15 +47,15 @@ class RenderContext
void setPointScale(float);
float getPointScale() const;
void setCameraOrientation(const Quatf& q);
Quatf getCameraOrientation() const;
void setCameraOrientation(const Eigen::Quaternionf& q);
Eigen::Quaternionf getCameraOrientation() const;
private:
const Mesh::Material* material;
bool locked;
RenderPass renderPass;
float pointScale;
Quatf cameraOrientation; // required for drawing billboards
Eigen::Quaternionf cameraOrientation; // required for drawing billboards
protected:
bool usePointSize;

View File

@ -282,7 +282,7 @@ void renderGeometry_GLSL(Geometry* geometry,
rc.setAtmosphere(atmosphere);
}
rc.setCameraOrientation(fromEigen(ri.orientation));
rc.setCameraOrientation(ri.orientation);
rc.setPointScale(ri.pointScale);
// Handle extended material attributes (per model only, not per submesh)

View File

@ -1,18 +1,24 @@
// mesh.cpp
// spheremesh.cpp
//
// Copyright (C) 2000, Chris Laurel <claurel@shatters.net>
// Copyright (C) 2001-2009, the Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// This program 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 2
// of the License, or (at your option) any later version.
// IMPORTANT: This file is a relic from the early days of Celestia.
// It's sole function now is to handle the now-deprecated .cms mesh files;
// it will eventually be removed from Celestia.
#include <cmath>
#include <celmath/mathlib.h>
#include <celmath/vecmath.h>
#include "gl.h"
#include "glext.h"
#include "spheremesh.h"
#include <celmath/mathlib.h>
using namespace Eigen;
SphereMesh::SphereMesh(float radius, int _nRings, int _nSlices) :
@ -21,14 +27,14 @@ SphereMesh::SphereMesh(float radius, int _nRings, int _nSlices) :
createSphere(radius, _nRings, _nSlices);
}
SphereMesh::SphereMesh(Vec3f size, int _nRings, int _nSlices) :
SphereMesh::SphereMesh(const Vector3f& size, int _nRings, int _nSlices) :
vertices(NULL), normals(NULL), texCoords(NULL), indices(NULL)
{
createSphere(1.0f, _nRings, _nSlices);
scale(size);
}
SphereMesh::SphereMesh(Vec3f size,
SphereMesh::SphereMesh(const Vector3f& size,
const DisplacementMap& dispmap,
float height) :
vertices(NULL), normals(NULL), texCoords(NULL), indices(NULL)
@ -40,7 +46,7 @@ SphereMesh::SphereMesh(Vec3f size,
fixNormals();
}
SphereMesh::SphereMesh(Vec3f size,
SphereMesh::SphereMesh(const Vector3f& size,
int _nRings, int _nSlices,
DisplacementMapFunc func,
void* info)
@ -123,7 +129,7 @@ void SphereMesh::createSphere(float radius, int _nRings, int _nSlices)
void SphereMesh::generateNormals()
{
int nQuads = nSlices * (nRings - 1);
Vec3f* faceNormals = new Vec3f[nQuads];
Vector3f* faceNormals = new Vector3f[nQuads];
int i;
// Compute face normals for the mesh
@ -139,21 +145,21 @@ void SphereMesh::generateNormals()
// Compute the face normal. Watch out for degenerate (zero-length)
// edges. If there are two degenerate edges, the entire face must
// be degenerate and we'll handle that later
Vec3f v0(p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2]);
Vec3f v1(p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]);
if (v0.length() < 1e-6f)
Vector3f v0(p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2]);
Vector3f v1(p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]);
if (v0.norm() < 1e-6f)
{
v0 = Vec3f(p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]);
v1 = Vec3f(p3[0] - p2[0], p3[1] - p2[1], p3[2] - p2[2]);
v0 = Vector3f(p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]);
v1 = Vector3f(p3[0] - p2[0], p3[1] - p2[1], p3[2] - p2[2]);
}
else if (v1.length() < 1e-6f)
else if (v1.norm() < 1e-6f)
{
v0 = Vec3f(p3[0] - p2[0], p3[1] - p2[1], p3[2] - p2[2]);
v1 = Vec3f(p0[0] - p3[0], p0[1] - p3[1], p0[2] - p3[2]);
v0 = Vector3f(p3[0] - p2[0], p3[1] - p2[1], p3[2] - p2[2]);
v1 = Vector3f(p0[0] - p3[0], p0[1] - p3[1], p0[2] - p3[2]);
}
Vec3f faceNormal = cross(v0, v1);
float length = faceNormal.length();
Vector3f faceNormal = v0.cross(v1);
float length = faceNormal.norm();
if (length != 0)
faceNormal *= (1 / length);
@ -178,21 +184,21 @@ void SphereMesh::generateNormals()
faceCounts[vertex] = 4;
int face = (i - 1) * nSlices + j % nSlices;
normals[vertex * 3] += faceNormals[face].x;
normals[vertex * 3 + 1] += faceNormals[face].y;
normals[vertex * 3 + 2] += faceNormals[face].z;
normals[vertex * 3] += faceNormals[face].x();
normals[vertex * 3 + 1] += faceNormals[face].y();
normals[vertex * 3 + 2] += faceNormals[face].z();
face = (i - 1) * nSlices + (j + nSlices - 1) % nSlices;
normals[vertex * 3] += faceNormals[face].x;
normals[vertex * 3 + 1] += faceNormals[face].y;
normals[vertex * 3 + 2] += faceNormals[face].z;
normals[vertex * 3] += faceNormals[face].x();
normals[vertex * 3 + 1] += faceNormals[face].y();
normals[vertex * 3 + 2] += faceNormals[face].z();
face = i * nSlices + (j + nSlices - 1) % nSlices;
normals[vertex * 3] += faceNormals[face].x;
normals[vertex * 3 + 1] += faceNormals[face].y;
normals[vertex * 3 + 2] += faceNormals[face].z;
normals[vertex * 3] += faceNormals[face].x();
normals[vertex * 3 + 1] += faceNormals[face].y();
normals[vertex * 3 + 2] += faceNormals[face].z();
face = i * nSlices + j % nSlices;
normals[vertex * 3] += faceNormals[face].x;
normals[vertex * 3 + 1] += faceNormals[face].y;
normals[vertex * 3 + 2] += faceNormals[face].z;
normals[vertex * 3] += faceNormals[face].x();
normals[vertex * 3 + 1] += faceNormals[face].y();
normals[vertex * 3 + 2] += faceNormals[face].z();
}
}
@ -205,9 +211,9 @@ void SphereMesh::generateNormals()
for (j = 0; j < nSlices; j++)
{
int face = j;
normals[vertex * 3] += faceNormals[face].x;
normals[vertex * 3 + 1] += faceNormals[face].y;
normals[vertex * 3 + 2] += faceNormals[face].z;
normals[vertex * 3] += faceNormals[face].x();
normals[vertex * 3 + 1] += faceNormals[face].y();
normals[vertex * 3 + 2] += faceNormals[face].z();
}
vertex = (nRings - 1) * (nSlices + 1) + i;
@ -215,9 +221,9 @@ void SphereMesh::generateNormals()
for (j = 0; j < nSlices; j++)
{
int face = nQuads - j - 1;
normals[vertex * 3] += faceNormals[face].x;
normals[vertex * 3 + 1] += faceNormals[face].y;
normals[vertex * 3 + 2] += faceNormals[face].z;
normals[vertex * 3] += faceNormals[face].x();
normals[vertex * 3 + 1] += faceNormals[face].y();
normals[vertex * 3 + 2] += faceNormals[face].z();
}
}
@ -252,28 +258,28 @@ void SphereMesh::fixNormals()
{
float* v0 = normals + (i * (nSlices + 1)) * 3;
float* v1 = normals + ((i + 1) * (nSlices + 1) - 1) * 3;
Vec3f n0(v0[0], v0[1], v0[2]);
Vec3f n1(v0[0], v0[1], v0[2]);
Vec3f normal = n0 + n1;
Vector3f n0(v0[0], v0[1], v0[2]);
Vector3f n1(v0[0], v0[1], v0[2]);
Vector3f normal = n0 + n1;
normal.normalize();
v0[0] = normal.x;
v0[1] = normal.y;
v0[2] = normal.z;
v1[0] = normal.x;
v1[1] = normal.y;
v1[2] = normal.z;
v0[0] = normal.x();
v0[1] = normal.y();
v0[2] = normal.z();
v1[0] = normal.x();
v1[1] = normal.y();
v1[2] = normal.z();
}
}
void SphereMesh::scale(Vec3f s)
void SphereMesh::scale(const Vector3f& s)
{
int i;
for (i = 0; i < nVertices; i++)
{
vertices[i * 3] *= s.x;
vertices[i * 3 + 1] *= s.y;
vertices[i * 3 + 2] *= s.z;
vertices[i * 3] *= s.x();
vertices[i * 3 + 1] *= s.y();
vertices[i * 3 + 2] *= s.z();
}
// Modify the normals
@ -281,15 +287,15 @@ void SphereMesh::scale(Vec3f s)
{
// TODO: Make a fast special case for uniform scale factors, where
// renormalization is not required.
Vec3f is(1.0f / s.x, 1.0f / s.y, 1.0f / s.z);
Vector3f is = s.cwise().inverse();
for (i = 0; i < nVertices; i++)
{
int n = i * 3;
Vec3f normal(normals[n] * is.x, normals[n + 1] * is.y, normals[n + 2] * is.z);
Vector3f normal(normals[n] * is.x(), normals[n + 1] * is.y(), normals[n + 2] * is.z());
normal.normalize();
normals[n] = normal.x;
normals[n + 1] = normal.y;
normals[n + 2] = normal.z;
normals[n] = normal.x();
normals[n + 1] = normal.y();
normals[n + 2] = normal.z();
}
}
}
@ -312,13 +318,13 @@ void SphereMesh::displace(const DisplacementMap& dispmap,
float y = (float) sin(phi);
float z = (float) (cos(phi) * sin(theta));
*/
Vec3f normal(normals[n], normals[n + 1], normals[n + 2]);
Vector3f normal(normals[n], normals[n + 1], normals[n + 2]);
int k = (j == nSlices) ? 0 : j;
Vec3f v = normal * dispmap.getDisplacement(k, i) * height;
vertices[n] += v.x;
vertices[n + 1] += v.y;
vertices[n + 2] += v.z;
Vector3f v = normal * dispmap.getDisplacement(k, i) * height;
vertices[n] += v.x();
vertices[n + 1] += v.y();
vertices[n + 2] += v.z();
}
}
}
@ -333,11 +339,11 @@ void SphereMesh::displace(DisplacementMapFunc func, void* info)
{
float u = (float) j / (float) nSlices;
int n = (i * (nSlices + 1) + j) * 3;
Vec3f normal(normals[n], normals[n + 1], normals[n + 2]);
Vec3f vert = normal * func(u, v, info);
vertices[n] += vert.x;
vertices[n + 1] += vert.y;
vertices[n + 2] += vert.z;
Vector3f normal(normals[n], normals[n + 1], normals[n + 2]);
Vector3f vert = normal * func(u, v, info);
vertices[n] += vert.x();
vertices[n + 1] += vert.y();
vertices[n + 2] += vert.z();
}
}
}

View File

@ -1,17 +1,20 @@
// mesh.h
// spheremesh.h
//
// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
// Copyright (C) 2001-2009, the Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// This program 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 2
// of the License, or (at your option) any later version.
#ifndef _SPHEREMESH_H_
#define _SPHEREMESH_H_
#ifndef _CELENGINE_SPHEREMESH_H_
#define _CELENGINE_SPHEREMESH_H_
// IMPORTANT: This file is a relic from the early days of Celestia.
// It's sole function now is to handle the now-deprecated .cms mesh files;
// it will eventually be removed from Celestia.
#include <celmath/vecmath.h>
#include <celmath/frustum.h>
#include <celengine/mesh.h>
#include <celengine/dispmap.h>
@ -24,11 +27,11 @@ class SphereMesh
{
public:
SphereMesh(float radius, int _nRings, int _nSlices);
SphereMesh(Vec3f size, int _nRings, int _nSlices);
SphereMesh(Vec3f size,
SphereMesh(const Eigen::Vector3f& size, int _nRings, int _nSlices);
SphereMesh(const Eigen::Vector3f& size,
const DisplacementMap& dispmap,
float height = 1.0f);
SphereMesh(Vec3f size,
SphereMesh(const Eigen::Vector3f& size,
int _nRings, int _nSlices,
DisplacementMapFunc func,
void* info);
@ -40,7 +43,7 @@ public:
private:
void createSphere(float radius, int nRings, int nSlices);
void generateNormals();
void scale(Vec3f);
void scale(const Eigen::Vector3f&);
void fixNormals();
void displace(const DisplacementMap& dispmap, float height);
void displace(DisplacementMapFunc func, void* info);
@ -56,4 +59,4 @@ public:
unsigned short* indices;
};
#endif // _SPHEREMESH_H_
#endif // _CELENGINE_SPHEREMESH_H_

View File

@ -11,8 +11,6 @@
#ifndef _CELENGINE_UNIVERSE_H_
#define _CELENGINE_UNIVERSE_H_
#include <celmath/vecmath.h>
#include <celmath/quaternion.h>
#include <celengine/univcoord.h>
#include <celengine/stardb.h>
#include <celengine/dsodb.h>

View File

@ -3,9 +3,10 @@
#include <math.h>
#include "mathlib.h"
#include "vecmath.h"
#include "perlin.h"
using namespace Eigen;
float bias(float a, float b)
{
@ -56,15 +57,15 @@ float turbulence(float v[], float freq)
}
float turbulence(const Point2f& p, float freq)
float turbulence(const Vector2f& p, float freq)
{
float t;
float vec[2];
for (t = 0.0f; freq >= 1.0f; freq *= 0.5f)
{
vec[0] = freq * p.x;
vec[1] = freq * p.y;
vec[0] = freq * p.x();
vec[1] = freq * p.y();
t += (float) fabs(noise2(vec)) / freq;
}
@ -72,16 +73,16 @@ float turbulence(const Point2f& p, float freq)
}
float turbulence(const Point3f& p, float freq)
float turbulence(const Vector3f& p, float freq)
{
float t;
float vec[3];
for (t = 0.0f; freq >= 1.0f; freq *= 0.5f)
{
vec[0] = freq * p.x;
vec[1] = freq * p.y;
vec[2] = freq * p.z;
vec[0] = freq * p.x();
vec[1] = freq * p.y();
vec[2] = freq * p.z();
t += (float) fabs(noise3(vec)) / freq;
}
@ -105,15 +106,15 @@ float fractalsum(float v[], float freq)
}
float fractalsum(const Point2f& p, float freq)
float fractalsum(const Vector2f& p, float freq)
{
float t;
float vec[2];
for (t = 0.0f; freq >= 1.0f; freq *= 0.5f)
{
vec[0] = freq * p.x;
vec[1] = freq * p.y;
vec[0] = freq * p.x();
vec[1] = freq * p.y();
t += noise2(vec) / freq;
}
@ -121,16 +122,16 @@ float fractalsum(const Point2f& p, float freq)
}
float fractalsum(const Point3f& p, float freq)
float fractalsum(const Vector3f& p, float freq)
{
float t;
float vec[3];
for (t = 0.0f; freq >= 1.0f; freq *= 0.5f)
{
vec[0] = freq * p.x;
vec[1] = freq * p.y;
vec[2] = freq * p.z;
vec[0] = freq * p.x();
vec[1] = freq * p.y();
vec[2] = freq * p.z();
t += noise3(vec) / freq;
}

View File

@ -3,7 +3,8 @@
#ifndef _PERLIN_H_
#define _PERLIN_H_
#include <celmath/vecmath.h>
#include <Eigen/Core>
extern float noise(float vec[], int len);
@ -12,10 +13,10 @@ extern float noise2(float vec[]);
extern float noise3(float vec[]);
extern float turbulence(float v[], float freq);
extern float turbulence(const Point2f& p, float freq);
extern float turbulence(const Point3f& p, float freq);
extern float turbulence(const Eigen::Vector2f& p, float freq);
extern float turbulence(const Eigen::Vector3f& p, float freq);
extern float fractalsum(float v[], float freq);
extern float fractalsum(const Point2f& p, float freq);
extern float fractalsum(const Point3f& p, float freq);
extern float fractalsum(const Eigen::Vector2f& p, float freq);
extern float fractalsum(const Eigen::Vector3f& p, float freq);
#endif // _PERLIN_H_