3D City v-1.0
Computer Graphics Project
engine Struct Reference

backend for all rendering purposes More...

#include <engine.h>

Public Member Functions

 engine (const uint32_t &x, const uint32_t &y)
 constructor and opengl draw surface initialization More...
 
 ~engine ()
 destructor More...
 
void clear ()
 clear all drawing surface More...
 
void draw ()
 send framebuffer from cpu to gpu as texture to be able to render using opengl More...
 
color getcolor (const Vertex2 &v, Mesh *mesh) const
 essentially a fragment shader : inputs fragment position and information and outputs color value for the fragment More...
 
void makeRequiredTriangles (const std::vector< Vertex > &vertices, const std::vector< uint32_t > &indices, const mat4f &modelmat)
 fill triangles array by doing required clipping and culling More...
 
void setTime (float time)
 

Public Attributes

framebuffer * fboCPU
 
uint32_t vao
 
uint32_t vbo
 
uint32_t ibo
 
uint32_t tex
 
uint32_t shader
 
std::vector< std::tuple< std::array< Vertex2, 3 >, Mesh * > > triangles
 set of triangles to be drawn to screen More...
 
float nearPlane = 1.0f
 nearplane of camera for clipping and culling More...
 
bool cullBackface = true
 switch for backface culling More...
 
Mesh * currentMesh = nullptr
 which material to use for further render calls More...
 
float ambientLightIntensity = 0.1f
 lights. More...
 
color ambientLightColor = color(255)
 
dirLight dirlight
 
std::vector< pointLight > pointLights
 
camera * cam = nullptr
 camera More...
 
bool wireframe = false
 
float lightRenderDistance = 0.05
 
bool printed = false
 

Private Member Functions

color CalcDirLight (const vec3 &normal, const vec3 &viewdir, const color &col, const Material *material) const
 function to calculate effect of directional light in a fragment with color col More...
 
void makeRequiredTrianglesHelper (std::array< Vertex, 3 > &points, const mat4f &modelmat, const mat4f &view, const mat4f &per)
 
void putpixel_adjusted (int x, int y, float z, const color &col=color(255))
 putpixel assuming middle of screen to be the origin More...
 
float getpixelZ_adjusted (int x, int y) const
 get z value of specified pixel from framebuffer More...
 
void draw_bresenham_adjusted (int x1, int y1, int x2, int y2, const color &col=color(0))
 bresenham line drawing function to draw wireframe of objects More...
 
void clip1 (const std::array< vec4, 3 > &tris, unsigned char which, float &u1, float &u2) const
 
void clip2 (const std::array< vec4, 3 > &tris, unsigned char idx1, unsigned char idx2, unsigned char rem, float &u1, float &u2) const
 
void clip2helper (const mat4f &per, const std::array< EXTRA_VERTEX_INFO, 3 > &extraInfoAboutVertex, const std::array< vec4, 3 > &modelviewTransformed, Vertex *points, unsigned char idx1, unsigned char idx2, unsigned char rem, std::array< Vertex2, 3 > &t)
 clips and perspective transforms input triangle vertices if two points are out of clip space More...
 
void clip1helper (const mat4f &per, const std::array< EXTRA_VERTEX_INFO, 3 > &extraInfoAboutVertex, const std::array< vec4, 3 > &modelviewTransformed, Vertex *points, unsigned char idx1, std::array< Vertex2, 3 > &t, std::vector< std::tuple< std::array< Vertex2, 3 >, Mesh * > > &triangles)
 clips and perspective transforms input triangle vertices if one point is out of the clip space More...
 
void fillBottomFlatTriangle (const Vertex2 &v0, const Vertex2 &v1, const Vertex2 &v2, Mesh *mesh)
 rasterize BottomFlatTriangle: v2 is top unique and v1.x > v0.x More...
 
void fillTopFlatTriangle (const Vertex2 &v0, const Vertex2 &v1, const Vertex2 &v2, Mesh *mesh)
 rasterize TopFlatTriangle: v0 is bottom unique and v2.x > v1.x More...
 
void calculateBarycentricCoordinates (const vec2 &p, const vec4 &v1, const vec4 &v2, const vec4 &v3, std::array< float, 3 > &values)
 
void fillBottomFlatTriangleBary (const Vertex2 &v0, const Vertex2 &v1, const Vertex2 &v2, Mesh *mesh)
 rasterize BottomFlatTriangle: v2 is top unique and v1.x > v0.x More...
 
void fillTopFlatTriangleBary (const Vertex2 &v0, const Vertex2 &v1, const Vertex2 &v2, Mesh *mesh)
 
