ubuntu20
4 years ago
7 changed files with 104 additions and 1 deletions
-
2Scene.cpp
-
39Texture.cpp
-
16Texture.h
-
3raytracing.vcxproj
-
6raytracing.vcxproj.filters
-
37util.cpp
-
2util.h
@ -0,0 +1,39 @@ |
|||
#include "Texture.h"
|
|||
#include "util.h"
|
|||
|
|||
TextureRGB::TextureRGB() |
|||
{ |
|||
mImageFileContent = nullptr; |
|||
mRGBPixel = nullptr; |
|||
mWidth = 0; |
|||
mHeight = 0; |
|||
} |
|||
|
|||
TextureRGB::~TextureRGB() |
|||
{ |
|||
if (mImageFileContent) { |
|||
delete[] mImageFileContent; |
|||
} |
|||
} |
|||
|
|||
void TextureRGB::Set(const char * image_path) |
|||
{ |
|||
int file_size = 0; |
|||
mImageFileContent = LoadFileContent(image_path, file_size); |
|||
mRGBPixel = DecodeBMP(mImageFileContent, mWidth, mHeight); |
|||
} |
|||
|
|||
Vector3 TextureRGB::Sample(float u, float v) |
|||
{ |
|||
int x = int(u * mWidth); |
|||
int y = int(u * mHeight); |
|||
x = x < 0 ? 0 : x; |
|||
y = y < 0 ? 0 : y; |
|||
x = x > (mWidth - 1) ? (mWidth - 1) : x; |
|||
y = y > (mHeight - 1) ? (mHeight - 1) : y; |
|||
int pixel_data_start_index = 3 * (x + y * mWidth); |
|||
float r = float(mRGBPixel[pixel_data_start_index]) / 255.0f; |
|||
float g = float(mRGBPixel[pixel_data_start_index + 1]) / 255.0f; |
|||
float b = float(mRGBPixel[pixel_data_start_index + 2]) / 255.0f; |
|||
return Vector3(r, g, b); |
|||
} |
@ -0,0 +1,16 @@ |
|||
#pragma once |
|||
#include "Vector3.h" |
|||
class Texture2D { |
|||
public: |
|||
virtual Vector3 Sample(float u, float v) = 0; |
|||
}; |
|||
class TextureRGB : public Texture2D { |
|||
public: |
|||
unsigned char* mImageFileContent; |
|||
unsigned char* mRGBPixel; |
|||
int mWidth, mHeight; |
|||
TextureRGB(); |
|||
~TextureRGB(); |
|||
void Set(const char* image_path); |
|||
Vector3 Sample(float u, float v); |
|||
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue