Przeglądaj źródła

initial: convert small square to texture and draw it

Douglas A 3 lat temu
rodzic
commit
cfa5431a44
1 zmienionych plików z 28 dodań i 2 usunięć
  1. 28 2
      src/main.cpp

+ 28 - 2
src/main.cpp

@@ -15,8 +15,10 @@ int main (int argc, char** argv)
         fprintf(stderr, "could not initialize glfw window\n");
         return 1;
     }
+    
     glfwMakeContextCurrent(window);
 
+    
     unsigned char* data = new unsigned char [100 * 100 * 3];
     for (int y=0; y < 100; y++) {
         for (int x = 0; x < 100; x++) {
@@ -26,13 +28,37 @@ int main (int argc, char** argv)
         }
     }
 
+    GLuint texture_handle;
+    glGenTextures(1, &texture_handle);
+    glBindTexture(GL_TEXTURE_2D, texture_handle);
+    glTexImage2D(texture_handle, 0, GL_RGB, 100, 100, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+
+    
     while (!glfwWindowShouldClose(window)) {
 
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glDrawPixels(100, 100, GL_RGB, GL_UNSIGNED_BYTE, data);
-        
-        glfwSwapBuffers(window);
 
+        int window_width, window_height;
+        glfwGetFramebufferSize(window, &window_width, &window_height);
+        glMatrixMode(GL_PROJECTION);
+
+        glLoadIdentity();
+        glOrtho(0, window_width, 0, window_height, -1, 1);
+
+        glMatrixMode(GL_MODELVIEW);
+
+        glEnable(GL_TEXTURE_2D);
+        glBindTexture(GL_TEXTURE_2D, texture_handle);
+        glBegin(GL_QUADS);
+            glTexCoord2d(0,0);glTexCoord2d(0,0);
+            glTexCoord2d(1,0);glTexCoord2d(100,0);
+            glTexCoord2d(1,1);glTexCoord2d(100,100);
+            glTexCoord2d(0,1);glTexCoord2d(0,100);
+        glEnd();
+        glDisable(GL_TEXTURE_2D);
+
+        glfwSwapBuffers(window);
         glfwWaitEvents();
 
     }