mirror of
https://github.com/MartinOpat/cuda-based-raytrace.git
synced 2025-06-07 02:13:10 +02:00
feat (wip): real-time gui with rendering in opengl
This commit is contained in:
46
src/linalg/mat.cu
Normal file
46
src/linalg/mat.cu
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "mat.h"
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
__device__ Vec3 computeGradient(float* volumeData, const int volW, const int volH, const int volD, int x, int y, int z) {
|
||||
// Finite difference for partial derivatives.
|
||||
// For boundary voxels - clamp to the boundary.
|
||||
// Normal should point from higher to lower intensities
|
||||
|
||||
int xm = max(x - 1, 0);
|
||||
int xp = min(x + 1, volW - 1);
|
||||
int ym = max(y - 1, 0);
|
||||
int yp = min(y + 1, volH - 1);
|
||||
int zm = max(z - 1, 0);
|
||||
int zp = min(z + 1, volD - 1);
|
||||
|
||||
// Note: Assuming data is linearized (idx = z*w*h + y*w + x) TODO: Unlinearize if data not linear
|
||||
float gx = volumeData[z * volW * volH + y * volW + xp]
|
||||
- volumeData[z * volW * volH + y * volW + xm];
|
||||
float gy = volumeData[z * volW * volH + yp * volW + x ]
|
||||
- volumeData[z * volW * volH + ym * volW + x ];
|
||||
float gz = volumeData[zp * volW * volH + y * volW + x ]
|
||||
- volumeData[zm * volW * volH + y * volW + x ];
|
||||
|
||||
return Vec3::init(gx, gy, gz);
|
||||
};
|
||||
|
||||
// TESTING: haven't tested this function at all tbh
|
||||
__device__ unsigned int packUnorm4x8(float r, float g, float b, float a) {
|
||||
union {
|
||||
unsigned char in[4];
|
||||
uint out;
|
||||
} u;
|
||||
|
||||
double len = sqrt(r*r + g*g + b*b + a*a);
|
||||
|
||||
// This is a Vec4 but i can't be bothered to make that its own struct/class; FIXME: maybe do that if we need to?
|
||||
std::vector<float> v{r/len, g/len, b/len, a/len};
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
u.in[i] = round(std::clamp(v[i], 0.0f, 1.0f) * 255.0f);
|
||||
}
|
||||
|
||||
return u.out;
|
||||
}
|
||||
@@ -1,24 +1,10 @@
|
||||
#pragma once
|
||||
#ifndef MAT_H
|
||||
#define MAT_H
|
||||
|
||||
__device__ Vec3 computeGradient(float* volumeData, const int volW, const int volH, const int volD, int x, int y, int z) {
|
||||
// Finite difference for partial derivatives.
|
||||
// For boundary voxels - clamp to the boundary.
|
||||
// Normal should point from higher to lower intensities
|
||||
#include "vec.h"
|
||||
|
||||
int xm = max(x - 1, 0);
|
||||
int xp = min(x + 1, volW - 1);
|
||||
int ym = max(y - 1, 0);
|
||||
int yp = min(y + 1, volH - 1);
|
||||
int zm = max(z - 1, 0);
|
||||
int zp = min(z + 1, volD - 1);
|
||||
__device__ Vec3 computeGradient(float* volumeData, const int volW, const int volH, const int volD, int x, int y, int z);
|
||||
|
||||
// Note: Assuming data is linearized (idx = z*w*h + y*w + x) TODO: Unlinearize if data not linear
|
||||
float gx = volumeData[z * volW * volH + y * volW + xp]
|
||||
- volumeData[z * volW * volH + y * volW + xm];
|
||||
float gy = volumeData[z * volW * volH + yp * volW + x ]
|
||||
- volumeData[z * volW * volH + ym * volW + x ];
|
||||
float gz = volumeData[zp * volW * volH + y * volW + x ]
|
||||
- volumeData[zm * volW * volH + y * volW + x ];
|
||||
__device__ unsigned int packUnorm4x8(float r, float g, float b, float a);
|
||||
|
||||
return Vec3::init(gx, gy, gz);
|
||||
}
|
||||
#endif // MAT_H
|
||||
|
||||
@@ -27,4 +27,4 @@ struct Vec3 { // TODO: Maybe make this into a class ... maybe
|
||||
};
|
||||
|
||||
typedef Vec3 Point3;
|
||||
typedef Vec3 Color3;
|
||||
typedef Vec3 Color3;
|
||||
|
||||
Reference in New Issue
Block a user