Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.42 KB | None | 0 0
  1. // Include standard headers
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Include GLEW
  6. #include <GL/glew.h>
  7.  
  8. // Include GLFW
  9. #include <glfw3.h>
  10. GLFWwindow* window;
  11.  
  12. // Include GLM
  13. #include <glm/glm.hpp>
  14. #include <glm/gtc/matrix_transform.hpp>
  15. using namespace glm;
  16.  
  17. #include <common/shader.hpp>
  18.  
  19. int main( void )
  20. {
  21. // Initialise GLFW
  22. if( !glfwInit() )
  23. {
  24. fprintf( stderr, "Failed to initialize GLFWn" );
  25. return -1;
  26. }
  27.  
  28. glfwWindowHint(GLFW_SAMPLES, 4);
  29. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  30. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  31. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  32.  
  33. // Open a window and create its OpenGL context
  34. window = glfwCreateWindow( 1024, 768, "Tutorial 04 - Colored Cube", NULL, NULL);
  35. if( window == NULL ){
  36. fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.n" );
  37. glfwTerminate();
  38. return -1;
  39. }
  40. glfwMakeContextCurrent(window);
  41.  
  42. // Initialize GLEW
  43. glewExperimental = true; // Needed for core profile
  44. if (glewInit() != GLEW_OK) {
  45. fprintf(stderr, "Failed to initialize GLEWn");
  46. return -1;
  47. }
  48.  
  49. // Ensure we can capture the escape key being pressed below
  50. glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  51.  
  52. // Dark blue background
  53. glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  54.  
  55. // Enable depth test
  56. glEnable(GL_DEPTH_TEST);
  57. // Accept fragment if it closer to the camera than the former one
  58. glDepthFunc(GL_LESS);
  59.  
  60. GLuint VertexArrayID;
  61. glGenVertexArrays(1, &VertexArrayID);
  62. glBindVertexArray(VertexArrayID);
  63.  
  64. // Create and compile our GLSL program from the shaders
  65. GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader" );
  66.  
  67. // Get a handle for our "MVP" uniform
  68. GLuint MatrixID = glGetUniformLocation(programID, "MVP");
  69.  
  70. // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  71. glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
  72. // Camera matrix
  73. glm::mat4 View = glm::lookAt(
  74. glm::vec3(4,3,-3), // Camera is at (4,3,-3), in World Space
  75. glm::vec3(0,0,0), // and looks at the origin
  76. glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
  77. );
  78. // Model matrix : an identity matrix (model will be at the origin)
  79. glm::mat4 Model = glm::mat4(1.0f);
  80. // Our ModelViewProjection : multiplication of our 3 matrices
  81. glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
  82.  
  83. // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
  84. // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
  85. static const GLfloat g_vertex_buffer_data[] = {
  86. -1.0f,-1.0f,-1.0f,
  87. -1.0f,-1.0f, 1.0f,
  88. -1.0f, 1.0f, 1.0f,
  89. 1.0f, 1.0f,-1.0f,
  90. -1.0f,-1.0f,-1.0f,
  91. -1.0f, 1.0f,-1.0f,
  92. 1.0f,-1.0f, 1.0f,
  93. -1.0f,-1.0f,-1.0f,
  94. 1.0f,-1.0f,-1.0f,
  95. 1.0f, 1.0f,-1.0f,
  96. 1.0f,-1.0f,-1.0f,
  97. -1.0f,-1.0f,-1.0f,
  98. -1.0f,-1.0f,-1.0f,
  99. -1.0f, 1.0f, 1.0f,
  100. -1.0f, 1.0f,-1.0f,
  101. 1.0f,-1.0f, 1.0f,
  102. -1.0f,-1.0f, 1.0f,
  103. -1.0f,-1.0f,-1.0f,
  104. -1.0f, 1.0f, 1.0f,
  105. -1.0f,-1.0f, 1.0f,
  106. 1.0f,-1.0f, 1.0f,
  107. 1.0f, 1.0f, 1.0f,
  108. 1.0f,-1.0f,-1.0f,
  109. 1.0f, 1.0f,-1.0f,
  110. 1.0f,-1.0f,-1.0f,
  111. 1.0f, 1.0f, 1.0f,
  112. 1.0f,-1.0f, 1.0f,
  113. 1.0f, 1.0f, 1.0f,
  114. 1.0f, 1.0f,-1.0f,
  115. -1.0f, 1.0f,-1.0f,
  116. 1.0f, 1.0f, 1.0f,
  117. -1.0f, 1.0f,-1.0f,
  118. -1.0f, 1.0f, 1.0f,
  119. 1.0f, 1.0f, 1.0f,
  120. -1.0f, 1.0f, 1.0f,
  121. 1.0f,-1.0f, 1.0f
  122. };
  123.  
  124. // One color for each vertex. They were generated randomly.
  125. static const GLfloat g_color_buffer_data[] = {
  126. 0.583f, 0.771f, 0.014f,
  127. 0.609f, 0.115f, 0.436f,
  128. 0.327f, 0.483f, 0.844f,
  129. 0.822f, 0.569f, 0.201f,
  130. 0.435f, 0.602f, 0.223f,
  131. 0.310f, 0.747f, 0.185f,
  132. 0.597f, 0.770f, 0.761f,
  133. 0.559f, 0.436f, 0.730f,
  134. 0.359f, 0.583f, 0.152f,
  135. 0.483f, 0.596f, 0.789f,
  136. 0.559f, 0.861f, 0.639f,
  137. 0.195f, 0.548f, 0.859f,
  138. 0.014f, 0.184f, 0.576f,
  139. 0.771f, 0.328f, 0.970f,
  140. 0.406f, 0.615f, 0.116f,
  141. 0.676f, 0.977f, 0.133f,
  142. 0.971f, 0.572f, 0.833f,
  143. 0.140f, 0.616f, 0.489f,
  144. 0.997f, 0.513f, 0.064f,
  145. 0.945f, 0.719f, 0.592f,
  146. 0.543f, 0.021f, 0.978f,
  147. 0.279f, 0.317f, 0.505f,
  148. 0.167f, 0.620f, 0.077f,
  149. 0.347f, 0.857f, 0.137f,
  150. 0.055f, 0.953f, 0.042f,
  151. 0.714f, 0.505f, 0.345f,
  152. 0.783f, 0.290f, 0.734f,
  153. 0.722f, 0.645f, 0.174f,
  154. 0.302f, 0.455f, 0.848f,
  155. 0.225f, 0.587f, 0.040f,
  156. 0.517f, 0.713f, 0.338f,
  157. 0.053f, 0.959f, 0.120f,
  158. 0.393f, 0.621f, 0.362f,
  159. 0.673f, 0.211f, 0.457f,
  160. 0.820f, 0.883f, 0.371f,
  161. 0.982f, 0.099f, 0.879f
  162. };
  163.  
  164. GLuint vertexbuffer;
  165. glGenBuffers(1, &vertexbuffer);
  166. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  167. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  168.  
  169. GLuint colorbuffer;
  170. glGenBuffers(1, &colorbuffer);
  171. glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  172. glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
  173.  
  174. do{
  175.  
  176. // Clear the screen
  177. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  178.  
  179. // Use our shader
  180. glUseProgram(programID);
  181.  
  182. // Send our transformation to the currently bound shader,
  183. // in the "MVP" uniform
  184. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  185.  
  186. // 1rst attribute buffer : vertices
  187. glEnableVertexAttribArray(0);
  188. glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  189. glVertexAttribPointer(
  190. 0, // attribute. No particular reason for 0, but must match the layout in the shader.
  191. 3, // size
  192. GL_FLOAT, // type
  193. GL_FALSE, // normalized?
  194. 0, // stride
  195. (void*)0 // array buffer offset
  196. );
  197.  
  198. // 2nd attribute buffer : colors
  199. glEnableVertexAttribArray(1);
  200. glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  201. glVertexAttribPointer(
  202. 1, // attribute. No particular reason for 1, but must match the layout in the shader.
  203. 3, // size
  204. GL_FLOAT, // type
  205. GL_FALSE, // normalized?
  206. 0, // stride
  207. (void*)0 // array buffer offset
  208. );
  209.  
  210. // Draw the triangle !
  211. glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
  212.  
  213. glDisableVertexAttribArray(0);
  214. glDisableVertexAttribArray(1);
  215.  
  216. // Swap buffers
  217. glfwSwapBuffers(window);
  218. glfwPollEvents();
  219.  
  220. } // Check if the ESC key was pressed or the window was closed
  221. while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  222. glfwWindowShouldClose(window) == 0 );
  223.  
  224. // Cleanup VBO and shader
  225. glDeleteBuffers(1, &vertexbuffer);
  226. glDeleteBuffers(1, &colorbuffer);
  227. glDeleteProgram(programID);
  228. glDeleteVertexArrays(1, &VertexArrayID);
  229.  
  230. // Close OpenGL window and terminate GLFW
  231. glfwTerminate();
  232.  
  233. return 0;
  234. }
  235.  
  236. #include <stdio.h>
  237. #include <string>
  238. #include <vector>
  239. #include <iostream>
  240. #include <fstream>
  241. #include <algorithm>
  242. using namespace std;
  243.  
  244. #include <stdlib.h>
  245. #include <string.h>
  246.  
  247. #include <GL/glew.h>
  248.  
  249. #include "shader.hpp"
  250.  
  251. GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
  252.  
  253. // Create the shaders
  254. GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  255. GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  256.  
  257. // Read the Vertex Shader code from the file
  258. std::string VertexShaderCode;
  259. std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  260. if(VertexShaderStream.is_open()){
  261. std::string Line = "";
  262. while(getline(VertexShaderStream, Line))
  263. VertexShaderCode += "n" + Line;
  264. VertexShaderStream.close();
  265. }else{
  266. printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !n", vertex_file_path);
  267. getchar();
  268. return 0;
  269. }
  270.  
  271. // Read the Fragment Shader code from the file
  272. std::string FragmentShaderCode;
  273. std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
  274. if(FragmentShaderStream.is_open()){
  275. std::string Line = "";
  276. while(getline(FragmentShaderStream, Line))
  277. FragmentShaderCode += "n" + Line;
  278. FragmentShaderStream.close();
  279. }
  280.  
  281.  
  282.  
  283. GLint Result = GL_FALSE;
  284. int InfoLogLength;
  285.  
  286.  
  287.  
  288. // Compile Vertex Shader
  289. printf("Compiling shader : %sn", vertex_file_path);
  290. char const * VertexSourcePointer = VertexShaderCode.c_str();
  291. glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
  292. glCompileShader(VertexShaderID);
  293.  
  294. // Check Vertex Shader
  295. glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  296. glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  297. if ( InfoLogLength > 0 ){
  298. std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
  299. glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  300. printf("%sn", &VertexShaderErrorMessage[0]);
  301. }
  302.  
  303.  
  304.  
  305. // Compile Fragment Shader
  306. printf("Compiling shader : %sn", fragment_file_path);
  307. char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  308. glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
  309. glCompileShader(FragmentShaderID);
  310.  
  311. // Check Fragment Shader
  312. glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  313. glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  314. if ( InfoLogLength > 0 ){
  315. std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
  316. glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  317. printf("%sn", &FragmentShaderErrorMessage[0]);
  318. }
  319.  
  320.  
  321.  
  322. // Link the program
  323. printf("Linking programn");
  324. GLuint ProgramID = glCreateProgram();
  325. glAttachShader(ProgramID, VertexShaderID);
  326. glAttachShader(ProgramID, FragmentShaderID);
  327. glLinkProgram(ProgramID);
  328.  
  329. // Check the program
  330. glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  331. glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  332. if ( InfoLogLength > 0 ){
  333. std::vector<char> ProgramErrorMessage(InfoLogLength+1);
  334. glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
  335. printf("%sn", &ProgramErrorMessage[0]);
  336. }
  337.  
  338. glDeleteShader(VertexShaderID);
  339. glDeleteShader(FragmentShaderID);
  340.  
  341. return ProgramID;
  342. }
  343.  
  344. using System;
  345. using OpenTK;
  346. using OpenTK.Graphics;
  347. using OpenTK.Input;
  348. using System.Windows.Forms;
  349. using System.IO;
  350.  
  351. #if DEBUG
  352. using OpenTK.Graphics.OpenGL4;
  353. #else
  354. using OpenTK.Graphics.OpenGL;
  355. #endif
  356.  
  357. namespace OpenTK_Test
  358. {
  359. class Game : GameWindow
  360. {
  361. /* Deklaration + Initialisierung */
  362. private Int32 vao; //aka VertexArrayID
  363. private Int32 vbo; //aka vertexbuffer
  364. private Int32 cbo; //aka colorbuffer
  365. private Int32 shaderProgram;//aka programID
  366. private Int32 MatrixID;
  367. private Matrix4 Projection;
  368. private Matrix4 View;
  369. private Matrix4 MVP;
  370.  
  371.  
  372. public Game()
  373. : base(1280, 720, GraphicsMode.Default, "OpenGL Test", GameWindowFlags.Default, DisplayDevice.Default, 4, 4, GraphicsContextFlags.ForwardCompatible)
  374. {
  375. VSync = VSyncMode.On;
  376. }
  377.  
  378. /// <summary>
  379. /// Initialisierung
  380. /// </summary>
  381. /// <param name="e"></param>
  382. protected override void OnLoad(EventArgs e)
  383. {
  384. base.OnLoad(e);
  385. WindowBorder = WindowBorder.Hidden;
  386. //GL.Viewport(0, 0, Width, Height);
  387.  
  388. Console.WriteLine("Spiel gestartetn");
  389. Console.WriteLine("Systeminfos:");
  390. Console.WriteLine("Renderer: " + GL.GetString(StringName.Renderer));
  391. Console.WriteLine("Unterstützte OpenGL Version: " + GL.GetString(StringName.Version));
  392. Console.WriteLine("Unterstützte GLSL Version: " + GL.GetString(StringName.ShadingLanguageVersion) + "n");
  393. Console.WriteLine("Spiel wird geladenn");
  394.  
  395. /* Hintergrundfarbe */
  396. GL.ClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  397.  
  398. /* Fragments weiter hinten werden automatisch zuerst gerendert */
  399. GL.Enable(EnableCap.DepthTest);
  400. GL.DepthFunc(DepthFunction.Less);
  401.  
  402. /* Vertex Array Object */
  403. GL.GenVertexArrays(1, out vao);
  404. GL.BindVertexArray(vao);
  405.  
  406. /* GLSL Shader laden */
  407. shaderProgram = LoadShaders("vs.glsl", "fs.glsl");
  408.  
  409. /* Matrizen */
  410. MatrixID = GL.GetUniformLocation(shaderProgram, "MVP");
  411. Projection = Matrix4.Perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
  412. View = Matrix4.LookAt(new Vector3(4, 3, -3), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
  413. //Matrix4 Model = new Matrix4(1.0f);
  414. MVP = Projection * View;
  415.  
  416. /* Vertices */
  417. var vertices = new Single[]
  418. {
  419. -1.0f,-1.0f,-1.0f,
  420. -1.0f,-1.0f, 1.0f,
  421. -1.0f, 1.0f, 1.0f,
  422. 1.0f, 1.0f,-1.0f,
  423. -1.0f,-1.0f,-1.0f,
  424. -1.0f, 1.0f,-1.0f,
  425. 1.0f,-1.0f, 1.0f,
  426. -1.0f,-1.0f,-1.0f,
  427. 1.0f,-1.0f,-1.0f,
  428. 1.0f, 1.0f,-1.0f,
  429. 1.0f,-1.0f,-1.0f,
  430. -1.0f,-1.0f,-1.0f,
  431. -1.0f,-1.0f,-1.0f,
  432. -1.0f, 1.0f, 1.0f,
  433. -1.0f, 1.0f,-1.0f,
  434. 1.0f,-1.0f, 1.0f,
  435. -1.0f,-1.0f, 1.0f,
  436. -1.0f,-1.0f,-1.0f,
  437. -1.0f, 1.0f, 1.0f,
  438. -1.0f,-1.0f, 1.0f,
  439. 1.0f,-1.0f, 1.0f,
  440. 1.0f, 1.0f, 1.0f,
  441. 1.0f,-1.0f,-1.0f,
  442. 1.0f, 1.0f,-1.0f,
  443. 1.0f,-1.0f,-1.0f,
  444. 1.0f, 1.0f, 1.0f,
  445. 1.0f,-1.0f, 1.0f,
  446. 1.0f, 1.0f, 1.0f,
  447. 1.0f, 1.0f,-1.0f,
  448. -1.0f, 1.0f,-1.0f,
  449. 1.0f, 1.0f, 1.0f,
  450. -1.0f, 1.0f,-1.0f,
  451. -1.0f, 1.0f, 1.0f,
  452. 1.0f, 1.0f, 1.0f,
  453. -1.0f, 1.0f, 1.0f,
  454. 1.0f,-1.0f, 1.0f
  455. };
  456.  
  457. /* Farben */
  458. var color = new Single[]
  459. {
  460. 0.583f, 0.771f, 0.014f,
  461. 0.609f, 0.115f, 0.436f,
  462. 0.327f, 0.483f, 0.844f,
  463. 0.822f, 0.569f, 0.201f,
  464. 0.435f, 0.602f, 0.223f,
  465. 0.310f, 0.747f, 0.185f,
  466. 0.597f, 0.770f, 0.761f,
  467. 0.559f, 0.436f, 0.730f,
  468. 0.359f, 0.583f, 0.152f,
  469. 0.483f, 0.596f, 0.789f,
  470. 0.559f, 0.861f, 0.639f,
  471. 0.195f, 0.548f, 0.859f,
  472. 0.014f, 0.184f, 0.576f,
  473. 0.771f, 0.328f, 0.970f,
  474. 0.406f, 0.615f, 0.116f,
  475. 0.676f, 0.977f, 0.133f,
  476. 0.971f, 0.572f, 0.833f,
  477. 0.140f, 0.616f, 0.489f,
  478. 0.997f, 0.513f, 0.064f,
  479. 0.945f, 0.719f, 0.592f,
  480. 0.543f, 0.021f, 0.978f,
  481. 0.279f, 0.317f, 0.505f,
  482. 0.167f, 0.620f, 0.077f,
  483. 0.347f, 0.857f, 0.137f,
  484. 0.055f, 0.953f, 0.042f,
  485. 0.714f, 0.505f, 0.345f,
  486. 0.783f, 0.290f, 0.734f,
  487. 0.722f, 0.645f, 0.174f,
  488. 0.302f, 0.455f, 0.848f,
  489. 0.225f, 0.587f, 0.040f,
  490. 0.517f, 0.713f, 0.338f,
  491. 0.053f, 0.959f, 0.120f,
  492. 0.393f, 0.621f, 0.362f,
  493. 0.673f, 0.211f, 0.457f,
  494. 0.820f, 0.883f, 0.371f,
  495. 0.982f, 0.099f, 0.879f
  496. };
  497.  
  498. /* Vertex Buffer Object */
  499. GL.GenBuffers(1, out vbo);
  500. GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
  501. GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length), vertices, BufferUsageHint.StaticDraw);
  502.  
  503. /* Color Buffer Object */
  504. GL.GenBuffers(1, out cbo);
  505. GL.BindBuffer(BufferTarget.ArrayBuffer, cbo);
  506. GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length), color, BufferUsageHint.StaticDraw);
  507.  
  508. GL.Viewport(0, 0, Width, Height);
  509. Console.WriteLine("Spiel geladenn");
  510. }
  511.  
  512. protected override void OnResize(EventArgs e)
  513. {
  514. base.OnResize(e);
  515. }
  516.  
  517. protected override void OnUpdateFrame(FrameEventArgs e)
  518. {
  519. base.OnUpdateFrame(e);
  520.  
  521. if (Keyboard[Key.Escape])
  522. {
  523. Exit();
  524. }
  525. }
  526.  
  527. protected override void OnRenderFrame(FrameEventArgs e)
  528. {
  529. base.OnRenderFrame(e);
  530.  
  531. /* Bildschirm loeschen */
  532. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  533.  
  534. /* Shader benutzen */
  535. GL.UseProgram(shaderProgram);
  536. GL.UniformMatrix4(MatrixID, false, ref MVP);
  537.  
  538. /* 1. Attribut */
  539. GL.EnableVertexAttribArray(0);
  540. GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
  541. GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);
  542.  
  543. /* 2. Attribut */
  544. GL.EnableVertexAttribArray(1);
  545. GL.BindBuffer(BufferTarget.ArrayBuffer, cbo);
  546. GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);
  547.  
  548. /* Zeichnen */
  549. GL.DrawArrays(PrimitiveType.Triangles, 0, 12 * 3);
  550. GL.DisableVertexAttribArray(0);
  551. GL.DisableVertexAttribArray(1);
  552.  
  553. /* Szene anzeigen */
  554. GL.Flush();
  555. SwapBuffers();
  556. }
  557.  
  558. protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  559. {
  560. Console.WriteLine("Spiel wird beendet");
  561. GL.DeleteProgram(shaderProgram);
  562. GL.DeleteBuffers(1, ref cbo);
  563. GL.DeleteBuffers(1, ref vbo);
  564. GL.DeleteVertexArrays(1, ref vao);
  565. }
  566.  
  567. /// <summary>
  568. /// Shaderdatei laden
  569. /// </summary>
  570. /// <param name="vertexFilePath"></param>
  571. /// <param name="fragmentFilePath"></param>
  572. /// <returns></returns>
  573. private Int32 LoadShaders(string vertexFilePath, string fragmentFilePath)
  574. {
  575. Int32 vertexShader;
  576. Int32 fragmentShader;
  577. Int32 shaderProgram;
  578.  
  579. /* Vertex Shader */
  580. vertexShader = GL.CreateShader(ShaderType.VertexShader);
  581. GL.ShaderSource(vertexShader, File.ReadAllText(vertexFilePath));
  582. Console.WriteLine("Vertex Shader wird kompiliert");
  583. GL.CompileShader(vertexShader);
  584. if (GL.GetShaderInfoLog(vertexShader) == "")
  585. {
  586. Console.WriteLine("Vertex Shader erfolgreich kompiliertn");
  587. }
  588. else
  589. {
  590. Console.WriteLine("!!! Vertex Shader fehlerhaft: !!!");
  591. Console.Write(GL.GetShaderInfoLog(vertexShader));
  592. }
  593.  
  594. /* Fragment Shader */
  595. fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
  596. GL.ShaderSource(fragmentShader, File.ReadAllText(fragmentFilePath));
  597. Console.WriteLine("Fragment Shader wird kompiliert");
  598. GL.CompileShader(fragmentShader);
  599. if (GL.GetShaderInfoLog(fragmentShader) == "")
  600. {
  601. Console.WriteLine("Fragment Shader erfolgreich kompiliertn");
  602. }
  603. else
  604. {
  605. Console.WriteLine("!!! Fragment Shader fehlerhaft: !!!");
  606. Console.WriteLine(GL.GetShaderInfoLog(fragmentShader));
  607. }
  608.  
  609. /* Shader Progrm */
  610. shaderProgram = GL.CreateProgram();
  611. GL.AttachShader(shaderProgram, vertexShader);
  612. GL.AttachShader(shaderProgram, fragmentShader);
  613. GL.BindFragDataLocation(shaderProgram, 0, "outColor");
  614. Console.WriteLine("Shader werden mit dem Programmobjekt gelinkt");
  615. GL.LinkProgram(shaderProgram);
  616. Console.WriteLine("Shader mit dem Programmobjekt gelinkt:");
  617. Console.WriteLine(GL.GetShaderInfoLog(shaderProgram));
  618.  
  619. /* Shader loeschen */
  620. GL.DeleteShader(fragmentShader);
  621. GL.DeleteShader(vertexShader);
  622.  
  623. return shaderProgram;
  624. }
  625.  
  626. [STAThread]
  627. static void Main()
  628. {
  629. Console.Title = "OpenTK Test - log";
  630. Console.ForegroundColor = ConsoleColor.Cyan;
  631. Console.WriteLine("Spiel wird gestartet");
  632. using (Game game = new Game())
  633. {
  634. game.Run(30.0);
  635. }
  636. Console.WriteLine("Spiel beendetn");
  637. Console.ReadLine();
  638. }
  639. }
  640. }
  641.  
  642. #version 330 core
  643.  
  644. // Input vertex data, different for all executions of this shader.
  645. layout(location = 0) in vec3 vertexPosition_modelspace;
  646. layout(location = 1) in vec3 vertexColor;
  647.  
  648. // Output data ; will be interpolated for each fragment.
  649. out vec3 fragmentColor;
  650. // Values that stay constant for the whole mesh.
  651. uniform mat4 MVP;
  652.  
  653. void main()
  654. {
  655. // Output position of the vertex, in clip space : MVP * position
  656. gl_Position = MVP * vec4(vertexPosition_modelspace,1);
  657.  
  658. // The color of each vertex will be interpolated
  659. // to produce the color of each fragment
  660. fragmentColor = vertexColor;
  661. }
  662.  
  663. #version 330 core
  664.  
  665. // Interpolated values from the vertex shaders
  666. in vec3 fragmentColor;
  667.  
  668. // Ouput data
  669. out vec3 color;
  670.  
  671. void main()
  672. {
  673. // Output color = color specified in the vertex shader,
  674. // interpolated between all 3 surrounding vertices
  675. color = fragmentColor;
  676. }
  677.  
  678. var posAttrib = GL.GetAttribLocation(shaderProgram, "position");
  679. GL.EnableVertexAttribArray(posAttrib);
  680. GL.VertexAttribPointer(posAttrib, 2, VertexAttribPointerType.Float, false, 5 * sizeof(Single), IntPtr.Zero);
  681.  
  682. var colAttrib = GL.GetAttribLocation(shaderProgram, "color");
  683. GL.EnableVertexAttribArray(colAttrib);
  684. GL.VertexAttribPointer(colAttrib, 3, VertexAttribPointerType.Float, false, 5 * sizeof(Single), new IntPtr(2 * sizeof(Single)));
  685.  
  686. /* 1. Attribut */
  687. GL.EnableVertexAttribArray(0);
  688. GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
  689. GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);
  690.  
  691. /* 2. Attribut */
  692. GL.EnableVertexAttribArray(1);
  693. GL.BindBuffer(BufferTarget.ArrayBuffer, cbo);
  694. GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement