main.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <stdio.h>
  2. #include <GLFW/glfw3.h>
  3. int main (int argc, char** argv)
  4. {
  5. GLFWwindow* window;
  6. if (!glfwInit()) {
  7. fprintf(stderr, "could not init glfw.\n");
  8. return 1;
  9. }
  10. window = glfwCreateWindow(640, 480, "Hello Window", NULL, NULL);
  11. if (!window) {
  12. fprintf(stderr, "could not initialize glfw window\n");
  13. return 1;
  14. }
  15. glfwMakeContextCurrent(window);
  16. unsigned char* data = new unsigned char [100 * 100 * 3];
  17. for (int y=0; y < 100; y++) {
  18. for (int x = 0; x < 100; x++) {
  19. data[y * 100 * 3 + x * 3] = 0xff;
  20. data[y * 100 * 3 + x * 3 + 1] = 0x00;
  21. data[y * 100 * 3 + x * 3 + 2] = 0x00;
  22. }
  23. }
  24. GLuint texture_handle;
  25. glGenTextures(1, &texture_handle);
  26. glBindTexture(GL_TEXTURE_2D, texture_handle);
  27. glTexImage2D(texture_handle, 0, GL_RGB, 100, 100, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  28. while (!glfwWindowShouldClose(window)) {
  29. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  30. glDrawPixels(100, 100, GL_RGB, GL_UNSIGNED_BYTE, data);
  31. int window_width, window_height;
  32. glfwGetFramebufferSize(window, &window_width, &window_height);
  33. glMatrixMode(GL_PROJECTION);
  34. glLoadIdentity();
  35. glOrtho(0, window_width, 0, window_height, -1, 1);
  36. glMatrixMode(GL_MODELVIEW);
  37. glEnable(GL_TEXTURE_2D);
  38. glBindTexture(GL_TEXTURE_2D, texture_handle);
  39. glBegin(GL_QUADS);
  40. glTexCoord2d(0,0);glTexCoord2d(0,0);
  41. glTexCoord2d(1,0);glTexCoord2d(100,0);
  42. glTexCoord2d(1,1);glTexCoord2d(100,100);
  43. glTexCoord2d(0,1);glTexCoord2d(0,100);
  44. glEnd();
  45. glDisable(GL_TEXTURE_2D);
  46. glfwSwapBuffers(window);
  47. glfwWaitEvents();
  48. }
  49. return 0;
  50. }