Exemplo n.º 1
0
int main()
{

    DisplaySDL2 display(0,0,1024,768,"Olá mundo");
	glm::mat4 model;
	glEnable(GL_DEPTH_TEST);

	GLfloat vertices[] = {
		 0.5f, 0.5f, 0.5f, 1.0f,0.0f,0.0f,
		-0.5f, 0.5f, 0.5f, 0.0f,1.0f,0.0f,
		-0.5f,-0.5f, 0.5f, 0.0f,0.0f,1.0f,
		 0.5f,-0.5f, 0.5f, 1.0f,1.0f,1.0f,
	
		 0.5f, 0.5f, -0.5f, 1.0f,0.0f,0.0f,
		-0.5f, 0.5f, -0.5f, 0.0f,1.0f,0.0f,
		-0.5f,-0.5f, -0.5f, 0.0f,0.0f,1.0f,
		 0.5f,-0.5f, -0.5f, 1.0f,1.0f,1.0f,
		
	};

	GLuint elements[] = {
		0,1,2,
		0,2,3,

		4,5,6,
		4,6,7,

		4,5,1,
		4,1,0,

		5,6,2,
		5,2,1,

		0,3,7,
		4,0,7,

		3,2,6,
		3,6,7

	};

	GLuint vao;
	glGenVertexArrays(1,&vao);
	glBindVertexArray(vao);
	
	GLuint vbo;
	glGenBuffers(1,&vbo);
	glBindBuffer(GL_ARRAY_BUFFER,vbo);
	glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
	
	GLuint ebo;
	glGenBuffers(1,&ebo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(elements),elements,GL_STATIC_DRAW);

	Shader vertex("vertex.glsl",GL_VERTEX_SHADER);
	Shader fragment("fragment.glsl",GL_FRAGMENT_SHADER);

	Program program;
	program.AttachShader(vertex);
	program.AttachShader(fragment);

	program.BindFragDataLocation("outColor",0);

	program.Link();
	program.Use();

	GLuint posAttrib = program.EnableAttrib("pos");
	GLuint colorAttrib = program.EnableAttrib("color");

	GLuint uniformModel = glGetUniformLocation(program.getId(),"model");
	GLuint uniformView  = glGetUniformLocation(program.getId(),"view");
	GLuint uniformProj  = glGetUniformLocation(program.getId(),"proj");
	glVertexAttribPointer(posAttrib,3,GL_FLOAT,GL_FALSE,6*sizeof(GLfloat),0);
	glVertexAttribPointer(colorAttrib,3,GL_FLOAT,GL_FALSE,6*sizeof(GLfloat),(void *) (3*sizeof(GLfloat)));

	glm::mat4 proj = glm::perspective(45.0f,display.getRatio(),0.1f,100.0f);
	glm::mat4 view = glm::lookAt( glm::vec3(0,1,3.0f),
								  glm::vec3(0,0,0),
								  glm::vec3(0,1.0f,0));
	glViewport(0,0,display.getWidth(),display.getHeight());

	glUniformMatrix4fv(uniformProj,1,GL_FALSE,glm::value_ptr(proj));
	glUniformMatrix4fv(uniformView,1,GL_FALSE,glm::value_ptr(view));

	float i = 1;
    while(!display.UserWannaQuit())
    {

		model = glm::rotate(model,i,glm::vec3(0,1,0));
		glUniformMatrix4fv(uniformModel,1,GL_FALSE,glm::value_ptr(model));

        display.Delay(1000/60);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        display.CleanScreen(0,0,0,1.0f);

		glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_INT,0);

        display.SwapBuffers();
    }

		
	glDeleteBuffers(1,&vbo);
	glDeleteVertexArrays(1,&vao);

    display.Quit();



    return EXIT_SUCCESS;

}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
	int width = 800;
	int height = 600;
	SDL_Window* window = SDL_CreateWindow("Demo: A Modern OpenGL Application in Modern C++", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
	if(window == nullptr)
	{
		std::cerr << SDL_GetError() << std::endl;
		return 0;
	}

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

	SDL_GLContext context = SDL_GL_CreateContext(window);
	if(context == nullptr)
	{
		SDL_DestroyWindow(window);
		std::cerr << SDL_GetError() << std::endl;
		return 0;
	}

	glewInit();

	// OpenGL initialization
	using namespace Render::OpenGL;
	Program program;
	Shader vertex("../shaders/simple.vert");
	program.AttachShader(vertex);
	vertex.Compile();
	vertex.PrintInfoLog();
	Shader fragment("../shaders/simple.frag");
	program.AttachShader(fragment);
	fragment.Compile();
	fragment.PrintInfoLog();
	program.BindFragDataLocation(0, "FragColor");
	program.Link();
	program.PrintInfoLog();
	program.Use();
	program.PrintInfoLog();
	PrintError();

	glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 1.0f, 100.f);
	program.UniformMatrix("Projection", Projection);
	glm::mat4 Modelview = glm::translate(glm::mat4(1.0f), glm::vec3(0.f,0.f,-4.0f));
	program.UniformMatrix("Modelview", Modelview);
	glViewport(0,0,800,600);



	for(int i = 0; i < 3000; i++)
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glBegin(GL_TRIANGLES);
		program.VertexAttrib("Vertex", 0.f, 0.f, 0.f);
		program.VertexAttrib("Vertex", 0.f, .5f, 0.f);
		program.VertexAttrib("Vertex", .5f, .5f, 0.f);
		glEnd();
		SDL_GL_SwapWindow(window);
		SDL_Delay(1);
	}

	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(window);

	return 0;
}