Compare commits
3 Commits
e6a49c4355
...
e9e4eed79b
| Author | SHA1 | Date | |
|---|---|---|---|
| e9e4eed79b | |||
| adafae2d84 | |||
| 949578f0db |
49
fastmath.hpp
49
fastmath.hpp
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
@@ -20,7 +19,7 @@
|
||||
|
||||
struct decimal {
|
||||
|
||||
const int32_t i;
|
||||
int32_t i;
|
||||
|
||||
decimal() : i(0) {}
|
||||
decimal(float i) : i(TO_INT(i)) {}
|
||||
@@ -60,6 +59,9 @@ struct decimal {
|
||||
friend bool operator==(const decimal &d1, const decimal &d2) {
|
||||
return d1.i == d2.i;
|
||||
}
|
||||
friend bool operator!=(const decimal &d1, const decimal &d2) {
|
||||
return d1.i != d2.i;
|
||||
}
|
||||
|
||||
decimal &operator=(decimal const &in) {
|
||||
if (this != &in) {
|
||||
@@ -153,6 +155,7 @@ template <int n, class Dev> struct vec {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
decimal &operator[](const int &i) { return v[i]; }
|
||||
|
||||
decimal len_sq() { return *this * *this; }
|
||||
|
||||
@@ -166,7 +169,21 @@ template <int n, class Dev> struct vec {
|
||||
protected:
|
||||
decimal v[n];
|
||||
};
|
||||
struct vec2 : public vec<2, vec2> {
|
||||
|
||||
vec2() : vec<2, vec2>() {}
|
||||
|
||||
vec2(float x, float y) : vec<2, vec2>({decimal(x), decimal(y)}) {}
|
||||
|
||||
vec2(double x, double y) : vec<2, vec2>({decimal(x), decimal(y)}) {}
|
||||
|
||||
vec2(int32_t x, int32_t y) : vec<2, vec2>({decimal(x), decimal(y)}) {}
|
||||
|
||||
vec2(decimal x, decimal y) : vec<2, vec2>({x, y}) {}
|
||||
|
||||
decimal &x() { return v[0]; }
|
||||
decimal &y() { return v[1]; }
|
||||
};
|
||||
struct vec3 : public vec<3, vec3> {
|
||||
|
||||
vec3() : vec<3, vec3>() {}
|
||||
@@ -182,14 +199,34 @@ struct vec3 : public vec<3, vec3> {
|
||||
|
||||
vec3(decimal x, decimal y, decimal z) : vec<3, vec3>({x, y, z}) {}
|
||||
|
||||
decimal x() const { return v[0]; }
|
||||
decimal y() const { return v[1]; }
|
||||
decimal z() const { return v[2]; }
|
||||
decimal &x() { return v[0]; }
|
||||
decimal &y() { return v[1]; }
|
||||
decimal &z() { return v[2]; }
|
||||
|
||||
vec3 cross(const vec3 &v) {
|
||||
vec3 cross(vec3 &v) {
|
||||
return vec3((y() * v.z()) - (z() * v.y()),
|
||||
(z() * v.x()) - (x() * v.z()),
|
||||
(x() * v.y()) - (y() * v.x()));
|
||||
}
|
||||
};
|
||||
struct vec4 : public vec<4, vec4> {
|
||||
|
||||
vec4() : vec<4, vec4>() {}
|
||||
|
||||
vec4(float x, float y, float z, float w)
|
||||
: vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {}
|
||||
|
||||
vec4(double x, double y, double z, double w)
|
||||
: vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {}
|
||||
|
||||
vec4(int32_t x, int32_t y, int32_t z, int32_t w)
|
||||
: vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {}
|
||||
|
||||
vec4(decimal x, decimal y, decimal z) : vec<4, vec4>({x, y, z}) {}
|
||||
|
||||
decimal &x() { return v[0]; }
|
||||
decimal &y() { return v[1]; }
|
||||
decimal &z() { return v[2]; }
|
||||
decimal &w() { return v[3]; }
|
||||
};
|
||||
#endif
|
||||
|
||||
64
main.cpp
64
main.cpp
@@ -1,12 +1,18 @@
|
||||
#include "fastmath.hpp"
|
||||
#include "polygon.hpp"
|
||||
#include "renderer.hpp"
|
||||
#include "rendertarget.hpp"
|
||||
#include <QApplication>
|
||||
#include <QImage>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
#include <functional>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define HIGHT 64
|
||||
#define WIDTH 64
|
||||
|
||||
@@ -56,24 +62,54 @@ void drawImage(unsigned short *img) {
|
||||
img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] =
|
||||
(unsigned short)23; // (((-normal[1]+(1 <<
|
||||
// SHIFT_AMOUNT))*14)>>SHIFT_AMOUNT);
|
||||
} else {
|
||||
img[(y.i >> SHIFT_AMOUNT) * RWIDTH + (x.i >> SHIFT_AMOUNT)] =
|
||||
(unsigned short)0;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("done writing %d \n", *(img + sizeof(unsigned short) * 32 * 32));
|
||||
}
|
||||
|
||||
int main() {
|
||||
unsigned short *img =
|
||||
(unsigned short *)malloc(128 * 128 * sizeof(unsigned short));
|
||||
while (true) {
|
||||
drawImage(img);
|
||||
printf("%s", drawToString(img));
|
||||
return 0;
|
||||
// sleep(0.001);
|
||||
int main(int argc, char *argv[]) {
|
||||
Rendertarget target(128, 128);
|
||||
Renderer renderer;
|
||||
renderer.target = ⌖
|
||||
|
||||
polygon poly =
|
||||
polygon(vec3(0.9, 0.9, 0.0), vec3(0.5, 0.1, 0.0), vec3(0.1, 0.9, 0.0));
|
||||
|
||||
renderer.render(&poly);
|
||||
QApplication a(argc, argv);
|
||||
|
||||
uint8_t *pixel = new uint8_t[64 * 64 * 3];
|
||||
/*for (int i = 0; i < 64 * 64 * 3; i++) {
|
||||
pixel[i] = target.pixels[i].i >> SHIFT_AMOUNT;
|
||||
}*/
|
||||
std::function<void(int, uint32_t *)> addTo = [&target](int start,
|
||||
uint32_t *arr) {
|
||||
for (int c = 0; c < 3; c++) {
|
||||
arr[c] += target.pixels[start + c].i >> SHIFT_AMOUNT;
|
||||
}
|
||||
// printf("%s", test);
|
||||
return 0;
|
||||
};
|
||||
|
||||
for (int x = 0; x < 64; x++) {
|
||||
for (int y = 0; y < 64; y++) {
|
||||
uint32_t result[3] = {};
|
||||
addTo((target.width * y * 2 + x * 2) * 3, result);
|
||||
addTo((target.width * (y * 2 + 1) + x * 2) * 3, result);
|
||||
addTo((target.width * y * 2 + (x * 2 + 1)) * 3, result);
|
||||
addTo((target.width * (y * 2 + 1) + (x * 2 + 1)) * 3, result);
|
||||
for (int c = 0; c < 3; c++) {
|
||||
pixel[(WIDTH * y + x) * 3 + c] = result[c] >> 2; /*
|
||||
target.pixels[(target.width * y * 2 + x * 2) * 3 + c].i >>
|
||||
SHIFT_AMOUNT;*/
|
||||
}
|
||||
}
|
||||
}
|
||||
QWidget widget;
|
||||
widget.setAutoFillBackground(true);
|
||||
widget.setGeometry(0, 0, 500, 500);
|
||||
QLabel display(&widget);
|
||||
QImage img((unsigned char *)pixel, 64, 64, QImage::Format_RGB888);
|
||||
display.setPixmap(QPixmap::fromImage(img).scaled(widget.size()));
|
||||
widget.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
42
renderer.hpp
Normal file
42
renderer.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
#ifndef RENDERER_H
|
||||
#define RENDERER_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
#include "polygon.hpp"
|
||||
#include "rendertarget.hpp"
|
||||
#include <memory.h>
|
||||
|
||||
class Renderer {
|
||||
|
||||
public:
|
||||
Rendertarget *target;
|
||||
|
||||
bool clearTarget = true;
|
||||
Renderer() : clearTarget(false) {}
|
||||
|
||||
void render(polygon *p) {
|
||||
decimal widthScale = decimal(1.0) / decimal((float)target->width);
|
||||
decimal heightScale = decimal(1.0) / decimal((float)target->height);
|
||||
// std::cout << widthScale << std::endl;
|
||||
vec3 pos = vec3();
|
||||
|
||||
if (clearTarget) {
|
||||
memset((wchar_t *)target->pixels, 0,
|
||||
target->height * target->width * sizeof(target[0]));
|
||||
}
|
||||
|
||||
for (int x = 0; x < target->height; x++) {
|
||||
for (int y = 0; y < target->height; y++) {
|
||||
if (p->contains(pos)) {
|
||||
target->set(x, y, vec3(0.0, 255.0, 0.0));
|
||||
}
|
||||
pos.y() += heightScale;
|
||||
}
|
||||
pos.y() = decimal(0);
|
||||
pos.x() += widthScale;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
32
rendertarget.hpp
Normal file
32
rendertarget.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
#ifndef RENDERTARGET_H
|
||||
#define RENDERTARGET_H
|
||||
|
||||
#include "fastmath.hpp"
|
||||
class Rendertarget {
|
||||
|
||||
public:
|
||||
const int width;
|
||||
const int height;
|
||||
|
||||
Rendertarget(int width, int height) : width(width), height(height) {
|
||||
pixels = new decimal[width * height * 3];
|
||||
}
|
||||
|
||||
void set(int x, int y, vec3 val) {
|
||||
int start = (width * y + x) * 3;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
pixels[start + i] = val[i];
|
||||
}
|
||||
}
|
||||
void get(int x, int y, vec3 *val) {
|
||||
int start = (width * y + x) * 3;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
(*val)[i] = pixels[start + i];
|
||||
}
|
||||
}
|
||||
|
||||
decimal *pixels;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user