Add perspective view
This commit is contained in:
		
							
								
								
									
										21
									
								
								include/opengl-playground/globalcanvassettings.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								include/opengl-playground/globalcanvassettings.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
#ifndef GLOBALCANVASSETTINGS_H
 | 
			
		||||
#define GLOBALCANVASSETTINGS_H
 | 
			
		||||
 | 
			
		||||
#include <glm/glm.hpp>
 | 
			
		||||
 | 
			
		||||
class GlobalCanvasSettings
 | 
			
		||||
{
 | 
			
		||||
	public:
 | 
			
		||||
		static void setProjectionMatrix(const glm::mat4 &proj);
 | 
			
		||||
		static void setViewMatrix(const glm::mat4 &view);
 | 
			
		||||
		static const glm::mat4 &getViewMatrix();
 | 
			
		||||
		static const glm::mat4 &getProjectionMatrix();
 | 
			
		||||
		static const glm::mat4 &getPVMatrix();
 | 
			
		||||
	private:
 | 
			
		||||
		static void calculatePVMatrix();
 | 
			
		||||
		static glm::mat4 m_proj;
 | 
			
		||||
		static glm::mat4 m_view;
 | 
			
		||||
		static glm::mat4 m_proj_view;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // GLOBALCANVASSETTINGS_H
 | 
			
		||||
@@ -9,21 +9,19 @@ class OpenGlShaderProgram
 | 
			
		||||
{
 | 
			
		||||
	public:
 | 
			
		||||
		OpenGlShaderProgram(std::string vertex_file, std::string geometry_file, std::string fragment_file);
 | 
			
		||||
		OpenGlShaderProgram(const OpenGlShaderProgram &) = delete;
 | 
			
		||||
		OpenGlShaderProgram(const OpenGlShaderProgram &&) = delete;
 | 
			
		||||
		~OpenGlShaderProgram();
 | 
			
		||||
		void setProjectionView(const glm::mat4 &projection, const glm::mat4 &view);
 | 
			
		||||
		glm::mat4 getProjection();
 | 
			
		||||
		glm::mat4 getView();
 | 
			
		||||
		int compile();
 | 
			
		||||
		void unload();
 | 
			
		||||
		int use();
 | 
			
		||||
		int getUniformLoc(const std::string &uniform);
 | 
			
		||||
		void setUniformFloat(const std::string &uniform, float value);
 | 
			
		||||
		void setUniformInt(const std::string &uniform, int value);
 | 
			
		||||
		void setUniformVec2(const std::string &uniform, float *vector);
 | 
			
		||||
		void setUniformMat4(const std::string &uniform, const glm::mat4 &matrix);
 | 
			
		||||
		void setUniformVec4(const std::string &uniform, const glm::vec4 &vector);
 | 
			
		||||
	private:
 | 
			
		||||
		glm::mat4 proj;
 | 
			
		||||
		glm::mat4 view;
 | 
			
		||||
		glm::mat4 proj_view;
 | 
			
		||||
		bool compiled;
 | 
			
		||||
		GLuint shader_program;
 | 
			
		||||
		std::string vertex_file;
 | 
			
		||||
 
 | 
			
		||||
@@ -7,9 +7,12 @@ class OpenGlTexture
 | 
			
		||||
{
 | 
			
		||||
	public:
 | 
			
		||||
		OpenGlTexture();
 | 
			
		||||
		OpenGlTexture(const OpenGlTexture &tex);
 | 
			
		||||
		OpenGlTexture(const OpenGlTexture &&tex);
 | 
			
		||||
		OpenGlTexture(const OpenGlTexture &tex) = delete;
 | 
			
		||||
		OpenGlTexture(const OpenGlTexture &&tex) = delete;
 | 
			
		||||
		~OpenGlTexture();
 | 
			
		||||
		void setRGBDataFromBytes(unsigned int width, unsigned int height, char *buffer);
 | 
			
		||||
		void use(unsigned int slot);
 | 
			
		||||
		void use();
 | 
			
		||||
 | 
			
		||||
	protected:
 | 
			
		||||
		GLuint texture;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,13 +2,15 @@
 | 
			
		||||
#define TEXTURED_RECTANGLE_H
 | 
			
		||||
 | 
			
		||||
#include <opengl-playground/openglgraphics.hpp>
 | 
			
		||||
#include <opengl-playground/opengltexture.hpp>
 | 
			
		||||
#include <epoxy/gl.h>
 | 
			
		||||
#include <memory>
 | 
			
		||||
 | 
			
		||||
class TexturedRectangle : OpenGlGraphics
 | 
			
		||||
class TexturedRectangle : public OpenGlGraphics
 | 
			
		||||
{
 | 
			
		||||
	public:
 | 
			
		||||
		TexturedRectangle(float x0, float y0, float x1, float y1, std::shared_ptr<OpenGlShaderProgram> shaderprog);
 | 
			
		||||
		TexturedRectangle(float x0, float y0, float x1, float y1, std::shared_ptr<OpenGlShaderProgram> &shaderprog,
 | 
			
		||||
				  std::shared_ptr<OpenGlTexture> &texture);
 | 
			
		||||
		void realize();
 | 
			
		||||
		void render();
 | 
			
		||||
		void setZoom(float zoom);
 | 
			
		||||
@@ -16,8 +18,11 @@ class TexturedRectangle : OpenGlGraphics
 | 
			
		||||
		void setOffset(float x_off, float y_off);
 | 
			
		||||
		float getOffsetX();
 | 
			
		||||
		float getOffsetY();
 | 
			
		||||
		void setBaseColor(const glm::vec4 &color);
 | 
			
		||||
	private:
 | 
			
		||||
		std::shared_ptr<OpenGlTexture> m_texture;
 | 
			
		||||
		void calculateModelMatrix();
 | 
			
		||||
		glm::vec4 base_color;
 | 
			
		||||
		float pos1[2];
 | 
			
		||||
		float pos2[2];
 | 
			
		||||
		float x_offset;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,13 @@
 | 
			
		||||
#version 330 core
 | 
			
		||||
 | 
			
		||||
out vec4 fragmentColor;
 | 
			
		||||
in vec2 textureCoord;
 | 
			
		||||
uniform vec4 base_color;
 | 
			
		||||
uniform sampler2D uni_texture;
 | 
			
		||||
 | 
			
		||||
void main()
 | 
			
		||||
{
 | 
			
		||||
	fragmentColor = vec4(1.0, 0.0, 0.0, 0.0);
 | 
			
		||||
	vec4 temp_color = texture(uni_texture, textureCoord);
 | 
			
		||||
	fragmentColor = temp_color * (1 - base_color.a) + base_color * base_color.a;
 | 
			
		||||
	//fragmentColor = vec4(1.0, 0.0, 0.0, 0.0);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,15 @@
 | 
			
		||||
#version 330 core
 | 
			
		||||
 | 
			
		||||
layout (location = 0) in vec2 inputVertex;
 | 
			
		||||
layout (location = 1) in vec2 textureInputVertex;
 | 
			
		||||
 | 
			
		||||
out vec2 textureCoord;
 | 
			
		||||
 | 
			
		||||
uniform mat4 model_matrix;
 | 
			
		||||
uniform mat4 projection_view_matrix;
 | 
			
		||||
 | 
			
		||||
void main()
 | 
			
		||||
{
 | 
			
		||||
	textureCoord = textureInputVertex;
 | 
			
		||||
	gl_Position = projection_view_matrix * model_matrix * vec4(inputVertex.x , inputVertex.y, 0.0, 1.0);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										37
									
								
								src/globalcanvassettings.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/globalcanvassettings.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
#include <opengl-playground/globalcanvassettings.hpp>
 | 
			
		||||
 | 
			
		||||
glm::mat4 GlobalCanvasSettings::m_proj = glm::mat4(1.0f);
 | 
			
		||||
glm::mat4 GlobalCanvasSettings::m_view = glm::mat4(1.0f);
 | 
			
		||||
glm::mat4 GlobalCanvasSettings::m_proj_view = glm::mat4(1.0f);
 | 
			
		||||
 | 
			
		||||
void GlobalCanvasSettings::setProjectionMatrix(const glm::mat4 &proj)
 | 
			
		||||
{
 | 
			
		||||
	GlobalCanvasSettings::m_proj = proj;
 | 
			
		||||
	GlobalCanvasSettings::calculatePVMatrix();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void GlobalCanvasSettings::setViewMatrix(const glm::mat4 &view)
 | 
			
		||||
{
 | 
			
		||||
	GlobalCanvasSettings::m_view = view;
 | 
			
		||||
	GlobalCanvasSettings::calculatePVMatrix();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const glm::mat4 &GlobalCanvasSettings::getViewMatrix()
 | 
			
		||||
{
 | 
			
		||||
	return GlobalCanvasSettings::m_view;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const glm::mat4 &GlobalCanvasSettings::getProjectionMatrix()
 | 
			
		||||
{
 | 
			
		||||
	return GlobalCanvasSettings::m_proj;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const glm::mat4 &GlobalCanvasSettings::getPVMatrix()
 | 
			
		||||
{
 | 
			
		||||
	return GlobalCanvasSettings::m_proj_view;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void GlobalCanvasSettings::calculatePVMatrix()
 | 
			
		||||
{
 | 
			
		||||
	GlobalCanvasSettings::m_proj_view = GlobalCanvasSettings::m_proj * GlobalCanvasSettings::m_view;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										65
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -2,6 +2,7 @@
 | 
			
		||||
#include <opengl-playground/sdlmainwindow.hpp>
 | 
			
		||||
#include <opengl-playground/openglshader.hpp>
 | 
			
		||||
#include <opengl-playground/textured-rectangle.hpp>
 | 
			
		||||
#include <opengl-playground/globalcanvassettings.hpp>
 | 
			
		||||
 | 
			
		||||
#include <glm/glm.hpp>
 | 
			
		||||
#include <glm/gtc/matrix_transform.hpp>
 | 
			
		||||
@@ -13,21 +14,31 @@
 | 
			
		||||
#define WINDOW_WIDTH 1200
 | 
			
		||||
#define WINDOW_HEIGHT 800
 | 
			
		||||
 | 
			
		||||
unsigned char tex[4*10000] = {255};
 | 
			
		||||
 | 
			
		||||
int main(int argc, char **argv)
 | 
			
		||||
{
 | 
			
		||||
	(void)argc;
 | 
			
		||||
	(void)argv;
 | 
			
		||||
	SdlMainWindow *window;
 | 
			
		||||
	SDL_Event event;
 | 
			
		||||
	bool run = true;
 | 
			
		||||
	bool got_event = false;
 | 
			
		||||
	glm::mat4 view_matrix;
 | 
			
		||||
	glm::mat4 projection_matrix;
 | 
			
		||||
	bool show_demo_window = true;
 | 
			
		||||
	glm::vec4 color = glm::vec4(1.0f);
 | 
			
		||||
	float y_rotation = 0.0f;
 | 
			
		||||
	bool proj_ortho = true;
 | 
			
		||||
 | 
			
		||||
	float nav_point[2] = {0.0f, 0.0f};
 | 
			
		||||
	float zoom = 1.0;
 | 
			
		||||
 | 
			
		||||
	for (int i = 0; i < 10000; i++) {
 | 
			
		||||
		tex[i*4] = 0xff;
 | 
			
		||||
		tex[i*4+1] = 0x00;
 | 
			
		||||
		tex[i*4+2] = 0x00;
 | 
			
		||||
		tex[i*4+3] = 0xFF;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	window = new SdlMainWindow(WINDOW_HEIGHT, WINDOW_WIDTH);
 | 
			
		||||
 | 
			
		||||
	window->show();
 | 
			
		||||
@@ -43,10 +54,14 @@ int main(int argc, char **argv)
 | 
			
		||||
							      "shaders/textured-rectangle-fragment.glsl");
 | 
			
		||||
 | 
			
		||||
	projection_matrix = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f);
 | 
			
		||||
	textured_rect_prog->setProjectionView(projection_matrix, glm::mat4(1.0f));
 | 
			
		||||
	GlobalCanvasSettings::setProjectionMatrix(projection_matrix);
 | 
			
		||||
	GlobalCanvasSettings::setViewMatrix(glm::mat4(1.0f));
 | 
			
		||||
	textured_rect_prog->compile();
 | 
			
		||||
 | 
			
		||||
	TexturedRectangle rect = TexturedRectangle(-1, -1, 1, 1, textured_rect_prog);
 | 
			
		||||
	std::shared_ptr<OpenGlTexture> texture = std::make_shared<OpenGlTexture>();
 | 
			
		||||
	texture->setRGBDataFromBytes(100,100, (char*)tex);
 | 
			
		||||
 | 
			
		||||
	TexturedRectangle rect = TexturedRectangle(-3, -3, 3, 3, textured_rect_prog, texture);
 | 
			
		||||
	rect.realize();
 | 
			
		||||
 | 
			
		||||
	// Setup Dear ImGui context
 | 
			
		||||
@@ -70,6 +85,15 @@ int main(int argc, char **argv)
 | 
			
		||||
		SDL_Event event;
 | 
			
		||||
		while (SDL_PollEvent(&event))
 | 
			
		||||
		{
 | 
			
		||||
			if (event.type == SDL_MOUSEWHEEL) {
 | 
			
		||||
				if (event.wheel.y > 0) {
 | 
			
		||||
					zoom += 0.1;
 | 
			
		||||
				} else {
 | 
			
		||||
					zoom -= 0.1;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			ImGui_ImplSDL2_ProcessEvent(&event);
 | 
			
		||||
			if (event.type == SDL_QUIT)
 | 
			
		||||
				run = false;
 | 
			
		||||
@@ -86,14 +110,37 @@ int main(int argc, char **argv)
 | 
			
		||||
		ImGui::NewFrame();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		view_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(zoom, zoom, zoom));
 | 
			
		||||
		view_matrix = glm::translate(view_matrix, glm::vec3(-nav_point[0], -nav_point[1], 0.0f));
 | 
			
		||||
		view_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(zoom, zoom, 1.0));
 | 
			
		||||
		view_matrix = glm::translate(view_matrix, glm::vec3(-nav_point[0], -nav_point[1], -10.0f));
 | 
			
		||||
 | 
			
		||||
		GlobalCanvasSettings::setViewMatrix(view_matrix);
 | 
			
		||||
 | 
			
		||||
		textured_rect_prog->setProjectionView(projection_matrix, view_matrix);
 | 
			
		||||
		rect.render();
 | 
			
		||||
 | 
			
		||||
		ImGui::Begin("Rectangle");
 | 
			
		||||
		ImGui::Text("Change the color of the rectangle below");
 | 
			
		||||
		ImGui::NewLine();
 | 
			
		||||
		ImGui::SliderFloat4("Rectangle Color", &color.r, 0.0f, 1.0f, "%.3f", 1.0f);
 | 
			
		||||
		ImGui::Text("Rectangle Rotation:");
 | 
			
		||||
		ImGui::NewLine();
 | 
			
		||||
		ImGui::SliderAngle("Y Rotation", &y_rotation, -180.0f, +180.0f);
 | 
			
		||||
		static int e = 0;
 | 
			
		||||
		ImGui::RadioButton("Orthographic", &e, 0); ImGui::SameLine();
 | 
			
		||||
		ImGui::RadioButton("Perspective", &e, 1);
 | 
			
		||||
 | 
			
		||||
		ImGui::ShowDemoWindow(&show_demo_window);
 | 
			
		||||
		if(!e) {
 | 
			
		||||
			GlobalCanvasSettings::setProjectionMatrix(glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 20.0f));
 | 
			
		||||
		} else {
 | 
			
		||||
 | 
			
		||||
			GlobalCanvasSettings::setProjectionMatrix(glm::perspective(glm::radians(120.0f), (float)1200/(float)800, 1.0f, 30.0f));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		ImGui::End();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		glm::mat4 model_matrix = glm::rotate(glm::mat4(1.0f), y_rotation, glm::vec3(0.0f, 1.0f, 0.0f));
 | 
			
		||||
		rect.setModelMatrix(model_matrix);
 | 
			
		||||
		rect.setBaseColor(color);
 | 
			
		||||
		ImGui::Render();
 | 
			
		||||
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
#include <opengl-playground/openglshader.hpp>
 | 
			
		||||
#include <opengl-playground/globalcanvassettings.hpp>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
@@ -9,7 +10,6 @@ OpenGlShaderProgram::OpenGlShaderProgram(std::string vertex_file, std::string ge
 | 
			
		||||
	this->geometry_file = geometry_file;
 | 
			
		||||
	this->fragment_file = fragment_file;
 | 
			
		||||
	this->compiled = false;
 | 
			
		||||
	this->setProjectionView(glm::mat4(1.0f), glm::mat4(1.0f));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
OpenGlShaderProgram::~OpenGlShaderProgram()
 | 
			
		||||
@@ -17,23 +17,6 @@ OpenGlShaderProgram::~OpenGlShaderProgram()
 | 
			
		||||
	this->unload();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void OpenGlShaderProgram::setProjectionView(const glm::mat4 &projection, const glm::mat4 &view)
 | 
			
		||||
{
 | 
			
		||||
	this->proj = projection;
 | 
			
		||||
	this->view = view;
 | 
			
		||||
	this->proj_view = projection * view;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
glm::mat4 OpenGlShaderProgram::getProjection()
 | 
			
		||||
{
 | 
			
		||||
	return this->proj;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
glm::mat4 OpenGlShaderProgram::getView()
 | 
			
		||||
{
 | 
			
		||||
	return this->view;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string load_shader_program_from_file(std::string &file_name)
 | 
			
		||||
{
 | 
			
		||||
	std::ifstream ifs(file_name);
 | 
			
		||||
@@ -172,7 +155,7 @@ int OpenGlShaderProgram::use()
 | 
			
		||||
		return -1;
 | 
			
		||||
 | 
			
		||||
	glUseProgram(this->shader_program);
 | 
			
		||||
	this->setUniformMat4("projection_view_matrix", this->proj_view);
 | 
			
		||||
	this->setUniformMat4("projection_view_matrix", GlobalCanvasSettings::getPVMatrix());
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -185,6 +168,14 @@ void OpenGlShaderProgram::setUniformFloat(const std::string &uniform, float valu
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void OpenGlShaderProgram::setUniformInt(const std::string &uniform, int value)
 | 
			
		||||
{
 | 
			
		||||
	int loc = this->getUniformLoc(uniform);
 | 
			
		||||
	if (loc >= 0) {
 | 
			
		||||
		glUniform1i(loc, value);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void OpenGlShaderProgram::setUniformVec2(const std::string &uniform, float *vector)
 | 
			
		||||
{
 | 
			
		||||
	int loc = this->getUniformLoc(uniform);
 | 
			
		||||
@@ -201,6 +192,14 @@ void OpenGlShaderProgram::setUniformMat4(const std::string &uniform, const glm::
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void OpenGlShaderProgram::setUniformVec4(const std::string &uniform, const glm::vec4 &vector)
 | 
			
		||||
{
 | 
			
		||||
	int loc = this->getUniformLoc(uniform);
 | 
			
		||||
	if (loc >= 0) {
 | 
			
		||||
		glUniform4fv(loc, 1, &vector.x);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int OpenGlShaderProgram::getUniformLoc(const std::string &uniform)
 | 
			
		||||
{
 | 
			
		||||
	if (!this->compiled)
 | 
			
		||||
 
 | 
			
		||||
@@ -10,19 +10,25 @@ OpenGlTexture::OpenGlTexture()
 | 
			
		||||
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
OpenGlTexture::OpenGlTexture(const OpenGlTexture &tex)
 | 
			
		||||
{
 | 
			
		||||
	this->texture = tex.texture;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
OpenGlTexture::OpenGlTexture(const OpenGlTexture &&tex)
 | 
			
		||||
{
 | 
			
		||||
	this->texture = tex.texture;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
OpenGlTexture::~OpenGlTexture()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	glDeleteTextures(1, &this->texture);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void OpenGlTexture::setRGBDataFromBytes(unsigned int width, unsigned int height, char *buffer)
 | 
			
		||||
{
 | 
			
		||||
	this->use();
 | 
			
		||||
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
 | 
			
		||||
	glGenerateMipmap(GL_TEXTURE_2D);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void OpenGlTexture::use(unsigned int slot)
 | 
			
		||||
{
 | 
			
		||||
	glActiveTexture(GL_TEXTURE0 + slot);
 | 
			
		||||
	this->use();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void OpenGlTexture::use()
 | 
			
		||||
{
 | 
			
		||||
	glBindTexture(GL_TEXTURE_2D, this->texture);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
#include <opengl-playground/sdlmainwindow.hpp>
 | 
			
		||||
#include <epoxy/gl.h>
 | 
			
		||||
 | 
			
		||||
SdlMainWindow::SdlMainWindow(int height, int width) : MainWindow()
 | 
			
		||||
{
 | 
			
		||||
@@ -16,6 +17,7 @@ SdlMainWindow::~SdlMainWindow()
 | 
			
		||||
 | 
			
		||||
void SdlMainWindow::show()
 | 
			
		||||
{
 | 
			
		||||
	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 16);
 | 
			
		||||
	this->window = SDL_CreateWindow("OpenGL Playground", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
 | 
			
		||||
					this->width, this->height, SDL_WINDOW_OPENGL);
 | 
			
		||||
	return;
 | 
			
		||||
@@ -38,6 +40,8 @@ void SdlMainWindow::activateGlContext()
 | 
			
		||||
		this->context = SDL_GL_CreateContext(this->window);
 | 
			
		||||
	else
 | 
			
		||||
		SDL_GL_MakeCurrent(this->window, this->context);
 | 
			
		||||
 | 
			
		||||
	glEnable(GL_MULTISAMPLE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SDL_Window *SdlMainWindow::getWindow()
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,8 @@
 | 
			
		||||
#include <glm/glm.hpp>
 | 
			
		||||
#include <glm/gtc/matrix_transform.hpp>
 | 
			
		||||
 | 
			
		||||
TexturedRectangle::TexturedRectangle(float x0, float y0, float x1, float y1, std::shared_ptr<OpenGlShaderProgram> shaderprog) :
 | 
			
		||||
TexturedRectangle::TexturedRectangle(float x0, float y0, float x1, float y1, std::shared_ptr<OpenGlShaderProgram> &shaderprog,
 | 
			
		||||
				     std::shared_ptr<OpenGlTexture> &texture) :
 | 
			
		||||
	OpenGlGraphics(shaderprog)
 | 
			
		||||
{
 | 
			
		||||
	this->pos1[0] = x0;
 | 
			
		||||
@@ -12,6 +13,8 @@ TexturedRectangle::TexturedRectangle(float x0, float y0, float x1, float y1, std
 | 
			
		||||
	this->setZoom(1.0f);
 | 
			
		||||
	this->setOffset(0.0f, 0.0f);
 | 
			
		||||
	this->vao = 0;
 | 
			
		||||
	this->base_color = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
 | 
			
		||||
	this->m_texture = texture;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TexturedRectangle::realize()
 | 
			
		||||
@@ -23,11 +26,11 @@ void TexturedRectangle::realize()
 | 
			
		||||
			2, 0, 3
 | 
			
		||||
		    };
 | 
			
		||||
 | 
			
		||||
	float vertices[8] = {
 | 
			
		||||
		this->pos1[0], this->pos1[1],
 | 
			
		||||
		this->pos1[0], this->pos2[1],
 | 
			
		||||
		this->pos2[0], this->pos2[1],
 | 
			
		||||
		this->pos2[0], this->pos1[1],
 | 
			
		||||
	float vertices[16] = {
 | 
			
		||||
		this->pos1[0], this->pos1[1],	0.0f, 0.0f,
 | 
			
		||||
		this->pos1[0], this->pos2[1],	0.0f, 1.0f,
 | 
			
		||||
		this->pos2[0], this->pos2[1],	1.0f, 1.0f,
 | 
			
		||||
		this->pos2[0], this->pos1[1],	1.0f, 0.0f,
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	glGenVertexArrays(1, &this->vao);
 | 
			
		||||
@@ -39,19 +42,23 @@ void TexturedRectangle::realize()
 | 
			
		||||
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elem_buff);
 | 
			
		||||
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
 | 
			
		||||
 | 
			
		||||
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
 | 
			
		||||
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
 | 
			
		||||
	glEnableVertexAttribArray(0);
 | 
			
		||||
 | 
			
		||||
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2*sizeof(float)));
 | 
			
		||||
	glEnableVertexAttribArray(1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TexturedRectangle::render()
 | 
			
		||||
{
 | 
			
		||||
	this->shaderprog->use();
 | 
			
		||||
 | 
			
		||||
	this->shaderprog->setUniformVec4("base_color", this->base_color);
 | 
			
		||||
	this->shaderprog->setUniformMat4("model_matrix", this->model_matrix);
 | 
			
		||||
	this->shaderprog->setUniformInt("uni_texture", 0);
 | 
			
		||||
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 | 
			
		||||
	this->m_texture->use(0UL);
 | 
			
		||||
	glBindVertexArray(this->vao);
 | 
			
		||||
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TexturedRectangle::setZoom(float zoom)
 | 
			
		||||
@@ -82,6 +89,11 @@ float TexturedRectangle::getOffsetY()
 | 
			
		||||
	return this->y_offset;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TexturedRectangle::setBaseColor(const glm::vec4 &color)
 | 
			
		||||
{
 | 
			
		||||
	this->base_color = color;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TexturedRectangle::calculateModelMatrix()
 | 
			
		||||
{
 | 
			
		||||
	glm::mat4 moved_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(-this->x_offset, -this->y_offset, 0.0f));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user