
openGL窗口一切换就没有了
void myDisplay()
{
glClear( GL_COLOR_BUFFER_BIT );
glShadeModel( GL_SMOOTH );
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-300,300,0,300);
glMatrixMode(GL_MODELVIEW);
//现在原点绘制一个蓝色长方形
glColor3f( 0.0, 0.0, 1.0 );
glRectf(50,100, 200,150);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 250);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
} 展开
#include <windows.h>
#include <gl/glut.h>
#include <GL/gl.h>
void initGL(void);
void resizeWidget(int w,int h)
{
if(0==h)
h=1;
//GLfloat aspect=(GLfloat)w/(GLfloat)h;
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,w,0,h);
glMatrixMode(GL_MODELVIEW);
}
void myDisplay()
{
initGL();
glClear( GL_COLOR_BUFFER_BIT );
glShadeModel( GL_SMOOTH );
//glMatrixMode(GL_PROJECTION);
//gluOrtho2D(-300,300,0,300);
//glMatrixMode(GL_MODELVIEW);
//现在原点绘制一个蓝色长方形
glColor3f( 0.0, 0.0, 1.0 );
glRectf(50,100, 200,150);
glFlush();
}
void initGL(void)
{
glClearColor(0,0,0,1);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 250);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);
glutReshapeFunc(resizeWidget);
glutMainLoop();
return 0;
}
我在你的原头文件里加了两个头文件和另外加了连接库=-lopengl32 -lfreeglut -lglu32
就能够显示一个黑色的窗口可是不能显示蓝色的矩形,我已经改好了。
// BlueRect.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <WindowsX.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f( 0.0, 0.0, 1.0 );
glRectf(5,10, 20,15);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D(0.0, 30.0, 0.0, 30.0*(GLfloat) h/(GLfloat) w);
else
gluOrtho2D(0.0, 30.0*(GLfloat) w/(GLfloat) h, 0.0, 30.0);
glMatrixMode(GL_MODELVIEW);
}
int _tmain(int argc, _TCHAR* argv[])
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("This is the first OpenGL program!");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}