Skip to content

Commit

Permalink
next
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Aug 22, 2024
1 parent bc96716 commit fc1eb98
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
inc
bin
dep
lib
Expand Down
2 changes: 2 additions & 0 deletions inc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GL*
glm
13 changes: 13 additions & 0 deletions inc/mkn/gl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _MKN_GL_HPP_
#define _MKN_GL_HPP_

#include "mkn/gl/fw.hpp"
#include "mkn/gl/fw/window.hpp"

#include "mkn/gl/sl/program.hpp"

#include "mkn/gl/dds.hpp"
#include "mkn/gl/view.hpp"
#include "mkn/gl/shaders.hpp"

#endif // _MKN_GL_HPP_
106 changes: 106 additions & 0 deletions inc/mkn/gl/dds.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#ifndef _MKN_GL_DDS_HPP_
#define _MKN_GL_DDS_HPP_

#include <GL/glew.h>

#include <vector>
#include <cstring>
#include <fstream>
#include <sstream>
#include <optional>

namespace mkn::gl::dds {

GLuint inline static load(const char *imagepath) {
auto constexpr static FOURCC_DXT1 = 0x31545844; // Equivalent to "DXT1" in ASCII
auto constexpr static FOURCC_DXT3 = 0x33545844; // Equivalent to "DXT3" in ASCII
auto constexpr static FOURCC_DXT5 = 0x35545844; // Equivalent to "DXT5" in ASCII

unsigned char header[124];

FILE *fp = fopen(imagepath, "rb");
if (fp == NULL) {
printf(
"%s could not be opened. Are you in the right directory ? Don't forget to read the FAQ !\n",
imagepath);
getchar();
return 0;
}

/* verify the type of file */
char filecode[4];
fread(filecode, 1, 4, fp);
if (strncmp(filecode, "DDS ", 4) != 0) {
fclose(fp);
return 0;
}

/* get the surface desc */
fread(&header, 124, 1, fp);

unsigned int height = *(unsigned int *)&(header[8]);
unsigned int width = *(unsigned int *)&(header[12]);
unsigned int linearSize = *(unsigned int *)&(header[16]);
unsigned int mipMapCount = *(unsigned int *)&(header[24]);
unsigned int fourCC = *(unsigned int *)&(header[80]);

unsigned char *buffer;
unsigned int bufsize;
/* how big is it going to be including all mipmaps? */
bufsize = mipMapCount > 1 ? linearSize * 2 : linearSize;
buffer = (unsigned char *)malloc(bufsize * sizeof(unsigned char));
fread(buffer, 1, bufsize, fp);
/* close the file pointer */
fclose(fp);

unsigned int components = (fourCC == FOURCC_DXT1) ? 3 : 4;
unsigned int format;
switch (fourCC) {
case FOURCC_DXT1:
format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
break;
case FOURCC_DXT3:
format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
break;
case FOURCC_DXT5:
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
break;
default:
free(buffer);
return 0;
}

// Create one OpenGL texture
GLuint textureID;
glGenTextures(1, &textureID);

// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
unsigned int offset = 0;

/* load the mipmaps */
for (unsigned int level = 0; level < mipMapCount && (width || height); ++level) {
unsigned int size = ((width + 3) / 4) * ((height + 3) / 4) * blockSize;
glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height, 0, size, buffer + offset);

offset += size;
width /= 2;
height /= 2;

// Deal with Non-Power-Of-Two textures. This code is not included in the webpage to reduce
// clutter.
if (width < 1) width = 1;
if (height < 1) height = 1;
}

free(buffer);

return textureID;
}

} // namespace mkn::gl::dds

#endif //_MKN_GL_DDS_HPP_
1 change: 1 addition & 0 deletions inc/mkn/gl/ew.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
glew.hpp
Empty file added inc/mkn/gl/ew/vertex.hpp
Empty file.
24 changes: 24 additions & 0 deletions inc/mkn/gl/fw.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _MKN_GL_FW_HPP_
#define _MKN_GL_FW_HPP_

#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <vector>
#include <cstring>
#include <fstream>
#include <sstream>
#include <optional>

#include "mkn/kul/except.hpp"

namespace mkn::gl::fw {

auto static inline init() {
if (!glfwInit()) throw std::runtime_error("glfwInit() failed!");
}

} // namespace mkn::gl::fw

