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.

49 lines
1.9 KiB

5 years ago
  1. #include "shader.h"
  2. #include "utils.h"
  3. #include "vertexbuffer.h"
  4. void Shader::Init(const char * vs, const char * fs)
  5. {
  6. int nFileSize = 0;
  7. const char*vsCode = (char*)LoadFileContent(vs, nFileSize);
  8. const char*fsCode = (char*)LoadFileContent(fs, nFileSize);
  9. GLuint vsShader = CompileShader(GL_VERTEX_SHADER, vsCode);
  10. if (vsShader == 0) {
  11. return;
  12. }
  13. GLuint fsShader = CompileShader(GL_FRAGMENT_SHADER, fsCode);
  14. if (fsShader == 0) {
  15. return;
  16. }
  17. mProgram = CreateProgram(vsShader, fsShader);
  18. glDeleteShader(vsShader);
  19. glDeleteShader(fsShader);
  20. if (mProgram != 0) {
  21. mModelMatrixLocation = glGetUniformLocation(mProgram, "ModelMatrix");
  22. mViewMatrixLocation = glGetUniformLocation(mProgram, "ViewMatrix");
  23. mProjectionMatrixLocation = glGetUniformLocation(mProgram, "ProjectionMatrix");
  24. mPositionLocation = glGetAttribLocation(mProgram, "position");
  25. mColorLocation = glGetAttribLocation(mProgram, "color");
  26. mTexcoordLocation = glGetAttribLocation(mProgram, "texcoord");
  27. mNormalLocation = glGetAttribLocation(mProgram, "normal");
  28. }
  29. }
  30. void Shader::Bind(float *M, float *V, float*P) {
  31. glUseProgram(mProgram);
  32. glUniformMatrix4fv(mModelMatrixLocation, 1, GL_FALSE, M);
  33. glUniformMatrix4fv(mViewMatrixLocation, 1, GL_FALSE, V);
  34. glUniformMatrix4fv(mProjectionMatrixLocation, 1, GL_FALSE, P);
  35. glEnableVertexAttribArray(mPositionLocation);
  36. glVertexAttribPointer(mPositionLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
  37. glEnableVertexAttribArray(mColorLocation);
  38. glVertexAttribPointer(mColorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float) * 4));
  39. glEnableVertexAttribArray(mTexcoordLocation);
  40. glVertexAttribPointer(mTexcoordLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float) * 8));
  41. glEnableVertexAttribArray(mNormalLocation);
  42. glVertexAttribPointer(mNormalLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float) * 12));
  43. }