Skip to content

Commit

Permalink
alot of work
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedhussein89 committed Feb 29, 2020
1 parent c02b091 commit 4663a9b
Show file tree
Hide file tree
Showing 38 changed files with 6,331 additions and 7 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
project(GraphicsDemo CXX)
cmake_minimum_required(VERSION 3.9)

option(ENABLE_SFML "" OFF)
option(ENABLE_GLFW "" OFF)
option(ENABLE_Qt5 "" OFF)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(options)

#add_subdirectory(window)
add_subdirectory(window)
add_subdirectory(opengl)
add_subdirectory(cameras)
add_subdirectory(mimicOpenGL)
add_subdirectory(scene)

12 changes: 11 additions & 1 deletion cameras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL REQUIRED)
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(gl3w REQUIRED)
find_package(glm REQUIRED)

add_executable(firstPersonShootingCamera firstPersonShootingCamera.cpp)

target_link_libraries(firstPersonShootingCamera PRIVATE OpenGL::GL SDL2::SDL2 glm options::options gl3w::gl3w)
target_link_libraries(
firstPersonShootingCamera
PRIVATE
OpenGL::GL
SDL2::SDL2
glm
options::options
gl3w::gl3w
SDL2::tff
)

65 changes: 62 additions & 3 deletions cameras/firstPersonShootingCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
// SDL
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_ttf.h>
// GLM
#include <glm/gtc/type_ptr.hpp>
// Internal
#include "firstPersonShootingCamera.hpp"

static constexpr auto GL3D_SUCCESS = 0;
static constexpr auto SDL_SUCCESS = 0;

[[maybe_unused]] static constexpr auto SDL_IMMEDIATE_UPDATE = 0;
[[maybe_unused]] static constexpr auto SDL_SYNCHRONIZED_UPDATE = 1;
Expand Down Expand Up @@ -56,6 +58,55 @@ static auto parseProgramOptions(int argc, char **argv) -> std::optional<std::pai
return std::make_pair(std::stoi(argv[1]), std::stoi(argv[2]));
}

static void drawText(const char *pMessage, glm::vec2 topLeft) {
if(TTF_Init() != SDL_SUCCESS) {
std::cerr << "Can not Initialize SDL2\n";
std::exit(EXIT_FAILURE);
}

auto pTitleFont = TTF_OpenFont("font/DroidSansMono.ttf", 48);
if(pTitleFont == nullptr) {
std::string ttferr = TTF_GetError();
std::cerr << "Can not load \"DroidSansMono.ttf\": " << ttferr << '\n';
std::exit(EXIT_FAILURE);
}

SDL_Color fg = {255, 128, 0, 255};
auto pSurface = TTF_RenderText_Blended(pTitleFont, pMessage, fg);
if(pSurface == nullptr) {
std::string ttferr = TTF_GetError();
std::cerr << "Can not create Blenderd Texture: " << ttferr << '\n';
std::exit(EXIT_FAILURE);
}

GLuint colors = pSurface->format->BytesPerPixel;
GLuint format;
if(colors == 4) { // alpha
if(pSurface->format->Rmask == 0x000000ff)
format = GL_RGBA;
else
format = GL_BGRA;
} else { // no alpha
if(pSurface->format->Rmask == 0x000000ff)
format = GL_RGB;
else
format = GL_BGR;
}

auto pPixels = reinterpret_cast<GLubyte *>(pSurface->pixels);
GLuint hTex;
glGenTextures(1, &hTex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, hTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pSurface->w, pSurface->h, 0, format, GL_UNSIGNED_BYTE, pPixels);
glGenerateMipmap(GL_TEXTURE_2D);
SDL_FreeSurface(pSurface);
}

auto main(int argc, char *argv[]) -> int {
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cerr << "Can not initialize SDL2\n";
Expand All @@ -64,7 +115,7 @@ auto main(int argc, char *argv[]) -> int {

SDL_LogSetOutputFunction(LogOutputFunction, nullptr);

int width = 640;
int width = 640;
int height = 480;
if(const auto args = parseProgramOptions(argc, argv); args) {
constexpr auto firstScreenIndex = 0;
Expand Down Expand Up @@ -526,6 +577,8 @@ auto main(int argc, char *argv[]) -> int {

FPSCamera camera;

char message[512] = "HelloWorld!";

GLuint query[2];
glGenQueries(2, query);
float delta = 0.F;
Expand All @@ -540,7 +593,10 @@ auto main(int argc, char *argv[]) -> int {
case SDL_KEYDOWN: break;
case SDL_MOUSEWHEEL: break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: break;
case SDL_MOUSEBUTTONUP: {
const auto mouseButtonEvent = event.button;
std::cout << "(" << mouseButtonEvent.x << ", " << mouseButtonEvent.y << ")\n";
} break;
case SDL_MOUSEMOTION: break;
}
}
Expand All @@ -563,6 +619,8 @@ auto main(int argc, char *argv[]) -> int {
glDrawArrays(GL_TRIANGLES, 0, 42);
}
glUseProgram(0);

drawText(message, glm::vec2{0, 0});
}
SDL_GL_SwapWindow(pWindow);

Expand All @@ -581,7 +639,8 @@ auto main(int argc, char *argv[]) -> int {
// get query results
glGetQueryObjectui64v(query[0], GL_QUERY_RESULT, &startTimeGL);
glGetQueryObjectui64v(query[1], GL_QUERY_RESULT, &stopTimeGL);
printf("\rFrame %+3.3F/%+3.3F ms", delta, static_cast<float>(stopTimeGL - startTimeGL) / 1000000.F);

snprintf(message, 512, "Frame %+3.3F/%+3.3F ms", delta, static_cast<float>(stopTimeGL - startTimeGL) / 1000000.F);
}

glBindVertexArray(0);
Expand Down
46 changes: 46 additions & 0 deletions cmake/FindSDL2_ttf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if(TARGET SDL2::ttf)
return()
endif()

set(
INC_SEARCH_PATH
/usr/include/
)

set(
LIB_SEARCH_PATH
/usr/lib/
)

find_path(
SDL2_TTF_INCLUDE_PATH
SDL2/SDL_ttf.h
${INC_SEARCH_PATH}
)

find_library(
SDL2_TTF_LIB
SDL2_ttf
${LIB_SEARCH_PATH}
)

find_package_handle_standard_args(
SDL2_ttf
DEFAULT_MSG
SDL2_TTF_INCLUDE_PATH
SDL2_TTF_LIB
)

add_library(
SDL2::tff
UNKNOWN
IMPORTED
)

set_target_properties(
SDL2::tff
PROPERTIES
IMPORTED_LOCATION "${SDL2_TTF_LIB}"
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_TTF_INCLUDE_PATH}"
)

25 changes: 25 additions & 0 deletions mimicOpenGL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ${CMAKE_SOURCE_DIR}/cameras/CMakeLists.txt
find_package(SDL2 REQUIRED)

add_executable(
mimicOpenGL
mimicOpenGL.cpp
src/Line.cpp
src/Image.cpp
)

target_link_libraries(
mimicOpenGL
PRIVATE
SDL2::SDL2
options::options
)

target_include_directories(
mimicOpenGL
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include/
)

add_subdirectory(test)

7 changes: 7 additions & 0 deletions mimicOpenGL/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ ] Geomtery processing.
- Vertex Shading
- Geomtery
[ ] Clipping.
[ ] Rasteriztion.
[ ] Pixel processing.

20 changes: 20 additions & 0 deletions mimicOpenGL/include/Image.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
// STL
#include <string_view>
// Internal
#include "Type.hpp"

struct Image {
Image(std::uint32_t width, std::uint32_t height);

std::uint32_t width;
std::uint32_t height;
vec3_8u *m_pData = nullptr;

auto operator()(std::uint32_t x, std::uint32_t y) -> vec3_8u& { return m_pData[y * width + x]; }

auto operator()(std::uint32_t x, std::uint32_t y) const -> const vec3_8u& { return m_pData[y * width + x]; }

auto saveImage(std::string_view outputFileName) -> bool;
};

19 changes: 19 additions & 0 deletions mimicOpenGL/include/Line.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "Type.hpp"
#include "Image.hpp"

namespace mimicOpenGL {

void line1(int x0, int y0, int x1, int y1, vec3_8u color, Image &output);

void line2(int x0, int y0, int x1, int y1, vec3_8u color, Image &output);

void line2a(int x0, int y0, int x1, int y1, vec3_8u color, Image &output);

void line3(int x0, int y0, int x1, int y1, vec3_8u color, Image &output);

void line4(int x0, int y0, int x1, int y1, vec3_8u color, Image &output);

void line5(int x0, int y0, int x1, int y1, vec3_8u color, Image &output);

} // namespace mimicOpenGL
9 changes: 9 additions & 0 deletions mimicOpenGL/include/PlyLoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
// STL
#include <optional>
#include <string_view>

struct Model {};

auto loadPlyFile(std::string_view fileName) -> std::optional<Model>;

14 changes: 14 additions & 0 deletions mimicOpenGL/include/Type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
// STL
#include <cstdint>

template<typename T>
struct vec3 {
union { T x, r, s; };
union { T y, g, t; };
union { T z, b, p; };
};

using vec3_f = vec3<float>;
using vec3_8u = vec3<std::uint8_t>;

Loading

0 comments on commit 4663a9b

Please sign in to comment.