Browse Source

3d架构基本完成

master
blobt 5 years ago
parent
commit
41c4cbf2a2
  1. 2
      src/5threed/Image.h
  2. 163
      src/5threed/Raster.cc
  3. 15
      src/5threed/Raster.h
  4. 5418
      src/5threed/common.h
  5. 159
      src/5threed/struct/common.h
  6. 175
      src/5threed/struct/plane.h
  7. 87
      src/5threed/struct/tfrustum.h
  8. 6
      src/5threed/struct/tmat4x4.h
  9. 67
      src/5threed/struct/tvec3.h
  10. 53
      src/5threed/struct/tvec4.h
  11. 15
      src/5threed/threed.cc

2
src/5threed/Image.h

@ -40,7 +40,7 @@ public:
//
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
if (fifmt == FIF_UNKNOWN) {
die("FreeImage_GetFileType");
//die("FreeImage_GetFileType");
}
//

163
src/5threed/Raster.cc

@ -43,6 +43,8 @@ Raster::Raster(int width, int height) {
_defaultUvPointer.data = _defaultUvArray;
_matModel = matrix4(1);
_matView = matrix4(1);
_matProj = matrix4(1);
}
Raster::~Raster() {
@ -186,7 +188,7 @@ void Raster::drawSpan(const Span &span, Image* image) {
setPixel(i, span.y, dstColor);
setPixel(i, span.y, blendColor);
}
}
@ -321,62 +323,73 @@ void Raster::drawArrays(DRAWMODE pri, int start, int count) {
char* cData = (char*) colorPointerdesc.data;
char* uvData = (char*) uvPointerdesc.data;
_matProjView = _matProj * _matView;
matrix4 matPVT = _matProjView.transpose();
_frust.loadFrustum(matPVT);
for (int i = start; i < start + count; i += 3) {
float* pp = (float*) posData;
float4 mp0(pp[0], pp[1], pp[2], 1);
mp0 = _matModel * mp0;
float3 mp0(pp[0], pp[1], pp[2]);
mp0 = mp0 * _matModel;
posData += _positionPointer.stride;
pp = (float*) posData;
float4 mp1(pp[0], pp[1], pp[2], 1);
mp1 = _matModel * mp1;
float3 mp1(pp[0], pp[1], pp[2]);
mp1 = mp1 * _matModel;
posData += _positionPointer.stride;
pp = (float*) posData;
float4 mp2(pp[0], pp[1], pp[2], 1);
mp2 = _matModel * mp2;
int2 p0(mp0.x, mp0.y);
int2 p1(mp1.x, mp1.y);
int2 p2(mp2.x, mp2.y);
posData += _positionPointer.stride;
Rgba* pc = (Rgba*) cData;
Rgba c0(*pc);
cData += _colorPointer.stride;
pc = (Rgba*) cData;
Rgba c1(*pc);
cData += _colorPointer.stride;
pc = (Rgba*) cData;
Rgba c2(*pc);
cData += _colorPointer.stride;
float* puv = (float*) uvData;
float2 uv0(puv[0], puv[1]);
uvData += _uvPointer.stride;
puv = (float*) uvData;
float2 uv1(puv[0], puv[1]);
uvData += _uvPointer.stride;
puv = (float*) uvData;
float2 uv2(puv[0], puv[1]);
uvData += _uvPointer.stride;
Ege eges[3] = {
Ege(p0.x, p0.y, p1.x, p1.y, c0, c1, uv0, uv1),
Ege(p1.x, p1.y, p2.x, p2.y, c1, c2, uv1, uv2),
Ege(p2.x, p2.y, p0.x, p0.y, c2, c0, uv2, uv0),
};
drawTriangle(eges, _texture);
if (_colorPointer.data == 0) {
cData = (char*) colorPointerdesc.data;
}
if (_uvPointer.data == 0) {
uvData = (char*) uvPointerdesc.data;
float3 mp2(pp[0], pp[1], pp[2]);
mp2 = mp2 * _matModel;
if (_frust.pointInFrustum(mp0) || _frust.pointInFrustum(mp1) || _frust.pointInFrustum(mp2)) {
mp0 = piplineTransform(mp0);
mp1 = piplineTransform(mp1);
mp2 = piplineTransform(mp2);
int2 p0(mp0.x, mp0.y);
int2 p1(mp1.x, mp1.y);
int2 p2(mp2.x, mp2.y);
posData += _positionPointer.stride;
Rgba* pc = (Rgba*) cData;
Rgba c0(*pc);
cData += _colorPointer.stride;
pc = (Rgba*) cData;
Rgba c1(*pc);
cData += _colorPointer.stride;
pc = (Rgba*) cData;
Rgba c2(*pc);
cData += _colorPointer.stride;
float* puv = (float*) uvData;
float2 uv0(puv[0], puv[1]);
uvData += _uvPointer.stride;
puv = (float*) uvData;
float2 uv1(puv[0], puv[1]);
uvData += _uvPointer.stride;
puv = (float*) uvData;
float2 uv2(puv[0], puv[1]);
uvData += _uvPointer.stride;
Ege eges[3] = {
Ege(p0.x, p0.y, p1.x, p1.y, c0, c1, uv0, uv1),
Ege(p1.x, p1.y, p2.x, p2.y, c1, c2, uv1, uv2),
Ege(p2.x, p2.y, p0.x, p0.y, c2, c0, uv2, uv0),
};
drawTriangle(eges, _texture);
if (_colorPointer.data == 0) {
cData = (char*) colorPointerdesc.data;
}
if (_uvPointer.data == 0) {
uvData = (char*) uvPointerdesc.data;
}
}
}
@ -390,6 +403,58 @@ void Raster::loadIdentity() {
_matModel = matrix4(1);
}
void Raster::loadViewMatrix(const matrix4& mat) {
_matView = mat;
}
void Raster::loadViewIdentity() {
_matView = matrix4(1);
}
void Raster::loadProjMatrix(const matrix4& mat) {
_matProj = mat;
}
void Raster::loadProjIdentity() {
_matProj = matrix4(1);
}
void Raster::setPerspective(float fovy, float aspect, float zNear, float zFar) {
_matProj = perspective<float>(fovy, aspect, zNear, zFar);
}
void Raster::lookat(const float3& eye, const float3& center, const float3& up) {
_matView = lookAt(eye, center, up);
}
void Raster::setViewPort(int x, int y, int w, int h) {
_viewPort.x = w;
_viewPort.y = h;
}
float3 Raster::piplineTransform(float3 pos) {
float4 world(pos.x, pos.y, pos.z, 1);
float4 screen = (_matProj * _matView * _matModel) * world;
if (screen.w == 0.0f) {
return false;
}
screen.x /= screen.w;
screen.y /= screen.w;
screen.z /= screen.w;
screen.x = screen.x * 0.5f + 0.5f;
screen.y = screen.y * 0.5f + 0.5f;
screen.z = screen.z * 0.5f + 0.5f;
screen.x = screen.x * _viewPort.x;
screen.y = screen.y * _viewPort.y;
return float3(screen.x, screen.y, screen.z);
}
void Raster::bindTexture(Image* image) {
_texture = image;
}

15
src/5threed/Raster.h

@ -141,6 +141,14 @@ public:
void bindTexture(Image* image);
void loadMatrix(const matrix4& mat);
void loadIdentity();
void loadViewMatrix(const matrix4& mat);
void loadViewIdentity();
void loadProjMatrix(const matrix4& mat);
void loadProjIdentity();
void setPerspective(float fovy, float aspect, float zNear, float zFar);
void lookat(float3 const &eye, float3 const &center, float3 const &up);
void setViewPort(int x, int y, int w, int h);
float3 piplineTransform(float3 pos);
int size();
void clean();
bool setPixel(int x, int y, Rgba color);
@ -151,8 +159,13 @@ private:
int _height;
Rgba _color;
Image* _texture;
matrix4 _matModel;
matrix4 _matView;
matrix4 _matProj;
matrix4 _matProjView;
float2 _viewPort;
Frustum _frust;
DataElementDes _positionPointer;
DataElementDes _colorPointer;

5418
src/5threed/common.h
File diff suppressed because it is too large
View File

159
src/5threed/struct/common.h

@ -0,0 +1,159 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: common.h
* Author: Blobt
*
* Created on October 5, 2019, 4:55 PM
*/
#ifndef COMMON_H
#define COMMON_H
#include <assert.h>
#include <cstddef>
#include <stdlib.h>
#include <cmath>
#include "Rgba.h"
#include "struct/tvec2.h"
#include "struct/tvec3.h"
#include "struct/tmat3x3.h"
#include "struct/tvec4.h"
#include "struct/tmat4x4.h"
#include "struct/tfrustum.h"
#define die(m) do { perror(m); exit(EXIT_FAILURE); } while(0)
#define def2rad(theta) (0.01745329251994329 * (theta)) //每个角度所对应的弧度
typedef tvec2<float> float2;
typedef tvec2<int> int2;
typedef tvec3<float> float3;
typedef tvec4<float> float4;
typedef unsigned char byte;
typedef tmat3x3<float> matrix3;
typedef tmat4x4<float> matrix4;
typedef tfrustum<float> Frustum;
template<class T> inline T tmin(T a, T b) {
return a < b ? a : b;
}
template<class T> inline T tmax(T a, T b) {
return a > b ? a : b;
}
inline Rgba colorLerp(const Rgba& color1, const Rgba& color2, float step) {
Rgba ret;
ret._r = (unsigned char) color1._r + step * (color2._r - color1._r);
ret._g = (unsigned char) color1._g + step * (color2._g - color1._g);
ret._b = (unsigned char) color1._b + step * (color2._b - color1._b);
ret._a = (unsigned char) color1._a + step * (color2._a - color1._a);
return ret;
}
inline float2 uvLerp(const float2& uv1, const float2& uv2, float step) {
if (step < 0 || step > 1) {
printf("step : %f\n", step);
die("step must more than zero and less than 1");
}
float2 ret;
ret.x = (float) uv1.x + (uv2.x - uv1.x) * step;
ret.y = (float) uv1.y + (uv2.y - uv1.y) * step;
return ret;
}
template <typename T>
tvec3<T> cross(tvec3<T> const & x, tvec3<T> const & y) {
return tvec3<T>
(
x.y * y.z - y.y * x.z,
x.z * y.x - y.z * x.x,
x.x * y.y - y.x * x.y
);
}
template <typename T>
typename tvec3<T>::value_type dot(tvec3<T> const & x, tvec3<T> const & y) {
return x.x * y.x + x.y * y.y + x.z * y.z;
}
template <typename T>
T inversesqrt(T x) {
return T(1) / sqrt(x);
}
template <typename T>
tvec2<T> normalize(tvec2<T> const & x) {
typename tvec2<T>::value_type sqr = x.x * x.x + x.y * x.y;
return x * inversesqrt(sqr);
}
template <typename T>
tvec3<T> normalize(tvec3<T> const & x) {
typename tvec3<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z;
return x * inversesqrt(sqr);
}
template <typename T>
tvec4<T> normalize(tvec4<T> const & x) {
typename tvec4<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w;
return x * inversesqrt(sqr);
}
template <typename valType>
tmat4x4<valType> perspective(valType fovy, valType aspect, valType zNear, valType zFar) {
valType range = tan(fovy * valType(DEG2RAD(0.5))) * zNear;
valType left = -range * aspect;
valType right = range * aspect;
valType bottom = -range;
valType top = range;
tmat4x4<valType> res(valType(0));
res[0][0] = (valType(2) * zNear) / (right - left);
res[1][1] = (valType(2) * zNear) / (top - bottom);
res[2][2] = -(zFar + zNear) / (zFar - zNear);
res[2][3] = -valType(1);
res[3][2] = -(valType(2) * zFar * zNear) / (zFar - zNear);
return res;
}
template <typename T>
tmat4x4<T> lookAt
(
tvec3<T> const & eye,
tvec3<T> const & center,
tvec3<T> const & up
) {
tvec3<T> f = normalize(center - eye);
tvec3<T> u = normalize(up);
tvec3<T> s = normalize(cross(f, u));
u = cross(s, f);
tmat4x4<T> res(1);
res[0][0] = s.x;
res[1][0] = s.y;
res[2][0] = s.z;
res[0][1] = u.x;
res[1][1] = u.y;
res[2][1] = u.z;
res[0][2] = -f.x;
res[1][2] = -f.y;
res[2][2] = -f.z;
res[3][0] = -dot(s, eye);
res[3][1] = -dot(u, eye);
res[3][2] = dot(f, eye);
return res;
}
#endif /* COMMON_H */

175
src/5threed/struct/plane.h

@ -0,0 +1,175 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: plane.h
* Author: Blobt
*
* Created on February 23, 2020, 11:33 AM
*/
#ifndef PLANE_H
#define PLANE_H
#include "../common.h"
template<class T>
class Plane {
public:
tvec3<T> _normal;
T _distance;
public:
Plane() {
_normal = tvec3<T>(0, 0, 0);
_distance = 0.0f;
}
Plane(const Plane& right) {
_normal = right._normal;
_distance = right._distance;
}
/** Construct a plane through a normal, and a distance to move the plane along the normal.*/
Plane(const tvec3<T>& rkNormal, T fConstant) {
_normal = rkNormal;
_distance = -fConstant;
}
/** Construct a plane using the 4 constants directly **/
Plane(T x, T y, T z, T o) {
_normal = tvec3<T>(x, y, z);
T invLen = 1.0f / (_normal).length();
_normal *= invLen;
_distance = o * invLen;
}
Plane(const tvec3<T>& rkNormal, const tvec3<T>& rkPoint) {
redefine(rkNormal, rkPoint);
}
Plane(const tvec3<T>& rkPoint0, const tvec3<T>& rkPoint1, const tvec3<T>& rkPoint2) {
redefine(rkPoint0, rkPoint1, rkPoint2);
}
/**
* ľ
*/
float distance(const tvec3<T> &pos) const {
return dot(_normal, pos) + _distance;
}
/** The "positive side" of the plane is the half space to which the
plane normal points. The "negative side" is the other half
space. The flag "no side" indicates the plane itself.
*/
enum Side {
NO_SIDE,
POSITIVE_SIDE,
NEGATIVE_SIDE,
BOTH_SIDE
};
Side getSide(const tvec3<T>& rkPoint) const {
float fDistance = getDistance(rkPoint);
if (fDistance < 0.0)
return Plane::NEGATIVE_SIDE;
if (fDistance > 0.0)
return Plane::POSITIVE_SIDE;
return Plane::NO_SIDE;
}
Side getSide(const tvec3<T>& centre, const tvec3<T>& halfSize) const {
// Calculate the distance between box centre and the plane
float dist = getDistance(centre);
// Calculate the maximise allows absolute distance for
// the distance between box centre and plane
float maxAbsDist = _normal.absDot(halfSize);
if (dist < -maxAbsDist)
return Plane::NEGATIVE_SIDE;
if (dist > +maxAbsDist)
return Plane::POSITIVE_SIDE;
return Plane::BOTH_SIDE;
}
float getDistance(const tvec3<T>& rkPoint) const {
return _normal.dot(rkPoint) + _distance;
}
void redefine(const tvec3<T>& rkPoint0, const tvec3<T>& rkPoint1,
const tvec3<T>& rkPoint2) {
tvec3<T> kEdge1 = rkPoint1 - rkPoint0;
tvec3<T> kEdge2 = rkPoint2 - rkPoint0;
_normal = cross(kEdge1, kEdge2);
_normal.normalise();
_distance = -dot(_normal, rkPoint0);
}
/** Redefine this plane based on a normal and a point. */
void redefine(const tvec3<T>& rkNormal, const tvec3<T>& rkPoint) {
_normal = rkNormal;
_distance = -dot(rkNormal, rkPoint);
}
// tvec3<T> projectVector(const tvec3<T>& p) const
// {
// matrix3 xform;
// xform[0][0] = 1.0f - _normal.x * _normal.x;
// xform[0][1] = -_normal.x * _normal.y;
// xform[0][2] = -_normal.x * _normal.z;
// xform[1][0] = -_normal.y * _normal.x;
// xform[1][1] = 1.0f - _normal.y * _normal.y;
// xform[1][2] = -_normal.y * _normal.z;
// xform[2][0] = -_normal.z * _normal.x;
// xform[2][1] = -_normal.z * _normal.y;
// xform[2][2] = 1.0f - _normal.z * _normal.z;
// return xform * p;
// }
/** Normalises the plane.
@remarks
This method normalises the plane's normal and the length scale of d
is as well.
@note
This function will not crash for zero-sized vectors, but there
will be no changes made to their components.
@returns The previous length of the plane's normal.
*/
float normalise(void) {
float fLength = _normal.length();
// Will also work for zero-sized vectors, but will change nothing
if (fLength > 1e-08f) {
float fInvLength = 1.0f / fLength;
_normal *= fInvLength;
_distance *= fInvLength;
}
return fLength;
}
/// Comparison operator
bool operator==(const Plane& right) const {
return (right._distance == _distance && right._normal == _normal);
}
bool operator!=(const Plane& right) const {
return (right._distance != _distance && right._normal != _normal);
}
};
#endif /* PLANE_H */

87
src/5threed/struct/tfrustum.h

@ -0,0 +1,87 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: tfrustum.h
* Author: Blobt
*
* Created on February 23, 2020, 11:22 AM
*/
#ifndef TFRUSTUM_H
#define TFRUSTUM_H
#include "../common.h"
#include "plane.h"
template<class T>
class tfrustum {
public:
enum {
FRUSTUM_LEFT = 0,
FRUSTUM_RIGHT = 1,
FRUSTUM_TOP = 2,
FRUSTUM_BOTTOM = 3,
FRUSTUM_FAR = 4,
FRUSTUM_NEAR = 5,
};
public:
/**
* project * view
*/
void loadFrustum(const tmat4x4<T> &mvp) {
const T* dataPtr = mvp.data();
_planes[FRUSTUM_LEFT ] = Plane<T>(dataPtr[12] - dataPtr[0], dataPtr[13] - dataPtr[1], dataPtr[14] - dataPtr[2], dataPtr[15] - dataPtr[3]);
_planes[FRUSTUM_RIGHT ] = Plane<T>(dataPtr[12] + dataPtr[0], dataPtr[13] + dataPtr[1], dataPtr[14] + dataPtr[2], dataPtr[15] + dataPtr[3]);
_planes[FRUSTUM_TOP ] = Plane<T>(dataPtr[12] - dataPtr[4], dataPtr[13] - dataPtr[5], dataPtr[14] - dataPtr[6], dataPtr[15] - dataPtr[7]);
_planes[FRUSTUM_BOTTOM] = Plane<T>(dataPtr[12] + dataPtr[4], dataPtr[13] + dataPtr[5], dataPtr[14] + dataPtr[6], dataPtr[15] + dataPtr[7]);
_planes[FRUSTUM_FAR ] = Plane<T>(dataPtr[12] - dataPtr[8], dataPtr[13] - dataPtr[9], dataPtr[14] - dataPtr[10], dataPtr[15] - dataPtr[11]);
_planes[FRUSTUM_NEAR ] = Plane<T>(dataPtr[12] + dataPtr[8], dataPtr[13] + dataPtr[9], dataPtr[14] + dataPtr[10], dataPtr[15] + dataPtr[11]);
}
bool pointInFrustum(const tvec3<T> &pos) const {
for (int i = 0; i < 6; i++) {
if (_planes[i].distance(pos) <= 0)
return false;
}
return true;
}
bool sphereInFrustum(const tvec3<T> &pos, const float radius) const {
for (int i = 0; i < 6; i++) {
if (_planes[i].distance(pos) <= -radius)
return false;
}
return true;
}
bool cubeInFrustum(T minX, T maxX, T minY, T maxY, T minZ, T maxZ) const {
for (int i = 0; i < 6; i++) {
if (_planes[i].distance(tvec3<T>(minX, minY, minZ)) > 0) continue;
if (_planes[i].distance(tvec3<T>(minX, minY, maxZ)) > 0) continue;
if (_planes[i].distance(tvec3<T>(minX, maxY, minZ)) > 0) continue;
if (_planes[i].distance(tvec3<T>(minX, maxY, maxZ)) > 0) continue;
if (_planes[i].distance(tvec3<T>(maxX, minY, minZ)) > 0) continue;
if (_planes[i].distance(tvec3<T>(maxX, minY, maxZ)) > 0) continue;
if (_planes[i].distance(tvec3<T>(maxX, maxY, minZ)) > 0) continue;
if (_planes[i].distance(tvec3<T>(maxX, maxY, maxZ)) > 0) continue;
return false;
}
return true;
}
const Plane<T> &getPlane(const int plane) const {
return _planes[plane];
}
protected:
Plane<T> _planes[6];
};
#endif /* TFRUSTUM_H */

6
src/5threed/struct/tmat4x4.h

@ -109,6 +109,10 @@ public:
this->value[3] = col_type(m[3]);
}
T const * data() const {
return &this->value[0][0];
}
tmat4x4<T>& translate(value_type x, value_type y, value_type z) {
this->value[0] = col_type(1, 0, 0, 0);
this->value[1] = col_type(0, 1, 0, 0);
@ -310,7 +314,7 @@ public:
);
}
public:
private:
col_type value[4];
};

67
src/5threed/struct/tvec3.h

@ -46,6 +46,12 @@ struct tvec3 {
z = 0;
}
inline tvec3(tvec3<T> const & v) :
x(v.x),
y(v.y),
z(v.z) {
}
tvec3(type &v) {
x = v.x;
y = v.y;
@ -70,9 +76,9 @@ struct tvec3 {
*/
template <typename U>
tvec3(U s) {
x = value_type(s);
y = value_type(s);
z = value_type(s);
x = value_type(s);
y = value_type(s);
z = value_type(s);
}
template <typename A, typename B, typename C>
@ -98,6 +104,14 @@ struct tvec3 {
return *this;
}
template <typename U>
tvec3<T>& operator=(tvec3<U> const & v) {
this->x = T(v.x);
this->y = T(v.y);
this->z = T(v.z);
return *this;
}
tvec3<T> & operator++() {
++x;
++y;
@ -105,7 +119,54 @@ struct tvec3 {
return *this;
}
template <typename U>
tvec3<T> & operator*=(U const & s) {
this->x *= T(s);
this->y *= T(s);
this->z *= T(s);
return *this;
}
};
template <typename T>
tvec3<T> operator-(tvec3<T> const & v, T const & s) {
return tvec3<T>(
v.x - T(s),
v.y - T(s),
v.z - T(s));
}
template <typename T>
tvec3<T> operator-(T const & s, tvec3<T> const & v) {
return tvec3<T>(
T(s) - v.x,
T(s) - v.y,
T(s) - v.z);
}
template <typename T>
tvec3<T> operator-(tvec3<T> const & v1, tvec3<T> const & v2) {
return tvec3<T>(
v1.x - T(v2.x),
v1.y - T(v2.y),
v1.z - T(v2.z));
}
template <typename T>
tvec3<T> operator*(tvec3<T> const & v, T const & s) {
return tvec3<T>(
v.x * T(s),
v.y * T(s),
v.z * T(s));
}
template <typename T>
tvec3<T> operator*(T const & s, tvec3<T> const & v) {
return tvec3<T>(
T(s) * v.x,
T(s) * v.y,
T(s) * v.z);
}
#endif /* TVEC3_H */

53
src/5threed/struct/tvec4.h

@ -126,5 +126,58 @@ struct tvec4 {
}
};
template <typename T>
tvec4<T> operator+(tvec4<T> const & v, T const & s) {
return tvec4<T>(
v.x + s,
v.y + s,
v.z + s,
v.w + s);
}
template <typename T>
tvec4<T> operator+(T const & s, tvec4<T> const & v) {
return tvec4<T>(
s + v.x,
s + v.y,
s + v.z,
s + v.w);
}
template <typename T>
tvec4<T> operator+(tvec4<T> const & v1, tvec4<T> const & v2) {
return tvec4<T>(
v1.x + v2.x,
v1.y + v2.y,
v1.z + v2.z,
v1.w + v2.w);
}
template <typename T>
tvec4<T> operator*(tvec4<T> const & v, T const & s) {
return tvec4<T>(
v.x * s,
v.y * s,
v.z * s,
v.w * s);
}
template <typename T>
tvec4<T> operator*(T const & s, tvec4<T> const & v) {
return tvec4<T>(
s * v.x,
s * v.y,
s * v.z,
s * v.w);
}
template <typename T>
tvec4<T> operator*(tvec4<T> const & v1, tvec4<T> const & v2) {
return tvec4<T>(
v1.x * v2.x,
v1.y * v2.y,
v1.z * v2.z,
v1.w * v2.w);
}
#endif /* TVEC4_H */

15
src/5threed/threed.cc

@ -9,19 +9,28 @@
using namespace std;
gint height = 50;
gint width = 50;
gint height = 600;
gint width = 800;
Raster raster(width, height);
struct Vertex {
float x, y;
float x, y, z;
float u, v;
Rgba color;
};
void example1() {
Vertex vertexs[] = {
{-1.0f, 0.0f, -5.0f, 0.0f, 0.0f, Rgba(255, 0, 0, 255)},
{0.0f, 1.0f, -5.0f, 1.0f, 1.0f, Rgba(0, 255, 0, 255)},
{1.0f, 0.0f, -5.0f, 1.0f, 0.0f, Rgba(0, 0, 255, 255)},
};
raster.vertexPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs[0].x);
raster.colorPointer(4, DT_BYTE, sizeof (Vertex), &vertexs[0].color);
raster.drawArrays(DM_TRIANGES, 0, 3);
}
unsigned char* makeBitmap() {

Loading…
Cancel
Save