i think i broke somthing

This commit is contained in:
Ben
2018-09-17 12:38:09 +01:00
parent 0cc6e9cef0
commit 2ca62a2201
16 changed files with 4972 additions and 41 deletions

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
@@ -22,7 +22,7 @@
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}</ProjectGuid>
<RootNamespace>OpenGL</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -77,6 +77,7 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>.\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>.\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
@@ -124,11 +125,15 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="mesh.cpp" />
<ClCompile Include="shader.cpp" />
<ClCompile Include="stb_image.c" />
<ClCompile Include="texture.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="headers/display.h" />
<ClInclude Include="headers/mesh.h" />
<ClInclude Include="headers/shader.h" />
<ClInclude Include="display.h" />
<ClInclude Include="mesh.h" />
<ClInclude Include="shader.h" />
<ClInclude Include="stb_image.h" />
<ClInclude Include="texture.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -5,21 +5,34 @@
<ClCompile Include="display.cpp" />
<ClCompile Include="mesh.cpp" />
<ClCompile Include="shader.cpp" />
<ClCompile Include="stb_image.c">
<Filter>helpers</Filter>
</ClCompile>
<ClCompile Include="texture.cpp" />
</ItemGroup>
<ItemGroup>
<Filter Include="headers">
<UniqueIdentifier>{3e9ef4e5-bcd4-4161-8243-90e67d85dc43}</UniqueIdentifier>
</Filter>
<Filter Include="helpers">
<UniqueIdentifier>{3b3efd97-0e64-421a-b292-4d37dfc52e96}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="headers/display.h">
<ClInclude Include="mesh.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="headers/mesh.h">
<ClInclude Include="display.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="headers/shader.h">
<ClInclude Include="shader.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="texture.h">
<Filter>headers</Filter>
</ClInclude>
<ClInclude Include="stb_image.h">
<Filter>helpers</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,7 +1,7 @@
#include <GL/glew.h>
#include <iostream>
#include "headers/display.h"
#include "display.h"
Display::Display(int width, int height, const std::string& title) {
SDL_Init(SDL_INIT_VIDEO);

19
OpenGL/display.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <sdl2/SDL.h>
#include <string>
class Display {
public:
Display(int width, int height, const std::string& title);
void Update();
bool isClosed();
virtual ~Display();
private:
SDL_Window* m_window;
SDL_GLContext m_glContext;
bool m_isClosed;
};

View File

@@ -1,29 +1,39 @@
#include <iostream>
#include <GL/glew.h>
#include "headers/display.h"
#include "headers/mesh.h"
#include "headers/shader.h"
#include "display.h"
#include "mesh.h"
#include "shader.h"
#include "texture.h"
#undef main
int main(int argc, char** argv) {
Display display(800, 600, "Crumpet Engine");
Display display(600, 600, "Crumpet Engine");
glClearColor(0.1f, 0.45f, 0.9f, 1.0f);
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
// 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
-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
};
Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
Shader shader;
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
Mesh mesh(vertices, indices, sizeof(vertices) / sizeof(vertices[0]));
Shader shader("C:/Users/Ben/Desktop/crumpet-engine/resources/shaders/simple2d");
Texture chanceCube("C:/Users/Ben/Desktop/crumpet-engine/resources/textures/chance-cube.jpg");
while(!display.isClosed()) {
glClear(GL_COLOR_BUFFER_BIT);
shader.Bind();
chanceCube.Bind(0);
mesh.Draw();
display.Update();

View File

@@ -1,20 +1,32 @@
#include "headers/mesh.h"
#include "mesh.h"
Mesh::Mesh(GLfloat *vertices, unsigned int numVerticies) {
Mesh::Mesh(GLfloat *vertices, unsigned int *indices, unsigned int numVerticies) {
m_drawCount = numVerticies;
glGenVertexArrays(1, &m_VAO);
glGenBuffers(1, &m_VBO);
glGenBuffers(1, &m_EBO);
glBindVertexArray(m_VAO);
glGenBuffers(NUM_BUFFERS, m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVerticies * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
std::cout << "Mesh loaded successfully" << std::endl;
}

25
OpenGL/mesh.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <GL/glew.h>
class Mesh {
public:
Mesh(GLfloat *vertices, unsigned int *indices, unsigned int numVerticies);
void Draw();
virtual ~Mesh();
private:
enum {
POSITION_VB,
NUM_BUFFERS,
TEXCOORD_VB
};
unsigned int m_VAO;
unsigned int m_VBO;
unsigned int m_EBO;
unsigned int m_drawCount;
};

View File

@@ -1,22 +1,12 @@
#include "headers/shader.h"
#include "shader.h"
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.25f, 1.0f, 0.49f, 1.0f);\n"
"}\n\0";
Shader::Shader(std::string path) {
std::string vertexPath = path + "_vertex.glsl";
std::string fragmentPath = path + "_fragment.glsl";
Shader::Shader() {
m_program = glCreateProgram();
m_shaders[0] = CreateShader(vertexShaderSource, GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(fragmentShaderSource, GL_FRAGMENT_SHADER);
m_shaders[0] = CreateShader(LoadFile(vertexPath), GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadFile(fragmentPath), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++) {
glAttachShader(m_program, m_shaders[i]);
@@ -69,6 +59,27 @@ void Shader::Bind() {
glUseProgram(m_program);
}
std::string Shader::LoadFile(std::string path) {
std::ifstream file;
file.open((path).c_str());
std::string output;
std::string line;
if (file.is_open()) {
while (file.good()) {
getline(file, line);
output.append(line + "\n");
}
}
else {
std::cout << "Unable to load shader: " << path << std::endl;
}
std::cout << "Successfully loaded " + path << std::endl;
return output;
}
Shader::~Shader() {
for (unsigned int i = 0; i < NUM_SHADERS; i++) {
glDetachShader(m_program, m_shaders[i]);

23
OpenGL/shader.h Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <GL/glew.h>
class Shader {
public:
Shader(std::string path);
void Bind();
virtual ~Shader();
private:
static const unsigned int NUM_SHADERS = 2;
GLuint CreateShader(const std::string& text, GLenum shaderType);
std::string LoadFile(std::string path);
GLuint m_program;
GLuint m_shaders[NUM_SHADERS];
// 0 = vertex, 1 = fragment
};

4475
OpenGL/stb_image.c Normal file

File diff suppressed because it is too large Load Diff

261
OpenGL/stb_image.h Normal file
View File

@@ -0,0 +1,261 @@
//// begin header file ////////////////////////////////////////////////////
//
// Limitations:
// - no jpeg progressive support
// - non-HDR formats support 8-bit samples only (jpeg, png)
// - no delayed line count (jpeg) -- IJG doesn't support either
// - no 1-bit BMP
// - GIF always returns *comp=4
//
// Basic usage (see HDR discussion below):
// int x,y,n;
// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
// // ... process data if not NULL ...
// // ... x = width, y = height, n = # 8-bit components per pixel ...
// // ... replace '0' with '1'..'4' to force that many components per pixel
// // ... but 'n' will always be the number that it would have been if you said 0
// stbi_image_free(data)
//
// Standard parameters:
// int *x -- outputs image width in pixels
// int *y -- outputs image height in pixels
// int *comp -- outputs # of image components in image file
// int req_comp -- if non-zero, # of image components requested in result
//
// The return value from an image loader is an 'unsigned char *' which points
// to the pixel data. The pixel data consists of *y scanlines of *x pixels,
// with each pixel consisting of N interleaved 8-bit components; the first
// pixel pointed to is top-left-most in the image. There is no padding between
// image scanlines or between pixels, regardless of format. The number of
// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
// If req_comp is non-zero, *comp has the number of components that _would_
// have been output otherwise. E.g. if you set req_comp to 4, you will always
// get RGBA output, but you can check *comp to easily see if it's opaque.
//
// An output image with N components has the following components interleaved
// in this order in each pixel:
//
// N=#comp components
// 1 grey
// 2 grey, alpha
// 3 red, green, blue
// 4 red, green, blue, alpha
//
// If image loading fails for any reason, the return value will be NULL,
// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
// can be queried for an extremely brief, end-user unfriendly explanation
// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
// more user-friendly ones.
//
// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
//
// ===========================================================================
//
// iPhone PNG support:
//
// By default we convert iphone-formatted PNGs back to RGB; nominally they
// would silently load as BGR, except the existing code should have just
// failed on such iPhone PNGs. But you can disable this conversion by
// by calling stbi_convert_iphone_png_to_rgb(0), in which case
// you will always just get the native iphone "format" through.
//
// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
// pixel to remove any premultiplied alpha *only* if the image file explicitly
// says there's premultiplied data (currently only happens in iPhone images,
// and only if iPhone convert-to-rgb processing is on).
//
// ===========================================================================
//
// HDR image support (disable by defining STBI_NO_HDR)
//
// stb_image now supports loading HDR images in general, and currently
// the Radiance .HDR file format, although the support is provided
// generically. You can still load any file through the existing interface;
// if you attempt to load an HDR file, it will be automatically remapped to
// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
// both of these constants can be reconfigured through this interface:
//
// stbi_hdr_to_ldr_gamma(2.2f);
// stbi_hdr_to_ldr_scale(1.0f);
//
// (note, do not use _inverse_ constants; stbi_image will invert them
// appropriately).
//
// Additionally, there is a new, parallel interface for loading files as
// (linear) floats to preserve the full dynamic range:
//
// float *data = stbi_loadf(filename, &x, &y, &n, 0);
//
// If you load LDR images through this interface, those images will
// be promoted to floating point values, run through the inverse of
// constants corresponding to the above:
//
// stbi_ldr_to_hdr_scale(1.0f);
// stbi_ldr_to_hdr_gamma(2.2f);
//
// Finally, given a filename (or an open file or memory block--see header
// file for details) containing image data, you can query for the "most
// appropriate" interface to use (that is, whether the image is HDR or
// not), using:
//
// stbi_is_hdr(char *filename);
//
// ===========================================================================
//
// I/O callbacks
//
// I/O callbacks allow you to read from arbitrary sources, like packaged
// files or some other source. Data read from callbacks are processed
// through a small internal buffer (currently 128 bytes) to try to reduce
// overhead.
//
// The three functions you must define are "read" (reads some bytes of data),
// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
#ifndef STBI_NO_STDIO
#if defined(_MSC_VER) && _MSC_VER >= 0x1400
#define _CRT_SECURE_NO_WARNINGS // suppress bogus warnings about fopen()
#endif
#include <stdio.h>
#endif
#define STBI_VERSION 1
enum
{
STBI_default = 0, // only used for req_comp
STBI_grey = 1,
STBI_grey_alpha = 2,
STBI_rgb = 3,
STBI_rgb_alpha = 4
};
typedef unsigned char stbi_uc;
#ifdef __cplusplus
extern "C" {
#endif
//////////////////////////////////////////////////////////////////////////////
//
// PRIMARY API - works on images of any type
//
//
// load image by filename, open file, or memory buffer
//
extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
#ifndef STBI_NO_STDIO
extern stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp);
extern stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
// for stbi_load_from_file, file pointer is left pointing immediately after image
#endif
typedef struct
{
int(*read) (void *user, char *data, int size); // fill 'data' with 'size' bytes. return number of bytes actually read
void(*skip) (void *user, unsigned n); // skip the next 'n' bytes
int(*eof) (void *user); // returns nonzero if we are at end of file/data
} stbi_io_callbacks;
extern stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
#ifndef STBI_NO_HDR
extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
#ifndef STBI_NO_STDIO
extern float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp);
extern float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp);
#endif
extern float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
extern void stbi_hdr_to_ldr_gamma(float gamma);
extern void stbi_hdr_to_ldr_scale(float scale);
extern void stbi_ldr_to_hdr_gamma(float gamma);
extern void stbi_ldr_to_hdr_scale(float scale);
#endif // STBI_NO_HDR
// stbi_is_hdr is always defined
extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
#ifndef STBI_NO_STDIO
extern int stbi_is_hdr(char const *filename);
extern int stbi_is_hdr_from_file(FILE *f);
#endif // STBI_NO_STDIO
// get a VERY brief reason for failure
// NOT THREADSAFE
extern const char *stbi_failure_reason(void);
// free the loaded image -- this is just free()
extern void stbi_image_free(void *retval_from_stbi_load);
// get image dimensions & components without fully decoding
extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
extern int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
#ifndef STBI_NO_STDIO
extern int stbi_info(char const *filename, int *x, int *y, int *comp);
extern int stbi_info_from_file(FILE *f, int *x, int *y, int *comp);
#endif
// for image formats that explicitly notate that they have premultiplied alpha,
// we just return the colors as stored in the file. set this flag to force
// unpremultiplication. results are undefined if the unpremultiply overflow.
extern void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
// indicate whether we should process iphone images back to canonical format,
// or just pass them through "as-is"
extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
// ZLIB client - used by PNG, available for other purposes
extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
// define faster low-level operations (typically SIMD support)
#ifdef STBI_SIMD
typedef void(*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
// compute an integer IDCT on "input"
// input[x] = data[x] * dequantize[x]
// write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
// CLAMP results to 0..255
typedef void(*stbi_YCbCr_to_RGB_run)(stbi_uc *output, stbi_uc const *y, stbi_uc const *cb, stbi_uc const *cr, int count, int step);
// compute a conversion from YCbCr to RGB
// 'count' pixels
// write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
// y: Y input channel
// cb: Cb input channel; scale/biased to be 0..255
// cr: Cr input channel; scale/biased to be 0..255
extern void stbi_install_idct(stbi_idct_8x8 func);
extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
#endif // STBI_SIMD
#ifdef __cplusplus
}
#endif
//
//
//// end header file /////////////////////////////////////////////////////

37
OpenGL/texture.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "texture.h"
#include "stb_image.h"
#include <cassert>
#include <iostream>
Texture::Texture(std::string fileName) {
int width, height, numComponents;
unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
if (imageData == NULL)
std::cout << "Loading failed for texture: " << fileName << std::endl;
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
stbi_image_free(imageData);
std::cout << "Loaded texture: " << fileName << std::endl;
}
void Texture::Bind(unsigned int unit) {
assert(unit >= 0 && unit <= 31);
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, m_texture);
}
Texture::~Texture() {
glDeleteTextures(1, &m_texture);
}

16
OpenGL/texture.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include <string>
#include <GL/glew.h>
class Texture {
public:
Texture(std::string fileName);
void Bind(unsigned int unit);
virtual ~Texture();
private:
GLuint m_texture;
};

View File

@@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main() {
FragColor = texture(ourTexture, TexCoord);
}

View File

@@ -0,0 +1,13 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
void main() {
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB