-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
77 lines (71 loc) · 2.24 KB
/
shader.cpp
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
#include "shader.h"
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <glad/glad.h>
using namespace std;
Shader::Shader(){
this->fs_id = 0;
this->vs_id = 0;
this->prog_id = 0;
}
void Shader::makeProgram(char * vsPath, char * fsPath){
attatchVs(vsPath);
attatchFs(fsPath);
if(!this->vs_id || !this->fs_id){
std::cout << "VS OR FS FAILED TO COMPILE\n";
}
this->prog_id = glCreateProgram();
cout << "hi\n";
glAttachShader(this->prog_id, this->vs_id);
glAttachShader(this->prog_id, this->fs_id);
glLinkProgram(this->prog_id);
int success;
char infoLog[512];
// print linking errors if any
glGetProgramiv(this->prog_id, GL_LINK_STATUS, &success);
if(!success){
glGetProgramInfoLog(this->prog_id, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
}
Shader::Shader(char * vsPath, char * fsPath){
cout << "hi\n";
makeProgram(vsPath, fsPath);
}
void Shader::attatchFs(char * fsPath){
int fd = open(fsPath, O_RDONLY);
char * mem = (char * )calloc(1024, sizeof(char));
read(fd, mem, 1024);
const char * c = mem;
this->fs_id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(this->fs_id, 1, &c, NULL);
glCompileShader(this->fs_id);
int success;
char infoLog[512];
glGetShaderiv(this->fs_id, GL_COMPILE_STATUS, &success);
if(!success){
glGetShaderInfoLog(this->fs_id, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
};
}
void Shader::attatchVs(char * vsPath){
int fd = open(vsPath, O_RDONLY);
char * mem = (char * )calloc(1024, sizeof(char));
read(fd, mem, 1024);
this->vs_id = glCreateShader(GL_VERTEX_SHADER);
cout << "attan\n";
const char * c = mem;
glShaderSource(this->vs_id, 1, &c, NULL);
glCompileShader(this->vs_id);
int success;
char infoLog[512];
glGetShaderiv(this->vs_id, GL_COMPILE_STATUS, &success);
if(!success){
glGetShaderInfoLog(this->vs_id, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
};
}
void Shader::use(){
glUseProgram(this->prog_id);
}