source: network-game/graphics_library/common/shader.cpp@ ace001a

Last change on this file since ace001a was af116c0, checked in by Dmitry Portnoy <dmp1488@…>, 11 years ago

new graphics library code

  • Property mode set to 100644
File size: 3.8 KB
Line 
1#include <stdio.h>
2#include <string>
3#include <vector>
4#include <iostream>
5#include <fstream>
6#include <algorithm>
7using namespace std;
8
9#include <stdlib.h>
10#include <string.h>
11
12#include <GL/glew.h>
13
14#include "shader.hpp"
15
16GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
17
18 // Create the shaders
19 GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
20 GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
21
22 // Read the Vertex Shader code from the file
23 std::string VertexShaderCode;
24 std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
25 if(VertexShaderStream.is_open()){
26 std::string Line = "";
27 while(getline(VertexShaderStream, Line))
28 VertexShaderCode += "\n" + Line;
29 VertexShaderStream.close();
30 }else{
31 printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
32 getchar();
33 return 0;
34 }
35
36 // Read the Fragment Shader code from the file
37 std::string FragmentShaderCode;
38 std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
39 if(FragmentShaderStream.is_open()){
40 std::string Line = "";
41 while(getline(FragmentShaderStream, Line))
42 FragmentShaderCode += "\n" + Line;
43 FragmentShaderStream.close();
44 }
45
46
47
48 GLint Result = GL_FALSE;
49 int InfoLogLength;
50
51
52
53 // Compile Vertex Shader
54 printf("Compiling shader : %s\n", vertex_file_path);
55 char const * VertexSourcePointer = VertexShaderCode.c_str();
56 glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
57 glCompileShader(VertexShaderID);
58
59 // Check Vertex Shader
60 glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
61 glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
62 if ( InfoLogLength > 0 ){
63 std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
64 glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
65 printf("%s\n", &VertexShaderErrorMessage[0]);
66 }
67
68
69
70 // Compile Fragment Shader
71 printf("Compiling shader : %s\n", fragment_file_path);
72 char const * FragmentSourcePointer = FragmentShaderCode.c_str();
73 glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
74 glCompileShader(FragmentShaderID);
75
76 // Check Fragment Shader
77 glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
78 glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
79 if ( InfoLogLength > 0 ){
80 std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
81 glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
82 printf("%s\n", &FragmentShaderErrorMessage[0]);
83 }
84
85
86
87 // Link the program
88 printf("Linking program\n");
89 GLuint ProgramID = glCreateProgram();
90 glAttachShader(ProgramID, VertexShaderID);
91 glAttachShader(ProgramID, FragmentShaderID);
92 glLinkProgram(ProgramID);
93
94 // Check the program
95 glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
96 glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
97 if ( InfoLogLength > 0 ){
98 std::vector<char> ProgramErrorMessage(InfoLogLength+1);
99 glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
100 printf("%s\n", &ProgramErrorMessage[0]);
101 }
102
103 glDeleteShader(VertexShaderID);
104 glDeleteShader(FragmentShaderID);
105
106 return ProgramID;
107}
Note: See TracBrowser for help on using the repository browser.