I'm messing around with glutBitmapString from the OpenGlut library. Right now I just want to write a string on the screen to make sure it works, but I cannot get it to appear:
const char* texto = "texto";
glRasterPos2i(100, 120);
glColor3f(0.0f, 0.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_10, (UCHAR*)texto);
I've also tried with glutBitmapCharacter in a for loop, but with no success. Could someone give me a clear example or tell me what I'm doing wrong?
glRasterPos() positions are transformed by the current projection/modelview matrices. Make sure they're reasonable.
Or use glWindowPos2i() to bypass them.
Example:
#include <GL/freeglut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
const char* texto = "texto";
glRasterPos2i( 100, 120 );
glColor3f( 0.0f, 0.0f, 1.0f );
glutBitmapString(GLUT_BITMAP_HELVETICA_10, (const unsigned char*)texto );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 300, 300 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
Related
I am trying to look at a cone in 3D, for which I used gluLookAt. I draw a cone in the middle of the screen at pos (250, 250, 0) and then look at it with a camera above the origin looking at the middle. The output is a circle, contrary to the cone I expected to see. It seems the camera is instead at point (250,250,0) also, but it is specified in gluLookAt it should be above the origin. What am I overlooking here?
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(250, 250, 0);
glutSolidCone(30, 10, 20, 20);
glTranslatef(-250, -250, 0);
gluLookAt(0, 0, 100, 250, 250, 0, 0, 0, 1);
glFlush();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
float x, y;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Cone");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(drag);
glutMouseFunc(click);
glutSpecialFunc(catchKey);
glEnable(GL_DEPTH_TEST);
glutMainLoop(); //calls do_one_simulation_step, keyboard, display, drag, reshape
return 0;
}
A few issues:
Set your camera matrix, then draw. The way you have it now your gluLookAt() does nothing useful.
The default identity projection matrix probably isn't what you want. Set a real perspective transform using gluPerspective() or glFrustum().
Your gluLookAt() eye/center coords were a bit wonky; I've put the eye at (50, 50, 50) and set it to look at the origin.
All together:
#include <GL/glut.h>
#include <GL/GLU.h>
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective( 60.0, w / h, 1.0, 1000.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 50, 50, 50, 0, 0, 0, 0, 0, 1 );
glutWireCone( 10, 30, 20, 20 );
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 500, 500 );
glutCreateWindow( "Cone" );
glutDisplayFunc( display );
glEnable( GL_DEPTH_TEST );
glutMainLoop();
return 0;
}
here is code
#include <gl/Gl.h>
#include <gl/Glu.h>
#include <gl/glut.h>
#include <windows.h>
using namespace std;
struct GLintPoint {
GLint x, y;
};
void drawDot(GLint x, GLint y)
{
glBegin (GL_POINTS);
glVertex2i(x,y);
glEnd();
}
int screenWidth = 640, screenHeight = 480;
void myDisplay() {
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f( 1.0f, 1.0f, 1.0f );
}
/*
if( selected ) {
glBegin( GL_QUADS );
glVertex2i( corner[0].x, corner[0].y );
glVertex2i( corner[0].x, corner[1].y );
glVertex2i( corner[1].x, corner[1].y );
glVertex2i( corner[1].x, corner[0].y );
glEnd();
}
glutSwapBuffers();
*/
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
GLint x = mouseX;
GLint y = screenHeight - mouseY; // flip the y value as always
switch(theKey)
{
case 'p':
drawDot(x, y); // draw a dot at the mouse position
break;
case 'E':
exit(-1); //terminate the program
default:
break; // do nothing
}
glutPostRedisplay();
}
void myMouse(int button, int state, int x, int y) {}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
// initialize window
glutInitWindowSize( screenWidth, screenHeight );
glutInitWindowPosition( 0, 0 );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
// create window
glutCreateWindow( "Rubber Rect Demo" );
// set the projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, screenWidth, 0, screenHeight );
glMatrixMode( GL_MODELVIEW );
// clear rendering surface
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // background is black
glViewport(0, 0, screenWidth, screenHeight);
glutMouseFunc( myMouse );
glutDisplayFunc( myDisplay );
glutKeyboardFunc(myKeyboard);
// glutPassiveMotionFunc( myPassiveMotion );
glutMainLoop();
return( 0 );
}
After i compile and run it,I press 'P',it can not plot a point in cursor's position,I don't know why it can not work.
here is link where the sourcecode comefrom http://www.4twk.com/shill/3rd-edition.html
in chapter 2,Chapter 2 - Getting Started Drawing Figures - zip file
Keep drawing operations to the display function. Do not call OpenGL drawing calls from the event handler. In the event handler set a variable that will make the point draw in the display function, then issue a redraw using glutPostRedisplay.
Give this a shot:
#include <GL/glut.h>
#include <vector>
using namespace std;
struct GLintPoint
{
GLint x, y;
};
vector< GLintPoint > points;
void myDisplay()
{
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
glViewport(0, 0, w, h);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // background is black
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, w, 0, h, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f( 1.0f, 1.0f, 1.0f );
glBegin (GL_POINTS);
for( size_t i = 0; i < points.size(); ++i )
{
glVertex2i( points[i].x, points[i].y );
}
glEnd();
glutSwapBuffers();
}
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
int h = glutGet( GLUT_WINDOW_HEIGHT );
GLint x = mouseX;
GLint y = h - mouseY; // flip the y value as always
switch(theKey)
{
case 'p':
{
GLintPoint temp;
temp.x = x;
temp.y = y;
points.push_back( temp );
glutPostRedisplay();
}
break;
case 'E':
exit(-1); //terminate the program
default:
break; // do nothing
}
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
// initialize window
glutInitWindowSize( 640, 480 );
glutInitWindowPosition( 0, 0 );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
// create window
glutCreateWindow( "Rubber Rect Demo" );
glutDisplayFunc( myDisplay );
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return( 0 );
}
I want to draw a series of connected lines (GL_LINE_STRIP) in following way.
I had tried to code by my own, but not get desired result, so i come here, help me to find out where i was wrong. here i am giving only my draw() function.
glBegin(GL_LINE_STRIP);
glVertex2f(-4.00, 0.00);
glVertex2f(-3.00, 2.00);
glVertex2f(-2.00, 0.00);
glVertex2f(-1.00, 2.00);
glVertex2f(0.0, 0.00);
glVertex2f(1.00, 2.00);
glVertex2f(2.00, 0.00);
glVertex2f(3.00, 2.00);
glVertex2f(4.00, 0.00);
glEnd();
Workin' perfectly here:
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -6, 6, -6, 6, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 255, 255 );
glBegin(GL_LINE_STRIP);
glVertex2f(-4.00, 0.00);
glVertex2f(-3.00, 2.00);
glVertex2f(-2.00, 0.00);
glVertex2f(-1.00, 2.00);
glVertex2f(0.0, 0.00);
glVertex2f(1.00, 2.00);
glVertex2f(2.00, 0.00);
glVertex2f(3.00, 2.00);
glVertex2f(4.00, 0.00);
glEnd();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
I can draw text by specifying the glRasterPos2i(WindowX, WindowY); and it draws the text at the specified position within my window just fine.
For example: glPrint(150, 150, "dgdg"); Where my viewport is 0, 0, 800, 500.
However, doing the same for a square doesn't work. So I tried setting up my view again:
const int W = 800, H = 500;
glViewport(0, 0, W, H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, W, H, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(100, 100);
glVertex2f(150, 100);
glVertex2f(150, 150);
glVertex2f(100, 150);
glEnd();
But my square never shows up :l
I disabled DepthTesting and GL_RECTANGLE_2D. Still no go.
What am I doing wrong? How can I get it to draw at window coords instead of specifying 1.0f, etc?
Workin' fine here:
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, h, 0, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(100, 100);
glVertex2f(150, 100);
glVertex2f(150, 150);
glVertex2f(100, 150);
glEnd();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
You can add glFlush() after your OpenGL code to require the drawings in the buffer to flush to the window.
For some reference to glFlush(), checkout this link.
I'm trying to highlight ("color #00FFFF") specific individual pixels in an image (already displayed in the background) using OpenGL/C++. The pixel coordinates and the image exist in exact 2D space, but all the OpenGL code I'm seeing in the project so far - glTranslatef(), glScalef() - is 3D and float-based, and the positioning of the object appears to happen separately from the time it's drawn.
I'm used to Java's Graphics2D package, where I can call something to the effect of
width = 1; height = 1;
buffer.drawRect(width, height, xPosition, yPosition);
and it'll fill in a pixel at the specified location. Is there anything similar to that syntax - where I can set size, set position, and draw all in one line - in OpenGL? If not, how would I go about adapting my 2D+pixel input to OpenGL's float and 3D structure?
I currently have this:
glPushMatrix();
glTranslatef(0.0f, 0.0f, -5.0f);
glColor3f(0, 1, 1);
glPointSize(5.0f);
glBegin(GL_POINTS);
glVertex3f(1.0f, 1.0f, 1.0f);
glPopMatrix();
which I pieced together from some Google searches and other parts of my code, but I don't see anything being draw. I have no idea as to the units for the translate, vertex, or pointsize commands. It'd be awesome if I could replace all of that with something like the Java command up above. If not, is there some way I can guarantee whatever I draw here will be "on top" of everything else, but still not behind the camera.
Is there anything similar to that syntax - where I can set size, set position, and draw all in one line - in OpenGL?
glRect():
#include <GL/glut.h>
void display()
{
glEnable( GL_CULL_FACE );
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glDepthMask( GL_TRUE );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// draw teapot
glEnable( GL_DEPTH_TEST );
glDepthMask( GL_TRUE );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60, w / h, 1, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0, 0, -5 );
glColor4ub( 255, 0, 0, 255 );
glPushMatrix();
float angle = 60.0f * ( glutGet(GLUT_ELAPSED_TIME) / 1000.0f );
glRotatef( angle, 0.1, 0.95, 0.05 );
glutSolidTeapot( 1.0 );
glPopMatrix();
// draw rectangle
glDisable( GL_DEPTH_TEST );
glDepthMask( GL_FALSE );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, w, 0, h, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor4ub( 0, 255, 0, 128 );
glRecti( 0 + 50, 0 + 50, w - 50, h - 50 );
glutSwapBuffers();
}
void timer( int extra )
{
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "Rect" );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}