Advertisement
aeroson

zpg diffuse main

Oct 15th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. /* Template ZPG */
  2. #include "zpg.h"
  3.  
  4. glm::mat4 projectionMatrix; // Store the projection matrix
  5. glm::mat4 viewMatrix; // Store the view matrix
  6. glm::mat4 modelMatrix; // Store the model matrix
  7.  
  8. int CurrentWidth = 800,
  9. CurrentHeight = 600,
  10. WindowHandle = 0;
  11.  
  12. int buttonDown =-1;
  13.  
  14. GLuint programID,
  15. vertexID,
  16. fragmentID;
  17.  
  18. GLuint VAO,
  19. VBO,
  20. IBO;
  21.  
  22. GLuint
  23. ProjectionMatrixUniformLocation,
  24. ViewMatrixUniformLocation,
  25. ModelMatrixUniformLocation,
  26. LightColor,
  27. LightLocation;
  28.  
  29.  
  30. float myView=-2;
  31.  
  32. vec3 lightPos=vec3(2,0,0);
  33. float rotationx = 45;
  34. void DrawCube(void)
  35. {
  36. lightPos=lightPos*glm::angleAxis(1.0f,vec3(0,1,0));
  37. rotationx+=1;
  38. modelMatrix = glm::rotate(glm::mat4(1.0f),rotationx,glm::vec3(0.0f, 1.0f, 0.0f));
  39. glUseProgram(programID);
  40. glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, &viewMatrix[0][0]); // Send our view matrix to the shader
  41. glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, &modelMatrix[0][0]); // Send our model matrix to the shader
  42. /*glBindVertexArray(VAO);
  43. glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid*)0);*/
  44.  
  45. glUniform4f(LightColor,1.0,0.5,0.5,1.0);
  46.  
  47. glUniform3f(LightLocation,lightPos.x,lightPos.y,lightPos.z);
  48.  
  49. glBindVertexArray(VAO);
  50.  
  51. glDrawArrays(GL_TRIANGLES,0,36); //nepoužíváme IBO
  52.  
  53.  
  54. glUseProgram(0);
  55.  
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62. void RenderFunction(void)
  63. {
  64. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  65. DrawCube();
  66. glutSwapBuffers();
  67. glutPostRedisplay();
  68. }
  69.  
  70. void ResizeFunction(int Width, int Height)
  71. {
  72. CurrentWidth = Width;
  73. CurrentHeight = Height;
  74. glViewport(0, 0, CurrentWidth, CurrentHeight);
  75. projectionMatrix = glm::perspective(60.0f, (float)CurrentWidth / CurrentHeight, 0.1f, 100.0f);
  76. glUseProgram(programID);
  77. glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, &projectionMatrix[0][0]);
  78. glUseProgram(0);
  79. }
  80.  
  81. void KeyboardFunction(unsigned char Key, int X, int Y)
  82. {
  83. switch (Key)
  84. {
  85. case 'M':
  86. case 'm':
  87. {
  88. printf("Key M\n");
  89. int i,j;
  90. for (j=0; j<4; j++){
  91. for (i=0; i<4; i++){
  92. printf("%f ",viewMatrix[i][j]);
  93. }
  94. printf("\n");
  95. }
  96. break;
  97. }
  98. case 'V':
  99. case 'v':
  100. {
  101. printf("Key V\n");
  102.  
  103. viewMatrix[0][0]=1.0; viewMatrix[1][0]=0.0; viewMatrix[2][0]=0.0; viewMatrix[3][0]=0.0;
  104. viewMatrix[0][1]=0.0; viewMatrix[1][1]=1.0; viewMatrix[2][1]=0.0; viewMatrix[3][1]=0.0;
  105. viewMatrix[0][2]=0.0; viewMatrix[1][2]=0.0; viewMatrix[2][2]=1.0; viewMatrix[3][2]=-10.0;
  106. viewMatrix[0][3]=0.0; viewMatrix[1][3]=0.0; viewMatrix[2][3]=0.0; viewMatrix[3][3]=1.0;
  107. break;
  108. }
  109.  
  110. case 27: // QUIT
  111. glutLeaveMainLoop();
  112. default:
  113. break;
  114.  
  115. }
  116. }
  117.  
  118. void processMouseActiveMotion(int x, int y)
  119. {
  120. printf("Active Mouse (%d) motion a x=%d e y=%d\n",buttonDown,x,y);
  121. }
  122.  
  123. void processMousePassiveMotion(int x, int y)
  124. {
  125.  
  126. }
  127.  
  128. void processMouse(int button, int state, int x, int y)
  129. {
  130. printf("ProcessMouse (%d), state %d a x=%d e y=%d\n",button,state,x,y);
  131. if ((state == GLUT_DOWN) && (button == GLUT_LEFT_BUTTON))
  132. {
  133. buttonDown=button;
  134. }else buttonDown=-1;
  135. }
  136.  
  137. void myMouseWheel(int wheel, int direction, int x, int y)
  138. {
  139. myView+=0.1f*(float)direction;
  140. viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, myView)); // Create our view matrix which will translate us back 5 units
  141. }
  142.  
  143. void IdleFunction(void)
  144. {
  145. glutPostRedisplay();
  146. }
  147.  
  148. void CreateShaders()
  149. {
  150.  
  151. vertexID = glCreateShader(GL_VERTEX_SHADER);
  152. glShaderSource(vertexID, 1, &VertexShader, NULL);
  153. glCompileShader(vertexID);
  154.  
  155. GLint status = GL_FALSE;
  156. //nactenivysledku kompilace
  157. glGetShaderiv(vertexID,GL_COMPILE_STATUS,&status);
  158. //test na chybu
  159. if(!status){
  160. fprintf(stderr,"Vertex shader: \n");
  161. GLchar ErrorLog[1024] = { 0 };
  162. //nacteni a vypis chyby
  163. glGetShaderInfoLog(vertexID,sizeof(ErrorLog),NULL,ErrorLog);
  164. fprintf(stderr,ErrorLog);
  165. //exit(EXIT_FAILURE);
  166. }
  167.  
  168. fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
  169. glShaderSource(fragmentID, 1, &FragmentShader, NULL);
  170. glCompileShader(fragmentID);
  171. programID = glCreateProgram();
  172. glAttachShader(programID, vertexID);
  173. glAttachShader(programID, fragmentID);
  174. glLinkProgram(programID);
  175.  
  176. status = GL_FALSE;
  177.  
  178. //nactenivysledkuLinkovani
  179.  
  180. glGetProgramiv(programID, GL_LINK_STATUS,&status);
  181.  
  182. //test na chybu
  183.  
  184. if(!status){
  185.  
  186. fprintf(stderr,"Vertex shadeeeer: \n");
  187. GLchar ErrorLog[1024] = { 0 };
  188. //nacteni a vypis chyby
  189. glGetShaderInfoLog(fragmentID,sizeof(ErrorLog),NULL,ErrorLog);
  190. fprintf(stderr,ErrorLog);
  191. //exit(EXIT_FAILURE);
  192.  
  193. //exit(EXIT_FAILURE);
  194.  
  195. }
  196.  
  197. LightColor=glGetUniformLocation(programID, "color");
  198. LightLocation=glGetUniformLocation(programID, "lightPos");
  199. ModelMatrixUniformLocation = glGetUniformLocation(programID, "ModelMatrix");
  200. ViewMatrixUniformLocation = glGetUniformLocation(programID, "ViewMatrix");
  201. ProjectionMatrixUniformLocation = glGetUniformLocation(programID, "ProjectionMatrix");
  202. }
  203.  
  204. void CreateCube()
  205. {
  206. glGenVertexArrays(1, &VAO); // generate the VAO
  207.  
  208. glBindVertexArray(VAO); //bind the VAO
  209.  
  210. glEnableVertexAttribArray(0); //enable vertex attributes
  211.  
  212. glEnableVertexAttribArray(1);
  213.  
  214. glGenBuffers(1, &VBO); // generate the VBO
  215.  
  216. glBindBuffer(GL_ARRAY_BUFFER, VBO); //generate the buffer objects
  217.  
  218. glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW); //bind the VBO to the VAO
  219.  
  220. //set VAO attributes
  221.  
  222. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)0);
  223.  
  224. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)sizeof(VERTICES[0].Position));
  225.  
  226. glBindVertexArray(0);
  227. }
  228.  
  229.  
  230.  
  231. void DestroyCube()
  232. {
  233. glDetachShader(programID, vertexID);
  234. glDetachShader(programID, fragmentID);
  235. glDeleteShader(vertexID);
  236. glDeleteShader(fragmentID);
  237. glDeleteProgram(programID);
  238. glDeleteBuffers(2, &VBO);
  239. glDeleteVertexArrays(1, &VAO);
  240. }
  241.  
  242.  
  243. void Initialize(int argc, char* argv[])
  244. {
  245.  
  246. glutInit(&argc, argv);
  247. glutInitContextVersion(3, 3); //Opengl version 3.3
  248. glutInitContextProfile(GLUT_CORE_PROFILE);
  249. glutInitContextFlags(GLUT_DEBUG);
  250. glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS); //glutLeaveMainLoop
  251. glutInitWindowSize(CurrentWidth, CurrentHeight);
  252. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL|GLUT_MULTISAMPLE);
  253.  
  254. WindowHandle = glutCreateWindow("Template ZPG");
  255. if(WindowHandle < 1) {
  256. printf("ERROR: Could not create a new rendering window.\n");
  257. exit(EXIT_FAILURE);
  258. }
  259. glewExperimental = GL_TRUE;
  260. GLenum GlewInitResult = glewInit();
  261.  
  262. if (GLEW_OK != GlewInitResult) {
  263. printf("Error: %s\n",glewGetErrorString(GlewInitResult));
  264. exit(EXIT_FAILURE);
  265. }
  266.  
  267. printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
  268. printf("Using GLEW %s\n",glewGetString(GLEW_VERSION));
  269. printf("Vendor %s\n",glGetString(GL_VENDOR));
  270. printf("Renderer %s\n",glGetString(GL_RENDERER));
  271. printf("GLSL %s\n",glGetString(GL_SHADING_LANGUAGE_VERSION));
  272.  
  273. //Callback Functions
  274. glutReshapeFunc(ResizeFunction);
  275. glutDisplayFunc(RenderFunction);
  276. glutIdleFunc(IdleFunction);
  277. glutCloseFunc(DestroyCube);
  278. glutKeyboardFunc(KeyboardFunction);
  279. glutMouseFunc(processMouse);
  280. glutMotionFunc(processMouseActiveMotion);
  281. glutPassiveMotionFunc(processMousePassiveMotion);
  282. glutMouseWheelFunc(myMouseWheel);
  283.  
  284. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  285. glEnable(GL_DEPTH_TEST);
  286. glDepthFunc(GL_LESS);
  287. glEnable(GL_CULL_FACE);
  288. glCullFace(GL_BACK);
  289. glFrontFace(GL_CCW);
  290. viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, myView)); // Create our view matrix which will translate us back 5 units
  291. CreateShaders();
  292. CreateCube();
  293. }
  294.  
  295.  
  296. int main(int argc, char* argv[])
  297. {
  298. Initialize(argc, argv);
  299. glutMainLoop();
  300. exit(EXIT_SUCCESS);
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement