① 用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(...)