-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (73 loc) · 2.31 KB
/
Copy pathmain.cpp
File metadata and controls
89 lines (73 loc) · 2.31 KB
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#include <bits/stdc++.h>
#include "utils.h"
#include "ObjUtils.h"
using namespace std;
int main(int argc, char *argv[])
{
// returns zero on success else non-zero
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window *win = SDL_CreateWindow("Renderer Kumar",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1000, 1000, 0);
if (!win)
{
cerr << "SDL_CreateWindow Error: " << SDL_GetError() << endl;
SDL_Quit();
return 1;
}
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
// creates a renderer to render our images
SDL_Renderer *rend = SDL_CreateRenderer(win, -1, render_flags);
bool running = true;
SDL_Event event;
float zOffset = 10.0f;
Obj ImportedModel;
vector<vertex> VertexBuffer;
vector<vector<int>> EdgeBuffer;
if (!ImportedModel.loadOBJ("plant.obj"))
{
cerr << "Failed to load model!" << endl;
return 1;
}
float theta = 0;
while (running)
{
vector<vertex> transformedVertices;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
running = false; // Exit the loop if close button is clicked
}
}
SDL_SetRenderDrawColor(rend, 0, 0, 0, 0); // white
// Clear the screen with the chosen color
SDL_RenderClear(rend);
vertex center = calculateCenter(ImportedModel.VertexBuffer);
for (auto &v : ImportedModel.VertexBuffer)
{
vertex temp = v;
temp.rotateY(theta, center);
transformedVertices.push_back(temp.projected());
}
theta += 0.01;
for (auto edges : ImportedModel.EdgeBuffer)
{
plotTriangle(transformedVertices[edges[0]], transformedVertices[edges[1]], transformedVertices[edges[2]], rend);
}
// Update the screen (present the renderer contents)
SDL_RenderPresent(rend);
SDL_Delay(16); // Delay for ~60 FPS
}
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}