fork download
  1.  
  2. // Include standard headers
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <vector>
  6.  
  7. // Include GLEW
  8. #include <GL/glew.h>
  9.  
  10. // Include GLFW
  11. #include <GLFW/glfw3.h>
  12. GLFWwindow* window;
  13.  
  14. // Include GLM
  15. #include <glm/glm.hpp>
  16. #include <glm/gtc/matrix_transform.hpp>
  17. using namespace glm;
  18.  
  19. #include <common/shader.cpp>
  20. #include <common/texture.cpp>
  21. #include <common/controls.cpp>
  22. #include <common/objloader.cpp>
  23. //#include <common/vboindexer.cpp>
  24. #include <QDebug>
  25.  
  26. void initGL();
  27.  
  28. struct BufferData{
  29. GLuint vertexbuffer;
  30. GLuint uvbuffer;
  31. GLuint normalbuffer;
  32. };
  33.  
  34. struct dataAboutOneModel{
  35. std::vector<glm::vec3> vertices;
  36. std::vector<glm::vec2> uvs;
  37. std::vector<glm::vec3> normals;
  38. GLuint Texture;
  39. GLuint TextureID;
  40. const char *texturePath;
  41. const char *modelPath;
  42. BufferData BufferModel;
  43. std::vector<GLuint> TexturesAvailable;
  44. std::vector<GLuint> TexturesIDAvailable;
  45. std::vector<std::string> PathToTexturesAvailable;
  46. int CurrectTextureNum;
  47. };
  48.  
  49. std::vector<std::string> ModelFilenames;
  50. std::vector<std::string> TexutureFilenames;
  51.  
  52. dataAboutOneModel modelTmp;
  53. BufferData bufferTmp;
  54.  
  55. GLuint programID;
  56.  
  57. std::vector<dataAboutOneModel> dataAboutModels;
  58. std::vector<BufferData> BuffersData;
  59.  
  60. void ActivateBuffer(int numModel);
  61. void initModel(dataAboutOneModel &model);
  62. void loopGLinit(GLuint & programID,GLuint & MatrixID,GLuint & ModelMatrixID, GLuint & ViewMatrixID, GLuint & LightID);
  63. void VertexBind();
  64. void InitArrayPathModelTexture(std::vector<std::string> &ModelFilenamesInit,std::vector<std::string> &TexutureFilenamesInit);
  65. GLuint VertexArrayID;
  66. GLuint MatrixID;
  67. GLuint ViewMatrixID;
  68. GLuint ModelMatrixID;
  69. int main( void )
  70. {
  71. initGL();
  72. InitArrayPathModelTexture(ModelFilenames,TexutureFilenames);
  73.  
  74. for(int i = 0; i < ModelFilenames.size();i++){
  75. modelTmp.texturePath = TexutureFilenames[i].c_str();
  76. modelTmp.modelPath = ModelFilenames[i].c_str();
  77. initModel(modelTmp);
  78. }
  79.  
  80.  
  81. dataAboutModels[0].TexturesAvailable.push_back(dataAboutModels[0].Texture);
  82. dataAboutModels[0].TexturesIDAvailable.push_back(dataAboutModels[0].TextureID);
  83.  
  84. dataAboutModels[0].TextureID = glGetUniformLocation(programID, "myTextureSampler");
  85. dataAboutModels[0].Texture = loadDDS(TexutureFilenames[1].c_str());
  86.  
  87. dataAboutModels[0].TexturesAvailable.push_back(dataAboutModels[0].Texture);
  88. dataAboutModels[0].TexturesIDAvailable.push_back(dataAboutModels[0].TextureID);
  89.  
  90.  
  91.  
  92. for(int i = 0 ; i <dataAboutModels.size();i++ )
  93. ActivateBuffer(i);
  94.  
  95. GLuint LightID = glGetUniformLocation(programID, "LightPosition_worldspace");
  96. glUseProgram(programID);
  97. do{
  98. loopGLinit(programID,MatrixID,ModelMatrixID,ViewMatrixID,LightID);
  99. VertexBind();
  100. glfwSwapBuffers(window);
  101. glfwPollEvents();
  102. } // Check if the ESC key was pressed or the window was closed
  103. while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  104. glfwWindowShouldClose(window) == 0 );
  105.  
  106. // Cleanup VBO and shader
  107. for(int i = 0 ; i < BuffersData.size(); i++){
  108. glDeleteBuffers(1, &BuffersData[i].vertexbuffer);
  109. glDeleteBuffers(1, &BuffersData[i].uvbuffer);
  110. glDeleteBuffers(1, &BuffersData[i].normalbuffer);
  111. }
  112. glDeleteProgram(programID);
  113.  
  114. for(int i = 0 ; i <dataAboutModels.size();i++ )
  115. glDeleteTextures(1, &dataAboutModels[i].Texture);
  116. glDeleteVertexArrays(1, &VertexArrayID);
  117.  
  118. // Close OpenGL window and terminate GLFW
  119. glfwTerminate();
  120.  
  121. return 0;
  122. }
  123.  
  124.  
  125. void AddAvailableTextures(){
  126.  
  127. }
  128.  
  129.  
  130. void InitArrayPathModelTexture(std::vector<std::string> &ModelFilenamesInit, std::vector<string> &TexutureFilenamesInit){
  131.  
  132. // TexutureFilenamesInit.push_back("/home/user/DG_Pult_02/Textures/null.dds");
  133. // ModelFilenamesInit.push_back("/home/user/openGL/Generator_Tris01.obj");
  134.  
  135. /* XML rewrite */
  136.  
  137. TexutureFilenamesInit.push_back("/home/user/DG_Pult_02/Textures/DG_Pult_Menu_A.dds");
  138. ModelFilenamesInit.push_back("/home/user/DG_Pult_02/Mesh/DG_Pult_Menu.obj");
  139.  
  140. TexutureFilenamesInit.push_back("/home/user/DG_Pult_02/Textures/null.dds");
  141. ModelFilenamesInit.push_back("/home/user/DG_Pult_02/Mesh/DG_Pult_Body.obj");
  142.  
  143. TexutureFilenamesInit.push_back("/home/user/DG_Pult_02/Textures/null_button.dds");
  144. ModelFilenamesInit.push_back("/home/user/DG_Pult_02/Mesh/DG_Pult_Button_1.obj");
  145.  
  146. TexutureFilenamesInit.push_back("/home/user/DG_Pult_02/Textures/null_button.dds");
  147. ModelFilenamesInit.push_back("/home/user/DG_Pult_02/Mesh/DG_Pult_Button_2.obj");
  148.  
  149. TexutureFilenamesInit.push_back("/home/user/DG_Pult_02/Textures/null_button.dds");
  150. ModelFilenamesInit.push_back("/home/user/DG_Pult_02/Mesh/DG_Pult_Button_3.obj");
  151. /*
  152. Подгрузка текстур и моделей - код различается только путями файлов.
  153. */
  154. }
  155.  
  156. void initModel(dataAboutOneModel &model){
  157. model.TextureID = glGetUniformLocation(programID, "myTextureSampler");
  158. model.Texture = loadDDS(model.texturePath);
  159. bool res = loadOBJ(model.modelPath, model.vertices, model.uvs, model.normals);
  160. if(res==true)
  161. dataAboutModels.push_back(model);
  162. else
  163. qDebug() << "Incorrect path *.obj file.";
  164.  
  165.  
  166. glGenVertexArrays(1, &VertexArrayID);
  167. glBindVertexArray(VertexArrayID);
  168.  
  169. programID = LoadShaders( "/home/user/openGL/StandardShading.vertexshader", "/home/user/openGL/StandardShading.fragmentshader" );
  170. MatrixID = glGetUniformLocation(programID, "MVP");
  171. ViewMatrixID = glGetUniformLocation(programID, "V");
  172. ModelMatrixID = glGetUniformLocation(programID, "M");
  173. }
  174.  
  175.  
  176.  
  177. void VertexBind(){
  178. for(int i = 0; i < BuffersData.size();i++){
  179. glActiveTexture(GL_TEXTURE0);
  180. glBindTexture(GL_TEXTURE_2D, dataAboutModels[i].Texture);
  181. glUniform1i(dataAboutModels[i].TextureID, 0);
  182.  
  183. glEnableVertexAttribArray(0);
  184. glBindBuffer(GL_ARRAY_BUFFER, BuffersData[i].vertexbuffer);
  185. glVertexAttribPointer(
  186. 0, // attribute
  187. 3, // size
  188. GL_FLOAT, // type
  189. GL_FALSE, // normalized?
  190. 0, // stride
  191. (void*)0 // array buffer offset
  192. );
  193.  
  194. // 2nd attribute buffer : UVs
  195. glEnableVertexAttribArray(1);
  196. glBindBuffer(GL_ARRAY_BUFFER, BuffersData[i].uvbuffer);
  197. glVertexAttribPointer(
  198. 1, // attribute
  199. 2, // size
  200. GL_FLOAT, // type
  201. GL_FALSE, // normalized?
  202. 0, // stride
  203. (void*)0 // array buffer offset
  204. );
  205.  
  206. // 3rd attribute buffer : normals
  207. glEnableVertexAttribArray(2);
  208. glBindBuffer(GL_ARRAY_BUFFER, BuffersData[i].normalbuffer);
  209. glVertexAttribPointer(
  210. 2, // attribute
  211. 3, // size
  212. GL_FLOAT, // type
  213. GL_FALSE, // normalized?
  214. 0, // stride
  215. (void*)0 // array buffer offset
  216. );
  217.  
  218.  
  219. // Draw the triangles !
  220. glDrawArrays(GL_TRIANGLES, 0, dataAboutModels[i].vertices.size() );
  221.  
  222. glDisableVertexAttribArray(0);
  223. glDisableVertexAttribArray(1);
  224. glDisableVertexAttribArray(2);
  225.  
  226.  
  227.  
  228. }
  229. }
  230.  
  231. void loopGLinit(GLuint & programID,GLuint & MatrixID,GLuint & ModelMatrixID, GLuint & ViewMatrixID, GLuint & LightID){
  232.  
  233. // Clear the screen
  234. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  235.  
  236. // Use our shader
  237. glUseProgram(programID);
  238.  
  239. // Compute the MVP matrix from keyboard and mouse input
  240. computeMatricesFromInputs();
  241. glm::mat4 ProjectionMatrix = getProjectionMatrix();
  242. glm::mat4 ViewMatrix = getViewMatrix();
  243. glm::mat4 ModelMatrix = glm::mat4(1.0);
  244. glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
  245.  
  246. // Send our transformation to the currently bound shader,
  247. // in the "MVP" uniform
  248. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  249. glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
  250. glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);
  251.  
  252. glm::vec3 lightPos = glm::vec3(4,4,4);
  253. glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z);
  254.  
  255.  
  256. }
  257.  
  258. void ActivateBuffer(int numModel){
  259.  
  260. dataAboutOneModel modelTmpAB = dataAboutModels[numModel];
  261. glGenBuffers(1, &bufferTmp.vertexbuffer);
  262. glBindBuffer(GL_ARRAY_BUFFER, bufferTmp.vertexbuffer);
  263. glBufferData(GL_ARRAY_BUFFER, modelTmpAB.vertices.size() * sizeof(glm::vec3), &modelTmpAB.vertices[0], GL_STATIC_DRAW);
  264.  
  265. //bufferTmp.uvbuffer;
  266. glGenBuffers(1, &bufferTmp.uvbuffer);
  267. glBindBuffer(GL_ARRAY_BUFFER, bufferTmp.uvbuffer);
  268. glBufferData(GL_ARRAY_BUFFER, modelTmpAB.uvs.size() * sizeof(glm::vec2), &modelTmpAB.uvs[0], GL_STATIC_DRAW);
  269.  
  270. //bufferTmp.normalbuffer;
  271. glGenBuffers(1, &bufferTmp.normalbuffer);
  272. glBindBuffer(GL_ARRAY_BUFFER, bufferTmp.normalbuffer);
  273. glBufferData(GL_ARRAY_BUFFER, modelTmpAB.normals.size() * sizeof(glm::vec3), &modelTmpAB.normals[0], GL_STATIC_DRAW);
  274.  
  275. BuffersData.push_back(bufferTmp);
  276. }
  277.  
  278. void initGL(){
  279. if( !glfwInit() )
  280. {
  281. fprintf( stderr, "Failed to initialize GLFW\n" );
  282. getchar();
  283. return ;
  284. }
  285.  
  286. glfwWindowHint(GLFW_SAMPLES, 32);
  287. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  288. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  289. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
  290. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  291.  
  292. // Open a window and create its OpenGL context
  293. window = glfwCreateWindow( 1024, 768, "Пульт управления", NULL, NULL);
  294. if( window == NULL ){
  295. 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" );
  296. getchar();
  297. glfwTerminate();
  298. return ;
  299. }
  300. glfwMakeContextCurrent(window);
  301.  
  302. // Initialize GLEW
  303. glewExperimental = true; // Needed for core profile
  304. if (glewInit() != GLEW_OK) {
  305. fprintf(stderr, "Failed to initialize GLEW\n");
  306. getchar();
  307. glfwTerminate();
  308. return ;
  309. }
  310.  
  311. // Ensure we can capture the escape key being pressed below
  312. glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  313. // Hide the mouse and enable unlimited mouvement
  314. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  315.  
  316. // Set the mouse at the center of the screen
  317. glfwPollEvents();
  318. // glfwSetCursorPos(window, 1024/2, 768/2);
  319.  
  320. // Dark blue background
  321. glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  322.  
  323. // Enable depth test
  324. glEnable(GL_DEPTH_TEST);
  325. // Accept fragment if it closer to the camera than the former one
  326. glDepthFunc(GL_LESS);
  327.  
  328. // Cull triangles which normal is not towards the camera
  329. glEnable(GL_CULL_FACE);
  330. }
  331.  
  332.  
  333. void nextTexture(){
  334. qDebug() << "NextTexture";
  335.  
  336.  
  337.  
  338. if(dataAboutModels[0].TexturesAvailable.size() > 1){
  339. if( dataAboutModels[0].CurrectTextureNum < dataAboutModels[0].TexturesAvailable.size()){
  340. dataAboutModels[0].Texture = dataAboutModels[0].TexturesAvailable[dataAboutModels[0].CurrectTextureNum];
  341. dataAboutModels[0].TextureID = dataAboutModels[0].TexturesAvailable[dataAboutModels[0].CurrectTextureNum];
  342. dataAboutModels[0].CurrectTextureNum++;
  343. }else{
  344. dataAboutModels[0].CurrectTextureNum =0;
  345. nextTexture();
  346. }
  347. }
  348. }
  349. </code>
  350.  
  351.  
  352. Фрагментный шейдер:
  353. <code>
  354. #version 330 core
  355.  
  356. // Interpolated values from the vertex shaders
  357. in vec2 UV;
  358. in vec3 Position_worldspace;
  359. in vec3 Normal_cameraspace;
  360. in vec3 EyeDirection_cameraspace;
  361. in vec3 LightDirection_cameraspace;
  362.  
  363. // Ouput data
  364. out vec3 color;
  365.  
  366. // Values that stay constant for the whole mesh.
  367. uniform sampler2D myTextureSampler;
  368. uniform mat4 MV;
  369. uniform vec3 LightPosition_worldspace;
  370.  
  371. void main(){
  372.  
  373. // Light emission properties
  374. // You probably want to put them as uniforms
  375. vec3 LightColor = vec3(1,1,1);
  376. float LightPower = 50.0f;
  377.  
  378. // Material properties
  379. vec3 MaterialDiffuseColor = texture( myTextureSampler, UV ).rgb;
  380. vec3 MaterialAmbientColor = vec3(0.1,0.1,0.1) * MaterialDiffuseColor;
  381. vec3 MaterialSpecularColor = vec3(0.3,0.3,0.3);
  382.  
  383. // Distance to the light
  384. float distance = length( LightPosition_worldspace - Position_worldspace );
  385.  
  386. // Normal of the computed fragment, in camera space
  387. vec3 n = normalize( Normal_cameraspace );
  388. // Direction of the light (from the fragment to the light)
  389. vec3 l = normalize( LightDirection_cameraspace );
  390. // Cosine of the angle between the normal and the light direction,
  391. // clamped above 0
  392. // - light is at the vertical of the triangle -> 1
  393. // - light is perpendicular to the triangle -> 0
  394. // - light is behind the triangle -> 0
  395. float cosTheta = clamp( dot( n,l ), 0 ,1 );
  396.  
  397. // Eye vector (towards the camera)
  398. vec3 E = normalize(EyeDirection_cameraspace);
  399. // Direction in which the triangle reflects the light
  400. vec3 R = reflect(-l,n);
  401. // Cosine of the angle between the Eye vector and the Reflect vector,
  402. // clamped to 0
  403. // - Looking into the reflection -> 1
  404. // - Looking elsewhere -> < 1
  405. float cosAlpha = clamp( dot( E,R ), 0,1 );
  406.  
  407. color =
  408. // Ambient : simulates indirect lighting
  409. MaterialAmbientColor +
  410. // Diffuse : "color" of the object
  411. MaterialDiffuseColor * LightColor * LightPower * cosTheta / (distance*distance) +
  412. // Specular : reflective highlight, like a mirror
  413. MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha,5) / (distance*distance);
  414.  
  415. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:8:21: fatal error: GL/glew.h: No such file or directory
 #include <GL/glew.h>
                     ^
compilation terminated.
stdout
Standard output is empty