Skip to content

Commit

Permalink
Merge pull request #24 from bozbalci/fullscreen
Browse files Browse the repository at this point in the history
Fulle bizi usta
  • Loading branch information
tansly authored Jan 8, 2020
2 parents 74757af + 04f2872 commit 59c743e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 7,581 deletions.
21 changes: 14 additions & 7 deletions assignments/hw3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ project(hw3)

set(CMAKE_CXX_STANDARD 17)

add_executable(hw3_flat src/flat.cpp src/util.cpp src/util.h src/globals.h src/state.h)
add_executable(hw3_sphere src/sphere.cpp src/globals.h)
add_executable(hw3_flat src/flat.cpp)
# add_executable(hw3_sphere src/sphere.cpp)
target_compile_options(hw3_flat PRIVATE -Wall -Wextra)
target_compile_options(hw3_sphere PRIVATE -Wall -Wextra)
# target_compile_options(hw3_sphere PRIVATE -Wall -Wextra)

find_package(GLEW REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)
find_package(GLEW REQUIRED)
find_package(jpeg REQUIRED)

target_link_libraries(hw3_flat ${JPEG_LIBRARIES})
# target_link_libraries(hw3_sphere ${JPEG_LIBRARIES})
include_directories(${JPEG_INCLUDE_DIR})

if (APPLE)
# No need for GLEW on Mac OS X
# Silence deprecation warnings on macOS
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGL_SILENCE_DEPRECATION")

# macOS clients does not need to use GLEW
target_link_libraries(hw3_flat glfw ${GLFW_LIBRARIES} ${CMAKE_DL_LIBS})
target_link_libraries(hw3_sphere glfw ${CMAKE_DL_LIBS})
# target_link_libraries(hw3_sphere glfw ${GLFW_LIBRARIES} ${CMAKE_DL_LIBS})
else ()
target_link_libraries(hw3_flat glfw GLEW ${CMAKE_DL_LIBS})
target_link_libraries(hw3_sphere glfw GLEW ${CMAKE_DL_LIBS})
# target_link_libraries(hw3_sphere glfw GLEW ${GLFW_LIBRARIES} ${CMAKE_DL_LIBS})
endif ()
6 changes: 3 additions & 3 deletions assignments/hw3/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ CXXFLAGS=-std=c++17 -Wall -O3

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LDFLAGS=-lGL -lglfw -lGLEW
LDFLAGS=-lGL -lglfw -lGLEW -ljpeg
endif
ifeq ($(UNAME_S),Darwin)
LDFLAGS=-framework OpenGL -lglfw
LDFLAGS=-framework OpenGL -lglfw -ljpeg
CXXFLAGS += -DGL_SILENCE_DEPRECATION
endif
UBUNTU_S := $(shell $(CXX) --version | grep -i ubuntu >/dev/null; echo $$?)
ifeq ($(UBUNTU_S),0)
LDFLAGS=-lGLEW -lglfw3 -lGL -lX11 -lXi -lXrandr -lXxf86vm -lXinerama -lXcursor -lrt -lm -pthread -ldl
LDFLAGS=-lGLEW -lglfw3 -lGL -lX11 -lXi -lXrandr -lXxf86vm -lXinerama -lXcursor -lrt -lm -pthread -ldl -ljpeg
endif

all: hw3_flat
Expand Down
115 changes: 103 additions & 12 deletions assignments/hw3/src/flat.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
Expand All @@ -17,15 +18,12 @@
#endif // __APPLE__

#include <GLFW/glfw3.h>
#include <jpeglib.h>

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"

#define STB_IMAGE_IMPLEMENTATION
#define STBI_ASSERT(x)
#include "stb_image.h"

// Uncomment the following line to enable debug messages.
// #define DEBUG

Expand All @@ -40,6 +38,54 @@ void die(const std::string& msg)
std::exit(1);
}

unsigned char *LoadImage(const char *Path, int *Width, int *Height)
{
// 1.) Load the file
struct jpeg_decompress_struct JpegDecompressInfo;
struct jpeg_error_mgr JpegErrorManager;
FILE *Infile = std::fopen(Path, "rb");
if (!Infile)
die("Could not open jpeg file for reading");

// 2.) Read the JPEG header
JpegDecompressInfo.err = jpeg_std_error(&JpegErrorManager);
jpeg_create_decompress(&JpegDecompressInfo);
jpeg_stdio_src(&JpegDecompressInfo, Infile);
jpeg_read_header(&JpegDecompressInfo, static_cast<boolean>(true));
jpeg_start_decompress(&JpegDecompressInfo);

// 3.) Compute image dimensions
auto Width_ = JpegDecompressInfo.image_width;
auto Height_ = JpegDecompressInfo.image_height;
auto NumberOfComponents = JpegDecompressInfo.num_components;
auto RowSize = Width_ * NumberOfComponents;
auto ImageSize = RowSize * Height_;

// 4.) Read the image into buffer
auto ImageData = static_cast<unsigned char *>(std::malloc(ImageSize));
JSAMPROW ImageRow[1] = {static_cast<JSAMPROW>(std::malloc(RowSize))};
auto NumberOfScanlines = static_cast<JDIMENSION>(Height_);
auto Location = 0;
while (JpegDecompressInfo.output_scanline < NumberOfScanlines) {
jpeg_read_scanlines(&JpegDecompressInfo, ImageRow, /* max_lines = */ 1);

for (auto i = 0; i < RowSize; ++i) {
ImageData[Location] = ImageRow[0][i];
++Location;
}
}

// 5.) Clean-up resources
jpeg_finish_decompress(&JpegDecompressInfo);
jpeg_destroy_decompress(&JpegDecompressInfo);
std::fclose(Infile);

// 6.) Return values
*Width = Width_;
*Height = Height_;
return ImageData;
}

