This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.h
96 lines (82 loc) · 3.54 KB
/
shader.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
#ifndef SHADER_H
#define SHADER_H
// GLEW
#define GLEW_STATIC
#include <glew.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
//************************************************************************************************************************************************
void shaderBuildProgram(const GLchar* vertexPath, const GLchar* fragmentPath, GLuint* Program)
{
// Ïîëó÷àåì èñõîäíûé êîä øåéäåðà èç filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
if (vShaderFile.is_open()&& fShaderFile.is_open())
{
std::stringstream vShaderStream, fShaderStream;
// Ñ÷èòûâàåì äàííûå â ïîòîêè
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// Çàêðûâàåì ôàéëû
vShaderFile.close();
fShaderFile.close();
// Ïðåîáðàçîâûâàåì ïîòîêè â ìàññèâ GLchar
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
else
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar* fShaderCode = fragmentCode.c_str();
// Ñáîðêà øåéäåðîâ
GLuint vertex, fragment;
GLint success;
const int sizeInfoLog = 512;
GLchar infoLog[sizeInfoLog];
// Âåðøèííûé øåéäåð
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); // Åñëè åñòü îøèáêè - âûâåñòè èõ
if (!success)
{
glGetShaderInfoLog(vertex, sizeInfoLog, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Ôðàãìåíòíûé øåéäåð
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); // Åñëè åñòü îøèáêè - âûâåñòè èõ
if (!success)
{
glGetShaderInfoLog(fragment, sizeInfoLog, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
*Program = glCreateProgram(); // Øåéäåðíàÿ ïðîãðàììà
glAttachShader(*Program, vertex);
glAttachShader(*Program, fragment);
glLinkProgram(*Program);
glGetProgramiv(*Program, GL_LINK_STATUS, &success); // Åñëè åñòü îøèáêè - âûâåñòè èõ
if (!success)
{
glGetProgramInfoLog(*Program, sizeInfoLog, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
// Óäàëÿåì øåéäåðû, ïîñêîëüêó îíè áîëüøå íå íóæíû.
glDeleteShader(vertex);
glDeleteShader(fragment);
}
//************************************************************************************************************************************************
void Use(GLuint program)
{
glUseProgram(program);
}
//************************************************************************************************************************************************
#endif