Hey there.
I used to use OGL 1.1, which was easy and all. I could use glOrthof to have pixel coordinates there, but now I have problems with OGL 2.x/3.x - I don't know how to do that.
Here's what I have:
Projection = glm::ortho(0.0f,800.0f,600.0f,0.0f,0.0f,100.0f);
//camera
View = glm::lookAt(
glm::vec3(0,0,1), //camera is at 4,3,3 in the world space
glm::vec3(0,0,0), //looks at the origin
glm::vec3(0,1,0) //head is up. (0,-1,0) to look upside-down.
);
There's also decent drawing code which gives me a square. Problem is, I cannot see it. When using glm::perspective everything was working fine, but with glm::ortho I see only my blue background..
And there's another thing, how can I have pixel coordinates (0,0 is left-upper corner)?
And if you want, here is full code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
using namespace glm;
extern GLuint LoadShaders(const char* vertexshader_path, const char* fragmentshader_path);
glm::mat4 Projection;
glm::mat4 View;
glm::mat4 Model;
glm::mat4 MVP;
GLuint MatrixID;
GLuint MainProgramID;
GLuint square_buffer;
const float square_buffer_data[] = {
0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,
1.0f,1.0f,0.0f,
0.0f,1.0f,0.0f
};
class sprite {
public:
float x, y, width, height, angle;
sprite(float x, float y, float width, float height) {
this->x = x; this->y = y; this->width = width; this->height = height;
angle = 0.0f;
}
sprite() {
x = 0.0f; y = 0.0f; width = 32.0f; height = 32.0f;
angle = 0.0f;
}
void draw() {
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,square_buffer);
glUseProgram(MainProgramID);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
// For each model you render, since the MVP will be different (at least the M part)
Model = glm::scale((float)width,(float)height,1.0f) * glm::rotate(angle,0.0f,0.0f,1.0f) *
glm::translate((float)x,(float)y,0.0f);
MVP = Projection * View * Model;
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glDrawArrays(GL_QUADS,0,4);
glDisableVertexAttribArray(0);
}
};
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE,GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 1);
//glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
if( !glfwOpenWindow( 800, 600, 0,0,0,0, 32,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3/2.1(?) compatible.\n" );
glfwTerminate();
return -1;
}
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetWindowTitle( "Playground" );
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
// Dark blue background
glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
glGenBuffers(1,&square_buffer);
glBindBuffer(GL_ARRAY_BUFFER,square_buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(square_buffer_data),square_buffer_data,GL_STATIC_DRAW);
//Projection = glm::ortho(0.0f,800.0f,600.0f,0.0f,0.0f,100.0f);
Projection = glm::perspective(90.0f,4.0f/3.0f,0.1f,100.0f);
//camera
View = glm::lookAt(
glm::vec3(0,0,1), //camera is at 4,3,3 in the world space
glm::vec3(0,0,0), //looks at the origin
glm::vec3(0,1,0) //head is up. (0,-1,0) to look upside-down.
);
// Model matrix : an identity matrix (model will be at the origin)
Model = glm::mat4(1.0f); // changes for each model.
// Our ModelViewProjection : multiplication of our 3 matrices
MVP = Projection * View * Model * glm::translate(64.0f,64.0f,0.0f);
MainProgramID = LoadShaders("shaders/basic/vertexshader.glsl","shaders/basic/fragmentshader.glsl");
//Get a handle for our "MVP" uniform - only at initialisation.
MatrixID = glGetUniformLocation(MainProgramID, "MVP");
sprite player(0.0f,0.0f,1.0f*0.64f,1.0f*0.64f);
do{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
player.x += 0.0001f;
player.y += 0.0001f;
player.draw();
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}