From 165040985e824281a686b37fca32d493602b2ffc Mon Sep 17 00:00:00 2001 From: ubuntu20 Date: Mon, 9 Nov 2020 10:13:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0texture2d=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scene.cpp | 2 +- Texture.cpp | 39 ++++++++++++++++++++++++++++++++++++++ Texture.h | 16 ++++++++++++++++ raytracing.vcxproj | 3 +++ raytracing.vcxproj.filters | 6 ++++++ util.cpp | 37 ++++++++++++++++++++++++++++++++++++ util.h | 2 ++ 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 Texture.cpp create mode 100644 Texture.h diff --git a/Scene.cpp b/Scene.cpp index 2a1fcc6..b0951a7 100644 --- a/Scene.cpp +++ b/Scene.cpp @@ -13,7 +13,7 @@ static Camera* sCamera = nullptr; Sphere sphere(Vector3(0.0f, 0.5f, 0.0f), 0.5f); static Object* sRootObject = nullptr; static Material* lambert = nullptr; -static int sSampleCount = 128; +static int sSampleCount = 6; static int sMaxBounceTime = 30; void AddObject(Object* object) { if (sRootObject == nullptr) { diff --git a/Texture.cpp b/Texture.cpp new file mode 100644 index 0000000..8640092 --- /dev/null +++ b/Texture.cpp @@ -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); +} diff --git a/Texture.h b/Texture.h new file mode 100644 index 0000000..4a07ad6 --- /dev/null +++ b/Texture.h @@ -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); +}; \ No newline at end of file diff --git a/raytracing.vcxproj b/raytracing.vcxproj index d8976d7..0a44122 100644 --- a/raytracing.vcxproj +++ b/raytracing.vcxproj @@ -76,6 +76,7 @@ Disabled true true + /D_CRT_SECURE_NO_WARNINGS %(AdditionalOptions) Windows @@ -133,6 +134,7 @@ + @@ -143,6 +145,7 @@ + diff --git a/raytracing.vcxproj.filters b/raytracing.vcxproj.filters index 5501549..5fb4662 100644 --- a/raytracing.vcxproj.filters +++ b/raytracing.vcxproj.filters @@ -42,6 +42,9 @@ 源文件 + + 源文件 + @@ -68,5 +71,8 @@ 源文件 + + 源文件 + \ No newline at end of file diff --git a/util.cpp b/util.cpp index 080927a..92e8583 100644 --- a/util.cpp +++ b/util.cpp @@ -1,4 +1,5 @@ #include "util.h" +#include static RenderableBuffer* sRenderableBuffer = nullptr; void InitRenderableBuffer(HDC dc, int width, int height) { @@ -41,3 +42,39 @@ float srandf() { return randf() * 2.0f - 1.0f; } + +unsigned char* LoadFileContent(const char* path, int& file_size) { + FILE* file = fopen(path, "rb"); + if (file != nullptr) { + fseek(file, 0, SEEK_END); + file_size = ftell(file); + if (file_size > 0) { + rewind(file); + unsigned char* content = new unsigned char[file_size + 1]; + fread(content, 1, file_size, file); + content[file_size] = 0; + fclose(file); + return content; + } + fclose(file); + } + return nullptr; +} + +unsigned char* DecodeBMP(unsigned char* bmp_file_content, int& width, int& height) { + if (0x4D42 == *((unsigned short*)bmp_file_content)) { + int pixel_data_offset = *((int*)(bmp_file_content + 10)); + width = *((int*)(bmp_file_content + 18)); + height = *((int*)(bmp_file_content + 22)); + unsigned char * pixel_data = bmp_file_content + pixel_data_offset; + //bgr->rgb + for (int i = 0; i < width * height; i++) { + int pixel_data_start_index = i * 3; + unsigned char r = pixel_data[pixel_data_start_index + 2]; + pixel_data[pixel_data_start_index + 2] = pixel_data[pixel_data_start_index]; + pixel_data[pixel_data_start_index] = r; + } + return pixel_data; + } + return nullptr; +} diff --git a/util.h b/util.h index 177227d..3cd1324 100644 --- a/util.h +++ b/util.h @@ -15,6 +15,8 @@ struct RenderableBuffer { void InitRenderableBuffer(HDC dc, int width, int height); void SetColor(int x, int y, AByte r, AByte g, AByte b, AByte a); +unsigned char* LoadFileContent(const char* path, int& filesize); +unsigned char* DecodeBMP(unsigned char* bmp_file, int& width, int& height); void ASwapBuffers(HDC dc); float randf(); float srandf(); \ No newline at end of file