Sign in to follow this  
Shamino

Vector3 Undeclared

Recommended Posts

#ifndef	GL_RENDERER_H
#define	GL_RENDERER_H

#include <vector>

using namespace std;

struct	BasicQuad
{
	Vector3 P1, P2, P3, P4;
	int	Texture;//Index into gpTextureManager
};

std::vector<BasicQuad*>	mBasicQuads;

class GLRenderer
{
public:
	GLRenderer()
	{

	}

	
	void	AddBasicQuadToRenderer(float P1X,float  P1Y,float	P1Z,
								   float P2X,float	P2Y,float	P2Z,
								   float P3X,float	P3Y,float	P3Z,
								   float P4X,float	P4Y,float	P4Z,
								   int	Texture)
	{
		BasicQuad	*pData = new	BasicQuad;
		pData->P1	=	Vector3(P1X,P1Y,P1Z);
		pData->P2	=	Vector3(P2X,P2Y,P2Z);
		pData->P3	=	Vector3(P3X,P3Y,P3Z);
		pData->P4	=	Vector3(P4X,P4Y,P4Z);
		pData->Texture	=	Texture;
		mBasicQuads.push_back(pData);
	}
};



#endif
Do I need to declare vector3 myself? is there a header file? How would I declare vector3?

Share this post


Link to post
Share on other sites
That vector include you have is not what you think. It is for the vector data structure (similar to a linked-list).

