I'm trying to draw a simple string (overlay) on the screen.
From what I've found over the internet, I'm using it this way:
void write(string text, int x, int y){
glRasterPos2i(x,y);
for(int i = 0; i < text.length(); i++){
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text.data()[i]);
}
}
But it draws the string according to the world coordinates. Say, if x and y are set to 10, they are drawn at (10,10,0) coordinates in the world. But I simple need this string at window's (10,10) coordinates in 2D.
This is part of a small project, and the draw method is given as below. I don't want to change it much as it may break something else in the project.
void disp(){
// set viewing translation and object rotations
glMatrixMode( GL_MODELVIEW );
glLoadIdentity ();
glTranslatef( INIT_VIEW_X, INIT_VIEW_Y, INIT_VIEW_Z );
glRotatef( xRot, 1.0, 0.0, 0.0 );
glRotatef( zRot, 0.0, 0.0, 1.0 );
glScalef( scaleFactor, scaleFactor, scaleFactor );
glClear(GL_COLOR_BUFFER_BIT);
draw();
glFlush();
}
I also don't exactly know what they do, and I think drawing the text to world coordinates have to do with something in this code. I've also tried Using GLUT bitmap fonts but it doesn't work either.
How can I simple draw onto the screen. OpenGL is over complicating things; I try to simply write to the window, but it takes the whole thing and translates into 3D world. I just don't want this.
From twall, yes you need to clear BOTH the modelview and projection matrices
//TEXT
glMatrixMode( GL_PROJECTION ) ;
glPushMatrix() ; // save
glLoadIdentity();// and clear
glMatrixMode( GL_MODELVIEW ) ;
glPushMatrix() ;
glLoadIdentity() ;
glDisable( GL_DEPTH_TEST ) ; // also disable the depth test so renders on top
glRasterPos2f( 0,0 ) ; // center of screen. (-1,0) is center left.
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
char buf[300];
sprintf( buf, "Oh hello" ) ;
const char * p = buf ;
do glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, *p ); while( *(++p) ) ;
glEnable( GL_DEPTH_TEST ) ; // Turn depth testing back on
glMatrixMode( GL_PROJECTION ) ;
glPopMatrix() ; // revert back to the matrix I had before.
glMatrixMode( GL_MODELVIEW ) ;
glPopMatrix() ;
From what I can gather, it seems what you're after is orthographic projection.
I'm not sure about OpenGL specifically (I'm used to higher-level graphics libraries), but here's a couple of links that could be a starting point:
Setting a orthographic projection matrix?
http://www.cprogramming.com/tutorial/opengl_projections.html
you can step back to "2D" coordinates like so:
//save your current matrix on the stack, so you don't lose it
glPushMatrix();
//load identity matrix, so you don't have any 3d transformations
glLoadIdentity ();
//now you also can transform the text
//to the position just as you like
//glTransformf( ... )
//make sure the text is draw, even though it might be outside the viewing area
glDisable( GL_DEPTH_TEST )
drawMyFunkyText();
glEnable( GL_DEPTH_TEST )
//restore the matrix as it was before,
//so you can draw all your shapes as before
glPopMatrix();
Related
i am Making a pool game using openGL and finding this problem vary irritating.
while i am trying to print one text on the screen and move my camera the text is also leaving its original position in the window and moving with the camera.
here is the code that i have in draw().
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
glLoadIdentity();
// GLfloat position1[] = { 00.0, 100.0, 00.0, 1.0 };
//cam position update
gluLookAt( world.camera->cameraFrom.x,world.camera->cameraFrom.y,world.camera->cameraFrom.z, world.camera->cameraTo.x,0,world.camera->cameraTo.z, 0,1,0); // Define a viewing transformation
// Pop the current matrix stack
//**************************************************************
drawTable(world.table);
world.update();
glPushMatrix();
sprintf(str, "Player 1 Score: 1, Player 2 Score: 10");
glRasterPos2f(10, 10);
glutBitmapString(font,(unsigned char*)str);
glPopMatrix();
glutSwapBuffers();
}
glRasterPos() transforms the given position by the modelview and projection matrices. You'll have to reset those to something that positions your text correctly:
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// set projection matrix here
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//cam position update
gluLookAt
(
world.camera->cameraFrom.x, world.camera->cameraFrom.y, world.camera->cameraFrom.z,
world.camera->cameraTo.x, 0, world.camera->cameraTo.z,
0,1,0
);
drawTable(world.table);
world.update();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// set appropriate projection matrix here
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
sprintf(str, "Player 1 Score: 1, Player 2 Score: 10");
glRasterPos2f(10, 10);
glutBitmapString(font,(unsigned char*)str);
glutSwapBuffers();
}
Or use glWindowPos2f() instead, which bypasses both matrices and sets the raster position directly.
For the entire night, I've been looking around the internet, both stackoverflow and elsewhere, to find something to say how to print text on GLUT. While I've found places that say how, none have explained it well, saying which parts of the function is neccessary, which parts aren't. I've also tried to copy in some of the code with the closest to a success is something that made my entire screen white except for some blue pixels. So I've given up, and I'm hoping this will clear up confusion for me and the many people who are confused, like me.
So, I have found this code:
glColor3f(1.0f, 0.0f, 0.0f);
glRasterPos2f(1280, 720);
int len = menu.length();
for (int i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]);
}
and I have placed it in my code:
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -9.0f + zoom);
glTranslatef(0.0f, -1.0f, 0.0f);
string menu = "Hello!";
glColor3f(1.0f, 0.0f, 0.0f);
glRasterPos2f(1280, 720);
int len = menu.length();
for (int i = 0; i < len; i++) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]);
} /*if I need to post the rest of drawScene(), which is the function delegated
as the Display Func, tell me. I don't want to because it's long
What I want to know is what am I doing wrong, and what do future readers in my position need to do in order to get good results.
You don't say what's specifically wrong, but I'm suspecting that your text is not showing up. The reason is likely that the raster position is being clipped, and this is causing your text to not be rendered.
The raster position is the "anchor point" of where a bitmap will be drawn. Usually, this is the lower-left corner of the bitmap (the glBitmap can change that with by setting the x and y parameters to something other than zero, but assume you're not doing that). The raster position is transformed by the model-view matrix, just like a vertex in a geometric primitive. And just like a vertex, if the transformed raster position lies outside of the viewport, it's clipped, and nothing is rendered. What's important to know here is that any rendering of a bitmap - regardless of its size - is predicated on the raster position being inside of the viewport.
In your example, you don't show the viewport you're using, nor the projection transformation (the matrix on the GL_PROJECTION stack), but you set the raster position to (1280, 720), which may well be outside of the viewport.
Let's say you want to render your text in the lower-left corner of your window (and for the sake of argument, let's say your window is 1280 x 1024). When it's time to render your text, drop the following into your rendering routine:
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 1280, 0, 1024 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glRasterPos2i( 10, 1014 ); // move in 10 pixels from the left and bottom edges
for ( int i = 0; i < len; ++i ) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]);
}
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
Depending on which version of OpenGL you're using, you may be able to use a simpler routine glWindowPos2i() (the 2i can be replaced with other dimension-type pairs like other OpenGL functions), which bypasses transforming the raster position by the model-view and projection matrices, and works directly in window coordinates. In that case, you'd write the above code as:
glWindowPos2i( 10, 1014 ); // move in 10 pixels from the left and bottom edges
for ( int i = 0; i < len; ++i ) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, menu[i]);
}
I'm going to show FPS on the screen with the freeglut function glutBitmapString,but it shows nothing. Here is my code. Is there anyone can figure where the problem is?
void PrintFPS()
{
frame++;
time=glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > 100) {
cout << "FPS:\t"<<frame*1000.0/(time-timebase)<<endl;
char* out = new char[30];
sprintf(out,"FPS:%4.2f",frame*1000.0f/(time-timebase));
glColor3f(1.0f,1.0f,1.0f);
glRasterPos2f(20,20);
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,(unsigned char* )out);
timebase = time;
frame = 0;
}
}
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 0.5f };
GLfloat vYellow[] = {1.0f,1.0f,0.0f,1.0f};
shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vYellow);
//triangleBatch.Draw();
squareBatch.Draw();
PrintFPS();
glutSwapBuffers();
}
it supposed to show the FPS on the top left of the screen
The position that's provided by glRasterPos is treated just like a vertex, and transformed by the current model-view and projection matrices. In you example, you specify the text to be position at (20,20), which I'm guessing is supposed to be screen (viewport, really) coordinates.
If it's the case that you're rendering 3D geometry, particularly with a perspective projection, your text may be clipped out. However, there are (at least) two simple solutions (presented in order of code simplicity):
use one of the glWindowPos functions instead of glRasterPos. This function bypasses the model-view and projection transformations.
use glMatrixMode, glPushMatrix, and glPopMatrix to temporarily switch to window coordinates for rendering:
// Switch to window coordinates to render
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, windowWidth, 0, windowHeight );
glRasterPos2i( 20, 20 ); // or wherever in window coordinates
glutBitmapString( ... );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
How do you rotate an object in OpenGL a certain number of degrees? Is there a built-in command or do I have to use a formula? I've been stuck on this issue for days. Its a program that draws a shape under my mouse as it moves.
Say I have a drawing function:
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor3f ( 1, 1, 1 );
glBegin (toggle_type );
//Where cur_x and cur_y is the current mouse location that gets auto-updated
//ratiox is 0.7 and ratioy is 0.6
if (toggle_type==GL_QUADS) //rectangle from (-length, -length) to (length,length)
{
glVertex2f ( cur_x- length*ratiox, cur_y + length*ratioy );
glVertex2f ( cur_x+ length*ratiox, cur_y + length*ratioy );
glVertex2f ( cur_x+ length*ratiox, cur_y- length*ratioy );
glVertex2f ( cur_x- length*ratiox, cur_y- length*ratioy );
}
else if (toggle_type==GL_TRIANGLES)//triangle with vertices (-length, -length), (length, -length), (0, length).
{
glVertex2f ( cur_x- length, cur_y - length );
glVertex2f ( cur_x+length, cur_y - length );
glVertex2f ( cur_x, cur_y + length );
}
else if (toggle_type==GL_LINES) //line brush with vertices (0,-length), (0,length)
{
glVertex2f ( cur_x, cur_y - length );
glVertex2f ( cur_x, cur_y + length );
}
I can't just use glRotatef() before I use glBegin can I? I want to rotate the way it's drawn around my mouse a certain number of degrees. Is there not a built in function? What formula should I look into using if not?
You need to learn how to use OpenGL transforms: glTranslate, glRotate, and glScale.
Translate means "move stuff." Scale means "make stuff bigger or smaller." Rotate means what it sounds like it means.
With OpenGL transforms, it helps to think in terms of changing the coordinate system every time you issue a transform.
So to do this, let's saying you're drawing a box around the cursor. First translate to the position of the mouse cursor. That's where you want to do your drawing. Then rotate the coordinate system around the cursor, so you can draw a box easily without having to do funny stuff with sines and cosines and angles. Then, scale the object to whatever size you want it to be -- this shrinks or expands the coordinate system. Finally, just draw a one-unit-across box around the origin (0,0), and it will appear on screen in the location, rotation, and size you want.
If you try to rotate before you translate, you'll get incorrect results. The technical reason for this is that OpenGL post-multiplies transform matrices by vertex vectors.
And, yes, you should do all transforms outside of your begin/end block. Your begin/end block is just for specifying vertices, normals, etc.
I was just playing with freeglut to see if multi windows can work (seem it does!), and i was using rotations to see some change in display:
#include <cstdio>
#include <cassert>
#include <GL/freeglut.h>
#define DEGREES_X_SEC 10.0
int w_dc, w_ds;
float yRotationAngle;
void DrawSphere()
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(yRotationAngle, .3f, .3f, .3f);
glutWireSphere(.3, 20, 20);
glFlush();
glutSwapBuffers();
}
void DrawCube()
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(yRotationAngle, .2f, .2f, .2f);
glutWireCube(.5);
glFlush();
glutSwapBuffers();
}
void Idle()
{
static int previousTime = 0;
int currentTime = glutGet(GLUT_ELAPSED_TIME);
if (currentTime - previousTime > 10)
{
float x_frame = (DEGREES_X_SEC / 1000.0) * (currentTime - previousTime);
yRotationAngle += x_frame;
glutPostWindowRedisplay(w_dc);
glutPostWindowRedisplay(w_ds);
previousTime = currentTime;
}
}
// other code here....
int main(int argc, char *argv[])
{
// let glut eat any command line args that it owns
glutInit(&argc, argv);
// full color, double buffered
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(800, 600);
w_dc = make_window("cube", DrawCube);
glutPositionWindow(100, 100);
w_ds = make_window("sphere", DrawSphere);
glutPositionWindow(200, 200);
// not bound to any window
glutIdleFunc(Idle);
glutMainLoop();
return 0;
}
HTH, of course requires freeglut...
you need to focus an allegraic material like c 5 and m2, this is automatic rotation and will be instantly rewarded by the government because it shows abnormal behaviour, let me know if this helps.
Rog.
I'm trying to make a simple game of pong using SDL and OpenGL in C++ and I'm having trouble displaying any sort of OpenGL image onto the screen and was wondering if anybody could help:
Initialization:
void init_everything()
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_SetVideoMode( width_of_screen, height_of_screen, bpp_of_screen, SDL_OPENGL );
glClearColor( 0, 0, 0, 0 );
// Sets the projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, width_of_screen, height_of_screen, 0, 1, -1 );
// initalises the modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
SDL_WM_SetCaption( "Pong - By Michael Clover", NULL );
}
After this I call the class function:
void paddle::show()
{
//Move to offset
glTranslatef( x, y, 0 );
//Start quad
glBegin( GL_QUADS );
//Set color to white
glColor4f( 1.0, 1.0, 1.0, 1.0 );
//Draw square
glVertex3f( 0, 0, 0 );
glVertex3f( paddle_width, 0, 0 );
glVertex3f( paddle_width, paddle_height, 0 );
glVertex3f( 0, paddle_height, 0 );
//End quad
glEnd();
//Reset
glLoadIdentity();
}
I then put this in my main function:
int main( int argc, char **argv )
{
bool quit = false;
init_everything();
paddle playerpaddle;
while (quit == false )
{
while( SDL_PollEvent( &event ) )
{
playerpaddle.handle_input();
if (event.type == SDL_QUIT)
{
quit = true;
}
}
playerpaddle.move();
glClear( GL_COLOR_BUFFER_BIT );
playerpaddle.show();
SDL_GL_SwapBuffers();
}
clean_up();
return 0;
}
All I get is a constant black background screen that I set within a 640 by 480px SDL screen.
Sorry for the huge amount of text and I would be extremely grateful for any insight on what the problem could be here, I'm guessing I'm making a silly mistake somewhere.
I think glOrtho might be set up wrong. The last 2 parameters are near and far clipping planes. Your near plane should be less than zero if you don't want to clip your paddle. And your far plane should be greater than zero. So try this:
glOrtho( 0, width_of_screen, height_of_screen, 0, -1, 1 );
Just 2 things to check now that you've ruled out errors:
Try removing the glTranslatef call. It may be too far off for you to see the scene.
Are you creating your vertices in a clockwise order? I can't tell by the variables you used, but if paddle_height is > 0 or paddle_width < 0, it's not going to be clockwise (which it needs to be).
Try this for your projection instead (if you want 0,0,0 at your center).
double range = 4.0; // Set this to whatever you want for the range of the display.
double ratio = double(displayWidth) / double(displayHeight);
glOrtho(-range* ratio, range* ratio, -range, range, -1.0, 1.0);
Temporarily try to render a square from -1.0,-1.0 to 1.0,1.0 to see if your display is working.