0% found this document useful (0 votes)
58 views

#Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include

The document contains the code for a basic OpenGL application that renders a 2D quad with textures. It initializes GLFW and GLEW, creates a window, loads and compiles shaders. It defines vertex and index buffers for a quad, loads two textures, and renders the textured quad continuously while processing user input events. The color and position of the quad are updated each frame to animate it.

Uploaded by

iDenis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

#Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include #Include

The document contains the code for a basic OpenGL application that renders a 2D quad with textures. It initializes GLFW and GLEW, creates a window, loads and compiles shaders. It defines vertex and index buffers for a quad, loads two textures, and renders the textured quad continuously while processing user input events. The color and position of the quad are updated each frame to animate it.

Uploaded by

iDenis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <GL/glew.

h>
#include <GLFW/glfw3.h>

#include "Renderer.h"

#include "VertexBuffer.h"
#include "IndexBuffer.h"
#include "VertexArray.h"
#include "Shader.h"
#include "VertexBufferLayout.h"
#include "Texture.h"

#include <iostream>
#include <cstdlib>
#include <ctime>

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"

int main(void)
{
srand(static_cast<unsigned int>(time(0)));

GLFWwindow* window;

/* Initialize the library */


if (!glfwInit())
return -1;

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

/* Create a windowed mode window and its OpenGL context */


window = glfwCreateWindow(960, 540, "OpenGL", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

/* Make the window's context current */


glfwMakeContextCurrent(window);

glfwSwapInterval(1);

if (glewInit() != GLEW_OK) {
std::cout << "Error!";
}
//std::cout << glGetString(GL_VERSION) << std::endl;

{
float positions[] = {
-1.5f, -1.5f, 0.0f, 0.0f, // 0
0.5f, -0.5f, 1.0f, 0.0f, // 1
0.5f, 0.5f, 1.0f, 1.0f, // 2
-0.5f, 0.5f, 0.0f, 1.0f // 3
};

unsigned int indices[] = {


0, 1, 2,
2, 3, 0
};

GLCall(glEnable(GL_BLEND));
GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

/* Construct a vertex array object */


VertexArray va;

/* Construct a vertex buffer object and set the coordonates into the buffer */
VertexBuffer vb(positions, 4 * 4 * sizeof(float));

/* Set the layout of the vertex buffer */


VertexBufferLayout layout;
layout.Push<float>(2);
layout.Push<float>(2);
va.AddBuffer(vb, layout);

/* Store an array buffer for index buffers */


IndexBuffer ib(indices, 6);

glm::mat4 proj = glm::ortho(-2.0f, 2.0f, -1.5f, 1.5f, -1.0f, 1.0f);

Shader shader("res/shaders/Basic.shader");
shader.Bind();
shader.SetUniform4f("u_Color", 0.0f, 0.8f, 0.2f, 1.0f);
shader.SetUniformMat4f("u_MVP", proj);

shader.PrintShaderCode();

Texture texture1("res/textures/profir.jpg");
Texture texture2("res/textures/rainbow.jpg");
texture1.Bind(0);
texture2.Bind(1);
shader.SetUniform1i("u_Texture1", 0);
shader.SetUniform1i("u_Texture2", 1);

/*
Texture texture("res/textures/profir.jpg");
texture.Bind();
shader.SetUniform1i("u_Texture", 0);
*/

va.Unbind();
shader.Unbind();
vb.Unbind();
ib.Unbind();

Renderer renderer;

float r = 0.0f;

/*
int randomX = rand() % 2 + 1;
int randomY = rand() % 2 + 1;

float incrementX = static_cast<float>(randomX) / 100;


float incrementY = static_cast<float>(randomY) / 100;

float texCoordX = 0.5f;


float texCoordY = 0.2f;
*/

/* Loop until the user closes the window */


while (!glfwWindowShouldClose(window))
{
/* Render here */
renderer.SetBackground(0.2f, 0.2f, 0.2f, 1.0f);

renderer.Clear();

shader.Bind();
shader.SetUniform4f("u_Color", r, 0.4f, 0.8f, 1.0f);
shader.SetUniform4f("c_Position", 0.0f, 0.0f, 0.0f, 0.0f);

renderer.Draw(va, ib, shader);

/*
if (r > 1.0f)
increment = -0.01f;
else if (r < 0.0f)
increment = 0.01f;

r += increment;

if (texCoordX >= 0.5f) {


randomX = rand() % 2 + 1;
incrementX = static_cast<float>(-randomX) / 100;
}
else if (texCoordX <= -0.5f) {
randomX = rand() % 2 + 1;
incrementX = static_cast<float>(randomX) / 100;
}

if (texCoordY >= 0.5f) {


randomY = rand() % 2 + 1;
incrementY = static_cast<float>(-randomY) / 100;
}
else if (texCoordY <= -0.5f) {
randomY = rand() % 2 + 1;
incrementY = static_cast<float>(randomY) / 100;
}

texCoordX += incrementX;
texCoordY += incrementY;
*/

/* Swap front and back buffers */


glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
}
glfwTerminate();
return 0;
}

You might also like