Hello everyone !, recently I started programming in OpenGL, I followed some tutorials that walk the web, but I’ve noticed how much all these tutorials show how to use perspective projection matrix. So here on my own I wanted to see how it worked the orthogonal projection but had no success.
I would like if possible to see my code and tell me if you find any errors, to run the empty screen, but commented projection matrix square appears in the center.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#include "textura.h"
void keyCallback( GLFWwindow *window, int key, int scancode, int action, int mode );
int main()
{
glfwInit();
GLFWwindow *window = glfwCreateWindow( 800, 600, "Title", NULL, NULL );
glfwMakeContextCurrent( window );
glewExperimental = GL_TRUE;
glewInit();
glViewport( 0, 0, 800, 600 );
glfwSetKeyCallback( window, keyCallback );
// -------------------------------------------Init-------------------------------------------
Shader shader( "quad.vert", "quad.frag" );
GLfloat vertices[] = {
0.5f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
-0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f
};
GLuint vbo, quad;
glGenVertexArrays( 1, &quad );
glGenBuffers( 1, &vbo );
glBindVertexArray( quad );
{
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, ( GLvoid * ) 0 );
glEnableVertexAttribArray( 0 );
}
glBindVertexArray( 0 );
// ----------------------------------------------------------------------------------------------------
while( !glfwWindowShouldClose( window ) )
{
glfwPollEvents();
// ----------------------------------------------Update------------------------------------------------
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
shaderCuadrado.use();
glm::mat4 projection;
projection = glm::ortho( 0, 800, 600, 0 );
glm::mat4 model;
model = glm::translate( model, glm::vec3( 100, 100, 0 ) );
model = glm::scale( model, glm::vec3( 100, 100, 0) );
glUniformMatrix4fv( glGetUniformLocation( shader.program, "projection" ), 1, GL_FALSE, glm::value_ptr( projection ) );
glUniformMatrix4fv( glGetUniformLocation( shader.program, "model" ), 1, GL_FALSE, glm::value_ptr( model ) );
// ----------------------------------------------------------------------------------------------------
// --------------------------------------------Render---------------------------------------------
glBindVertexArray( quad );
glDrawArrays( GL_TRIANGLES, 0, 3 );
glBindVertexArray( 0 );
// ----------------------------------------------------------------------------------------------------
glfwSwapBuffers( window );
}
glfwDestroyWindow( window );
window = NULL;
glfwTerminate();
}
void keyCallback( GLFWwindow *window, int key, int scancode, int action, int mode )
{
if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS )
glfwSetWindowShouldClose( window, GL_TRUE );
}
The transformations work well, the problem is in the orthogonal view( Clarified, changes in the code are on a trial basis to see if it worked ).
Thank you.