How does the 2D openGL co-ordinate system work?
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0);
creates a 2d perspective SCREEN_WIDTH wide and SCREEN_HEIGHT high
glTranslatef( 0.f, SCREEN_HEIGHT, 0.f );
moves the origin(i think) to the bottom of the screen on the left.
If I wanted to create a square here what would the co-ords be?
To draw a square I would use:
glBegin( GL_QUADS );
glVertex2f( ?.f, ?.f );
glVertex2f( ?.f, ?.f );
glVertex2f( ?.f, ?.f );
glVertex2f( ?.f, ?.f );
glEnd();
what coordinates do I need to enter to draw a square at the bottom left corner(any size)
Related
Lines are dissapearing when z depth increases while rotating the object around x axis by 10 degrees. I edited the glDepthRange value to -100,100 or higher but nothing changes. How can i solve this?
void render()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
if( gRenderQuad )
{
glBegin( GL_LINE_LOOP );
glVertex3f(0.3f, 0.5f, 0.4f );
glVertex2f( -0.5f, -0.9f );
glVertex2f( 0.5f, -0.5f );
glVertex2f( 0.5f, 0.5f );
glVertex2f( -0.5f, 0.5f );
glEnd();
}
glDepthRange(-100,100);
glEnable(GL_DEPTH_TEST);
}
I edited the glDepthRange value to -100, 100
This is not possible. The values for the depth range must be in [0.0, 1.0]. You can just set a sub range of the range [0.0, 1.0]. See glDepthRange.
If you want to increase the viewing volume, you need to use a projection matrix. An Orthographic projection can be set with glOrtho:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -100.0, 100.0);
glMatrixMode(GL_MODELVIEW);
I want to draw a cube of side 40 at bootom left corner. my glortho function is
glOrtho(0, // left
1000, // right
0, // bottom
1000, // top
0, // zNear
1000 // zFar
);
and lenght of x,y,z axis is up to 1000. so cube should be at bottom left and dimensions should be as i given. and what should be the gluLookAt(); function. I am not getting correct output. If there is any mistakes in the code, correct it and what function should add to the code.
#include <gl/glut.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#else
#endif
void display();
void specialKeys();
double rotate_y=0;
double rotate_x=0;
void display(){
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Rotate when user changes rotate_x and rotate_y
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );
// side - FRONT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0, 0, 0);
glVertex3f( 40,0,0);
glVertex3f(40,40,0 );
glVertex3f(0,40,0 );
glEnd();
// side - BACK
glBegin(GL_POLYGON);
glColor3f( 1.0,0.0,1.0 );
glVertex3f( 0,0,40 );
glVertex3f( 0,40,40);
glVertex3f( 40,40,40 );
glVertex3f( 40,0,40 );
glEnd();
// side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 40,40,0 );
glVertex3f( 40,0,0 );
glVertex3f( 40,0,40 );
glVertex3f( 40,40,40 );
glEnd();
// side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 0,0,0 );
glVertex3f( 0,40,0 );
glVertex3f( 0,40,40 );
glVertex3f( 0,0,40 );
glEnd();
// side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0,0.0,1.0 );
glVertex3f( 0,40,0);
glVertex3f( 0,40,40 );
glVertex3f( 40,40,40 );
glVertex3f( 40,40,0 );
glEnd();
// side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.5, 0.0 );
glVertex3f( 0,0,0 );
glVertex3f( 40,0,0 );
glVertex3f( 40,0,40 );
glVertex3f( 0,0,40);
glEnd();
glFlush();
glutSwapBuffers();
}
void init()
{
glClearColor(0.5,0.5,0.0, 0.0);
glColor3f(1,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(-1.0,1.0,-1.0,1.0);
glOrtho(0, // left
1000, // right
0, // bottom
1000, // top
0, // zNear
1001 // zFar
);
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -1000.0, 0.0, 1000.0, 0.0);
}
void specialKeys( int key, int x, int y ) {
// Right arrow - increase rotation by 5 degree
if (key == GLUT_KEY_RIGHT)
rotate_y += 5;
// Left arrow - decrease rotation by 5 degree
else if (key == GLUT_KEY_LEFT)
rotate_y -= 5;
else if (key == GLUT_KEY_UP)
rotate_x += 5;
else if (key == GLUT_KEY_DOWN)
rotate_x -= 5;
// Request display update
glutPostRedisplay();
}
int main(int argc, char* argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(10, 10);
// Create window
glutCreateWindow("Awesome Cube");
// Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
init();
glutMainLoop();
return 0;
}
You screwed up your transformations. In Init() you set the current matrix mode to GL_PROJECTION and load some ortho matrix. Then you multiply the lookAt matrix onto this. This is wrong in principle, as the lookAt matrix should be applied to the GL_MODELVIEW stack. (The lookAt parameters you chose actually result in an identity lookAt matrix, so that call has no effect, but that is only a side note).
However, the real error is in display(). There you have glLoadIdentity() which will just overwrite your previous matrix with an identity matrix, so you lose the Ortho transform you did set up, since you still have GL_PROJECTION matrix stack active.
The correct way would be something like:
void init()
{
// ... your other stuff
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( /* your ortho params */ );
glMatrixMode(GL_MODELVIEW); // switch back to the modelView matrix stack
}
void display()
{
glLoadIdentity();
gluLookAt( /* your Lookat parameters */ );
glRotate/Scale/Translate(...); // your local transformations
// ...
}
Note that all of that stuff is completely deprecated and has been removed from the core profile of modern OpenGL versions. When learing OpenGL nowadays, you should consider not learning that old cruft from 20 years ago.
I have a 3D labyrinth with a 3d model that the user controls to exit the labyrinth. I want to draw a rectangle on top of the models head, in which the idea is to show his "energy". The rectangle is to be above his head at all times.
I have this code so far:
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glColor3f(1, 0, 0);
glTranslatef(modelo.objecto.pos.x, modelo.objecto.pos.y+1, modelo.objecto.pos.z);
glBegin(GL_QUADS); //Begin quadrilateral coordinates
//Trapezoid
glVertex2f(0, 0);
glVertex2f(2, 0);
glVertex2f(2, .5);
glVertex2f(0, .5);
glEnd(); //End quadrilateral coordinates
This is the result: Result
It doensn't appear neither red, nor at the right position..
Project the world-space coordinate of the top of the model's head into window space using gluProject() or similar.
Swap your projection matrix over to an ortho one
Draw quad centered on window space coords acquired in #1.
All together:
#include <GL/glut.h>
float angle = 0;
void timer( int extra )
{
angle += 0.5;
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
void display(void)
{
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
const double ar = w / h;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 60.0, ar, 0.1, 100.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( 0, 0, -4 );
glRotatef( angle, 0.1, 0.5, 0.3 );
glColor3ub( 255, 255, 255 );
glutWireCube( 2.0 );
GLdouble modelview[16];
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
GLdouble projection[16];
glGetDoublev( GL_PROJECTION_MATRIX, projection );
GLint viewport[4];
glGetIntegerv( GL_VIEWPORT, viewport );
double x, y, z;
gluProject( 1, 1, 1, modelview, projection, viewport, &x, &y, &z );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( x, y, 0 );
glScalef( 10, 10, 1 );
glBegin( GL_QUADS );
glColor3ub( 255, 0, 0 );
glVertex2i( -1, -1 );
glVertex2i( 1, -1 );
glVertex2i( 1, 1 );
glVertex2i( -1, 1 );
glEnd();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}
The easiest way to accomplish that would probably be to a have a plane and place it right over the model, then turn the rotation to the camera. The only problem with that might be that the energy bar is affected by perspective in that case and to get rid of that you would need to render in ortho. But if you are fine with the perspective changing the look of it, that should be a good enough solution.
I want to draw a logo(3D awards) at the corner of the window(fixed position when change camera)
here is my code for drawing fullscreen rectangle (in old opengl)
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f( 0,0 );
glVertex3d( -1.0,-1.0, 0 );
glTexCoord2f( 1,0 );
glVertex3d( 1.0,-1.0, 0 );
glTexCoord2f( 1,1 );
glVertex3d( 1.0, 1.0, 0 );
glTexCoord2f( 0,1 );
glVertex3d( -1.0, 1.0, 0 );
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
just disable depth buffer abd set the texture to be able to draw your logo in front of eferything
Of course you can change the position and the size of it
Could do it like so:
Draw your scene
Disable the depth test (or clear the depth buffer if you need self-depth testing to draw the model correctly)
Set a new matrix on the stack which ignores camera position.
Draw the logo.
I am rendering a scene with some map image in OpenGL and using lat, lon of the map as coordinates directly. So my scene does not start at 0,0 and goes up to width, height. Although I can see my polygon (very small), I can't zoom by changing the z-value of the eye in the gluLookAt().
/*
* My boundary for map quad with texture
*/
#define TOP 41.9061
#define BOTTOM 41.8546
#define LEFT -87.7012
#define RIGHT -87.6054
/*
* window size
*/
const unsigned int window_width = 1024;
const unsigned int window_height = 739;
/*
* drawing a polygon with texture
*/
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mainMapTextureId);
glBegin( GL_POLYGON );
//bottom left
glTexCoord2f( 0.0f, 1.0f );
glVertex3f(LEFT, BOTTOM, 0.0);
//bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f(RIGHT, BOTTOM, 0.0);
//top right
glTexCoord2f( 1.0f, 0.0f );
glVertex3f(RIGHT, TOP, 0.0);
//top left
glTexCoord2f( 0.0f, 0.0f );
glVertex3f(LEFT, TOP, 0.0);
glEnd();
/*
* Setting up the camera
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)window_width/(GLfloat)window_height, 0.0, 10.0);
gluLookAt( (float)(LEFT+RIGHT)/2.0, (float)(TOP+BOTTOM)/2.0, 1.0,
(float)(LEFT+RIGHT)/2.0, (float)(TOP+BOTTOM)/2.0, 0.0,
0.0f, 1.0f, 0.0f);
It is not legal to have a value of 0.0 for the near plane of gluPerspective. Try replacing it with a nominal small value (0.1f).
Also always put a glGetError call in your code at the end, it will alert you to problems.