#include <iostream>
#include <X11/Xlib.h>
#include <GL/glut.h>
#include <GL/glui.h>
using namespace std;
GLsizei wh = 500;
GLsizei ww = 500;
void MyInit();
void DisplayLine(void);
void DisplaySquare(void );
void MyInit()
{
glClearColor( 1, 1, 1, 0 );
glColor3f( 1, 0, 0 );
glPointSize( 10 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glOrtho( 0.0, ww, 0.0, wh, -1.0, 1.0 );
}
void DisplayLine()
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_LINES );
glVertex2i( 100, 100 );
glVertex2i( 170, 170 );
glVertex2i( 30, 370 );
glVertex2i( 160, 50 );
glEnd( );
glFlush( );
}
void DisplaySquare()
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_LINE_STRIP );
glBegin( GL_POLYGON );
glVertex2f( 150, 150 );
glVertex2f( 350, 150 );
glVertex2f( 350, 350 );
glVertex2f( 150, 350 );
glVertex2f( 150, 150 );
glEnd( );
glFlush( );
}
void DisplayRSquare()
{
glClear( GL_COLOR_BUFFER_BIT );
#if 0
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glBegin( GL_LINE_LOOP );
glVertex2f( 50, 50 );
glVertex2f( 250, 250 );
glVertex2f( 50, 250 );
glEnd( );
#endif
glBegin( GL_LINE_LOOP );
glVertex2f( 0, 0 );
glVertex2f( 500, 0 );
glVertex2f( 500, 500 );
glVertex2f( 0, 500 );
glEnd( );
glTranslatef( 100, 0, 0 );
#if 0
glRotatef( 20, 0, 0, 1 );
glTranslatef( -50, -50, 0 );
glColor3f( 0, 1, 0 );
glBegin( GL_LINE_LOOP );
glVertex2f( 0, 0 );
glVertex2f( 500, 0 );
glVertex2f( 500, 500 );
glVertex2f( 0, 500 );
glEnd( );
glBegin( GL_LINE_LOOP );
glVertex2f( 50, 50 );
glVertex2f( 250, 50 );
glVertex2f( 250, 250 );
glVertex2f( 50, 250 );
glEnd( );
glRotatef( 12, 0, 0, 1 );
glBegin( GL_LINE_LOOP );
glVertex2f( 75, 75 );
glVertex2f( 75, 550 );
glVertex2f( 550, 550 );
glVertex2f( 550, 75 );
glEnd( );
glFlush( );
glBegin( GL_LINES );
glVertex2f( 0, 0 );
glVertex2f( 150, 150 );
glEnd()
#endif
glFlush( );
}
void DisplayCube()
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_LINE_STRIP );
glBegin( GL_POLYGON );
glVertex2i( 150, 150 );
glVertex2i( 350, 150 );
glVertex2i( 350, 350 );
glVertex2i( 150, 350 );
glVertex2i( 150, 150 );
glEnd( );
glBegin( GL_LINE_STRIP );
glBegin( GL_POLYGON );
glVertex2i( 250, 250 );
glVertex2i( 450, 250 );
glVertex2i( 450, 450 );
glVertex2i( 250, 450 );
glVertex2i( 250, 250 );
glEnd( );
glBegin( GL_LINE_STRIP );
glBegin( GL_LINES );
glVertex2i( 150, 150 );
glVertex2i( 250, 250 );
glVertex2i( 350, 150 );
glVertex2i( 450, 250 );
glVertex2i( 350, 350 );
glVertex2i( 450, 450 );
glVertex2i( 150, 350 );
glVertex2i( 250, 450 );
glEnd( );
glFlush( );
}
void DisplayRotate()
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 0.0, 1.0, 0.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
for( int angle = 0; angle < 18; angle += 3 )
{
glRotatef( angle, 1, 0, 0 );
if( angle > 8 )
{
glRotatef( angle * 2, 1, 0.0, 0 );
glColor3f( 1.0, 0.0, 0.0 );
}
glBegin( GL_LINE_LOOP );
glVertex2f( 150, 150 );
glVertex2f( 350, 150 );
glVertex2f( 350, 350 );
glVertex2f( 150, 350 );
glEnd( );
}
glFlush( );
}
void pressMouse(int button, int state, int x, int y )
{
if( state == GLUT_DOWN )
{
glBegin( GL_POINTS );
glPointSize( 50 );
glVertex2i( x, wh - y );
glEnd( );
glFlush( );
}
}
void holdMouse(int x, int y )
{
glBegin( GL_POINTS );
glPointSize( 50 );
glVertex2i( x, wh - y );
glEnd( );
glFlush( );
}
void mouse(int btn, int state, int x, int y )
{
if( state == GLUT_DOWN )
{
mouseState = state;
mouseButton = btn;
mouseX = x;
mouseY = y;
}
else
{
mouseState = 0;
}
}
void motion(int x, int y )
{
if( mouseState == GLUT_DOWN )
{
if( MouseButton == GLUT_LEFT_BUTTON )
{
yrotation -= ( mouseX - x );
xrotation -= ( mouseY - y );
}
}
mouseX = x;
mouseY = y;
glutPostRedisplay( );
}
void DisplayFSquare()
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 0, 0, 1 );
glBegin( GL_LINE_LOOP );
glVertex2f( ( GLdouble ) 0.25, ( GLdouble ) 0.25 );
glVertex2f( ( GLdouble ) 0.75, ( GLdouble ) 0.25 );
glVertex2f( ( GLdouble ) 0.75, ( GLdouble ) .75 );
glVertex2f( ( GLdouble ) 0.25, ( GLdouble ) 0.75 );
glEnd( );
glFlush( );
}
int main(int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( ww, wh );
glutInitWindowPosition( 150, 150 );
glutCreateWindow( "Line" );
MyInit( );
glutDisplayFunc( DisplayRSquare );
glutMouseFunc( pressMouse );
glutMotionFunc( holdMouse );
glutMainLoop( );
}
我刚刚创建了几个简单的模型来测试基本转换。我有两个问题:
- DisplayRSquare:在这个函数中,我用窗口大小绘制了一个正方形,并希望将其移动到 (100, 0),但它转换为 (200, 0)。如果我将它移动到 (50, 0),它将移动到 (100, 0)。
更奇怪的是,该程序有时有效,有时无效。
- 对于函数
glVertex2f(0.3,0.7)
,我们应该在 0.3*widthofthewindow,0.7*height 的位置找到一个点,对吧?但我找不到它。我必须计算坐标并运行glVertex2f(0.3*width,0.7*height)
。
我的编译命令是 g++ -o line Line.cpp -lGL -lGLU -lglut -lglui 然后运行 ./line(File name is Line.cpp)