triangle

 avatar
unknown
c_cpp
2 years ago
2.1 kB
6
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.0f,  0.5f, 0.0f  // top   
};
	unsigned int VAO, VBO;
	glGenVertexArrays(1 , &VAO);
	glGenBuffers(1 , &VBO);
	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER , VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, 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);
	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);
		glDrawArrays(GL_TRIANGLES,0,3);
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	
	glDeleteVertexArrays(1,&VAO);
	glDeleteBuffers(1 , &VBO);

}


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);
}
Editor is loading...
Leave a Comment