void rasterizeTrianglesHelper (std::tuple< std::array< Vertex2, 3 >, Mesh * > &tris)
 
void rasterizeTriangles ()
 helper function to rasterize triangles in vector of triangles. More...
 

Static Private Member Functions

static float max (const float &first, const float &second)
 
static vec3 reflect (const vec3 &I, const vec3 &N)
 
static color CalcPointLight (const pointLight &light, const vec3 &normal, const vec3 &fragPos, const vec3 &viewDir, const color &diffuseColor, float int_by_at, const Material *material)
 function to calculate effect of a point light in a fragment with color col More...
 

Private Attributes

thread_pool pool
 
const char * vertexShaderSource
 
const char * fragmentShaderSource
 
float vertices [20]
 
uint32_t indices [6]
 
std::unordered_map< uint8_t, float > greenTimeMap = {{0, 0}, {1, 0.1}, {2, 0.3}, {3, 0.3}, {4, 0.35}, {5, 0.4}, {6, 0.5}, {7, 0.55}, {8, 0.6}, {9, 1}, {10, 1}, {11, 1}, {12, 1}, {13, 1}, {14, 1}, {15, 1}, {16, 1}, {17, 0.8}, {18, 0.5}, {19, 0.4}, {20, 0.3}, {21, 0.25}, {22, 0.1}, {23, 0.06}, {24, 0}}
 
std::unordered_map< uint8_t, float > blueTimeMap = {{0, 0}, {1, 0}, {2, 0}, {3, 0.05}, {4, 0.06}, {5, 0.1}, {6, 0.15}, {7, 0.25}, {8, 0.35}, {9, 0.75}, {10, 0.8}, {11, 1}, {12, 1}, {13, 1}, {14, 1}, {15, 1}, {16, 0.8}, {17, 0.6}, {18, 0.4}, {19, 0.1}, {20, 0.01}, {21, 0}, {22, 0}, {23, 0}, {24, 0}}
 
std::mutex fboCPUMutex
 

Detailed Description

backend for all rendering purposes

Definition at line 94 of file engine.h.

Constructor & Destructor Documentation

◆ engine()

engine::engine ( const uint32_t &  x,
const uint32_t &  y 
)
inline

constructor and opengl draw surface initialization

Definition at line 148 of file engine.h.

◆ ~engine()

engine::~engine ( )
inline

destructor

Definition at line 231 of file engine.h.

Member Function Documentation

◆ CalcDirLight()

color engine::CalcDirLight ( const vec3 &  normal,
const vec3 &  viewdir,
const color &  col,
const Material *  material 
) const
inlineprivate

function to calculate effect of directional light in a fragment with color col

Definition at line 427 of file engine.h.

◆ CalcPointLight()

static color engine::CalcPointLight ( const pointLight &  light,
const vec3 &  normal,
const vec3 &  fragPos,
const vec3 &  viewDir,
const color &  diffuseColor,
float  int_by_at,
const Material *  material 
)
inlinestaticprivate

function to calculate effect of a point light in a fragment with color col

Definition at line 440 of file engine.h.

◆ calculateBarycentricCoordinates()

void engine::calculateBarycentricCoordinates ( const vec2 &  p,
const vec4 &  v1,
const vec4 &  v2,
const vec4 &  v3,
std::array< float, 3 > &  values 
)
inlineprivate

Definition at line 784 of file engine.h.

◆ clear()

void engine::clear ( )
inline

clear all drawing surface

Definition at line 242 of file engine.h.

◆ clip1()

void engine::clip1 ( const std::array< vec4, 3 > &  tris,
unsigned char  which,
float &  u1,
float &  u2 
) const
inlineprivate

Definition at line 618 of file engine.h.

◆ clip1helper()

void engine::clip1helper ( const mat4f &  per,
const std::array< EXTRA_VERTEX_INFO, 3 > &  extraInfoAboutVertex,
const std::array< vec4, 3 > &  modelviewTransformed,
Vertex *  points,
unsigned char  idx1,
std::array< Vertex2, 3 > &  t,
std::vector< std::tuple< std::array< Vertex2, 3 >, Mesh * > > &  triangles 
)
inlineprivate

clips and perspective transforms input triangle vertices if one point is out of the clip space

Definition at line 647 of file engine.h.

◆ clip2()

void engine::clip2 ( const std::array< vec4, 3 > &  tris,
unsigned char  idx1,
unsigned char  idx2,
unsigned char  rem,
float &  u1,
float &  u2 
) const
inlineprivate

Definition at line 623 of file engine.h.

◆ clip2helper()

