So I have moved my rendering stuff into a class with the vertices, induces, shader compiler and texture class within a class so that when I get it working all I have to do is modify some of the values within the class and not have all of this stuff in my main function. So I have tried to get what I have now to scroll by increasing an X offset. It renders ok but doesn’t scroll.
This is my code:
[type or paste code here](//main.cpp)//main.cpp
#include <vector>
#include <iostream>
#define GLEW_STATIC
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "ShaderProgram.h"
#include "Texture2D.h"
#include "Background.h"
#include "TimerHolder.h"
using namespace std;
GLFWwindow* pWindow;
bool fullscreen = false;
bool gWire;
void onkey(GLFWwindow* window, int key, int scancode, int action, int mode);
bool initOpengl();
int main()
{
if (!initOpengl()) {
cout << "Initialization failed" << endl;
return -1;
}
Background back;
back.pWindow = pWindow;
float adv = 0;
while (!glfwWindowShouldClose(pWindow)) {
glfwPollEvents();
adv += 0.01;
if (adv >= 1) adv -= 1;
back.offsetX = adv;
back.update();
back.render();
}
glfwTerminate();
return 0;
}
bool initOpengl() {
if (!glfwInit()) {
cout << "GLFW Initialization failed" << endl;
false;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
if (fullscreen) {
GLFWmonitor* mon = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(mon);
if (mode != NULL) {
pWindow = glfwCreateWindow(mode->width, mode->height, "Hello", NULL, NULL);
}
}
else {
pWindow = glfwCreateWindow(1024, 768, "Hello", NULL, NULL);
}
if (pWindow == NULL) {
cout << "Could not make window" << endl;
glfwTerminate();
return false;
}
glfwMakeContextCurrent(pWindow);
glfwSetKeyCallback(pWindow, onkey);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
cout << "GLEW Initialization failed" << endl;
return false;
}
glClearColor(.5, .5, 1, 1);
return true;
}
void onkey(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE);
}
//Background.h
#pragma once
#define GLEW_STATIC
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include <vector>
#include "ShaderProgram.h"
#include "Texture2D.h"
using namespace std;
class Background
{
public:
Background();
~Background();
void render();
void update();
GLFWwindow* pWindow;
short tile[12][10];
float offsetX;
float offsetY;
bool darkness;
private:
ShaderProgram shader;
ShaderProgram dark;
Texture2D tex;
GLuint vbo, ibo, vao;
vector<GLuint> induces;
vector<GLfloat> vertices;
};
//Background.ccp
#include "Background.h"
Background::Background() {
darkness = false;
GLuint indus[] = {
0, 1, 2, 0, 2, 3 };
GLfloat verts[] = {
0, 0, 0, 0, 1,
1, 0, 0, 1, 1,
1, 1, 0, 1, 0,
0, 1, 0, 0, 0
};
shader.loadShaders("Shaders/basic.vert", "Shaders/basic.frag");
dark.loadShaders("Shaders/dark.vert", "Shaders/dark.frag");
tex.loadTexture("Textures/tiles.png", true, true);
for (int i = 0; i < 120; i++) {
for (int j = 0; j < 6; j++) induces.push_back(i * 4 + indus[j]);
}
int muld;
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 12; x++) {
for (int i = 0; i < 4; i++) {
muld = i * 5;
vertices.push_back(-1.1818181818181818 + verts[muld] / 5.5 + (GLfloat)x / 5.5 + offsetX / 5.5);
vertices.push_back((GLfloat)1.222222222222 - (verts[muld + 1] / 4.5 + (GLfloat)y / 4.5 + offsetY / 4.5));
vertices.push_back((GLfloat)0);
vertices.push_back(verts[muld + 3] / (GLfloat)85);
vertices.push_back(1 - verts[muld + 4] / (GLfloat)85);
}
}
}
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_DYNAMIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(0);
//tex coords
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * induces.size(), &induces[0], GL_DYNAMIC_DRAW);
}
Background::~Background() {
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
}
void Background::render() {
glClear(GL_COLOR_BUFFER_BIT);
tex.bind();
if (darkness) {
dark.use();
}
else {
glUniform1i(glGetUniformLocation(shader.getProgram(), "MyTexture"), 0);
shader.use();
}
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 720, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
tex.unbind();
glfwSwapBuffers(pWindow);
}
void Background::update() {
GLfloat verts[] = {
0, 0, 0, 0, 1,
1, 0, 0, 1, 1,
1, 1, 0, 1, 0,
0, 1, 0, 0, 0
};
int muld;
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 12; x++) {
for (int i = 0; i < 4; i++) {
muld = i * 5;
vertices.push_back(-1.1818181818181818 + verts[muld] / 5.5 + (GLfloat)x / 5.5 + offsetX / 5.5);
vertices.push_back((GLfloat)1.222222222222 - (verts[muld + 1] / 4.5 + (GLfloat)y / 4.5 + offsetY / 4.5));
vertices.push_back((GLfloat)0);
vertices.push_back(verts[muld + 3] / (GLfloat)85);
vertices.push_back(1 - verts[muld + 4] / (GLfloat)85);
}
}
}
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), &vertices[0], GL_DYNAMIC_DRAW);
glBindVertexArray(vao);
//position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(0);
//tex coords
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * induces.size(), &induces[0], GL_DYNAMIC_DRAW);
}