You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.1 KiB

5 years ago
  1. #include "scene.h"
  2. #include "ggl.h"
  3. #include "utils.h"
  4. #include "ground.h"
  5. GLuint vbo, ebo;
  6. GLuint program;
  7. GLint positionLocation, modelMatrixLocation, viewMatrixLocation, projectionMatrixLocation, colorLocation;//���㡢ģ�;�������ͼ������ͶӰ������λ��
  8. GLint texcoordLocation, textureLocation;//������������λ�á���ͼ����λ��
  9. GLuint texture;
  10. glm::mat4 modelMatrix, viewMatrix, projectionMatrix;
  11. Ground ground;
  12. void Init() {
  13. /*1.��ʼ��vbo��Ϊvbo��ֵ*/
  14. float data[] = {
  15. -0.2f,-0.2f,0.0f,1.0f, 1.0f,0.0f,0.0f,1.0f, 0.0f,0.0f,
  16. 0.2f,-0.2f,0.0f,1.0f, 0.0f,1.0f,0.0f,1.0f, 1.0f,0.0f,
  17. 0.0f,0.2f,0.0f,1.0f, 0.0f,0.0f,1.0f,1.0f, 0.5f,1.0f
  18. };
  19. glGenBuffers(1, &vbo);//����vbo
  20. glBindBuffer(GL_ARRAY_BUFFER, vbo);//��������Ҫ������vbo
  21. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 30, data, GL_STATIC_DRAW);//����cpu���ݵ�GPU
  22. glBindBuffer(GL_ARRAY_BUFFER, 0);//�رհ���
  23. /*1.2ʹ��vbo�����滭˳��*/
  24. unsigned short indexes[] = { 0,1,2 };
  25. glGenBuffers(1, &ebo);
  26. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  27. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * 3, indexes, GL_STATIC_DRAW);
  28. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  29. /*2.����shader*/
  30. int fileSize = 0;
  31. unsigned char * shaderCode = LoadFileContent("Res/test.vs", fileSize);
  32. GLuint vsShader = CompileShader(GL_VERTEX_SHADER, (char*)shaderCode);//���� vertex shader
  33. delete shaderCode;
  34. shaderCode = LoadFileContent("Res/test.fs", fileSize);
  35. GLuint fsShader = CompileShader(GL_FRAGMENT_SHADER, (char*)shaderCode);//���� fragment shader
  36. delete shaderCode;
  37. /*3.���� vertex shader �� fragment shader*/
  38. program = CreateProgram(vsShader, fsShader);
  39. glDeleteShader(vsShader);
  40. glDeleteShader(fsShader);
  41. /*4.��ȡ���㡢ģ�;�������ͼ������ͶӰ��������GPU�е�λ�ã�������λ�ú������ܶ������и�ֵ����*/
  42. /*
  43. attribute vec4 position;
  44. uniform mat4 ModelMatrix;
  45. uniform mat4 ViewMatrix;
  46. uniform mat4 ProjectionMatrix;
  47. */
  48. positionLocation = glGetAttribLocation(program, "position");
  49. colorLocation = glGetAttribLocation(program, "color");
  50. texcoordLocation = glGetAttribLocation(program, "texcoord");
  51. modelMatrixLocation = glGetUniformLocation(program, "ModelMatrix");
  52. viewMatrixLocation = glGetUniformLocation(program, "ViewMatrix");
  53. projectionMatrixLocation = glGetUniformLocation(program, "ProjectionMatrix");
  54. textureLocation = glGetUniformLocation(program, "U_Texture");
  55. modelMatrix = glm::translate(0.0f, 0.0f, -0.6f);
  56. texture = CreateTexture2DFromBMP("Res/test.bmp");
  57. ground.Init();
  58. }
  59. void SetViewPortSize(float width, float height) {
  60. //5.1.����ͶӰ����
  61. projectionMatrix = glm::perspective(60.0f, width / height, 0.1f, 1000.0f);
  62. }
  63. void Draw() {
  64. float frameTime = GetFrameTime();
  65. glClearColor(0.1f, 0.4f, 0.6f, 1.0f);
  66. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67. ground.Draw(viewMatrix, projectionMatrix);
  68. glUseProgram(program);
  69. /*5.2.��MVP������ֵ���Կ���*/
  70. glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));//����ģ�;���
  71. glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix));
  72. glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  73. /*6.���û��Ƶ�����*/
  74. glBindTexture(GL_TEXTURE_2D, texture);
  75. glUniform1i(textureLocation, 0);//������ͼ
  76. glBindBuffer(GL_ARRAY_BUFFER, vbo);//����vbo������1�������õļ�����������
  77. glEnableVertexAttribArray(positionLocation);//���ö�������
  78. glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, 0);//��vbo�����������붥������
  79. glEnableVertexAttribArray(colorLocation);//������ɫ����
  80. glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(sizeof(float) * 4));//��vbo��ɫ����������ɫ����
  81. glEnableVertexAttribArray(texcoordLocation);
  82. glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 10, (void*)(sizeof(float) * 8));
  83. glBindBuffer(GL_ARRAY_BUFFER, 0);
  84. /*7.����*/
  85. //glDrawArrays(GL_TRIANGLES, 0, 3);//˳������
  86. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  87. glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);//����ebo˳������
  88. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  89. /*8.��������*/
  90. glUseProgram(0);
  91. }