void engine::clip2helper ( const mat4f &  per,
const std::array< EXTRA_VERTEX_INFO, 3 > &  extraInfoAboutVertex,
const std::array< vec4, 3 > &  modelviewTransformed,
Vertex *  points,
unsigned char  idx1,
unsigned char  idx2,
unsigned char  rem,
std::array< Vertex2, 3 > &  t 
)
inlineprivate

clips and perspective transforms input triangle vertices if two points are out of clip space

Definition at line 631 of file engine.h.

◆ draw()

void engine::draw ( )
inline

send framebuffer from cpu to gpu as texture to be able to render using opengl

Definition at line 251 of file engine.h.

◆ draw_bresenham_adjusted()

void engine::draw_bresenham_adjusted ( int  x1,
int  y1,
int  x2,
int  y2,
const color &  col = color(0) 
)
inlineprivate

bresenham line drawing function to draw wireframe of objects

Definition at line 580 of file engine.h.

◆ fillBottomFlatTriangle()

void engine::fillBottomFlatTriangle ( const Vertex2 &  v0,
const Vertex2 &  v1,
const Vertex2 &  v2,
Mesh *  mesh 
)
inlineprivate

rasterize BottomFlatTriangle: v2 is top unique and v1.x > v0.x

Definition at line 669 of file engine.h.

◆ fillBottomFlatTriangleBary()

void engine::fillBottomFlatTriangleBary ( const Vertex2 &  v0,
const Vertex2 &  v1,
const Vertex2 &  v2,
Mesh *  mesh 
)
inlineprivate

rasterize BottomFlatTriangle: v2 is top unique and v1.x > v0.x

Definition at line 830 of file engine.h.

◆ fillTopFlatTriangle()

void engine::fillTopFlatTriangle ( const Vertex2 &  v0,
const Vertex2 &  v1,
const Vertex2 &  v2,
Mesh *  mesh 
)
inlineprivate

rasterize TopFlatTriangle: v0 is bottom unique and v2.x > v1.x

Definition at line 729 of file engine.h.

◆ fillTopFlatTriangleBary()

void engine::fillTopFlatTriangleBary ( const Vertex2 &  v0,
const Vertex2 &  v1,
const Vertex2 &  v2,
Mesh *  mesh 
)
inlineprivate

Definition at line 882 of file engine.h.

◆ getcolor()

color engine::getcolor ( const Vertex2 &  v,
Mesh *  mesh 
) const
inline

essentially a fragment shader : inputs fragment position and information and outputs color value for the fragment

Definition at line 314 of file engine.h.

◆ getpixelZ_adjusted()

float engine::getpixelZ_adjusted ( int  x,
int  y 
) const
inlineprivate

get z value of specified pixel from framebuffer

Definition at line 572 of file engine.h.

◆ makeRequiredTriangles()

void engine::makeRequiredTriangles ( const std::vector< Vertex > &  vertices,
const std::vector< uint32_t > &  indices,
const mat4f &  modelmat 
)
inline

fill triangles array by doing required clipping and culling

Definition at line 361 of file engine.h.

◆ makeRequiredTrianglesHelper()

void engine::makeRequiredTrianglesHelper ( std::array< Vertex, 3 > &  points,
const mat4f &  modelmat,
const mat4f &  view,
const mat4f &  per 
)
inlineprivate

Definition at line 454 of file engine.h.

◆ max()

static float engine::max ( const float &  first,
const float &  second 
)
inlinestaticprivate

Definition at line 416 of file engine.h.

◆ putpixel_adjusted()

void engine::putpixel_adjusted ( int  x,
int  y,
float  z,
const color &  col = color(255) 
)
inlineprivate

putpixel assuming middle of screen to be the origin

Definition at line 561 of file engine.h.

◆ rasterizeTriangles()

void engine::rasterizeTriangles ( )
inlineprivate

helper function to rasterize triangles in vector of triangles.

Definition at line 1007 of file engine.h.

◆ rasterizeTrianglesHelper()

void engine::rasterizeTrianglesHelper ( std::tuple< std::array< Vertex2, 3 >, Mesh * > &  tris)
inlineprivate

Definition at line 935 of file engine.h.

◆ reflect()

static vec3 engine::reflect ( const vec3 &  I,
const vec3 &  N 
)
inlinestaticprivate

Definition at line 420 of file engine.h.

◆ setTime()

void engine::setTime ( float  time)
inline

Definition at line 382 of file engine.h.

Member Data Documentation

◆ ambientLightColor

color engine::ambientLightColor = color(255)

Definition at line 298 of file engine.h.

◆ ambientLightIntensity

float engine::ambientLightIntensity = 0.1f

lights.

Definition at line 297 of file engine.h.

