-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.h
120 lines (102 loc) · 3.11 KB
/
utils.h
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
#pragma once
#include <SDL.h>
#include "glad.h"
#include <iostream>
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <assimp/scene.h>
typedef unsigned int uint;
typedef unsigned char byte;
inline glm::mat4 assimpToGlmMatrix(aiMatrix4x4 mat) {
glm::mat4 m;
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
m[x][y] = mat[y][x];
}
}
return m;
}
inline glm::vec3 assimpToGlmVec3(aiVector3D vec) {
return glm::vec3(vec.x, vec.y, vec.z);
}
inline glm::quat assimpToGlmQuat(aiQuaternion quat) {
glm::quat q;
q.x = quat.x;
q.y = quat.y;
q.z = quat.z;
q.w = quat.w;
return q;
}
inline unsigned int createShader(const char* vertexStr, const char* fragmentStr) {
int success;
char info_log[512];
uint
program = glCreateProgram(),
vShader = glCreateShader(GL_VERTEX_SHADER),
fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, &vertexStr, 0);
glCompileShader(vShader);
glGetShaderiv(vShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vShader, 512, 0, info_log);
std::cout << "vertex shader compilation failed!\n" << info_log << std::endl;
}
glShaderSource(fShader, 1, &fragmentStr, 0);
glCompileShader(fShader);
glGetShaderiv(fShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fShader, 512, 0, info_log);
std::cout << "fragment shader compilation failed!\n" << info_log << std::endl;
}
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(program, 512, 0, info_log);
std::cout << "program linking failed!\n" << info_log << std::endl;
}
glDetachShader(program, vShader);
glDeleteShader(vShader);
glDetachShader(program, fShader);
glDeleteShader(fShader);
return program;
}
inline SDL_Window* initWindow(int &windowWidth,int &windowHeight) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_LoadLibrary(NULL);
//window
SDL_Window* window = SDL_CreateWindow("skin Animation",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
glClearColor(1.0, 0.0, 0.4, 1.0);
glEnable(GL_DEPTH_TEST);
SDL_ShowWindow(window);
SDL_GL_SetSwapInterval(1);
return window;
}