Browse Source

more rendering work

testing
= 3 years ago
parent
commit
d617cc5525

+ 1
- 1
include/graphics/drawable.h View File

@@ -2,7 +2,7 @@
* Drawable class
*/

#pragma once
#include <memory>

namespace vtk {

+ 20
- 0
include/graphics/rendertarget.h View File

@@ -0,0 +1,20 @@
/*
* Rendertarget like an FBO or window
*/

#pragma once

namespace vtk {

class RenderTarget {
public:
virtual void activate() = 0;
double getAspect();
int width();
int height();
protected:
int mWidth;
int mHeight;
};

}

+ 19
- 0
include/graphics/shader.h View File

@@ -8,9 +8,15 @@
#include <GL/gl.h>

#include <string>
#include <glm/mat4x4.hpp>
#include <glm/vec4.hpp>
#include <glm/vec3.hpp>

namespace vtk {

//forward declaration
class ShaderUniform;

class Shader {
public:
Shader();
@@ -23,10 +29,23 @@ public:

GLuint loadShader(const std::string& code, const GLenum& shaderType);
ShaderUniform getUniform(const std::string& name);

protected:
GLuint mShaderID;

};


//uniform setter
class ShaderUniform {
public:
ShaderUniform(const GLint& uniformID);
void set(const glm::mat4& data);
void set(const glm::vec4& data);
void set(const glm::vec3& data);
protected:
GLint mUniformID;
};

}

+ 4
- 4
include/window.h View File

@@ -22,23 +22,23 @@
#include <utility>
#include <glm/glm.hpp>

#include "graphics/rendertarget.h"

namespace vtk {

class Window {
class Window : public RenderTarget {
public:
void activate();
bool create();
void setTitle(const std::string& title);
void setResolution(const int& x, const int& y);
std::pair<int, int> getResolution();
float getAspect();
void setFOV(const float& angle);
glm::mat4 getProjectionMatrix();
SDL_Window* getWindow();

protected:
int resX;
int resY;
float fov;

std::string title;

+ 11
- 0
src/graphics/drawable.cpp View File

@@ -1,3 +1,14 @@
#include "graphics/drawable.h"

namespace vtk {


void Drawable::setShader(std::shared_ptr<Shader> shader) {
mLinkedShader = shader;
}

std::shared_ptr<Shader> Drawable::getShader() {
return mLinkedShader;
}

}

+ 17
- 0
src/graphics/rendertarget.cpp View File

@@ -0,0 +1,17 @@
#include "graphics/rendertarget.h"

namespace vtk {

double RenderTarget::getAspect() {
return (double)mWidth / (double)mHeight;
}

int RenderTarget::width() {
return mWidth;
}

int RenderTarget::height() {
return mHeight;
}

}

+ 21
- 0
src/graphics/rendertask.cpp View File

@@ -0,0 +1,21 @@
#include "graphics/rendertask.h"
#include "graphics/camera.h"
#include "graphics/drawable.h"
#include "graphics/rendertarget.h"

namespace vtk {

RenderTask::RenderTask(Drawable& drawable, Camera& camera, RenderTarget& renderTarget) :
mDrawable(drawable),
mCamera(camera),
mRenderTarget(renderTarget)
{

}

void RenderTask::draw() {
mRenderTarget.activate();
mDrawable.draw(*this);
}

}

+ 28
- 3
src/graphics/shader.cpp View File

@@ -1,13 +1,14 @@
/*
* Shader source
*/
#include <glm/gtc/type_ptr.hpp>

#include "graphics/shader.h"
#include "graphics/glstate.h"
#include "spdlog/spdlog.h"
#include "fileutils.h"
#include "loadShader.h"


namespace vtk {

Shader::Shader() {
@@ -19,8 +20,8 @@ bool Shader::activate() {
spdlog::get("general")->warn("Attempted to load unloaded shader");
return false;
}
//gls::setShader(mShaderID);
glUseProgram(mShaderID);
gls::setShader(mShaderID);
//glUseProgram(mShaderID);
return true;
}

@@ -68,6 +69,8 @@ bool Shader::loadShaderFiles(const std::string& vertShader,
glDeleteShader(vShaderID);
glDeleteShader(fShaderID);

return true;
}

GLuint Shader::loadShader(const std::string& code, const GLenum& shaderType) {
@@ -89,5 +92,27 @@ GLuint Shader::loadShader(const std::string& code, const GLenum& shaderType) {
return id;
}


ShaderUniform Shader::getUniform(const std::string& name) {
return(ShaderUniform(glGetAttribLocation(mShaderID, name.c_str())));
}

ShaderUniform::ShaderUniform(const GLint& uniformID) :
mUniformID(uniformID)
{

}
void ShaderUniform::set(const glm::mat4& data) {
glUniformMatrix4fv(mUniformID, 1, GL_FALSE, glm::value_ptr(data));
}

void ShaderUniform::set(const glm::vec4& data) {
glUniform4fv(mUniformID, 1, glm::value_ptr(data));
}

void ShaderUniform::set(const glm::vec3& data) {
glUniform3fv(mUniformID, 1, glm::value_ptr(data));
}

//namespace vtk
}

+ 1
- 2
src/graphics/skybox.cpp View File

@@ -82,8 +82,7 @@ namespace vtk {
auto viewMat = camera.getAngleMatrix();
auto pos = camera.getPosition();
//gls::setShader(mShader);
glUseProgram(mShader);
gls::setShader(mShader);
glUniformMatrix4fv(mViewMat, 1, GL_FALSE, glm::value_ptr(viewMat));
glUniformMatrix4fv(mProjMat, 1, GL_FALSE, glm::value_ptr(proj));

+ 3
- 3
src/scenes/testscene.cpp View File

@@ -21,6 +21,7 @@
#include "tileset.h"
#include "world.h"
#include "mathplus.h"
#include "graphics/glstate.h"


#include <GL/glew.h>
@@ -129,7 +130,7 @@ void TestScene::init() {
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
std::cout << "\rGenerating chunks (" << chunkCount << "/" << 8*8*8 << ")" << std::flush;
world.generateChunk(i,j,k);
world.generateChunk(i-1,j,k);
chunkCount++;
}
}
@@ -186,12 +187,11 @@ void TestScene::update(const float& dTime) {
void TestScene::draw() {
mSkybox.draw(camera, linkedGame->window.getProjectionMatrix());

glUseProgram(shaders);
gls::setShader(shaders);
glUniformMatrix4fv(viewMatUni, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
world.draw();
glDisable(GL_DEPTH_TEST);
//glUseProgram(mCursorShader.mShaderID);
mCursorShader.activate();
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);

+ 10
- 7
src/window.cpp View File

@@ -19,13 +19,19 @@
#include "window.h"

#include <glm/gtc/matrix_transform.hpp>
#include <GL/glew.h>
#include <GL/gl.h>

namespace vtk {

void Window::activate() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

bool Window::create() {
window = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
resX, resY, SDL_WINDOW_OPENGL);
mWidth, mHeight, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
return true;

@@ -36,8 +42,8 @@ void Window::setTitle(const std::string& title) {
}

void Window::setResolution(const int& x, const int& y) {
resX = x;
resY = y;
mWidth = x;
mHeight = y;
}

std::pair<int, int> Window::getResolution() {
@@ -46,9 +52,6 @@ std::pair<int, int> Window::getResolution() {
return resolution;
}

float Window::getAspect() {
return (float)resX / (float)resY;
}

SDL_Window* Window::getWindow() {
return window;
@@ -59,7 +62,7 @@ void Window::setFOV(const float& angle) {
}

glm::mat4 Window::getProjectionMatrix() {
return glm::perspective(glm::radians(fov), getAspect(), 0.1f, 500.0f);
return glm::perspective(glm::radians(fov), (float)getAspect(), 0.1f, 500.0f);
}

}

Loading…
Cancel
Save