◆ blueTimeMap

std::unordered_map<uint8_t, float> engine::blueTimeMap = {{0, 0}, {1, 0}, {2, 0}, {3, 0.05}, {4, 0.06}, {5, 0.1}, {6, 0.15}, {7, 0.25}, {8, 0.35}, {9, 0.75}, {10, 0.8}, {11, 1}, {12, 1}, {13, 1}, {14, 1}, {15, 1}, {16, 0.8}, {17, 0.6}, {18, 0.4}, {19, 0.1}, {20, 0.01}, {21, 0}, {22, 0}, {23, 0}, {24, 0}}
private

Definition at line 414 of file engine.h.

◆ cam

camera* engine::cam = nullptr

camera

Definition at line 305 of file engine.h.

◆ cullBackface

bool engine::cullBackface = true

switch for backface culling

Definition at line 287 of file engine.h.

◆ currentMesh

Mesh* engine::currentMesh = nullptr

which material to use for further render calls

Definition at line 292 of file engine.h.

◆ dirlight

dirLight engine::dirlight

Definition at line 299 of file engine.h.

◆ fboCPU

framebuffer* engine::fboCPU

Definition at line 96 of file engine.h.

◆ fboCPUMutex

std::mutex engine::fboCPUMutex
private

Definition at line 452 of file engine.h.

◆ fragmentShaderSource

const char* engine::fragmentShaderSource
private
Initial value:
= R"(#version 330 core
out vec4 FragColor;
in vec2 f_texCoord;
uniform sampler2D tex;
void main()
{
FragColor = texture(tex,f_texCoord);
//FragColor = vec4(f_texCoord.x,f_texCoord.y,0,0);
// if(FragColor.xyz==vec3(0)){
// //FragColor =vec4(FragColor.w);
// }
}
)"

Definition at line 114 of file engine.h.

◆ greenTimeMap

std::unordered_map<uint8_t, float> engine::greenTimeMap = {{0, 0}, {1, 0.1}, {2, 0.3}, {3, 0.3}, {4, 0.35}, {5, 0.4}, {6, 0.5}, {7, 0.55}, {8, 0.6}, {9, 1}, {10, 1}, {11, 1}, {12, 1}, {13, 1}, {14, 1}, {15, 1}, {16, 1}, {17, 0.8}, {18, 0.5}, {19, 0.4}, {20, 0.3}, {21, 0.25}, {22, 0.1}, {23, 0.06}, {24, 0}}
private

Definition at line 413 of file engine.h.

◆ ibo

uint32_t engine::ibo

Definition at line 98 of file engine.h.

◆ indices

uint32_t engine::indices[6]
private
Initial value:
=
{
0, 1, 2,
0, 3, 1}

Definition at line 136 of file engine.h.

◆ lightRenderDistance

float engine::lightRenderDistance = 0.05

Definition at line 309 of file engine.h.

◆ nearPlane

float engine::nearPlane = 1.0f

nearplane of camera for clipping and culling

Definition at line 282 of file engine.h.

◆ pointLights

std::vector<pointLight> engine::pointLights

Definition at line 300 of file engine.h.

◆ pool

thread_pool engine::pool
private

Definition at line 101 of file engine.h.

◆ printed

bool engine::printed = false

Definition at line 357 of file engine.h.

◆ shader

uint32_t engine::shader

Definition at line 98 of file engine.h.

◆ tex

uint32_t engine::tex

Definition at line 98 of file engine.h.

◆ triangles

std::vector<std::tuple<std::array<Vertex2, 3>, Mesh *> > engine::triangles

set of triangles to be drawn to screen

Definition at line 277 of file engine.h.

◆ vao

uint32_t engine::vao

Definition at line 98 of file engine.h.

◆ vbo

uint32_t engine::vbo

Definition at line 98 of file engine.h.

◆ vertexShaderSource

const char* engine::vertexShaderSource
private
Initial value:
= R"(#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 texCoord;
out vec2 f_texCoord;
void main()
{
gl_Position = vec4(aPos.xyz, 1.0);
f_texCoord=texCoord;
}
)"

Definition at line 103 of file engine.h.

◆ vertices

float engine::vertices[20]
private
Initial value:
=
{
-1.0f, -1.0f, -0.0f, 0.f, 0.f,
1.0f, 1.0f, -0.0f, 1.f, 1.f,
-1.0f, 1.0f, -0.0f, 0.f, 1.f,
1.0f, -1.0f, -0.0f, 1.f, 0.f}

Definition at line 128 of file engine.h.

◆ wireframe

bool engine::wireframe = false

Definition at line 307 of file engine.h.