glTranslatef之类的变换函数都是以单位坐标进行的,比如我想要在窗口客户区坐标0,0画一个矩形,应该怎么做呢?
解决方案 »
- 大家在三层架构中.如何处理数据表对像模型(modules)?
- (最好用API处理)如何删除TREEIVEW中某个节点的子节点?
- 关于程序自动生成dll,然后调用的问题
- 在enhanced metafile中播放另外一个enhanced metafile的问题
- 如何实现增加CListBox的列以及设置CListBox的列名
- 怎样设计WIN98中“科学”那样的屏幕保护?
- 如何设置主框架上的title呀?一般显示为“无标题”,调用哪个函数?
- 怎样使对话框中的控件失效?
- 随便给出一个email地址如[email protected],能否自动检测此email地址对应的smtp及pop3服务器地址?
- 指针问题
- 关于创建文本的问题
- 想更深入的了解一些东西。。。
#include <windows.h>
#include <gl/glut.h>GLfloat x = 0.0f;
GLfloat y = 0.0f;
GLfloat rsize = 25;GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;GLfloat windowWidth;
GLfloat windowHeight;///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT); // Set current drawing color to red
// R G B
glColor3f(1.0f, 0.0f, 0.0f); // Draw a filled rectangle with current color
glRectf(x, y, x + rsize, y - rsize); // Flush drawing commands and swap
glutSwapBuffers();
}///////////////////////////////////////////////////////////
// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
{
// Reverse direction when you reach left or right edge
if(x > windowWidth-rsize || x < -windowWidth)
xstep = -xstep; // Reverse direction when you reach top or bottom edge
if(y > windowHeight || y < -windowHeight + rsize)
ystep = -ystep; // Actually move the square
x += xstep;
y += ystep; if(x > (windowWidth-rsize + xstep))
x = windowWidth-rsize-1;
else if(x < -(windowWidth + xstep))
x = -windowWidth -1; if(y > (windowHeight + ystep))
y = windowHeight-1;
else if(y < -(windowHeight - rsize + ystep))
y = -windowHeight + rsize - 1; // Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
///////////////////////////////////////////////////////////
// Setup the rendering state
void SetupRC(void)
{
// Set clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
void ChangeSize(int w, int h)
{
GLfloat aspectRatio; // Prevent a divide by zero
if(h == 0)
h = 1; // Set Viewport to window dimensions
glViewport(0, 0, w, h); // Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far)
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
{
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
}
else
{
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
} glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}///////////////////////////////////////////////////////////
// Main program entry point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800,600);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1); SetupRC(); glutMainLoop(); return 0;
}