source: opengl-game/common/shader.cpp@ 92bc4fe

feature/imgui-sdl points-test
Last change on this file since 92bc4fe was 8a6d19d, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 7 years ago

Implement movement in a basic 3D scene and add some examples from the new OpenGL tutorials site

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