I'm not sure if Vector3 is defined anyhere, but it's easy enough to write your own. (I'm working without a compiler here so forgive any syntax mistakes)

For starters you could just do a tyopedef:

typedef float[3] Vector3


But you probably want to create a class:

class Vector3
{
float x;
float y;
float z;

Vector3();
Vector3(float a_x, float a_y, float a_z);

// And so on
};


Hope that helps!
Matt

Share this post


Link to post
Share on other sites
Quote:
Original post by Xpyder
#include <vector>


shouldnt that be vector.h?

I'm not sure because its a while since I did any C++, but its worth a try


Nope. Header files in C++ generally don't use the .h suffix - well at least the ones that are part of the language, feel free to use .h in your own files. In addition, if you want to use header files from C do it like this:

// don't do it this way
#include <stdlib.h>
#include <stdio.h"

// do it this way
#include <cstdlib>
#include <cstdio>


As mentioned above, there's a difference between std::vector, which is part of the Standard Library, and Vector3 which isn't part of the language. Writing your own Vector3 shouldn't be too tough.

Share this post


Link to post
Share on other sites
WOOT, this worked, finally, I have a working AddBasicQuadToRenderer function!

#include <vector>

using namespace std;

class Vector3
{
public:
float x;
float y;
float z;

Vector3();
Vector3(float a_x, float a_y, float a_z);

// And so on
};
struct BasicQuad
{
Vector3 P1, P2, P3, P4;
int Texture;//Index into gpTextureManager
};

std::vector<BasicQuad*> mBasicQuads;



class GLRenderer
{
public:
GLRenderer()
{

}


void AddBasicQuadToRenderer(float P1X,float P1Y,float P1Z,
float P2X,float P2Y,float P2Z,
float P3X,float P3Y,float P3Z,
float P4X,float P4Y,float P4Z,
int Texture)
{
BasicQuad *pData = new BasicQuad;
pData->P1 = Vector3(P1X,P1Y,P1Z);
pData->P2 = Vector3(P2X,P2Y,P2Z);
pData->P3 = Vector3(P3X,P3Y,P3Z);
pData->P4 = Vector3(P4X,P4Y,P4Z);
pData->Texture = Texture;
mBasicQuads.push_back(pData);
}
};



Kept it simple, rewrote it, now it looks like this

class Vector3
{
public:

Vector3()
{
}

Vector3(float x, float y, float z);


};

Share this post


Link to post
Share on other sites
Next you'll want to add operator overloads to your Vector3 class, ie. provide functions to perform addition, subtraction, etc. These will allow you to work with the vector more 'naturally'.

For example:

Vector3 operator+(const Vector3& lhs, const Vector3& rhs)
{
Vector3 result;
result.x = lhs.x + rhs.x;
result.y = lhs.y + rhs.y;
result.z = lhs.z + rhs.z;
return result;
}

void test()
{
Vector3 a(1,2,3);
Vector3 b(4,5,6);
a = a + b;
// Now a==(5,7,9)
}


Share this post


Link to post
Share on other sites
I just wrote this one yesterday, if anyone's interested:

#ifndef MATH_VECTOR_HPP
#define MATH_VECTOR_HPP

/*
* math/vector.hpp - Generic N-ary vector
*
* Copyright (c) 2005 Scott McMurray ( me22@users.sf.net )
*
* Distributed under the Boost Software License, Version 1.0.
* ( See http://www.boost.org/LICENSE_1_0.txt )
*
*/


#include <cstddef>
#include <algorithm>
#include <functional>
#include <numeric>

namespace math {

template < unsigned N = 3, typename T = float >
class vector {
T v_[N];
public:
typedef T value_type;
typedef value_type const const_value_type;

typedef value_type& reference;
typedef const_value_type& const_reference;

typedef value_type* pointer;
typedef const_value_type* const_pointer;

typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef const_value_type parameter_type;

vector() {
std::fill_n( v_, N, 0 );
}
explicit vector(parameter_type x) {
std::fill_n( v_, N, x );
}
vector(parameter_type x, parameter_type y) {
v_[0] = x; if ( N > 1 )
v_[1] = y;
}
vector(parameter_type x, parameter_type y, parameter_type z) {
v_[0] = x; if ( N > 1 )
v_[1] = y; if ( N > 2 )
v_[2] = z;
}
explicit vector(const_pointer p, size_type n = N) {
if ( n > N ) n = N;
copy( p, p+n,
v_ );
fill_n( v_+n, N-n, 0 );
}

const_pointer v() const {
return v_;
}
pointer v() {
return const_cast<pointer>(
const_cast<vector const &>(*this).v()
);
}
static size_type size() {
return N;
}

const_reference operator[](size_type i) const {
assert( i < N && "=> index out of range" );
return v_[i];
}
reference operator[](size_type i) {
return const_cast<reference>(
const_cast<vector const &>(*this).operator[](i)
);
}

vector &operator+=(vector const &vec) {
std::transform( v(), v()+size(),
vec.v(),
v(),
std::plus<T>() );
return *this;
}
vector &operator-=(vector const &vec) {
std::transform( v(), v()+size(),
vec.v(),
v(),
std::minus<T>() );
return *this;
}

vector &operator*=(parameter_type c) {
std::transform( v(), v()+size(),
v(),
std::bind2nd( std::multiplies<T>(), c ) );
return *this;
}
vector &operator/=(parameter_type c) {
return operator*=( 1 / c );
}

};

template < unsigned N, typename T >
bool operator==(vector<N,T> const &lhs, vector<N,T> const &rhs) {
return std::equal( lhs.v(), lhs.v()+N,
rhs.v() );
}
template < unsigned N, typename T >
bool operator!=(vector<N,T> const &lhs, vector<N,T> const &rhs) {
return !( lhs == rhs );
}

template < unsigned N, typename T >
vector<N,T> operator+(vector<N,T> lhs, vector<N,T> const &rhs) {
return lhs += rhs;
}
template < unsigned N, typename T >
vector<N,T> operator-(vector<N,T> lhs, vector<N,T> const &rhs) {
return lhs -= rhs;
}

template < unsigned N, typename T >
vector<N,T> operator*(vector<N,T> vec,
typename vector<N,T>::parameter_type c) {
return vec*=c;
}
template < unsigned N, typename T >
vector<N,T> operator*(typename vector<N,T>::parameter_type c,
vector<N,T> vec) {
return vec*=c;
}
template < unsigned N, typename T >
vector<N,T> operator/(vector<N,T> vec,
typename vector<N,T>::parameter_type c) {
return vec/=c;
}

template < unsigned N, typename T >
vector<N,T> operator-(vector<N,T> const &vec) {
return vec * -1;
}

template < unsigned N, typename T >
T dot(vector<N,T> lhs, vector<N,T> const &rhs) {
return std::inner_product( lhs.v(), lhs.v()+N,
rhs.v(),
static_cast<T>(0) );
}

template < typename T >
T cross(vector<2,T> const &lhs, vector<2,T> const &rhs) {
return lhs[0]*rhs[1]-lhs[1]*rhs[0];
}
template < typename T >
vector<3,T> cross(vector<3,T> const &lhs, vector<3,T> const &rhs) {
return vector<3,T>( lhs[1]*rhs[2]-lhs[2]*rhs[1],
lhs[2]*rhs[0]-lhs[0]*rhs[2],
lhs[0]*rhs[1]-lhs[1]*rhs[0] );
}

// Note: norm == length*length
template < unsigned N, typename T >
T norm(vector<N,T> const &vec) {
return dot( vec, vec );
}

} // namespace math

#endif




you could always typedef math::vector<3,float> Vector3 :P

Note that it's functional, but not optimized. I just used the std algorithms to get it working and testable. It might benefit from some template unrolling in the dot products and such, although a compiler might be able to do that itself...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this