Guest User

Untitled

a guest
Dec 16th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. ATTENTION: default value of option force_s3tc_enable overridden by environment.
  2. ERROR::SHADER::VERTEX::COMPILATION_FAILED
  3. 0:1(1): error: syntax error, unexpected $end
  4.  
  5. ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
  6. 0:1(1): error: syntax error, unexpected $end
  7.  
  8. ERROR::SHADER::PROGRAM::LINKING_FAILED
  9. error: linking with uncompiled shadererror: linking with uncompiled shader
  10.  
  11. #include <iostream>
  12.  
  13. // GLEW
  14. #define GLEW_STATIC
  15. #include <GL/glew.h>
  16.  
  17. // GLFW
  18. #include <GLFW/glfw3.h>
  19.  
  20. // Other includes
  21. #include "Shader.h"
  22.  
  23. // Window dimensions
  24. const GLuint WIDTH = 800, HEIGHT = 600;
  25.  
  26. // The MAIN function, from here we start the application and run the game loop
  27. int main( )
  28. {
  29. // Init GLFW
  30. glfwInit( );
  31.  
  32. // Set all the required options for GLFW
  33. glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
  34. glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
  35. glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
  36. glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
  37.  
  38. glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
  39.  
  40. // Create a GLFWwindow object that we can use for GLFW's functions
  41. GLFWwindow *window = glfwCreateWindow( WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr );
  42.  
  43. int screenWidth, screenHeight;
  44. glfwGetFramebufferSize( window, &screenWidth, &screenHeight );
  45.  
  46. if ( nullptr == window )
  47. {
  48. std::cout << "Failed to create GLFW window" << std::endl;
  49. glfwTerminate( );
  50.  
  51. return EXIT_FAILURE;
  52. }
  53.  
  54. glfwMakeContextCurrent( window );
  55.  
  56. // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
  57. glewExperimental = GL_TRUE;
  58. // Initialize GLEW to setup the OpenGL Function pointers
  59. if ( GLEW_OK != glewInit( ) )
  60. {
  61. std::cout << "Failed to initialize GLEW" << std::endl;
  62. return EXIT_FAILURE;
  63. }
  64.  
  65. // Define the viewport dimensions
  66. glViewport( 0, 0, screenWidth, screenHeight );
  67.  
  68. // Build and compile our shader program
  69. Shader ourShader( "core.vs", "core.frag" );
  70.  
  71.  
  72. // Set up vertex data (and buffer(s)) and attribute pointers
  73. GLfloat vertices[] =
  74. {
  75. // Positions // Colors
  76. 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Right
  77. -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Bottom Left
  78. 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // Top
  79. };
  80. GLuint VBO, VAO;
  81. glGenVertexArrays( 1, &VAO );
  82. glGenBuffers( 1, &VBO );
  83. // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
  84. glBindVertexArray( VAO );
  85.  
  86. glBindBuffer( GL_ARRAY_BUFFER, VBO );
  87. glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW );
  88.  
  89. // Position attribute
  90. glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof( GLfloat ), ( GLvoid * ) 0 );
  91. glEnableVertexAttribArray( 0 );
  92. // Color attribute
  93. glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof( GLfloat ), ( GLvoid * )( 3 * sizeof( GLfloat ) ) );
  94. glEnableVertexAttribArray( 1 );
  95.  
  96. glBindVertexArray( 0 ); // Unbind VAO
  97.  
  98. // Game loop
  99. while ( !glfwWindowShouldClose( window ) )
  100. {
  101. // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
  102. glfwPollEvents( );
  103.  
  104. // Render
  105. // Clear the colorbuffer
  106. glClearColor( 0.2f, 0.3f, 0.3f, 1.0f );
  107. glClear( GL_COLOR_BUFFER_BIT );
  108.  
  109. // Draw the triangle
  110. ourShader.Use( );
  111. glBindVertexArray( VAO );
  112. glDrawArrays( GL_TRIANGLES, 0, 3 );
  113. glBindVertexArray(0);
  114.  
  115. // Swap the screen buffers
  116. glfwSwapBuffers( window );
  117. }
  118.  
  119. // Properly de-allocate all resources once they've outlived their purpose
  120. glDeleteVertexArrays( 1, &VAO );
  121. glDeleteBuffers( 1, &VBO );
  122.  
  123. // Terminate GLFW, clearing any resources allocated by GLFW.
  124. glfwTerminate( );
  125.  
  126. return EXIT_SUCCESS;
  127. }
  128.  
  129. #ifndef SHADER_H
  130. #define SHADER_H
  131.  
  132. #include <string>
  133. #include <fstream>
  134. #include <sstream>
  135. #include <iostream>
  136.  
  137. #include <GL/glew.h>
  138. #include <cstring>
  139.  
  140. class Shader
  141. {
  142. public:
  143. GLuint Program;
  144. // Constructor generates the shader on the fly
  145. Shader( const GLchar *vertexPath, const GLchar *fragmentPath )
  146. {
  147. // 1. Retrieve the vertex/fragment source code from filePath
  148. std::string vertexCode;
  149. std::string fragmentCode;
  150. std::ifstream vShaderFile;
  151. std::ifstream fShaderFile;
  152. // ensures ifstream objects can throw exceptions:
  153. vShaderFile.exceptions ( std::ifstream::badbit );
  154. fShaderFile.exceptions ( std::ifstream::badbit );
  155. try
  156. {
  157. // Open files
  158. vShaderFile.open( vertexPath );
  159. fShaderFile.open( fragmentPath );
  160. std::stringstream vShaderStream, fShaderStream;
  161. // Read file's buffer contents into streams
  162. vShaderStream << vShaderFile.rdbuf( );
  163. fShaderStream << fShaderFile.rdbuf( );
  164. // close file handlers
  165. vShaderFile.close( );
  166. fShaderFile.close( );
  167. // Convert stream into string
  168. vertexCode = vShaderStream.str( );
  169. fragmentCode = fShaderStream.str( );
  170. }
  171. catch ( std::ifstream::failure e )
  172. {
  173. std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
  174. }
  175. const GLchar *vShaderCode = vertexCode.c_str( );
  176. const GLchar *fShaderCode = fragmentCode.c_str( );
  177. // 2. Compile shaders
  178. GLuint vertex, fragment;
  179. GLint success;
  180. GLchar infoLog[512];
  181. // Vertex Shader
  182. vertex = glCreateShader( GL_VERTEX_SHADER );
  183. glShaderSource( vertex, 1, &vShaderCode, NULL);
  184. glCompileShader( vertex );
  185. // Print compile errors if any
  186. glGetShaderiv( vertex, GL_COMPILE_STATUS, &success );
  187. if ( !success )
  188. {
  189. glGetShaderInfoLog( vertex, 512, NULL, infoLog );
  190. std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILEDn" << infoLog << std::endl;
  191. }
  192. // Fragment Shader
  193. fragment = glCreateShader( GL_FRAGMENT_SHADER );
  194. glShaderSource( fragment, 1, &fShaderCode, NULL);
  195. glCompileShader( fragment );
  196. // Print compile errors if any
  197. glGetShaderiv( fragment, GL_COMPILE_STATUS, &success );
  198. if ( !success )
  199. {
  200. glGetShaderInfoLog( fragment, 512, NULL, infoLog );
  201. std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILEDn" << infoLog << std::endl;
  202. }
  203. // Shader Program
  204. this->Program = glCreateProgram( );
  205. glAttachShader( this->Program, vertex );
  206. glAttachShader( this->Program, fragment );
  207. glLinkProgram( this->Program );
  208. // Print linking errors if any
  209. glGetProgramiv( this->Program, GL_LINK_STATUS, &success );
  210. if (!success)
  211. {
  212. glGetProgramInfoLog( this->Program, 512, NULL, infoLog );
  213. std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILEDn" << infoLog << std::endl;
  214. }
  215. // Delete the shaders as they're linked into our program now and no longer necessery
  216. glDeleteShader( vertex );
  217. glDeleteShader( fragment );
  218.  
  219. }
  220. // Uses the current shader
  221. void Use( )
  222. {
  223. glUseProgram( this->Program );
  224. }
  225. };
  226.  
  227. #endif
  228.  
  229. #version 330 core
  230. layout (location = 0) in vec3 position;
  231. layout (location = 1) in vec3 color;
  232. layout (location = 2) in vec2 texCoord;
  233.  
  234. out vec3 ourColor;
  235. out vec2 TexCoord;
  236.  
  237. void main()
  238. {
  239. gl_Position = vec4(position, 1.0f);
  240. ourColor = color;
  241. // We swap the y-axis by substracing our coordinates from 1. This is done because most images have the top y-axis inversed with OpenGL's top y-axis.
  242. // TexCoord = texCoord;
  243. TexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
  244. }
  245.  
  246. #version 330 core
  247. in vec3 ourColor;
  248. in vec2 TexCoord;
  249.  
  250. out vec4 color;
  251.  
  252. // Texture samplers
  253. uniform sampler2D ourTexture1;
  254.  
  255. void main()
  256. {
  257. // Linearly interpolate between both textures (second texture is only slightly combined)
  258. color = texture(ourTexture1, TexCoord);
  259. }
  260.  
  261. cmake_minimum_required(VERSION 3.9)
  262. project(STUDY_GL)
  263.  
  264. set(CMAKE_CXX_STANDARD 11)
  265.  
  266. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fpermissive")
  267.  
  268. find_package (OpenGL REQUIRED)
  269. find_package (GLUT REQUIRED)
  270. find_package (glfw3 REQUIRED)
  271. find_library (glew REQUIRED)
  272. find_library (glad REQUIRED)
  273.  
  274.  
  275. include_directories(${/usr/include/GLFW/})
  276. include_directories(${/usr/include/GL/})
  277.  
  278.  
  279. file(GLOB SOURCE_FILES
  280. *.cpp
  281. *.h
  282. )
  283.  
  284. add_executable(main.cpp ${SOURCE_FILES} Shader.h)
  285.  
  286. target_link_libraries (main.cpp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${GLFW3_LIBRARIES} -lGL -lglfw -lglut -lGLEW)
Add Comment
Please, Sign In to add comment