bool ReadFileIntoBuffer(const std::string& Path, std::stringstream& Buffer)
{
try {
Expand Down Expand Up @@ -77,20 +123,34 @@ struct UIState {
GLint HeightFactorLocation;
GLint TextureHorizontalShiftLocation;

/// UI PARAMETERS
// UI PARAMETERS
double HeightFactor = HEIGHT_FACTOR_INITIAL;
double TextureHorizontalShift = TEXTURE_HORIZONTAL_SHIFT_INITIAL;
float Pitch = PITCH_INITIAL;
float Yaw = YAW_INITIAL;
float Speed = SPEED_INITIAL;

/// UNIFORMS
// UNIFORMS
struct CameraType {
glm::vec3 Position;
glm::vec3 Gaze{0.0f, 0.0f, 0.1f};
glm::vec3 Up{0.0f, 1.0f, 0.0f};
} Camera;

// WINDOW STATE
struct {
struct {
int X, Y;
} Position;

struct {
int Width, Height;
} Size;

bool IsFullScreen = false;
bool FullScreenKeyDown = false;
} Window;

CameraType CAMERA_INITIAL;

struct {
Expand Down Expand Up @@ -319,7 +379,39 @@ void KeyCallback(GLFWwindow *Window, int Key, int ScanCode, int Action, int Mods
ON_KEY(X) TheState.ResetSpeed();
ON_KEY(I) TheState.ResetPositionAndCamera();

// TODO P for Fullscreen
auto KeyState = glfwGetKey(Window, GLFW_KEY_P);
if (KeyState == GLFW_PRESS && !TheState.Window.FullScreenKeyDown) {
TheState.Window.FullScreenKeyDown = true;

if (!TheState.Window.IsFullScreen) {
// Save window position and size so that they can be restored.
glfwGetWindowPos(Window, &TheState.Window.Position.X, &TheState.Window.Position.Y);
glfwGetWindowSize(Window, &TheState.Window.Size.Width, &TheState.Window.Size.Height);

auto Monitor = glfwGetPrimaryMonitor();
auto VideoMode = glfwGetVideoMode(Monitor);
auto Width = VideoMode->width;
auto Height = VideoMode->height;

glfwSetWindowMonitor(Window, Monitor, /* xpos = */ 0, /* ypos = */ 0,
Width, Height, GLFW_DONT_CARE);
glViewport(/* x = */ 0, /* y = */ 0, Width, Height);

TheState.Window.IsFullScreen = true;
} else {
auto Width = TheState.Window.Size.Width;
auto Height = TheState.Window.Size.Height;
auto XOffset = TheState.Window.Position.X;
auto YOffset = TheState.Window.Position.Y;
glfwSetWindowMonitor(Window, nullptr, XOffset, YOffset, Width, Height, GLFW_DONT_CARE);
glViewport(/* x = */ 0, /* y = */ 0, Width, Height);

TheState.Window.IsFullScreen = false;
}

} else if (KeyState == GLFW_RELEASE && TheState.Window.FullScreenKeyDown) {
TheState.Window.FullScreenKeyDown = false;
}

if (glfwGetKey(Window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(Window, true);
Expand Down Expand Up @@ -464,7 +556,6 @@ struct HW3Utility {
}

void InitializeTextures() {
int Comp; // Number of channels in the file being read
unsigned char *ImageData;
GLuint Textures[2];

Expand All @@ -473,21 +564,21 @@ struct HW3Utility {
// Load height map
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Textures[0]);
ImageData = stbi_load(HeightMapPath.c_str(), &TextureWidth, &TextureHeight, &Comp,
/* desired_channels = */ 0);
ImageData = LoadImage(HeightMapPath.c_str(), &TextureWidth, &TextureHeight);
glTexImage2D(GL_TEXTURE_2D, /* level = */ 0, GL_RGB, TextureWidth, TextureHeight,
/* border = */ 0, GL_RGB, GL_UNSIGNED_BYTE, ImageData);
std::free(ImageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateMipmap(GL_TEXTURE_2D);

// Load diffuse map
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Textures[0]);
ImageData = stbi_load(TexturePath.c_str(), &TextureWidth, &TextureHeight, &Comp,
/* desired_channels = */ 0);
ImageData = LoadImage(TexturePath.c_str(), &TextureWidth, &TextureHeight);
glTexImage2D(GL_TEXTURE_2D, /* level = */ 0, GL_RGB, TextureWidth, TextureHeight,
/* border = */ 0, GL_RGB, GL_UNSIGNED_BYTE, ImageData);
std::free(ImageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateMipmap(GL_TEXTURE_2D);
Expand Down
Loading

0 comments on commit 59c743e

Please sign in to comment.