① 用DEV C++ 写一个OpenGL的小程序
你用的是glut库吗?
#include<windows.h>
#include <GL/glut.h>
const int size = 50;
GLfloat windowWidth = 200.0f;
float speed = 1.0f;
int direction = 1;
GLfloat xOffset = 0;
void KeyFunc(unsigned char key, int x, int y)
{
switch( key )
{
case 'f':
direction = -direction;
break;
case 'S':
speed += 0.1f;
break;
case 's':
if( speed > 0 )
{
speed -= 0.1f;
}
break;
default:
break;
}
}
void TimerFunction(int value)
{
xOffset += speed * direction;
if( direction == 1 && xOffset > windowWidth - size )
{
direction = -1;
}
else if( direction == -1 && xOffset < -windowWidth )
{
direction = 1;
}
glutPostRedisplay();
glutTimerFunc(40,TimerFunction, 1);
}
void Display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 1.0f, 0, 0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( xOffset, 0, 0 );
glRectf( 0, 0, size, size );
glutSwapBuffers();
}
void ChangeSize( int w, int h )
{
GLfloat windowHeight;
if( h == 0 )
{
h = 1;
}
glViewport( 0, 0, w, h );
GLfloat aspect = ( GLfloat )w / ( GLfloat )h;
if( w <= h )
{ windowWidth= 200.0f;
windowHeight = 200.0f / aspect;
}
else
{
windowWidth = 200.0f * aspect;
windowHeight = 200.0f;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -windowWidth, windowWidth, -windowHeight, windowHeight, -1.0f, 1.0f );
}
int main( void )
{
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize( 800, 600 );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( "Test" );
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glutReshapeFunc( ChangeSize );
glutKeyboardFunc( KeyFunc );
glutDisplayFunc( Display );
glutTimerFunc(40,TimerFunction, 1);
glutMainLoop();
return 0;
}
S键加速,s键减速,f键反向。到两边界后会自动反向。
我刚开始学opengl的时候用的书是《opengl超级宝典 第三版》,这本书带了很多不错的例子。如果你要的话,我把随书光盘给你传过去。
有问题欢迎随时提问,我学opengl的时候没人教我,郁闷死了。现在算是入了门吧。
② 用OpenGL编的小程序
实现三维金字塔的绘制#include <windows.h>#include <GL/glut.h>GLfloat rtri=60.0; // 设置三角形的旋转角度void myDisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); // 重置模型观察矩阵 glScalef (0.25, 0.25, 0.25); // 设置金字塔的缩放大小 glRotatef(rtri,-0.25f,1.0f,0.0f); // 金字塔绕Y轴旋转1.0倍rtri,绕X轴旋//转-0.25倍rtri glBegin(GL_TRIANGLES); // 开始绘制金字塔的各个面 glColor3f(1.0f,0.0f,0.0f); // 红色 glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点(前侧面) glColor3f(0.0f,1.0f,0.0f); // 绿色 glVertex3f(-1.0f,-1.0f, 1.0f); // 三角形的左下顶点(前侧面) glColor3f(0.0f,0.0f,1.0f); // 蓝色 glVertex3f( 1.0f,-1.0f, 1.0f); // 三角形的右下顶点(前侧面) glColor3f(1.0f,0.0f,0.0f); // 红色 glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点(右侧面) glColor3f(0.0f,0.0f,1.0f); // 蓝色 glVertex3f( 1.0f,-1.0f, 1.0f); // 三角形的左下顶点(右侧面) glColor3f(0.0f,1.0f,0.0f); // 绿色 glVertex3f( 1.0f,-1.0f, -1.0f); // 三角形的右下顶点(右侧面) glColor3f(1.0f,0.0f,0.0f); // 红色 glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点(后侧面) glColor3f(0.0f,1.0f,0.0f); // 绿色 glVertex3f( 1.0f,-1.0f, -1.0f); // 三角形的左下顶点(后侧面) glColor3f(0.0f,0.0f,1.0f); // 蓝色 glVertex3f(-1.0f,-1.0f, -1.0f); // 三角形的右下顶点(后侧面) glColor3f(1.0f,0.0f,0.0f); // 红色 glVertex3f( 0.0f, 1.0f, 0.0f); // 三角形的上顶点(左侧面) glColor3f(0.0f,0.0f,1.0f); // 蓝色 glVertex3f(-1.0f,-1.0f,-1.0f); // 三角形的左下顶点(左侧面) glColor3f(0.0f,1.0f,0.0f); // 绿色 glVertex3f(-1.0f,-1.0f, 1.0f); // 三角形的右下顶点(左侧面) glEnd(); rtri+=0.2f; // 增加三角形的旋转变量(新增) glFlush(); }void main(int argc, char *argv[]) // 主函数,与例4.1相同{ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(600, 600); glutCreateWindow("第二个OpenGL程序"); glutDisplayFunc(&myDisplay); glutMainLoop();}
③ OpenGL程序
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "glut.h"
static GLfloat spin = 0.0;
void init( void )
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_FLAT );
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix( );
glRotatef( spin, 0.0, 0.0, 1.0 );
glColor3f( 1.0, 1.0, 1.0 );
glRectf( -25.0, -25.0, 25.0, 25.0 );
glPopMatrix( );
glutSwapBuffers( );
}
void spinDisplay( void )
{
spin = spin + 2.0;
if ( spin > 360.0 )
spin = spin - 360.0;
glutPostRedisplay( );
}
void reshape( int w, int h )
{
glViewport( 0, 0, (GLsizei)w, (GLsizei)h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
//void glOrtho(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble near,GLdouble far);
glOrtho( -50.0, 50.0, -50.0, 50.0, -1.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
}
void mouse( int button, int state, int x, int y )
{
switch ( button )
{
case GLUT_LEFT_BUTTON:
if ( state == GLUT_DOWN )
glutIdleFunc( spinDisplay );
break;
case GLUT_MIDDLE_BUTTON:
if ( state == GLUT_DOWN )
glutIdleFunc( 0 );
break;
default:
break;
}
}
void keyboard( unsigned char key, int x, int y )
{
switch (key)
{
case 'a':
glutIdleFunc( spinDisplay );
break;
case 's':
glutIdleFunc( 0 );
break;
}
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize( 250, 250 );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( argv [0] );
init( );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMouseFunc( mouse );
glutKeyboardFunc( keyboard );
glutMainLoop( );
return 0;
}
④ OpenGL小程序的问题
我前几天遇到过和你同样的问题,后来自己解决了。
解决方法:
直接将glut.h、glut.lib、glut32.lib、glut.dll、glut.32.dll这5个文件复制到你的homework文件夹中,然后,把代码中的 #include <gl/glut.h> 改成 #include "glut.h" 就可以了。
以上5个glut文件打包下载地址:
我的程序头部如下:
//装载OpenGL的lib库文件
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"glaux.lib")
//装载OpenGL头文件
#include <gl/gl.h> //标准OpenGL头文件
#include <gl/glu.h> //OpenGL实用工具库
#include <gl/glaux.h> //OpenGL辅助函数库
//#include <gl/glut.h>
#include "glut.h" //修改后的
......
......
⑤ OPENGL小程序下载
没有,分太少了,先给我份,我就发给你
⑥ OpenGL程序的小问题
一个很小很小的错误。
在绘制文本的时候,改变了xRaster的值:// xRaster += 50;
所以下次重绘的时候,*号的位置就变了。
解决办法也很简单,在绘制*号之前,重新把xRaster修改为25就行了。
glColor3f(1.0,0.0,0.0); //设置标记颜色为红色
xRaster = 25;
for(k = 0;k < 12;k++){ //将数据画为星号多点标记
glRasterPos2i (xRaster + k*50,dataValue [k] - 4); //设置当前光栅位置
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'*');
另外想问下楼主你学OpenGL是做什么?之前在学校里因为导师的项目需要,用了一年的OpenGL,现在基本上荒废掉了。
⑦ opengl程序怎么写
你的代码中因为没有进行适当放缩,导致正方体无法正常显示。可以使用:glScalef(0.5,0.5,0.5);后观察效果。另外,代码之中存在两处错误:第一,绕序问题,推测你使用逆时针正面绕序。这样的话,则需要改正ABCD面为:ColoredVertex(ColorR, PointB);ColoredVertex(ColorG, PointA);ColoredVertex(ColorB, PointD);ColoredVertex(ColorB, PointC);第二:#define ColoredVertex(c,v)do{glColor3fv(c);glVertex3fv(v);}while(0);这个宏中既然定义了颜色参数,就把它用上。最后效果为:
⑧ OPENGL红宝书英文第八版第一个小程序我敲出来了出错,谁看看#include "stdafx.h" #include <iostream>
你把库文件和编译环境都设置到32位下试一试,不要用64位编译
⑨ 求一个OpenGL的小程序
以发送你的邮箱,你看一下。用VB编的
⑩ OpenGL一个简单的程序
肯定不是的啥,这个跟投影矩阵的设置有关系,opengl 默认的是-1 到1,可以自己设置
glPerspective(...) glLookAt(...)