Commit d90a5194 authored by Håkon Berg Borhaug's avatar Håkon Berg Borhaug
Browse files

Basic shader code works

parent ac2abb2e
......@@ -44,6 +44,7 @@ set( INCLUDE
include/VertexBuffer.h
include/VertexBufferLayout.h
include/Texture.h
include/Projection.h
include/GLErrorHandler.h
include/stb_image.h
......
......@@ -9,6 +9,7 @@
#include "../include/FpsCounter.hpp"
#include "../include/GameManager.hpp"
#include "../../glm-master/glm/glm.hpp"
#include "../include/Projection.h"
#include <iostream>
......@@ -181,7 +182,7 @@ void reshape(int w, int h)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, float(w)/float(h) ,1.0f, 3000.0f);
gluPerspective(proj::PoV, proj::aspectRatio, proj::nearClip, proj::farClip);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
......@@ -191,7 +192,7 @@ int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutInitWindowSize(900, 700);
glutInitWindowSize(proj::width, proj::height);
glutInitWindowPosition(200, 10);
window = glutCreateWindow("Game application");
init();
......
......@@ -15,8 +15,8 @@
#include "../../glm-master/glm/glm.hpp"
// 4 points with 3D position and 2D texture position
#define POS_ARRAY_SIZE 4 * 3 //4 * 4 * (3 + 2)
#define IND_ARRAY_SIZE 4 //4 * 4
#define POS_ARRAY_SIZE 4 * 4 * 3 //4 * 4 * (3 + 2)
#define IND_ARRAY_SIZE 4 * 4 //4 * 4
class Landscape : public SceneObject
{
......@@ -38,40 +38,40 @@ private:
std::vector< glm::vec3 > textureArray_;
float positions_[POS_ARRAY_SIZE] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 3.0f,
0.5f, 0.5f, 3.0f,
-0.5f, 0.5f, 0.0f
// // Left plane
// -100.0f, -50.0f, 100.0f, 0.0f, 0.0f,
// 100.0f, -50.0f, 100.0f, 1.0f, 0.0f,
// 100.0f, -50.0f, -100.0f, 1.0f, 1.0f,
// -100.0f, -50.0f, -100.0f, 0.0f, 1.0f,
// -0.5f, -0.5f, 0.0f,
// 0.5f, -0.5f, 3.0f,
// 0.5f, 0.5f, 3.0f,
// -0.5f, 0.5f, 0.0f
// Left plane
-100.0f, -50.0f, 100.0f, //0.0f, 0.0f,
100.0f, -50.0f, 100.0f, //1.0f, 0.0f,
100.0f, -50.0f, -100.0f, //1.0f, 1.0f,
-100.0f, -50.0f, -100.0f, //0.0f, 1.0f,
// // Middle plane
// 0.0f, -50.0f, 50.0f,
// 100.0f, -50.0f, 50.0f,
// 100.0f, -50.0f, -50.0f,
// 0.0f, -50.0f, -50.0f,
// Middle plane
0.0f, -50.0f, 50.0f,
100.0f, -50.0f, 50.0f,
100.0f, -50.0f, -50.0f,
0.0f, -50.0f, -50.0f,
// // Right plane
// 100.0f, -50.0f, 50.0f,
// 200.0f, -50.0f, 50.0f,
// 200.0f, -50.0f, -50.0f,
// 100.0f, -50.0f, -50.0f,
// Right plane
100.0f, -50.0f, 50.0f,
200.0f, -50.0f, 50.0f,
200.0f, -50.0f, -50.0f,
100.0f, -50.0f, -50.0f,
// // Background
// -1000.0f, -1000.0f, -500.0f,
// 1000.0f, -1000.0f, -500.0f,
// 1000.0f, 1000.0f, -500.0f,
// -1000.0f, 1000.0f, -500.0f
// Background
-1000.0f, -1000.0f, -500.0f,
1000.0f, -1000.0f, -500.0f,
1000.0f, 1000.0f, -500.0f,
-1000.0f, 1000.0f, -500.0f
};
unsigned int indices_[IND_ARRAY_SIZE] = {
0, 1, 2, 3
// 4, 5, 6, 7,
// 8, 9, 10, 11,
// 12, 13, 14, 15
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15
};
VertexArray vao;
......
#pragma once
namespace proj {
const int width = 1280;
const int height = 720;
const float PoV = 60.0f;
const float aspectRatio = float(width) / float(height);
const float nearClip = 1.0f;
const float farClip = 3000.0f;
}
......@@ -69,6 +69,9 @@ class SceneObject
// Relative to the object's parent. Defaults to the identity matrix.
glm::mat4 matrix_;
// View matrix
glm::mat4 viewMatrix_;
private:
// List of all SceneObjects that belong to the current object.
std::vector<std::shared_ptr<SceneObject> > children_;
......
......@@ -2,6 +2,8 @@
#include "../../glm-master/glm/glm.hpp"
#include "../../glm-master/glm/gtc/type_ptr.hpp"
#include "../include/Projection.h"
Landscape::Landscape()
{
}
......@@ -49,14 +51,11 @@ void Landscape::privateRender()
shader.Bind();
vao.Bind();
ibo.Bind();
// shader.SetUniformMat4f("u_MVP", matrix_);
glm::mat4 trans = glm::mat4(1.0f);
// trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
// trans = glm::rotate(trans, 45.0f, glm::vec3(0.0f, 0.0f, 1.0f));
matrix_ = matrix_ * trans;
shader.SetUniformMat4f("u_MVP", matrix_);
glm::mat4 projectionMatrix = glm::perspective(glm::radians(proj::PoV), proj::aspectRatio, proj::nearClip, proj::farClip);
glm::mat4 mvp = projectionMatrix * viewMatrix_ * matrix_;
shader.SetUniformMat4f("u_MVP", mvp);
GLCall(glDrawElements(GL_QUADS, ibo.GetCount(), GL_UNSIGNED_INT, nullptr));
......
......@@ -32,8 +32,11 @@ void SceneObject::update(double fps)
this->fps_ = fps;
this->privateUpdate();
for(std::vector<std::shared_ptr<SceneObject> >::iterator it = children_.begin();
it != children_.end(); it++)
it != children_.end(); it++) {
(*it)->viewMatrix_ = matrix_;
(*it)->update(fps);
}
}
void SceneObject::init()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment