Browse Source

添加texture2d类

master
ubuntu20 4 years ago
parent
commit
165040985e
  1. 2
      Scene.cpp
  2. 39
      Texture.cpp
  3. 16
      Texture.h
  4. 3
      raytracing.vcxproj
  5. 6
      raytracing.vcxproj.filters
  6. 37
      util.cpp
  7. 2
      util.h

2
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) {

39
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);
}

16
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);
};

3
raytracing.vcxproj

@ -76,6 +76,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalOptions>/D_CRT_SECURE_NO_WARNINGS %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@ -133,6 +134,7 @@
<ClCompile Include="Ray.cpp" />
<ClCompile Include="Scene.cpp" />
<ClCompile Include="Sphere.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="util.cpp" />
<ClCompile Include="Vector3.cpp" />
</ItemGroup>
@ -143,6 +145,7 @@
<ClInclude Include="Ray.h" />
<ClInclude Include="Scene.h" />
<ClInclude Include="Sphere.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="util.h" />
<ClInclude Include="Vector3.h" />
</ItemGroup>

6
raytracing.vcxproj.filters

@ -42,6 +42,9 @@
<ClCompile Include="Material.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Texture.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Scene.h">
@ -68,5 +71,8 @@
<ClInclude Include="Material.h">
<Filter>源文件</Filter>
</ClInclude>
<ClInclude Include="Texture.h">
<Filter>源文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

37
util.cpp

@ -1,4 +1,5 @@
#include "util.h"
#include<functional>
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;
}

2
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();
Loading…
Cancel
Save