Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

877251 Posts in 32852 Topics- by 24294 Members - Latest Member: RopeDrink

May 19, 2013, 12:50:19 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)OpenGL (Core Profile) changing to pixel coordinates (2D) + ortho problem
Pages: [1]
Print
Author Topic: OpenGL (Core Profile) changing to pixel coordinates (2D) + ortho problem  (Read 843 times)
kamac
Level 10
*****


Notorious posts editor


View Profile WWW Email
« on: June 10, 2012, 08:25:09 AM »

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:

Code:
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:

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;
}
« Last Edit: June 11, 2012, 06:31:08 AM by kamac » Logged

Polly
Level 2
**


View Profile
« Reply #1 on: June 10, 2012, 11:21:52 AM »

If you want a orthographic projection with coordinate 0,0 at the top-left, simply set the projection matrix like this;

Code:
m11 =  2.0f / x;
m22 = -2.0f / y;
m33 = -2.0f / z;

m41 = -1.0f;
m42 =  1.0f;
m43 =  1.0f;
m44 =  1.0f;

Of which x / y are the viewport dimensions and z is the clipping range ( all other values are 0.0f ). Make sure to call glFrontFace(GL_CW) due to the inverted coordinates.
Logged
kamac
Level 10
*****


Notorious posts editor


View Profile WWW Email
« Reply #2 on: June 11, 2012, 06:32:30 AM »

Hm. It's almost all good now, except that when I translate something by 1 unit to the right, it get's translated by it's width (32) units to the right.

Aka. I want to move something by one pixel - I cannot, since glm::translate(1,0,0) moves the whole thing to the right by it's width.
Not sure if you understand me. Also, maybe it's the fault of glm::translate??
Logged

Polly
Level 2
**


View Profile
« Reply #3 on: June 11, 2012, 10:01:12 AM »

Sounds like your matrix multiplication order is incorrect. Also, get rid of that GLM library Roll Eyes
Logged
kamac
Level 10
*****


Notorious posts editor


View Profile WWW Email
« Reply #4 on: June 11, 2012, 11:41:30 AM »

Quote
Also, get rid of that GLM library

Eew. Sorry, but I don't really know what should I switch to, since I follow these tutorials:

Code:
http://www.opengl-tutorial.org/
http://en.wikibooks.org/wiki/OpenGL_Programming

And they use GLM there  Apoplectic

I'll look at my drawing order when I'm back home, thanks Smiley
(current order is like: scale*rotation*translation, and I believe it's wrong)
Logged

Polly
Level 2
**


View Profile
« Reply #5 on: June 11, 2012, 12:13:53 PM »

current order is like: scale*rotation*translation, and I believe it's wrong

If you want to scale + translate + rotate a 2D element you only need 1 matrix ( and thus 0 matrix multiplications ). Each 4x4 matrix multiplication adds 128 reads, 64 multiplications, 48 additions and 16 writes .. great way to waste CPU cycles.
Logged
kamac
Level 10
*****


Notorious posts editor


View Profile WWW Email
« Reply #6 on: June 12, 2012, 07:40:33 AM »

Hm. I am slowly getting there  Smiley

I have removed glm since I am doing 2d only and got it mostly working (fixing colors atm.)


One question, should I never use GLM? That's weird since they keep using it in various tutorials on the web.. And if yes, what to replace it with?
Logged

Polly
Level 2
**


View Profile
« Reply #7 on: June 12, 2012, 10:16:30 AM »

One question, should I never use GLM? That's weird since they keep using it in various tutorials on the web.. And if yes, what to replace it with?

The primary reason OpenGL tutorials use libraries such as GLM is so they can focus on explaining OpenGL instead of 3D math. And when you're doing a ( predominantly ) 2D game, you don't need a math library.
Logged
kamac
Level 10
*****


Notorious posts editor


View Profile WWW Email
« Reply #8 on: June 13, 2012, 03:18:24 AM »

Oh, I have a slight problem with GL_DYNAMIC_DRAW. I mean, I want to update my buffer in each loop (update it's stats such as position, width and height), but once I try to do that, it starts to draw my rectangle on the whole screen. Well, here's how I initialize my buffer:

Code:
[code]GLfloat buffer_data[] = {
float(x+width),(float)y,0.0f,
float(x+width),float(y+height),0.0f,
(float)x,float(y+height),0.0f,
(float)x,(float)y,0.0f
};
glGenBuffers(1,&stats_buffer);
glBindBuffer(GL_ARRAY_BUFFER,stats_buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(buffer_data),buffer_data,GL_DYNAMIC_DRAW);
[/code]

And here's how I try to update it:

Code:
//POSITION
glEnableVertexAttribArray(stats);
glBindBuffer(GL_ARRAY_BUFFER,stats_buffer);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(buffer_data),buffer_data);

glVertexAttribPointer(
stats,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glDrawArrays(GL_QUADS,0,4);
glDisableVertexAttribArray(stats);

Am I doing it wrong?


@EDIT

Nevermind. Fixed it. (Was my fault)
« Last Edit: June 13, 2012, 04:16:02 AM by kamac » Logged

PompiPompi
Level 10
*****



View Profile WWW
« Reply #9 on: June 14, 2012, 07:13:42 AM »

current order is like: scale*rotation*translation, and I believe it's wrong

If you want to scale + translate + rotate a 2D element you only need 1 matrix ( and thus 0 matrix multiplications ). Each 4x4 matrix multiplication adds 128 reads, 64 multiplications, 48 additions and 16 writes .. great way to waste CPU cycles.
Premature optimization?
Logged


Kickstarter? no no no... it's Kicksucker...
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic