Dengine/src/dengine.cpp

399 lines
11 KiB
C++
Raw Normal View History

2016-06-03 05:07:40 +00:00
#include <Dengine/OpenGL.h>
#include <Dengine/Common.h>
#include <Dengine/Shader.h>
#define STBI_FAILURE_USERMSG
#define STB_IMAGE_IMPLEMENTATION
#include <STB/stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <cstdint>
#include <fstream>
#include <string>
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
f32 lastX = 400;
f32 lastY = 300;
f32 yaw = -90.0f;
f32 pitch = 0.0f;
b32 firstMouse;
b32 keys[1024];
2016-06-03 05:07:40 +00:00
enum BytesPerPixel
{
Greyscale = 1,
GreyscaleAlpha = 2,
RGB = 3,
RGBA = 4,
};
INTERNAL GLint getGLFormat(BytesPerPixel bytesPerPixel, b32 srgb)
{
switch (bytesPerPixel)
{
case Greyscale:
return GL_LUMINANCE;
case GreyscaleAlpha:
return GL_LUMINANCE_ALPHA;
case RGB:
return (srgb ? GL_SRGB : GL_RGB);
case RGBA:
return (srgb ? GL_SRGB_ALPHA : GL_RGBA);
default:
// TODO(doyle): Invalid
std::cout << "getGLFormat() invalid bytesPerPixel: "
<< bytesPerPixel << std::endl;
return GL_LUMINANCE;
}
}
void doMovement(f32 deltaTime)
{
f32 cameraSpeed = 5.00f * deltaTime;
if (keys[GLFW_KEY_W])
cameraPos += (cameraSpeed * cameraFront);
if (keys[GLFW_KEY_S])
cameraPos -= (cameraSpeed * cameraFront);
if (keys[GLFW_KEY_A])
cameraPos -=
glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos +=
glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}
void key_callback(GLFWwindow *window, int key, int scancode, int action,
int mode)
2016-06-03 05:07:40 +00:00
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key >= 0 && key < 1024)
{
if (action == GLFW_PRESS)
keys[key] = TRUE;
else if (action == GLFW_RELEASE)
keys[key] = FALSE;
}
}
void mouse_callback(GLFWwindow *window, double xPos, double yPos)
{
if (firstMouse)
{
lastX = (f32)xPos;
lastY = (f32)yPos;
firstMouse = false;
}
f32 xOffset = (f32)xPos - lastX;
f32 yOffset = lastY - (f32)yPos;
lastX = (f32)xPos;
lastY = (f32)yPos;
f32 sensitivity = 0.05f;
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset;
pitch += yOffset;
if (pitch > 89.0f)
pitch = 89.0f;
else if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
front.y = sin(glm::radians(pitch));
front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
cameraFront = glm::normalize(front);
2016-06-03 05:07:40 +00:00
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow *window =
glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);
if (!window)
{
std::cout << "glfwCreateWindow() failed: Failed to create window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
/* Make GLEW use more modern technies for OGL on core profile*/
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "glewInit() failed: Failed to initialise GLEW"
<< std::endl;
return -1;
}
2016-06-03 07:05:28 +00:00
i32 screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
glViewport(0, 0, screenWidth, screenHeight);
2016-06-03 05:07:40 +00:00
glfwSetKeyCallback(window, key_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
firstMouse = TRUE;
2016-06-03 05:07:40 +00:00
2016-06-03 07:05:28 +00:00
glEnable(GL_DEPTH_TEST);
2016-06-03 05:07:40 +00:00
/* Initialise shaders */
Dengine::Shader shader = Dengine::Shader("data/shaders/default.vert.glsl",
"data/shaders/default.frag.glsl");
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// Set texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Load a texture */
i32 imgWidth, imgHeight, bytesPerPixel;
stbi_set_flip_vertically_on_load(TRUE);
u8 *image = stbi_load("data/textures/container.jpg", &imgWidth, &imgHeight,
&bytesPerPixel, 0);
2016-06-03 05:07:40 +00:00
if (!image)
{
std::cerr << "stdbi_load() failed: " << stbi_failure_reason()
<< std::endl;
}
glTexImage2D(GL_TEXTURE_2D, 0,
getGLFormat((BytesPerPixel)bytesPerPixel, FALSE), imgWidth,
imgHeight, 0, getGLFormat((BytesPerPixel)bytesPerPixel, FALSE),
GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
// Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(image);
/* Create OGL Vertex objects */
2016-06-03 07:05:28 +00:00
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0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] = {
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3( 2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3( 1.3f, -2.0f, -2.5f),
glm::vec3( 1.5f, 2.0f, -2.5f),
glm::vec3( 1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
#if 0
2016-06-03 05:07:40 +00:00
GLfloat vertices[] = {
// Positions Colors Texture Coords
+0.5f, +0.5f, +0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
+0.5f, -0.5f, +0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
2016-06-03 07:05:28 +00:00
-0.5f, -0.5f, +0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom left
-0.5f, +0.5f, +0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top left
2016-06-03 05:07:40 +00:00
};
2016-06-03 07:05:28 +00:00
#endif
2016-06-03 05:07:40 +00:00
GLuint indices[] = {
0, 1, 3, // First triangle
1, 2, 3, // First triangle
};
2016-06-03 07:05:28 +00:00
GLuint vbo, vao;
2016-06-03 05:07:40 +00:00
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
2016-06-03 07:05:28 +00:00
#if 0
GLuint ebo;
2016-06-03 05:07:40 +00:00
glGenBuffers(1, &ebo);
2016-06-03 07:05:28 +00:00
#endif
2016-06-03 05:07:40 +00:00
// 1. Bind vertex array object
glBindVertexArray(vao);
// 2. Bind and set vertex buffer(s) and attribute pointer(s)
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
2016-06-03 07:05:28 +00:00
#if 0
2016-06-03 05:07:40 +00:00
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
const i32 numPos = 3;
const i32 numColors = 3;
const i32 numTexCoord = 2;
const GLint vertexSize =
(numPos + numColors + numTexCoord) * sizeof(GLfloat);
const GLint vertByteOffset = 0;
const GLint colorByteOffset = vertByteOffset + (numPos * sizeof(GLfloat));
const GLint texCoordByteOffset = colorByteOffset + (numColors * sizeof(GLfloat));
glVertexAttribPointer(0, numPos, GL_FLOAT, GL_FALSE, vertexSize,
(GLvoid *)vertByteOffset);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, numColors, GL_FLOAT, GL_FALSE, vertexSize,
(GLvoid *)colorByteOffset);
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, numTexCoord, GL_FLOAT, GL_FALSE, vertexSize,
(GLvoid *)texCoordByteOffset);
glEnableVertexAttribArray(2);
2016-06-03 07:05:28 +00:00
#endif
2016-06-03 05:07:40 +00:00
2016-06-03 07:05:28 +00:00
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
(GLvoid *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
(GLvoid *)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
2016-06-03 05:07:40 +00:00
// 4. Unbind to prevent mistakes
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
f32 deltaTime = 0.0f; // Time between current frame and last frame
f32 lastFrame = 0.0f; // Time of last frame
2016-06-03 07:05:28 +00:00
2016-06-03 05:07:40 +00:00
/* Main game loop */
while (!glfwWindowShouldClose(window))
{
f32 currentFrame = (f32)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
2016-06-03 05:07:40 +00:00
/* Check and call events */
glfwPollEvents();
doMovement(deltaTime);
2016-06-03 05:07:40 +00:00
/* Rendering commands here*/
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
2016-06-03 07:05:28 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2016-06-03 05:07:40 +00:00
/* Bind textures */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shader.mProgram, "ourTexture"), 0);
2016-06-03 05:07:40 +00:00
shader.use();
2016-06-03 07:05:28 +00:00
/* Camera/View transformation */
glm::mat4 view;
// NOTE(doyle): Lookat generates the matrix for camera coordinate axis
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
2016-06-03 07:05:28 +00:00
/* Projection */
2016-06-03 07:05:28 +00:00
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f),
((f32)screenWidth / (f32)screenHeight),
0.1f, 100.0f);
2016-06-03 07:05:28 +00:00
/* Get shader uniform locations */
2016-06-03 07:05:28 +00:00
GLuint modelLoc = glGetUniformLocation(shader.mProgram, "model");
GLuint viewLoc = glGetUniformLocation(shader.mProgram, "view");
GLuint projectionLoc = glGetUniformLocation(shader.mProgram, "projection");
2016-06-03 05:07:40 +00:00
/* Pass matrices to the shader */
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
2016-06-03 05:07:40 +00:00
glBindVertexArray(vao);
2016-06-03 07:05:28 +00:00
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
for (GLuint i = 0; i < 10; i++)
{
/* Calculate model matrix for each object and pass it to shader */
2016-06-03 07:05:28 +00:00
glm::mat4 model;
model = glm::translate(model, cubePositions[i]);
GLfloat angle = glm::radians(20.0f * i);
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
2016-06-03 05:07:40 +00:00
glBindVertexArray(0);
/* Swap the buffers */
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
2016-06-03 07:05:28 +00:00
#if 0
2016-06-03 05:07:40 +00:00
glDeleteBuffers(1, &ebo);
2016-06-03 07:05:28 +00:00
#endif
2016-06-03 05:07:40 +00:00
glfwTerminate();
return 0;
}