Changeset e856d62 in opengl-game


Ignore:
Timestamp:
Apr 3, 2018, 3:11:47 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
046ce72
Parents:
267c4c5
Message:

Make texture images appear right-side up and streamline the code for creating a window

Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • NewOpenGLGame.vcxproj

    r267c4c5 re856d62  
    135135  <ItemGroup>
    136136    <Text Include="gl.log" />
     137    <Text Include="TODO.txt" />
    137138  </ItemGroup>
    138139  <ItemGroup>
  • new-game.cpp

    r267c4c5 re856d62  
    147147
    148148   GLFWwindow* window = NULL;
     149   GLFWmonitor* mon = NULL;
    149150
    150151   if (FULLSCREEN) {
    151       GLFWmonitor* mon = glfwGetPrimaryMonitor();
     152      mon = glfwGetPrimaryMonitor();
    152153      const GLFWvidmode* vmode = glfwGetVideoMode(mon);
    153 
    154       cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
    155       window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
    156154
    157155      width = vmode->width;
    158156      height = vmode->height;
    159    } else {
    160       window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
     157      cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
    161158   }
     159   window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
    162160
    163161   if (!window) {
     
    172170   glewExperimental = GL_TRUE;
    173171   glewInit();
    174 
    175    // Check the extended initialization section of the book to learn how to use this
    176    // Maybe move this and other OpenGL setup/settings code into a separate function
    177    // glViewport(0, 0, width*2, height*2);
    178172
    179173   const GLubyte* renderer = glGetString(GL_RENDERER);
     
    195189     cout << x << endl;
    196190     cout << y << endl;
    197      printf ("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
     191     printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
    198192   }
    199193
     
    560554    cout << "Loaded successfully" << endl;
    561555  } else {
    562     cout << "Failed to loade the file" << endl;
     556    cout << "Failed to load the file" << endl;
    563557  }
    564558
     
    581575unsigned char* loadImage(string file_name, int* x, int* y) {
    582576  int n;
    583   int force_channels = 4;
     577  int force_channels = 4; // This forces RGBA (4 bytes per pixel)
    584578  unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
     579
     580  int width_in_bytes = *x * 4;
     581  unsigned char *top = NULL;
     582  unsigned char *bottom = NULL;
     583  unsigned char temp = 0;
     584  int half_height = *y / 2;
     585
     586  // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
     587  for (int row = 0; row < half_height; row++) {
     588     top = image_data + row * width_in_bytes;
     589     bottom = image_data + (*y - row - 1) * width_in_bytes;
     590     for (int col = 0; col < width_in_bytes; col++) {
     591        temp = *top;
     592        *top = *bottom;
     593        *bottom = temp;
     594        top++;
     595        bottom++;
     596     }
     597  }
     598
    585599  if (!image_data) {
    586600    fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
    587601  }
     602
     603  // Not Power-of-2 check
     604  if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
     605     fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
     606  }
     607
    588608  return image_data;
    589609}
Note: See TracChangeset for help on using the changeset viewer.