Skip to content

arinaivanova/softgl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

softgl: Simple 3D Software Rasterizer

An educational project that aims to implement a graphics pipeline in C++. It takes 3D scene information, including vertex attributes, textures associated with the 3D models, and camera settings as input, processes and rasterizes the image entirely in software using its own math library, and produces a 2D raster image of the scene.

See Source Code.

Features

  • Custom camera support
  • Triangle mesh support
  • Texture mapping: diffuse, normal, specular
  • Homogeneous polygon clipping (Sutherland-Hodgman)
  • Perspective-correct interpolation
  • Depth buffering
  • Viewing frustum culling
  • Back-face culling

Input and usage overview

Context stores information of the 3D scene, and frame buffer contains the output image.

	gl::Context context;
	gl::FrameBuffer image( width, height );
  1. Add 3D scene information
	// Pool of vertex attributes associated with a 3D scene
	gl::AttribArrays attribArrays;
	
	// Add the three types of vertex attributes to arrays
	attribArrays.vertices.push_back( gmath::Vec4{ x, y, z, w } );	  // position point in space
  	attribArrays.normals.push_back( gmath::Vec3{ x, y, w } ); 	  // normal vector
	attribArrays.texCoords.push_back( gmath::Vec2{ u, v } ); 	  // point on the texture map
	
	// Add vertex attributes to the scene context
	context.attribArr = &attribArrays;
	
	// Create vertices that make up a 3D scene from the pool of vertex attributes
	gl::IndexBuffer indexBuffer;
	// Each vertex has an index to a position point in space, a normal vector and a point on the texture map
	indexBuffer.push_back(positionIndex), indexBuffer.push_back(normalIndex), indexBuffer.push_back(texCoordIndex);
  1. Render a 2D image of the scene
	gl::drawIndexedArrays( &context, indexBuffer );
  1. Access the rendered image
	for (int y = 0; y < height; y++) {
  		for (int x = 0; x < width; x++) {
			// RGB color of the pixel at (x,y)
 			gmath::Vec3 pixelColor = image.colorBuf[ y * width + x ];
 		}
 	}

See full sample input code.

Sample output

3D models were input to softgl render the following images:

alt textalt text

Models obtained from

Samuel (arshlevon) Sharit (n.d.). Diablo 3 pose.

Vidar Rapp (2007). Male african head example.