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

Started on implementing minimap using FBO

parent ebca63d4
......@@ -37,7 +37,9 @@ set( INCLUDE
include/Utils.hpp
include/Snow.hpp
include/Text.hpp
include/Minimap.hpp
include/GLErrorHandler.h
include/stb_image.h
)
set( SRCS
......@@ -48,7 +50,9 @@ set( SRCS
src/Character.cpp
src/Snow.cpp
src/Text.cpp
src/Minimap.cpp
src/GLErrorHandler.cpp
src/stb_image.cpp
app/main.cpp
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.15.2, 2022-04-07T23:31:44. -->
<!-- Written by QtCreator 4.15.2, 2022-04-30T16:14:05. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
#include <windows.h>
#include "GL/glew.h"
#include <GL/freeglut.h>
#include <stdlib.h>
#include <stdio.h>
......@@ -22,6 +23,8 @@ float moveX, moveY;
void init()
{
glewInit();
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
......
#pragma once
#include <iostream>
#include <GL/glew.h>
#define ASSERT(x) if (!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
void GLClearError();
bool GLLogCall(const char* function, const char* file, int line);
#pragma once
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
......@@ -10,6 +11,7 @@
#include "Camera.hpp"
#include "Snow.hpp"
#include "Text.hpp"
#include "Minimap.hpp"
class GameManager : public SceneObject
{
......@@ -31,5 +33,6 @@ class GameManager : public SceneObject
std::shared_ptr<Camera> cam_;
std::shared_ptr<Snow> snow_;
std::shared_ptr<Text> text_;
std::shared_ptr<Minimap> minimap_;
};
#pragma once
#include <windows.h>
#include <iostream>
#include "SceneObject.hpp"
#include <GL/gl.h>
class Minimap : public SceneObject {
public:
Minimap();
~Minimap();
protected:
void privateInit();
void privateRender();
void privateUpdate();
void CheckFrameBufferStatus();
private:
unsigned int g_fbo; // frame buffer object
unsigned int g_pbo; // pixel buffer object
unsigned int g_texture; // texture object
int g_iTexWidth = 512; // texture width
int g_iTexHeight = 512; // texture height
};
......@@ -45,8 +45,8 @@ void Character::privateInit()
// buff = (char*)glGetString(GL_RENDERER);
// std::cout<< buff;
char *buff = (char*)glGetString(GL_EXTENSIONS);
std::cout<< buff;
// char *buff = (char*)glGetString(GL_EXTENSIONS);
// std::cout<< buff;
}
void Character::privateRender()
......@@ -162,33 +162,38 @@ void Character::setUpVertices()
void Character::setUpNormals()
{
// Normals
for (size_t current_offset = 0; current_offset < vertexArray_.size(); current_offset += 4) {
for (size_t face_offset = 0; face_offset < vertexArray_.size(); face_offset += 4) {
// Bottom left
normalArray_.push_back(glm::cross(
vertexArray_[BOTTOMRIGHT + current_offset] - vertexArray_[BOTTOMLEFT + current_offset],
vertexArray_[TOPLEFT + current_offset] - vertexArray_[BOTTOMLEFT + current_offset]
));
normalArray_[BOTTOMLEFT + current_offset] =
normalArray_[BOTTOMLEFT + current_offset] / glm::length(normalArray_[BOTTOMLEFT + current_offset]);
vertexArray_[BOTTOMRIGHT + face_offset] - vertexArray_[BOTTOMLEFT + face_offset],
vertexArray_[TOPLEFT + face_offset] - vertexArray_[BOTTOMLEFT + face_offset]));
normalArray_[BOTTOMLEFT + face_offset] =
normalArray_[BOTTOMLEFT + face_offset] / glm::length(normalArray_[BOTTOMLEFT + face_offset]);
// Bottom right
normalArray_.push_back(glm::cross(
vertexArray_[TOPRIGHT + current_offset] - vertexArray_[BOTTOMRIGHT + current_offset],
vertexArray_[BOTTOMLEFT + current_offset] - vertexArray_[BOTTOMRIGHT + current_offset]));
normalArray_[BOTTOMRIGHT + current_offset] =
normalArray_[BOTTOMRIGHT + current_offset] / glm::length(normalArray_[BOTTOMRIGHT + current_offset]);
vertexArray_[TOPRIGHT + face_offset] - vertexArray_[BOTTOMRIGHT + face_offset],
vertexArray_[BOTTOMLEFT + face_offset] - vertexArray_[BOTTOMRIGHT + face_offset]));
normalArray_[BOTTOMRIGHT + face_offset] =
normalArray_[BOTTOMRIGHT + face_offset] / glm::length(normalArray_[BOTTOMRIGHT + face_offset]);
// Top right
normalArray_.push_back(glm::cross(
vertexArray_[TOPLEFT + current_offset] - vertexArray_[TOPRIGHT + current_offset],
vertexArray_[BOTTOMRIGHT + current_offset] - vertexArray_[TOPRIGHT + current_offset]
));
normalArray_[TOPRIGHT + current_offset] =
normalArray_[TOPRIGHT + current_offset] / glm::length(normalArray_[TOPRIGHT + current_offset]);
vertexArray_[TOPLEFT + face_offset] - vertexArray_[TOPRIGHT + face_offset],
vertexArray_[BOTTOMRIGHT + face_offset] - vertexArray_[TOPRIGHT + face_offset]));
normalArray_[TOPRIGHT + face_offset] =
normalArray_[TOPRIGHT + face_offset] / glm::length(normalArray_[TOPRIGHT + face_offset]);
// Top left
normalArray_.push_back(glm::cross(
vertexArray_[BOTTOMLEFT + current_offset] - vertexArray_[TOPLEFT + current_offset],
vertexArray_[TOPRIGHT + current_offset] - vertexArray_[TOPLEFT + current_offset]
));
normalArray_[TOPLEFT + current_offset] =
normalArray_[TOPLEFT + current_offset] / glm::length(normalArray_[TOPLEFT + current_offset]);
vertexArray_[BOTTOMLEFT + face_offset] - vertexArray_[TOPLEFT + face_offset],
vertexArray_[TOPRIGHT + face_offset] - vertexArray_[TOPLEFT + face_offset]));
normalArray_[TOPLEFT + face_offset] =
normalArray_[TOPLEFT + face_offset] / glm::length(normalArray_[TOPLEFT + face_offset]);
}
// Shared normals
......@@ -360,9 +365,9 @@ void Character::setUpHBTextures()
void Character::privateUpdate()
{
// auto translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.2f * speed_, 0.0f, 0.0f));
// auto rotation = glm::rotate(matrix_, 0.01f, glm::vec3(0.0f,1.0f,0.0f));
// matrix_ = translation * rotation;
auto translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.2f * speed_, 0.0f, 0.0f));
auto rotation = glm::rotate(matrix_, 0.01f, glm::vec3(0.0f,1.0f,0.0f));
matrix_ = translation * rotation;
}
void Character::increaseSpeed()
......
#include "../include/GLErrorHandler.h"
void GLClearError() {
while (glGetError() != GL_NO_ERROR) {
}
};
bool GLLogCall(const char* function, const char* file, int line) {
while (GLenum error = glGetError()) {
std::cout << "OpenGL error: " << std::hex << error << "\nFunction: " << function << "\nFile: " << file << "\nLine: " << line << std::endl;
return false;
}
return true;
};
......@@ -12,12 +12,13 @@ void GameManager::privateInit()
{
// Set default OpenGL states
glEnable(GL_CULL_FACE);
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START, 1.0f);
glFogf(GL_FOG_END, 500.0f);
GLfloat fog_color[4] = {0.5, 0.5, 0.5, 1.0};
glFogfv(GL_FOG_COLOR, fog_color);
// glEnable(GL_FOG);
// glFogi(GL_FOG_MODE, GL_LINEAR);
// glFogf(GL_FOG_START, 1.0f);
// glFogf(GL_FOG_END, 500.0f);
// GLfloat fog_color[4] = {0.5, 0.5, 0.5, 1.0};
// glFogfv(GL_FOG_COLOR, fog_color);
// Adding the camera to the scene
cam_.reset(new Camera());
......@@ -34,6 +35,9 @@ void GameManager::privateInit()
text_.reset(new Text());
this->addSubObject(text_);
minimap_.reset(new Minimap());
this->addSubObject(minimap_);
}
void GameManager::privateRender()
......
#include <windows.h>
#include "GL/glew.h"
#include <GL/glut.h>
#include <GL/gl.h>
#include "../include/Minimap.hpp"
#include "../include/GLErrorHandler.h"
//GLuint g_fbo; // frame buffer object
//GLuint g_pbo; // pixel buffer object
//GLuint g_texture; // texture object
//int g_iTexWidth = 512; // texture width
//int g_iTexHeight = 512; // texture height
Minimap::Minimap() {
}
Minimap::~Minimap() {
glDeleteFramebuffersEXT(1, &g_fbo);
glDeleteBuffers(1, &g_pbo);
}
void Minimap::privateInit() {
// Create FBO
glGenFramebuffers(1, &g_fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_fbo);
// Initialize Texture
glGenTextures(1, &g_texture);
glBindTexture(GL_TEXTURE_2D, g_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, g_iTexWidth, g_iTexHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Attach Texture to Framebuffer Color Buffer
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_texture, 0);
}
void Minimap::privateRender() {
// glMatrixMode(GL_MODELVIEW);
// render to texture
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_fbo);
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glLoadIdentity();
// place camera/eye
// glTranslatef(0.0f, 1000.0f, 100.0f);
// glRotatef(0.0f,1.0,0.0,0.0);
// glRotatef(-90.0f,0.0,1.0,0.0);
// Save State
float fPrevColor[4];
int iPrevPolygonMode[2];
glGetIntegerv(GL_POLYGON_MODE, iPrevPolygonMode);
glGetFloatv(GL_CURRENT_COLOR, fPrevColor);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.683f, 0.113f, 0.234f);
// glutSolidTorus(0.2f, 2.0f, 10, 100);
//imdebugTexImage(GL_TEXTURE_2D, g_texture, GL_RGBA);
glColor4fv(fPrevColor);
glPolygonMode(GL_FRONT, iPrevPolygonMode[0]);
glPolygonMode(GL_BACK, iPrevPolygonMode[1]);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
// render to screen
// glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glLoadIdentity();
// glTranslatef(0.0f, 0.0f, -2.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-100.0f, -50.0f, 100.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-50.0f, -50.0f, 100.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-50.0f, 0.0f, 100.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-100.0f, 0.0f, 100.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
// glutSwapBuffers();
}
void Minimap::privateUpdate() {
}
void Minimap::CheckFrameBufferStatus() {
GLenum eStatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
switch (eStatus)
{
// everything is OK
case GL_FRAMEBUFFER_COMPLETE_EXT:
break;
// Unsupported Format
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
std::cout << "Error: Unsupported Format" << std::endl;
break;
// Other Error
default:
assert(0);
break;
}
}
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