Skip to content

Commit

Permalink
remove texture mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
A-deLuna committed Oct 24, 2019
1 parent fa13319 commit b4ba7a1
Showing 1 changed file with 4 additions and 54 deletions.
58 changes: 4 additions & 54 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "assimp/cimport.h"
#include "assimp/postprocess.h"
#include "assimp/scene.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

using glm::vec2;
using glm::vec3;
Expand Down Expand Up @@ -36,16 +34,8 @@ struct screen {
uint8_t depth;
};

struct image {
uint8_t const* buffer;
int32_t x;
int32_t y;
int32_t channels;
};

struct resources {
aiScene const* scene;
image* image;
};

void Initialize(SDL_Window** window,
Expand Down Expand Up @@ -82,15 +72,7 @@ bool PointInTriangle(vec3 baricenter) {
baricenter.x + baricenter.y <= 1.f;
}

void DrawTriangle(screen* screen,
vec3 v1,
vec3 v2,
vec3 v3,
vec3 normal,
vec2 t1,
vec2 t2,
vec2 t3,
image* image) {
void DrawTriangle(screen* screen, vec3 v1, vec3 v2, vec3 v3, vec3 normal) {
vec4 bbox = BoundingBox(v1, v2, v3);
float minx = bbox[0];
float miny = bbox[1];
Expand All @@ -111,19 +93,9 @@ void DrawTriangle(screen* screen,
uint8_t* pixelDepth = screen->depthbuffer + (x + y * screen->width);
if (pointz > *pixelDepth) {
*pixelDepth = pointz;
vec2 textureCoords =
t1 * baricenter.x + t2 * baricenter.y + t3 * baricenter.z;
glm::ivec2 screenTextCoords(textureCoords.x * image->x,
(1.f - textureCoords.y) * image->y);

uint8_t const* pixelData =
&image->buffer[image->channels * (screenTextCoords.x +
image->x * screenTextCoords.y)];
color color = ColorRGB(pixelData[0], pixelData[1], pixelData[2]);
screen->framebuffer[x + y * screen->width] =
ColorRGB((uint8_t)(magnitude * color.red),
(uint8_t)(magnitude * color.green),
(uint8_t)(magnitude * color.blue));
ColorRGB((uint8_t)(magnitude * 0xff), (uint8_t)(magnitude * 0xff),
(uint8_t)(magnitude * 0xff));
}
}
}
Expand Down Expand Up @@ -163,15 +135,7 @@ void Draw(screen* screen, resources* resources) {
vec3 normal =
glm::normalize(glm::cross(v3_model - v1_model, v2_model - v1_model));

vec2 t1 = vec2(mesh->mTextureCoords[0][face.mIndices[0]].x,
mesh->mTextureCoords[0][face.mIndices[0]].y);
vec2 t2 = vec2(mesh->mTextureCoords[0][face.mIndices[1]].x,
mesh->mTextureCoords[0][face.mIndices[1]].y);
vec2 t3 = vec2(mesh->mTextureCoords[0][face.mIndices[2]].x,
mesh->mTextureCoords[0][face.mIndices[2]].y);

DrawTriangle(screen, v1_screen, v2_screen, v3_screen, normal, t1, t2, t3,
resources->image);
DrawTriangle(screen, v1_screen, v2_screen, v3_screen, normal);
}
}

Expand Down Expand Up @@ -218,19 +182,6 @@ int main() {
exit(0);
}

// Load texture
image image = {};
image.channels = 3;
resources.image = &image;
int imageChannels;
image.buffer = stbi_load("african_head/african_head_diffuse.tga", &image.x,
&image.y, &imageChannels,
/*desired_channels=*/image.channels);
if (!image.buffer) {
std::cout << stbi_failure_reason();
exit(0);
}

screen screen = {};
screen.width = 1024;
screen.height = 768;
Expand All @@ -251,7 +202,6 @@ int main() {

Destroy(&screen, window, renderer, texture);
aiReleaseImport(resources.scene);
stbi_image_free((void*)image.buffer);
}

void Initialize(SDL_Window** window,
Expand Down

0 comments on commit b4ba7a1

Please sign in to comment.