draw.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   draw.c                                             :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/05/24 19:15:42 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/05/24 19:50:58 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "draw.h"
14  
15  static double	_clamp(double x, double min, double max)
16  {
17  	if (x < min)
18  		return (min);
19  	if (x > max)
20  		return (max);
21  	return (x);
22  }
23  
24  void	my_mlx_pixel_put(t_img *img, int x, int y, t_color3 pixel)
25  {
26  	char	*dst;
27  	int		pixel_color;
28  
29  	pixel.x = _clamp(pixel.x, 0, 0.999);
30  	pixel.y = _clamp(pixel.y, 0, 0.999);
31  	pixel.z = _clamp(pixel.z, 0, 0.999);
32  	pixel_color = (int)(255.999 * pixel.x) << 16 | \
33  				(int)(255.999 * pixel.y) << 8 | (int)(255.999 * pixel.z);
34  	dst = img->addr + (y * img->len + x * (img->bpp / 8));
35  	*(unsigned int *)dst = pixel_color;
36  }