#endif // _MKN_GL_FW_HPP_
35 changes: 35 additions & 0 deletions inc/mkn/gl/fw/mouse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _MKN_GL_FW_MOUSE_HPP_
#define _MKN_GL_FW_MOUSE_HPP_

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

namespace mkn::gl::fw {

struct WindowParams {};

static inline GLFWwindow* create_window(WindowParams const& wp = {}) {
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make macOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

// Open a window and create its OpenGL context
GLFWwindow* window = glfwCreateWindow(1024, 768, "Tutorial 18 - Particles", NULL, NULL);
if (window == NULL) {
fprintf(stderr,
"Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. "
"Try the 2.1 version of the tutorials.\n");
getchar();
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
return window;
}

} // namespace mkn::gl::fw

#endif // _MKN_GL_FW_MOUSE_HPP_
35 changes: 35 additions & 0 deletions inc/mkn/gl/fw/window.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _MKN_GLFW_WINDOW_HPP_
#define _MKN_GLFW_WINDOW_HPP_

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

namespace mkn::gl::fw {

struct WindowParams {};

static inline GLFWwindow* create_window(WindowParams const& wp = {}) {
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make macOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

// Open a window and create its OpenGL context
GLFWwindow* window = glfwCreateWindow(1024, 768, "Tutorial 18 - Particles", NULL, NULL);
if (window == NULL) {
fprintf(stderr,
"Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. "
"Try the 2.1 version of the tutorials.\n");
getchar();
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
return window;
}

} // namespace mkn::gl::fw

#endif // _MKN_GLFW_WINDOW_HPP_
79 changes: 79 additions & 0 deletions inc/mkn/gl/keys.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef _MKN_GL_KEYS_HPP_
#define _MKN_GL_KEYS_HPP_

// #include <GLFW/glfw3.h>
// #include <glm/glm.hpp>
// #include <glm/gtc/matrix_transform.hpp>

// #include <vector>
// #include <cstring>
// #include <fstream>
// #include <sstream>
// #include <optional>

// void computeMatricesFromInputs(GLFWwindow* window) {
// // glfwGetTime is called only once, the first time this function is called
// static double lastTime = glfwGetTime();

// // Compute time difference between current and last frame
// double currentTime = glfwGetTime();
// float deltaTime = float(currentTime - lastTime);

// // Get mouse position
// double xpos, ypos;
// glfwGetCursorPos(window, &xpos, &ypos);

// // Reset mouse position for next frame
// glfwSetCursorPos(window, 1024 / 2, 768 / 2);

// // Compute new orientation
// horizontalAngle += mouseSpeed * float(1024 / 2 - xpos);
// verticalAngle += mouseSpeed * float(768 / 2 - ypos);

// // Direction : Spherical coordinates to Cartesian coordinates conversion
// glm::vec3 direction(cos(verticalAngle) * sin(horizontalAngle), sin(verticalAngle),
// cos(verticalAngle) * cos(horizontalAngle));

// // Right vector
// glm::vec3 right =
// glm::vec3(sin(horizontalAngle - 3.14f / 2.0f), 0, cos(horizontalAngle - 3.14f / 2.0f));

// // Up vector
// glm::vec3 up = glm::cross(right, direction);

// // Move forward
// if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
// position += direction * deltaTime * speed;
// }
// // Move backward
// if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
// position -= direction * deltaTime * speed;
// }
// // Strafe right
// if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
// position += right * deltaTime * speed;
// }
// // Strafe left
// if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
// position -= right * deltaTime * speed;
// }

// float FoV = initialFoV; // - 5 * glfwGetMouseWheel(); // Now GLFW 3 requires setting up a
// // callback for this. It's a bit too complicated for this beginner's
// // tutorial, so it's disabled instead.

// // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
// ProjectionMatrix = glm::perspective(glm::radians(FoV), 4.0f / 3.0f, 0.1f, 100.0f);
// // Camera matrix
// ViewMatrix =
// glm::lookAt(position, // Camera is here
// position + direction, // and looks here : at the same position, plus
// "direction" up // Head is up (set to 0,-1,0 to look
// upside-down)
// );

// // For the next frame, the "last time" will be "now"
// lastTime = currentTime;
// }

#endif // _MKN_GL_KEYS_HPP_
Loading

0 comments on commit fc1eb98

Please sign in to comment.