blu
This commit is contained in:
parent
d91d0da1cc
commit
7215fd5433
5 changed files with 96 additions and 0 deletions
7
assets/shaders/orange.fs
Normal file
7
assets/shaders/orange.fs
Normal file
|
@ -0,0 +1,7 @@
|
|||
#version 330 core
|
||||
|
||||
out vec3 color;
|
||||
|
||||
void main() {
|
||||
color = vec3(0.2f, 0.2f, 0.8f);
|
||||
}
|
7
assets/shaders/orange.vs
Normal file
7
assets/shaders/orange.vs
Normal file
|
@ -0,0 +1,7 @@
|
|||
#version 330 core
|
||||
|
||||
in vec3 pos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(pos, 1.0f);
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <GL/gl.h>
|
||||
#include <memory>
|
||||
#include "shader.h"
|
||||
|
||||
#include "renderer.h"
|
||||
|
||||
|
@ -11,6 +13,7 @@ float verts[] {
|
|||
};
|
||||
|
||||
unsigned int VAO, VBO;
|
||||
Shader *shader = NULL;
|
||||
|
||||
void renderInit(GLFWwindow* window) {
|
||||
glViewport(0, 0, 500, 500);
|
||||
|
@ -27,12 +30,18 @@ void renderInit(GLFWwindow* window) {
|
|||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Compile shader
|
||||
shader = new Shader("assets/shaders/orange.vs", "assets/shaders/orange.fs");
|
||||
}
|
||||
|
||||
void render(GLFWwindow* window) {
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Use shader
|
||||
shader->use();
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
}
|
62
src/shader.cpp
Normal file
62
src/shader.cpp
Normal file
|
@ -0,0 +1,62 @@
|
|||
#include <fstream>
|
||||
#include <GL/glew.h>
|
||||
#include <GL/gl.h>
|
||||
|
||||
#include "shader.h"
|
||||
|
||||
std::string getContents(std::string filePath) {
|
||||
std::ifstream ifs(filePath);
|
||||
std::string content( (std::istreambuf_iterator<char>(ifs) ),
|
||||
(std::istreambuf_iterator<char>() ) );
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
unsigned int compileShader(std::string path, GLenum type) {
|
||||
int success;
|
||||
unsigned int shader = glCreateShader(type);
|
||||
|
||||
std::string source = getContents(path);
|
||||
const char* source2 = source.c_str();
|
||||
glShaderSource(shader, 1, &source2, NULL);
|
||||
glCompileShader(shader);
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if(success != 1) {
|
||||
char infoLog[256];
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
printf("Fragment shader %s failed to compile: [%d]: %s\n", path.c_str(), success, infoLog);
|
||||
abort();
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
Shader::Shader(std::string vertexShaderPath, std::string fragmentShaderPath) {
|
||||
unsigned int vertexShader = compileShader(vertexShaderPath, GL_VERTEX_SHADER);
|
||||
unsigned int fragmentShader = compileShader(fragmentShaderPath, GL_FRAGMENT_SHADER);
|
||||
|
||||
int success;
|
||||
|
||||
id = glCreateProgram();
|
||||
glAttachShader(id, vertexShader);
|
||||
glAttachShader(id, fragmentShader);
|
||||
glLinkProgram(id);
|
||||
glGetProgramiv(id, GL_LINK_STATUS, &success);
|
||||
if(success != 1) {
|
||||
char infoLog[256];
|
||||
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
||||
printf("Shader program failed to link: [%d]: %s\n", success, infoLog);
|
||||
abort();
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
|
||||
Shader::~Shader() {
|
||||
glDeleteProgram(id);
|
||||
}
|
||||
|
||||
void Shader::use() {
|
||||
glUseProgram(id);
|
||||
}
|
11
src/shader.h
Normal file
11
src/shader.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class Shader {
|
||||
unsigned int id;
|
||||
|
||||
public:
|
||||
void use();
|
||||
Shader(std::string vertexShaderPath, std::string fragmentShaderPath);
|
||||
~Shader();
|
||||
};
|
Loading…
Add table
Reference in a new issue