Skip to content

Instantly share code, notes, and snippets.

@SupremeDeity
Created March 1, 2020 12:26
Show Gist options
  • Select an option

  • Save SupremeDeity/5ea1e1b7c4706fae63d0f5820ed37649 to your computer and use it in GitHub Desktop.

Select an option

Save SupremeDeity/5ea1e1b7c4706fae63d0f5820ed37649 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "CoreIncludes.h"
GLFWwindow* window;
void HandleEvent() {
if (glfwGetKey(window, GLFW_KEY_1)) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
if (glfwGetKey(window, GLFW_KEY_2)) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(800, 600, "OPENGL", 0, 0);
if (!window) {
std::cout << "FAILED TO CREATE WINDOW!." << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "FAILED TO CREATE WINDOW!." << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwShowWindow(window);
std::cout << glGetString(GL_VERSION) << std::endl;
float positions[] = {
-0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Positions(2), Color(4), TexCoords(2)
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.6f, 0.8f, 0.5f, 1.0f, 1.0f, 0.0f
};
unsigned int indices[6] = {
0, 1, 2,
0, 3, 2
};
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(2*sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (const void*)(6 * sizeof(float)));
unsigned int ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
Shader shader;
unsigned int program = shader.CreateProgram("res/vertex.shader", "res/fragment.shader");
shader.Bind();
std::cout << glGetError() << std::endl;
Texture texture("res/img/cherno.jpg");
texture.Bind();
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.4f, 0.3f, 0.4f, 1.0f);
glBindVertexArray(vao);
HandleEvent();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices);
glfwSwapInterval(1);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
};
#version 330 core
in vec4 v_Color;
in vec2 v_TexCoords;
out vec4 color;
uniform sampler2D m_texture;
void main()
{
color = texture(m_texture, v_TexCoords);
};
#include "Texture.h"
#include <iostream>
Texture::Texture(std::string path)
{
//stbi_set_flip_vertically_on_load(true);
glGenTextures(1, &m_RendererId);
glBindTexture(GL_TEXTURE_2D, m_RendererId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* data = stbi_load(path.c_str(), &width, &height, &BPP, 4);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "STB_IMAGE: Failed to load Texture." << std::endl;
}
stbi_image_free(data);
}
Texture::~Texture()
{
}
void Texture::Bind(unsigned int slot) const
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_RendererId);
}
void Texture::Unbind() const
{
glBindTexture(GL_TEXTURE_2D, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment