Integrated hip new vector use that all the young people are into these days, from Bryan Kadzban. Much cleaner.

This commit is contained in:
Shamus Young 2009-05-08 09:04:53 +00:00
parent 00afbde126
commit 94871004c0
6 changed files with 140 additions and 260 deletions

View File

@ -157,7 +157,7 @@ void CBuilding::ConstructCube (int left, int right, int front, int back, int bot
GLvertex p[10];
float x1, x2, z1, z2, y1, y2;
int i;
int index_list[10];
cube c;
float u, v1, v2;
float mapping;
int base_index;
@ -194,10 +194,9 @@ void CBuilding::ConstructCube (int left, int right, int front, int back, int bot
for (i = 0; i < 10; i++) {
p[i].uv.x = (p[i].position.x + p[i].position.z) / (float)SEGMENTS_PER_TEXTURE;
_mesh->VertexAdd (p[i]);
index_list[i] = base_index + i;
c.index_list.push_back(base_index + i);
}
_mesh->CubeAdd (&index_list[0]);
_mesh->CubeAdd (c);
}
@ -211,7 +210,7 @@ void CBuilding::ConstructCube (float left, float right, float front, float back,
GLvertex p[10];
float x1, x2, z1, z2, y1, y2;
int i;
int index_list[10];
cube c;
int base_index;
x1 = left;
@ -235,10 +234,9 @@ void CBuilding::ConstructCube (float left, float right, float front, float back,
for (i = 0; i < 10; i++) {
p[i].uv.x = (p[i].position.x + p[i].position.z) / (float)SEGMENTS_PER_TEXTURE;
_mesh_flat->VertexAdd (p[i]);
index_list[i] = base_index + i;
c.index_list.push_back(base_index + i);
}
_mesh_flat->CubeAdd (&index_list[0]);
_mesh_flat->CubeAdd (c);
}
@ -362,13 +360,13 @@ void CBuilding::ConstructSpike (int left, int right, int front, int back, int bo
{
GLvertex p;
int index_list[6];
fan f;
int i;
GLvector center;
for (i = 0; i < 5; i++)
index_list[i] = _mesh_flat->VertexCount () + i;
index_list[5] = index_list[1];
f.index_list.push_back(_mesh_flat->VertexCount () + i);
f.index_list.push_back(f.index_list[1]);
p.uv = glVector (0.0f, 0.0f);
center.x = ((float)left + (float)right) / 2.0f;
center.z = ((float)front + (float)back) / 2.0f;
@ -387,7 +385,7 @@ void CBuilding::ConstructSpike (int left, int right, int front, int back, int bo
p.position = glVector ((float)left, (float)bottom, (float)front);
_mesh_flat->VertexAdd (p);
_mesh_flat->FanAdd (&index_list[0], 6);
_mesh_flat->FanAdd (f);
}
@ -406,8 +404,7 @@ float CBuilding::ConstructWall (int start_x, int start_y, int start_z, int direc
int x, z;
int step_x, step_z;
int i;
int index_list[100];
int current_index;
quad_strip qs;
int column;
int mid;
int odd;
@ -415,6 +412,8 @@ float CBuilding::ConstructWall (int start_x, int start_y, int start_z, int direc
bool blank;
bool last_blank;
qs.index_list.reserve(100);
switch (direction) {
case NORTH:
step_z = 1; step_x = 0; break;
@ -427,7 +426,6 @@ float CBuilding::ConstructWall (int start_x, int start_y, int start_z, int direc
}
x = start_x;;
z = start_z;
current_index = 0;
mid = (length / 2) - 1;
odd = 1 - (length % 2);
if (length % 2)
@ -452,13 +450,11 @@ float CBuilding::ConstructWall (int start_x, int start_y, int start_z, int direc
v.position = glVector ((float)x, (float)start_y, (float)z);
v.uv.y = (float)start_y / SEGMENTS_PER_TEXTURE;
_mesh->VertexAdd (v);
index_list[current_index] = _mesh->VertexCount () - 1;
current_index++;
qs.index_list.push_back(_mesh->VertexCount () - 1);
v.position.y = (float)(start_y + height);
v.uv.y = (float)(start_y + height) / SEGMENTS_PER_TEXTURE;;
_mesh->VertexAdd (v);
index_list[current_index] = _mesh->VertexCount () - 1;
current_index++;
qs.index_list.push_back(_mesh->VertexCount () - 1);
}
//if (!blank && i != 0 && i != (length - 1))
if (!blank && i != length)
@ -466,13 +462,11 @@ float CBuilding::ConstructWall (int start_x, int start_y, int start_z, int direc
x += step_x;
z += step_z;
}
_mesh->QuadStripAdd (&index_list[0], current_index);
_mesh->QuadStripAdd (qs);
return v.uv.x;
}
static int maxt;
/*-----------------------------------------------------------------------------
This makes a big chunky building of intersecting cubes.
@ -524,9 +518,6 @@ void CBuilding::CreateBlocky ()
max_tiers = 2;
else
max_tiers = 1;
max_tiers = MIN (maxt, max_tiers);
maxt++;
maxt %= 8;
//We begin at the top of the building, and work our way down.
//Viewed from above, the sections of the building are randomly sized
//rectangles that ALWAYS include the center of the building somewhere within
@ -588,11 +579,14 @@ void CBuilding::CreateSimple ()
GLvertex p;
float x1, x2, z1, z2, y1, y2;
int index_list[] = {0,1,2,3,4,5,6,7,8,9,10};
quad_strip qs;
float u, v1, v2;
float cap_height;
float ledge;
for(int i=0; i<=10; i++)
qs.index_list.push_back(i);
//How tall the flat-color roof is
cap_height = (float)(1 + RandomVal (4));
//how much the ledge sticks out
@ -638,12 +632,13 @@ void CBuilding::CreateSimple ()
p.position = glVector (x1, y2, z1); p.uv = glVector (u, v2);
_mesh->VertexAdd (p);
_mesh->QuadStripAdd (index_list, 10);
_mesh->QuadStripAdd (qs);
ConstructCube (x1 - ledge, x2 + ledge, z2 - ledge, z1 + ledge, (float)_height, (float)_height + cap_height);
_mesh->Compile ();
}
/*-----------------------------------------------------------------------------
This makes a deformed cylinder building.
@ -664,11 +659,13 @@ void CBuilding::CreateModern ()
int half_depth, half_width;
float dist;
float length;
int* index_list;
quad_strip qs;
fan f;
int points;
int skip_interval;
int skip_counter;
int skip_delta;
int i;
bool logo_done;
bool do_trim;
CDeco* d;
@ -735,21 +732,20 @@ void CBuilding::CreateModern ()
d = new CDeco;
d->CreateLightTrim (vector_buffer, (points / 2) - 2, (float)cap_height / 2, _seed, RANDOM_COLOR);
}
index_list = new int[points];
qs.index_list.reserve(points);
//Add the outer walls
for (int i = 0; i < points; i++)
index_list[i] = i;
_mesh->QuadStripAdd (index_list, points);
_mesh_flat->QuadStripAdd (index_list, points);
for (i = 0; i < points; i++)
qs.index_list.push_back(i);
_mesh->QuadStripAdd (qs);
_mesh_flat->QuadStripAdd (qs);
//add the fan to cap the top of the buildings
f.index_list.push_back(points);
for (i = 0; i < points / 2; i++)
index_list[i + 1] = points - (1 + i * 2);
f.index_list.push_back(points - (1 + i * 2));
p.position.x = _center.x;
p.position.z = _center.z;
_mesh_flat->VertexAdd (p);
index_list[0] = points;
_mesh_flat->FanAdd (index_list, 1 + points / 2);
delete index_list;
_mesh_flat->FanAdd (f);
radius /= 2.0f;
//ConstructRoof ((int)(_center.x - radius), (int)(_center.x + radius), (int)(_center.z - radius), (int)(_center.z + radius), _height + cap_height);
_mesh->Compile ();

View File

@ -121,7 +121,10 @@ void CDeco::CreateRadioTower (GLvector pos, float height)
CLight* l;
float offset;
GLvertex v;
int index_list[] = {0,1,2,3,4,5};
fan f;
for(int i=0; i<6; i++)
f.index_list.push_back(i);
offset = height / 15.0f;
_center = pos;
@ -139,7 +142,7 @@ void CDeco::CreateRadioTower (GLvector pos, float height)
_mesh->VertexAdd (v);
v.position = glVector (_center.x - offset, _center.y, _center.z - offset); v.uv = glVector (1,0);
_mesh->VertexAdd (v);
_mesh->FanAdd (&index_list[0], 6);
_mesh->FanAdd (f);
l = new CLight (glVector (_center.x, _center.y + height + 1.0f, _center.z), glRgba (255,192,160), 1);
l->Blink ();
_texture = TextureId (TEXTURE_LATTICE);
@ -153,15 +156,20 @@ void CDeco::CreateRadioTower (GLvector pos, float height)
void CDeco::CreateLogo (GLvector2 start, GLvector2 end, float bottom, int seed, GLrgba color)
{
GLvertex p;
int index_list[] = {0,1,3,2};
float u1, u2, v1, v2;
float top;
float height, length;
GLvector2 center2d;
GLvector to;
GLvector out;
int logo_index;
GLvertex p;
quad_strip qs;
float u1, u2, v1, v2;
float top;
float height, length;
GLvector2 center2d;
GLvector to;
GLvector out;
int logo_index;
qs.index_list.push_back(0);
qs.index_list.push_back(1);
qs.index_list.push_back(3);
qs.index_list.push_back(2);
_use_alpha = true;
_color = color;
@ -186,7 +194,7 @@ void CDeco::CreateLogo (GLvector2 start, GLvector2 end, float bottom, int seed,
_mesh->VertexAdd (p);
p.position = glVector (start.x, top, start.y) + out; p.uv = glVector (u1, v2);
_mesh->VertexAdd (p);
_mesh->QuadStripAdd (&index_list[0], 4);
_mesh->QuadStripAdd (qs);
_texture = TextureId (TEXTURE_LOGOS);
}
@ -198,10 +206,14 @@ void CDeco::CreateLogo (GLvector2 start, GLvector2 end, float bottom, int seed,
void CDeco::CreateLightStrip (float x, float z, float width, float depth, float height, GLrgba color)
{
GLvertex p;
int index_list1[] = {0,1,3,2};
float u, v;
GLvertex p;
quad_strip qs1;
float u, v;
qs1.index_list.push_back(0);
qs1.index_list.push_back(1);
qs1.index_list.push_back(3);
qs1.index_list.push_back(2);
_color = color;
_use_alpha = true;
_center = glVector (x + width / 2, height, z + depth / 2);
@ -221,8 +233,7 @@ void CDeco::CreateLightStrip (float x, float z, float width, float depth, float
_mesh->VertexAdd (p);
p.position = glVector (x + width, height, z); p.uv = glVector (u, 0.0f);
_mesh->VertexAdd (p);
_mesh->QuadStripAdd (&index_list1[0], 4);
_mesh->QuadStripAdd (qs1);
_mesh->Compile ();
}
@ -234,19 +245,19 @@ void CDeco::CreateLightStrip (float x, float z, float width, float depth, float
void CDeco::CreateLightTrim (GLvector* chain, int count, float height, int seed, GLrgba color)
{
GLvertex p;
GLvector to;
GLvector out;
int i;
int index;
int prev, next;
float u, v1, v2;
float row;
int* index_list;
GLvertex p;
GLvector to;
GLvector out;
int i;
int index;
int prev, next;
float u, v1, v2;
float row;
quad_strip qs;
_color = color;
_center = glVector (0.0f, 0.0f, 0.0f);
index_list = new int[count * 2 + 2];
qs.index_list.reserve(count * 2 + 2);
for (i = 0; i < count; i++)
_center += chain[i];
_center /= (float)count;
@ -267,18 +278,14 @@ void CDeco::CreateLightTrim (GLvector* chain, int count, float height, int seed,
out = glVectorCrossProduct (glVector (0.0f, 1.0f, 0.0f), to) * LOGO_OFFSET;
p.position = chain[i % count] + out; p.uv = glVector (u, v2);
_mesh->VertexAdd (p);
index_list[index] = index;
index++;
qs.index_list.push_back(index++);
//Top point
p.position.y += height;p.uv = glVector (u, v1);
_mesh->VertexAdd (p);
index_list[index] = index;
index++;
qs.index_list.push_back(index++);
}
_mesh->QuadStripAdd (index_list, index);
delete index_list;
_mesh->QuadStripAdd (qs);
_texture = TextureId (TEXTURE_TRIM);
_mesh->Compile ();
}

128
Mesh.cpp
View File

@ -16,12 +16,12 @@
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include "glTypes.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "mesh.h"
#include <vector>
#include "glTypes.h"
#include "Mesh.h"
/*-----------------------------------------------------------------------------
@ -30,20 +30,9 @@
CMesh::CMesh ()
{
_vertex_count = 0;
_triangle_count = 0;
_quad_strip_count = 0;
_fan_count = 0;
_cube_count = 0;
_polycount = 0;
_list = glGenLists(1);
_compiled = false;
_vertex = NULL;
_normal = NULL;
_triangle = NULL;
_cube = NULL;
_quad_strip = NULL;
_fan = NULL;
_polycount = 0;
}
@ -54,24 +43,6 @@ CMesh::CMesh ()
CMesh::~CMesh ()
{
unsigned i;
if (_vertex)
free (_vertex);
if (_normal)
free (_normal);
if (_triangle)
free (_triangle);
if (_cube)
free (_cube);
for (i = 0; i < _quad_strip_count; i++)
delete _quad_strip[i].index_list;
if (_quad_strip)
delete _quad_strip;
for (i = 0; i < _fan_count; i++)
delete _fan[i].index_list;
if (_fan)
delete _fan;
if (_list)
glDeleteLists (_list, 1);
@ -81,12 +52,10 @@ CMesh::~CMesh ()
-----------------------------------------------------------------------------*/
void CMesh::VertexAdd (GLvertex v)
void CMesh::VertexAdd (const GLvertex& v)
{
_vertex = (GLvertex*)realloc (_vertex, sizeof (GLvertex) * (_vertex_count + 1));
_vertex[_vertex_count] = v;
_vertex_count++;
_vertex.push_back(v);
}
@ -94,12 +63,10 @@ void CMesh::VertexAdd (GLvertex v)
-----------------------------------------------------------------------------*/
void CMesh::CubeAdd (int* index)
void CMesh::CubeAdd (const cube& c)
{
_cube = (cube*)realloc (_cube, sizeof (cube) * (_cube_count + 1));
memcpy (&_cube[_cube_count].index_list[0], index, sizeof (int) * 10);
_cube_count++;
_cube.push_back(c);
_polycount += 5;
}
@ -108,15 +75,11 @@ void CMesh::CubeAdd (int* index)
-----------------------------------------------------------------------------*/
void CMesh::QuadStripAdd (int* index, int count)
void CMesh::QuadStripAdd (const quad_strip& qs)
{
_quad_strip = (quad_strip*)realloc (_quad_strip, sizeof (quad_strip) * (_quad_strip_count + 1));
_quad_strip[_quad_strip_count].index_list = (int*)malloc (sizeof (int) * count);
_quad_strip[_quad_strip_count].count = count;
memcpy (&_quad_strip[_quad_strip_count].index_list[0], &index[0], sizeof (int) * count);
_quad_strip_count++;
_polycount += (count - 2) / 2;
_quad_strip.push_back(qs);
_polycount += (qs.index_list.size() - 2) / 2;
}
@ -125,15 +88,11 @@ void CMesh::QuadStripAdd (int* index, int count)
-----------------------------------------------------------------------------*/
void CMesh::FanAdd (int* index, int count)
void CMesh::FanAdd (const fan& f)
{
_fan = (fan*)realloc (_fan, sizeof (fan) * (_fan_count + 1));
_fan[_fan_count].index_list = (int*)malloc (sizeof (int) * count);
_fan[_fan_count].count = count;
memcpy (&_fan[_fan_count].index_list[0], &index[0], sizeof (int) * count);
_fan_count++;
_polycount += count - 2;
_fan.push_back(f);
_polycount += f.index_list.size() - 2;
}
@ -145,55 +104,54 @@ void CMesh::FanAdd (int* index, int count)
void CMesh::Render ()
{
unsigned i, n;
int* index;
std::vector<quad_strip>::iterator qsi;
std::vector<cube>::iterator ci;
std::vector<fan>::iterator fi;
std::vector<int>::iterator n;
if (_compiled) {
glCallList (_list);
return;
}
for (i = 0; i < _quad_strip_count; i++) {
index = &_quad_strip[i].index_list[0];
for (qsi = _quad_strip.begin(); qsi < _quad_strip.end(); ++qsi) {
glBegin (GL_QUAD_STRIP);
for (n = 0; n < _quad_strip[i].count; n++) {
glTexCoord2fv (&_vertex[index[n]].uv.x);
glVertex3fv (&_vertex[index[n]].position.x);
for (n = qsi->index_list.begin(); n < qsi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
for (i = 0; i < _cube_count; i++) {
index = &_cube[i].index_list[0];
for (ci = _cube.begin(); ci < _cube.end(); ++ci) {
glBegin (GL_QUAD_STRIP);
for (n = 0; n < 10; n++) {
glTexCoord2fv (&_vertex[index[n]].uv.x);
glVertex3fv (&_vertex[index[n]].position.x);
for (n = ci->index_list.begin(); n < ci->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[index[7]].uv.x);
glVertex3fv (&_vertex[index[7]].position.x);
glVertex3fv (&_vertex[index[5]].position.x);
glVertex3fv (&_vertex[index[3]].position.x);
glVertex3fv (&_vertex[index[1]].position.x);
glTexCoord2fv (&_vertex[ci->index_list[7]].uv.x);
glVertex3fv (&_vertex[ci->index_list[7]].position.x);
glVertex3fv (&_vertex[ci->index_list[5]].position.x);
glVertex3fv (&_vertex[ci->index_list[3]].position.x);
glVertex3fv (&_vertex[ci->index_list[1]].position.x);
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[index[6]].uv.x);
glVertex3fv (&_vertex[index[0]].position.x);
glVertex3fv (&_vertex[index[2]].position.x);
glVertex3fv (&_vertex[index[4]].position.x);
glVertex3fv (&_vertex[index[6]].position.x);
glTexCoord2fv (&_vertex[ci->index_list[6]].uv.x);
glVertex3fv (&_vertex[ci->index_list[0]].position.x);
glVertex3fv (&_vertex[ci->index_list[2]].position.x);
glVertex3fv (&_vertex[ci->index_list[4]].position.x);
glVertex3fv (&_vertex[ci->index_list[6]].position.x);
glEnd ();
}
for (i = 0; i < _fan_count; i++) {
index = &_fan[i].index_list[0];
for (fi = _fan.begin(); fi < _fan.end(); ++fi) {
glBegin (GL_TRIANGLE_FAN);
for (n = 0; n < _fan[i].count; n++) {
glTexCoord2fv (&_vertex[index[n]].uv.x);
glVertex3fv (&_vertex[index[n]].position.x);
for (n = fi->index_list.begin(); n < fi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
@ -213,4 +171,4 @@ void CMesh::Compile ()
glEndList();
_compiled = true;
}
}

49
Mesh.h
View File

@ -1,50 +1,43 @@
#include <vector>
struct cube
{
int index_list[10];
std::vector<int> index_list; // probably always .size() == 10...
};
struct quad_strip
{
int* index_list;
unsigned count;
std::vector<int> index_list;
};
struct fan
{
int* index_list;
unsigned count;
std::vector<int> index_list;
};
class CMesh
{
public:
CMesh ();
~CMesh ();
unsigned _vertex_count;
unsigned _triangle_count;
unsigned _cube_count;
unsigned _quad_strip_count;
unsigned _fan_count;
unsigned _normal_count;
unsigned _list;
int _polycount;
GLvertex* _vertex;
GLvector* _normal;
GLtriangle* _triangle;
cube* _cube;
quad_strip* _quad_strip;
fan* _fan;
bool _compiled;
CMesh ();
~CMesh ();
unsigned _list;
int _polycount;
std::vector<GLvertex> _vertex;
std::vector<GLvector> _normal;
std::vector<GLtriangle> _triangle;
std::vector<cube> _cube;
std::vector<quad_strip> _quad_strip;
std::vector<fan> _fan;
bool _compiled;
void NormalAdd (GLvector n);
void VertexAdd (GLvertex v);
int VertexCount () { return _vertex_count; }
void NormalAdd (const GLvector& n);
void VertexAdd (const GLvertex& v);
int VertexCount () { return _vertex.size(); }
int PolyCount () { return _polycount; }
void CubeAdd (int* index);
void QuadStripAdd (int* index, int count);
void FanAdd (int* index, int count);
void CubeAdd (const cube& c);
void QuadStripAdd (const quad_strip& qs);
void FanAdd (const fan& f);
void Render ();
void Compile ();

View File

@ -64,13 +64,13 @@ void CSky::Render ()
GLvector angle, position;
if (!RenderFog ())
if (!TextureReady ())
return;
glDepthMask (false);
glPushAttrib (GL_POLYGON_BIT | GL_FOG_BIT);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glDisable (GL_FOG);
glDisable (GL_CULL_FACE);
glDisable (GL_FOG);
glPushMatrix ();
glLoadIdentity();
angle = CameraAngle ();

74
foo.h
View File

@ -1,74 +0,0 @@
i
Green
Mega
Super
Omni
e
Hyper
Global
Vital
Next
Pacific
Metro
Unity
G-
Trans
Infinity
Superior
Monolith
Best
Atlantic
First
Union
National
Biotic
Info
Data
Solar
Aerospace
Motors
Nano
Online
Circuits
Energy
Med
Robotic
Exports
Security
Systems
Financial
Industrial
Media
Materials
Foods
Networks
Shipping
Tools
Medical
Publishing
Enterpise
Audio
Health
Bank
Imports
Apparel
Petroleum
Studios
Corp
Inc.
Co
World
.Com
USA
Ltd.
Net
Tech
Labs
Mfg.
UK
Unlimited
One