mirror of
https://github.com/MichaelFisher1997/opengl-cpp.git
synced 2025-04-27 22:23:10 +00:00
Added a simple shader hardcoded in a string, and a function for creating and compiling the shader
This commit is contained in:
parent
3cab7282dc
commit
087343599c
66
src/sdl.cpp
66
src/sdl.cpp
@ -1,6 +1,10 @@
|
|||||||
#include <GL/glew.h> // Include GLEW before <SDL2/SDL.h>?
|
#include <GL/glew.h> // Include GLEW before <SDL2/SDL.h>?
|
||||||
#include "sdl.hpp"
|
#include "sdl.hpp"
|
||||||
|
#include <alloca.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
//#include <iterator>
|
||||||
|
#include <ostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
SdlWindow::SdlWindow(const char* title, int width, int height)
|
SdlWindow::SdlWindow(const char* title, int width, int height)
|
||||||
: m_window(nullptr),
|
: m_window(nullptr),
|
||||||
@ -69,6 +73,27 @@ SdlWindow::SdlWindow(const char* title, int width, int height)
|
|||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
|
||||||
|
|
||||||
|
std::string vertexShader =
|
||||||
|
"#version 330 core\n"
|
||||||
|
"\n"
|
||||||
|
"layout(location = 0) in vec4 position;\n"
|
||||||
|
"\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" gl_Position = position;\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
std::string fragmentShader =
|
||||||
|
"#version 330 core\n"
|
||||||
|
"\n"
|
||||||
|
"layout(location = 0) out vec4 color;\n"
|
||||||
|
"\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
|
||||||
|
"}\n";
|
||||||
|
unsigned int shader = createShader(vertexShader, fragmentShader);
|
||||||
|
glUseProgram(shader);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,3 +212,44 @@ void SdlWindow::setFullscreen(bool fullscreen) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int SdlWindow::compileShader(unsigned int type, const std::string& source) {
|
||||||
|
unsigned int id = glCreateShader(type);
|
||||||
|
const char* src = source.c_str(); // <--- this string needs to exist when compiling/running
|
||||||
|
glShaderSource(id, 1, &src, nullptr);
|
||||||
|
glCompileShader(id);
|
||||||
|
//TODO: error handling
|
||||||
|
int result;
|
||||||
|
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
|
||||||
|
if (result == GL_FALSE) {
|
||||||
|
int length;
|
||||||
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
|
||||||
|
char* message = (char*)alloca(length * sizeof(char)); //do i need to deallocate this??
|
||||||
|
glGetShaderInfoLog(id, length, &length, message);
|
||||||
|
std::cout << "Failed to compile" << (type == GL_VERTEX_SHADER ? "vertex":"fragment") << "shader" << std::endl; // print out what type of shader it is
|
||||||
|
std::cout << message << std::endl;
|
||||||
|
glDeleteShader(id);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
//
|
||||||
|
return id;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int SdlWindow::createShader(const std::string& vertexShader, const std::string& fragmentShader) {
|
||||||
|
unsigned int program = glCreateProgram();
|
||||||
|
unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
|
||||||
|
unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
|
||||||
|
|
||||||
|
glAttachShader(program, vs);
|
||||||
|
glAttachShader(program, fs);
|
||||||
|
glLinkProgram(program);
|
||||||
|
glValidateProgram(program);
|
||||||
|
|
||||||
|
glDeleteShader(vs);
|
||||||
|
glDeleteShader(fs);
|
||||||
|
|
||||||
|
return program;
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <string>
|
||||||
// Include the OpenGL headers (these may vary by system)
|
// Include the OpenGL headers (these may vary by system)
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
#include <OpenGL/gl3.h> // On macOS, <OpenGL/gl3.h> is often used
|
#include <OpenGL/gl3.h> // On macOS, <OpenGL/gl3.h> is often used
|
||||||
@ -8,7 +9,7 @@
|
|||||||
#include <GL/glew.h> // or <glad/glad.h> if using GLAD
|
#include <GL/glew.h> // or <glad/glad.h> if using GLAD
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <iostream>
|
//#include <iostream>
|
||||||
|
|
||||||
// Forward declaration of classes and structs if needed
|
// Forward declaration of classes and structs if needed
|
||||||
// class SomethingElse;
|
// class SomethingElse;
|
||||||
@ -23,6 +24,8 @@ public:
|
|||||||
|
|
||||||
// Run the main loop
|
// Run the main loop
|
||||||
void run();
|
void run();
|
||||||
|
unsigned int compileShader(unsigned int type, const std::string& source);
|
||||||
|
unsigned int createShader(const std::string& vetexShader, const std::string& fragmentShader);
|
||||||
|
|
||||||
void setFullscreen(bool fullscreen);
|
void setFullscreen(bool fullscreen);
|
||||||
|
|
||||||
@ -37,6 +40,10 @@ private:
|
|||||||
int m_windowedWidth; // stored width before fullscreen
|
int m_windowedWidth; // stored width before fullscreen
|
||||||
int m_windowedHeight; // stored height before fullscreen
|
int m_windowedHeight; // stored height before fullscreen
|
||||||
SDL_GLContext m_glContext;
|
SDL_GLContext m_glContext;
|
||||||
|
// temp shader stuff
|
||||||
|
std::string vetexShader;
|
||||||
|
std::string fragmentShader;
|
||||||
|
unsigned int shader;
|
||||||
|
|
||||||
|
|
||||||
// Private methods
|
// Private methods
|
||||||
|
Loading…
x
Reference in New Issue
Block a user