Commit f57614fd authored by Anakin's avatar Anakin
Browse files

added old working project

parent 07bf3f93
#pragma once
#include <iostream>
#include <gl\glew.h>
#include <gl\glfw3.h>
#include "setup.h"
#define GLFW_KEY_PLUS_GER GLFW_KEY_RIGHT_BRACKET
#define GLFW_KEY_MINUS_GER GLFW_KEY_SLASH
struct {
double posX;
double posY;
bool leftHold;
bool middleHold;
bool rightHold;
double speed = 1;
} mouse;
void windowResize(GLFWwindow * window, int width, int height)
{
monitor.witdh = width;
monitor.heigh = height;
glViewport(0, 0, width, height);
}
void mouseButton(GLFWwindow *window, int button, int action, int mod)
{
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
mouse.leftHold = true;
glfwGetCursorPos(window, &mouse.posX, &mouse.posY);
}
else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
{
mouse.leftHold = false;
}
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
{
mouse.rightHold = true;
glfwGetCursorPos(window, &mouse.posX, &mouse.posY);
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE)
{
mouse.rightHold = false;
}
}
void mouseMove(GLFWwindow *window, double xpos, double ypos)
{
if (mouse.leftHold)
{
monitor.rotX += static_cast<float>((ypos - mouse.posY) * 0.01 * mouse.speed);
monitor.rotY += static_cast<float>((xpos - mouse.posX) * 0.01 * mouse.speed);
mouse.posX = xpos;
mouse.posY = ypos;
}
if (mouse.rightHold)
{
monitor.transX -= (xpos - mouse.posX) * 0.01 * mouse.speed;
monitor.transY += (ypos - mouse.posY) * 0.01 * mouse.speed;
mouse.posX = xpos;
mouse.posY = ypos;
}
}
void mouseWheel(GLFWwindow *window, double xoffset, double yoffset)
{
monitor.transZ -= yoffset * 0.5 * mouse.speed;
}
void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS || action == GLFW_REPEAT)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GLFW_TRUE);
break;
case GLFW_KEY_MINUS_GER: case GLFW_KEY_KP_SUBTRACT:
mouse.speed -= 0.1;
if (mouse.speed < 0.1)
mouse.speed = 0;
break;
case GLFW_KEY_PLUS_GER: case GLFW_KEY_KP_ADD:
mouse.speed += 0.1;
break;
case GLFW_KEY_A:
std::cout << mouse.speed << std::endl;
break;
default:
std::cout << key << " " << scancode << std::endl;
break;
}
}
}
#pragma once
#include <vector>
#include <fstream>
#include <Windows.h>
#include <gl\glew.h>
std::vector<GLfloat> loadData()
{
return std::vector<GLfloat>(
{
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f
}
);
}
std::vector<GLfloat> loadColor()
{
return std::vector<GLfloat>(
{
0.583f, 0.771f, 0.014f,
0.609f, 0.115f, 0.436f,
0.327f, 0.483f, 0.844f,
0.822f, 0.569f, 0.201f,
0.435f, 0.602f, 0.223f,
0.310f, 0.747f, 0.185f,
0.597f, 0.770f, 0.761f,
0.559f, 0.436f, 0.730f,
0.359f, 0.583f, 0.152f,
0.483f, 0.596f, 0.789f,
0.559f, 0.861f, 0.639f,
0.195f, 0.548f, 0.859f,
0.014f, 0.184f, 0.576f,
0.771f, 0.328f, 0.970f,
0.406f, 0.615f, 0.116f,
0.676f, 0.977f, 0.133f,
0.971f, 0.572f, 0.833f,
0.140f, 0.616f, 0.489f,
0.997f, 0.513f, 0.064f,
0.945f, 0.719f, 0.592f,
0.543f, 0.021f, 0.978f,
0.279f, 0.317f, 0.505f,
0.167f, 0.620f, 0.077f,
0.347f, 0.857f, 0.137f,
0.055f, 0.953f, 0.042f,
0.714f, 0.505f, 0.345f,
0.783f, 0.290f, 0.734f,
0.722f, 0.645f, 0.174f,
0.302f, 0.455f, 0.848f,
0.225f, 0.587f, 0.040f,
0.517f, 0.713f, 0.338f,
0.053f, 0.959f, 0.120f,
0.393f, 0.621f, 0.362f,
0.673f, 0.211f, 0.457f,
0.820f, 0.883f, 0.371f,
0.982f, 0.099f, 0.879f
}
);
}
std::vector<GLfloat> loadUV()
{
return std::vector<float>(
{
0.000059f, 1.0f - 0.000004f,
0.000103f, 1.0f - 0.336048f,
0.335973f, 1.0f - 0.335903f,
1.000023f, 1.0f - 0.000013f,
0.667979f, 1.0f - 0.335851f,
0.999958f, 1.0f - 0.336064f,
0.667979f, 1.0f - 0.335851f,
0.336024f, 1.0f - 0.671877f,
0.667969f, 1.0f - 0.671889f,
1.000023f, 1.0f - 0.000013f,
0.668104f, 1.0f - 0.000013f,
0.667979f, 1.0f - 0.335851f,
0.000059f, 1.0f - 0.000004f,
0.335973f, 1.0f - 0.335903f,
0.336098f, 1.0f - 0.000071f,
0.667979f, 1.0f - 0.335851f,
0.335973f, 1.0f - 0.335903f,
0.336024f, 1.0f - 0.671877f,
1.000004f, 1.0f - 0.671847f,
0.999958f, 1.0f - 0.336064f,
0.667979f, 1.0f - 0.335851f,
0.668104f, 1.0f - 0.000013f,
0.335973f, 1.0f - 0.335903f,
0.667979f, 1.0f - 0.335851f,
0.335973f, 1.0f - 0.335903f,
0.668104f, 1.0f - 0.000013f,
0.336098f, 1.0f - 0.000071f,
0.000103f, 1.0f - 0.336048f,
0.000004f, 1.0f - 0.671870f,
0.336024f, 1.0f - 0.671877f,
0.000103f, 1.0f - 0.336048f,
0.336024f, 1.0f - 0.671877f,
0.335973f, 1.0f - 0.335903f,
0.667969f, 1.0f - 0.671889f,
1.000004f, 1.0f - 0.671847f,
0.667979f, 1.0f - 0.335851f
}
);
}
#pragma once
#include <gl/glew.h>
#include <gl/glfw3.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <Windows.h>
#include <vector>
#include "shader.h"
#include "import.h"
#include "texture.h"
#define OGL_MAJOR_VERSION 4
#define OGL_MINOR_VERSION 5
#define AA_VALUE 4
#define MAINWINDOW_NAME "MeshViewer 2.0 pre-alpha"
#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
#define TEXTURE_NAME "Textures/dice.tga"
struct oglVariables {
GLFWwindow* window; // window
GLuint vertexArrayID; // vertex array
GLuint vertexBufferID; // vertex buffer
GLuint colorBufferID; // color buffer
GLuint samplerID; // sampler handler
GLuint uvBufferID; // uv buffer
GLuint shaderPrgmID; // shader
struct {
GLuint id; // matrix ID
glm::mat4 projection; // projection
glm::mat4 view; // view
glm::mat4 model; // model
glm::mat4 mvp; // mvp
} matrix;
struct {
std::vector<GLfloat> data; // obj data
std::vector<GLfloat> color; // obj color
std::vector<GLfloat> uv; // obj uv
GLuint textureID; // texture data
} object;
};
struct {
float fov = 45.f;
float minView = 0.1f;
float maxView = 100.0f;
int witdh = 640;
int heigh = 480;
float rotX = 0;
float rotY = 0;
float rotZ = 0;
double transX = 0.0;
double transY = 0.0;
double transZ = 5.0;
} monitor;
void setupGLFW()
{
if (!glfwInit())
{
MessageBox(NULL, "Failed to initialize GLFW", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
exit(0);
}
glfwWindowHint(GLFW_SAMPLES, AA_VALUE); // antialiasing level
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, OGL_MAJOR_VERSION); // OpenGL major version number
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, OGL_MINOR_VERSION); // OpenGL minor verison number
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // something we need for new OGL
}
GLFWwindow* createWindow()
{
GLFWwindow* window = glfwCreateWindow(monitor.witdh, monitor.heigh, MAINWINDOW_NAME, NULL, NULL);
if (window == NULL)
{
char tempMajor[10], tempMinor[10];
_itoa_s(OGL_MAJOR_VERSION, tempMajor, 10, 10);
_itoa_s(OGL_MINOR_VERSION, tempMinor, 10, 10);
MessageBox(NULL,
(std::string("Your GPU does not support OpenGL ") += std::string(tempMajor) += std::string(".") += std::string(tempMinor)).c_str(),
"MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
glfwTerminate();
exit(0);
}
glfwMakeContextCurrent(window);
return window;
}
void setupGLEW()
{
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
MessageBox(NULL, "Failed to initialize GLEW!", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
glfwTerminate();
exit(0);
}
}
void calcMatrices(oglVariables &scene)
{
scene.matrix.projection = glm::perspective(monitor.fov, float(monitor.witdh) / float(monitor.heigh), monitor.minView, monitor.maxView);
scene.matrix.view = glm::lookAt(
glm::vec3(monitor.transX, monitor.transY, monitor.transZ),
glm::vec3(monitor.transX, monitor.transY, monitor.transZ - 1),
glm::vec3(0, 1, 0)
);
scene.matrix.model = glm::mat4(1.0f);
scene.matrix.model = glm::rotate(scene.matrix.model, monitor.rotX, glm::vec3(1, 0, 0));
scene.matrix.model = glm::rotate(scene.matrix.model, monitor.rotY, glm::vec3(0, 1, 0));
scene.matrix.model = glm::rotate(scene.matrix.model, monitor.rotZ, glm::vec3(0, 0, 1));
scene.matrix.mvp = scene.matrix.projection * scene.matrix.view * scene.matrix.model;
}
void initVariables(oglVariables &temp)
{
glGenVertexArrays(1, &(temp.vertexArrayID));
glGenBuffers(1, &temp.vertexBufferID);
glGenBuffers(1, &temp.colorBufferID);
glGenBuffers(1, &temp.uvBufferID);
temp.shaderPrgmID = LoadShaders(VERTEX_SHADER, FRAGMENT_SHADER);
temp.matrix.id = glGetUniformLocation(temp.shaderPrgmID, "MVP");
calcMatrices(temp);
temp.samplerID = glGetUniformLocation(temp.shaderPrgmID, "textureSampler");
temp.object.data = loadData();
temp.object.color = loadColor();
temp.object.uv = loadUV();
glGenTextures(1, &temp.object.textureID);
glBindTexture(GL_TEXTURE_2D, temp.object.textureID);
TextureTGA tempTex(TEXTURE_NAME);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}
void cleanUp(oglVariables &scene)
{
glDeleteBuffers(1, &scene.uvBufferID);
glDeleteBuffers(1, &scene.colorBufferID);
glDeleteBuffers(1, &scene.vertexBufferID);
glDeleteVertexArrays(1, &scene.vertexArrayID);
glDeleteProgram(scene.shaderPrgmID);
glDeleteTextures(1, &scene.samplerID);
glfwTerminate();
}
#pragma once
#include <gl\glew.h>
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path);
\ No newline at end of file
#pragma once
#include <vector>
class TextureTGA
{
public:
TextureTGA(const char* filePath);
~TextureTGA();
private:
std::vector<std::uint8_t> vui8Pixels;
bool bCompressed;
std::uint32_t ui32IDLength;
bool bColorTabel;
std::uint32_t ui32PicType;
std::uint32_t ui32PaletteBegin;
std::uint32_t ui32PaletteLength;
std::uint32_t ui32PaletteBpP;
std::uint32_t ui32Width;
std::uint32_t ui32Height;
std::uint32_t ui32Size;
std::uint32_t ui32BpP;
std::uint32_t ui32Attribut;
public:
std::vector<std::uint8_t> getData() const;
bool hasAlpha() const;
std::uint32_t getWidth() const;
std::uint32_t getHeight() const;
};
#pragma once
#include <vector>
#include <gl\glut.h>
#include <Windows.h>
#include <fstream>
typedef union PixelInfo
{
std::uint32_t Colour;
struct
{
std::uint8_t B, G, R, A;
};
} *PPixelInfo;
class Tga
{
private:
std::vector<std::uint8_t> Pixels;
bool ImageCompressed;
std::uint32_t width, height, size, BitsPerPixel;
public:
Tga(const char* FilePath);
std::vector<std::uint8_t> GetPixels() { return this->Pixels; }
std::uint32_t GetWidth() const { return this->width; }
std::uint32_t GetHeight() const { return this->height; }
bool HasAlphaChannel() { return BitsPerPixel == 32; }
};
Tga::Tga(const char* FilePath)
{
std::fstream hFile(FilePath, std::ios::in | std::ios::binary);
if (!hFile.is_open()) { throw std::invalid_argument("File Not Found."); }
std::uint8_t Header[18] = { 0 };
std::vector<std::uint8_t> ImageData;
static std::uint8_t DeCompressed[12] = { 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
static std::uint8_t IsCompressed[12] = { 0x0, 0x0, 0xA, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
hFile.read(reinterpret_cast<char*>(&Header), sizeof(Header));
if (!std::memcmp(DeCompressed, &Header, sizeof(DeCompressed)))
{
BitsPerPixel = Header[16];
width = Header[13] * 0xFF + Header[12];
height = Header[15] * 0xFF + Header[14];
size = ((width * BitsPerPixel + 31) / 32) * 4 * height;
if ((BitsPerPixel != 24) && (BitsPerPixel != 32))
{
hFile.close();
throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image.");
}
ImageData.resize(size);
ImageCompressed = false;
hFile.read(reinterpret_cast<char*>(ImageData.data()), size);
}
else if (!std::memcmp(IsCompressed, &Header, sizeof(IsCompressed)))
{
BitsPerPixel = Header[16];
width = Header[13] * 0xFF + Header[12];
height = Header[15] * 0xFF + Header[14];
size = ((width * BitsPerPixel + 31) / 32) * 4 * height;
if ((BitsPerPixel != 24) && (BitsPerPixel != 32))
{
hFile.close();
throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit Image.");
}
PixelInfo Pixel = { 0 };
int CurrentByte = 0;
std::size_t CurrentPixel = 0;
ImageCompressed = true;
std::uint8_t ChunkHeader = { 0 };
int BytesPerPixel = (BitsPerPixel / 8);
ImageData.resize(width * height * sizeof(PixelInfo));
do
{
hFile.read(reinterpret_cast<char*>(&ChunkHeader), sizeof(ChunkHeader));
if (ChunkHeader < 128)
{
++ChunkHeader;
for (int I = 0; I < ChunkHeader; ++I, ++CurrentPixel)
{
hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel);
ImageData[CurrentByte++] = Pixel.B;
ImageData[CurrentByte++] = Pixel.G;
ImageData[CurrentByte++] = Pixel.R;
if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A;
}
}
else
{
ChunkHeader -= 127;
hFile.read(reinterpret_cast<char*>(&Pixel), BytesPerPixel);
for (int I = 0; I < ChunkHeader; ++I, ++CurrentPixel)
{
ImageData[CurrentByte++] = Pixel.B;
ImageData[CurrentByte++] = Pixel.G;
ImageData[CurrentByte++] = Pixel.R;
if (BitsPerPixel > 24) ImageData[CurrentByte++] = Pixel.A;
}
}
} while (CurrentPixel < (width * height));
}
else
{
hFile.close();
throw std::invalid_argument("Invalid File Format. Required: 24 or 32 Bit TGA File.");
}
//fix color mix
std::uint8_t temp;
unsigned int it = 0;
while (1)
{
temp = ImageData[it];
ImageData[it] = ImageData[it + 2];
ImageData[it + 2] = temp;
BitsPerPixel == 32 ? it += 2 : it += 3;
if (it + 2 >= size)
break;
}
hFile.close();
this->Pixels = ImageData;
}
#version 450 core
// Input
in vec3 fragmentColor;
// Ouput data
out vec3 color;
void main()
{
color = fragmentColor;
}
\ No newline at end of file