square

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.4 kB
2
Indexable
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <shader_s.h>

void framebuffe_call(GLFWwindow *window , int width , int hight);
void processinput (GLFWwindow * window);

int main()
{
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR , 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow *window = glfwCreateWindow(800 , 600 , "afifi" , NULL , NULL);
	if ( window == NULL)
	{
		std::cout << "error in creating windows" << std::endl;
		glfwTerminate();
		return -1;
	}

	glfwMakeContextCurrent(window);
	glfwSetFramebufferSizeCallback(window, framebuffe_call);

	if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
	{
		std::cout << "error in creating windows" << std::endl;
		return -2;
	}

	Shader ourshader("src/shader.vs" , "src/shader.fs");
	float vertices[] = {
    -0.5f, -0.5f, 0.0f, // left  
     0.5f, -0.5f, 0.0f, // right 
     0.5f,  0.5f, 0.0f,  // top   
	 -0.5f, 0.5f, 0.0f, // left  

};
	unsigned int indices [] = {
		0 , 1 , 3 ,
		1, 2, 3
	};

	unsigned int VAO, VBO ,EBO;
	glGenVertexArrays(1 , &VAO);
	glGenBuffers(1 , &VBO);
	glGenBuffers(1 , &EBO);

	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER , VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER , EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	glVertexAttribPointer(0 , 3 ,GL_FLOAT ,GL_FALSE, 3*sizeof(float), (void*)0);
	glEnableVertexAttribArray(0); // Enable the vertex attribute array here
	glBindBuffer(GL_ARRAY_BUFFER , 0);
	//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER , 0);
	//glBindVertexArray(0);

	while (!glfwWindowShouldClose(window))
	{
		processinput(window);
		glClearColor(0.5f , 0.5f , 0.0f ,1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		ourshader.use();
		glBindVertexArray(VAO);
		glDrawElements(GL_TRIANGLES ,6 ,GL_UNSIGNED_INT, 0);
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	
	glDeleteVertexArrays(1,&VAO);
	glDeleteBuffers(1 , &VBO);
	glDeleteBuffers(1 , &EBO);


}


void framebuffe_call(GLFWwindow *window , int width , int hight)
{
	glViewport(0,0,width,hight);
}

void processinput(GLFWwindow *window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}
Leave a Comment