PDA

View Full Version : problem orthogonal view glm::ortho



Pablo Valoppi
01-08-2015, 08:12 PM
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.

Alfonse Reinheart
01-08-2015, 10:15 PM
It's not clear exactly what you expect to see here when you use orthographic projections. Or more specifically, the orthographic projection you're currently using.

You're drawing a square (well, technically, just one triangle of the square, since you only send 3 vertices). And that square is a unit square, so its length on each side is 1 unit. In camera space (since your model-to-camera matrix is identity).

Your orthographic projection however defines a camera space that is 800x600 units in size. A 1 unit size square would be... one pixel in size. Or maybe larger, depending on your window's exact resolution, but it's going to be very small. And since the unit square you drew is centered around the origin, that would put 3/4ths of the square off screen (the lower-left corner, to be precise).

So, at best, the most you would see is a couple of pixels in the bottom left of the screen. I'd guess that's not what you wanted, but it's not clear exactly what you did want.


the orthogonal projection

The term is "orthographical".

Pablo Valoppi
01-09-2015, 09:20 AM
Now solve the problem, I leave my code in case someone else has the same problem as me.

#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include "shader.h"
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;

int main()
{

glfwInit();


GLFWwindow *ventana = glfwCreateWindow( SCREEN_WIDTH, SCREEN_HEIGHT, "Hola", NULL, NULL );
glfwMakeContextCurrent( ventana );

glewExperimental = GL_TRUE;
glewInit();


Shader shader("transform.vs", "transform.frag");



GLfloat vertices[] = {
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,

0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
};

GLuint vbo, cuadrado;
glGenVertexArrays( 1, &cuadrado );
glGenBuffers( 1, &vbo );
glBindVertexArray( cuadrado );
{
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );

glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, ( GLvoid * ) 0 );
glEnableVertexAttribArray( 0 );
}
glBindVertexArray( 0 );
while ( !glfwWindowShouldClose( ventana ) )
{

glfwPollEvents();

glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

shader.use();


glm::mat4 projection = glm::ortho(
0.0f,
static_cast<float>(SCREEN_WIDTH),
static_cast<float>(SCREEN_HEIGHT),
0.0f,
0.0f,
100.0f
);

glm::mat4 view;

glm::mat4 model = glm::mat4(1.0f);


glUniformMatrix4fv( glGetUniformLocation( shader.program, "model" ), 1, GL_FALSE, glm::value_ptr( model ) );
glUniformMatrix4fv( glGetUniformLocation( shader.program, "view" ), 1, GL_FALSE, glm::value_ptr( view ) );
glUniformMatrix4fv( glGetUniformLocation( shader.program, "projection" ), 1, GL_FALSE, glm::value_ptr( projection ) );

glBindVertexArray( cuadrado );

model = glm::mat4();
model = glm::translate( model, glm::vec3( 0.0f, 52.0f, 0.0f ));
model = glm::scale( model, glm::vec3( 100.0f, 100.0f, 0.0f ));
glUniformMatrix4fv( glGetUniformLocation( shader.program, "model" ), 1, GL_FALSE, glm::value_ptr( model ) );
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray( 0 );

glfwSwapBuffers( ventana );
}

glfwTerminate();
return 0;
}

Alfonse Reinheart
01-09-2015, 12:20 PM
To fully debug your problem, we'd have to see things like your shader.