aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 375be0a99262ad8dbfba09ed4a43544d11301656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/05/09 10:20:09 by charles           #+#    #+#             */
/*   Updated: 2020/05/13 11:45:32 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "scop.h"

/*
** TODO
** - texture
**   - parse vt
**   - parse coord index of f
** - center object
** - parse mtl file
** - color
**   - transition with texture
*/

void	debugmat(t_ftmmat4 *mat)
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			printf("%.2f, ", mat->m[i * 4 + j]);
		}
		printf("\n");
	}
	printf("\n");
}

extern bool g_window_resized;

int main(int argc, char **argv)
{
	GLFWwindow	*window;
	t_object	object;
	t_gl_state	state;

	if (argc != 2)
	{
		ft_putstr("Usage: ");
		ft_putstr(argv[0]);
		ft_putendl(" [obj file]");
		return (1);
	}
	if (parse(argv[1], &object) == -1)
	{
		ft_putstr("Error: couldn't parse ");
		ft_putendl(argv[1]);
		return (1);
	}

	t_ftmmat4	center_trans;
	center_mat4_init_translate(&center_trans, object.vertices, object.vertices_len);
	/* debugmat(&center_trans); */

	/* for (size_t i = 0; i < object.indices_len; i++) */
	/* { */
	/* 	printf("%u, ", object.indices[i++]); */
	/* 	printf("%u, ", object.indices[i++]); */
	/* 	printf("%u\n", object.indices[i]); */
	/* } */
	/* printf("yo %lu\n", object.indices_len); */
	/* for (size_t i = 0; i < object.vertices_len; i++) */
	/* { */
	/* 	printf("%f, ", object.vertices[i++]); */
	/* 	printf("%f, ", object.vertices[i++]); */
	/* 	printf("%f\n", object.vertices[i]); */
	/* } */
	/* printf("yo %lu\n", object.vertices_len); */

	t_ftmmat4	model;
	t_ftmmat4	view;
	t_ftmmat4	proj;
	t_ftmvec3	vec;

	ftm_mat4init_eye(&model, 1.0);
	/* ftm_mat4translate(&model, 0.0, 0.0, -10.0); */
	/* ftm_mat4rotate(&model, ftm_radian(45.0f), ftm_vec3init(&vec, 0.0, 1.0, 0.0)); */
	/* ftm_mat4mul(&model, &center_trans); */
	/* ftm_mat4rotate(&model, M_PI_4, ftm_vec3init(&vec, 0.0, 1.0, 0.0)); */
	/* ftm_mat4scale(&model, 1.1, 1.1, 1.1); */

	ftm_mat4init_eye(&view, 1.0);

	ftm_mat4init_perspective(&proj, M_PI_2 / 2.0, 1.0, 0.1, 100.0);

	/* printf("asfd\n"); */
	/* printf("%f %f %f\n", vec.v[0], vec.v[1], vec.v[2]); */
	/* debugmat(&model); */
	/* debugmat(&view); */
	/* debugmat(&proj); */


	if ((window = glfw_init(400, 400)) == NULL
		|| gl_state_init(&state, &object) == -1)
		return (1);

	GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
	GL_CALL(glBindVertexArray(0));

	GL_CALL(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE));

	GL_CALL(glUseProgram(state.shader));
	gl_state_set_mvp(&state, &model, &view, &proj);

	float deg = 0.0;
	while (!glfwWindowShouldClose(window))
	{
		GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
		GL_CALL(glClear(GL_COLOR_BUFFER_BIT));

		GL_CALL(glUseProgram(state.shader));

		ftm_mat4init_eye(&model, 1.0);
		ftm_mat4translate(&model, 0.0, 0.0, -10.0);
		ftm_mat4rotate(&model, ftm_radian(deg), ftm_vec3init(&vec, 0.0, 1.0, 0.0));
		ftm_mat4mul(&model, &center_trans);
		deg += 0.4;

		if (g_window_resized)
		{
			int w, h;
			glfwGetFramebufferSize(window, &w, &h);
			/* printf("%d %d\n", w, h); */
			ftm_mat4init_perspective(&proj, M_PI_4, (float)w / (float)h, 0.1, 100.0);
			g_window_resized = false;
		}

		gl_state_set_mvp(&state, &model, &view, &proj);

		GL_CALL(glBindVertexArray(state.vertex_array));
		GL_CALL(glDrawElements(GL_TRIANGLES, object.indices_len, GL_UNSIGNED_INT, (void*)0));

		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	gl_state_quit(&state, &object);
	glfwDestroyWindow(window);
	glfwTerminate();
	return 0;
}