當前位置:首頁 » 操作系統 » opengl源碼

opengl源碼

發布時間: 2022-01-13 01:58:12

❶ OpenGL代碼的問題

int flag=0;
void mykey(unsigned char key, int x, int y)
{
if(key=='L' || key=='l') flag=1;
}
void line()
{
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.8,0.8);
glVertex2f(0.8,-0.8);
glEnd();

}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
if(flag==1)
line();
glutPostRedisplay();
glFlush();
}
void myInit()
{
glClearColor(0.0,0.0,0.0,0.0); /* Background color (black). */
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Simple");
glutKeyboardFunc(mykey);
glutDisplayFunc(display);
myInit();
glutMainLoop();
}
//經過調試 可用

❷ openGL 代碼問題

經測試,可以顯示啊

編譯後,有沒有出現什麼錯誤啊?

❸ 能給我一個編譯好的opengl程序和代碼嗎~急用

// 3D_object.cpp : 定義控制台應用程序的入口點。
//
#define BMP_Header_Length 54
#define WINDOW_SIZE 600
#include "stdafx.h"
#include<windows.h>
#include<gl/glut.h>
#include<iostream>
using namespace std;
bool selected;
float xc=0,yc=0;
GLfloat mat_ambient[]={0.3,1.0,0.5,1.0}; //環境光的反射系數
GLfloat mat_diffuse[]={1.0,1.0,1.0,1.0}; //漫反射的反射系數
GLfloat mat_specular[]={0.3,1.0,0.5,1.0}; //鏡面光的反射系數
GLfloat mat_shininess[]={100.0}; //鏡面反射指數

GLfloat light_position[]={7.0,7.0,3.0,1.0}; //光源的位置
GLfloat light_ambient[]={1.0,0.0,0.0,1.0};
GLfloat light_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat light_specular[]={1.0,1.0,1.0,1.0};
GLfloat spot_direction[]={1,1,-1.0};
int currentx=0;
int currenty=0;

GLuint loadTexture(const char* bmpName)
{
GLint width, height,totalBytes;
GLubyte* pixels = 0;
GLuint textureID = 0;
GLint lineByte;
FILE* pFile = fopen(bmpName, "rb");
if( pFile == 0 ){
printf("Can not open bmp file");
return 0;
}

fseek(pFile, 0x0012, SEEK_SET);
fread(&width, 4, 1, pFile);
fread(&height, 4, 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET);
lineByte = width * 3;
while( lineByte % 4 != 0 ){
++lineByte;
}
totalBytes = lineByte * height;
pixels = (GLubyte*)malloc(totalBytes);
if ( pixels == 0 ) {
fclose(pFile);
return 0;
}

if ( fread(pixels, totalBytes, 1, pFile) <= 0 ) {
free(pixels);
fclose(pFile);
return 0;
}
glGenTextures(1, &textureID);

glBindTexture(GL_TEXTURE_2D, textureID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, pixels);
free(pixels);
fclose(pFile);
return textureID;
}

void init()
{
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

glClearColor (1.0, 1.0, 1.0, 1.0);
glLoadIdentity ();
glOrtho(-10.0, 10.0, -10.0,10.0, -10.0, 10.0);

}
void drawlight(int x,int y)
{
glPointSize(20.0);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(x,y);
glEnd();
}
void setlightandmaterial()
{
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);

glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,100.0);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spot_direction);
}
void mydisplay()
{
GLuint number= loadTexture("lines.bmp");
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
setlightandmaterial();
//glPushMatrix();
//glTranslated(-3.0,-3.0,3.0);
//glPopMatrix();
drawlight(light_position[0],light_position[1]);
glBindTexture(GL_TEXTURE_2D, number);
glutSolidTeapot(5);
glFlush();

}

void mymouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
GLfloat wc;
glReadPixels(x,WINDOW_SIZE-y,1,1,GL_RED,GL_FLOAT,&wc);
if (wc!=1.0){selected=1;printf("Selected!");currentx=x;currenty=y;}
else {selected=0;printf("Not Selected!");}

}
}

void myMotion(int x,int y)
{
float fx,fy;

float dx,dy;

int i;
if (selected)
{
x=x-WINDOW_SIZE/2;
y=WINDOW_SIZE/2-y;
fx=(float)x/((float)WINDOW_SIZE/3);
fy=(float)y/((float)WINDOW_SIZE/3);

dx=fx-xc;
dy=fy-yc;
light_position[0]+=dx;
light_position[1]+=dy;
xc+=dx;yc+=dy;
glutPostRedisplay();
}
}
/*void myMotion(int x,int y)
{
cout<<"x:"<<x<<endl;
cout<<"y:"<<y<<endl;
int dx=(currentx-x)/100;
int dy=(currenty-y)/100;
if (selected)
{
light_position[0]+=dx;
light_position[1]+=dy;
glutPostRedisplay();
}
}*/
void mykey(unsigned char key,int x,int y)
{
if(key=='w')
light_position[1]+=0.7;
if(key=='s')
light_position[1]-=0.7;
if(key=='a')
light_position[0]-=0.7;
if(key=='d')
light_position[0]+=0.7;
if(key=='q')
light_position[2]-=0.7;
if(key=='e')
light_position[2]+=0.7;

glutPostRedisplay();
}
void user()
{
int a=1;
cout<<"select material :"<<endl;
cout<<"1.default"<<endl;
cout<<"2.change"<<endl;
cin>>a;
if(a==1)
return;
else if(a==2)
{
cout<<"input ambient for red:";
cin>>mat_ambient[0];
if(mat_ambient[0]>1||mat_ambient[0]<0)
{
cout<<"illegal input the ambient for red will be default"<<endl;
mat_ambient[0]=0.3;
}
cout<<"input ambient for greed:";
cin>>mat_ambient[1];
if(mat_ambient[1]>1||mat_ambient[1]<0)
{
cout<<"illegal input the ambient for greed will be default"<<endl;
mat_ambient[1]=1.0;
}
cout<<"input ambient for blue:";
cin>>mat_ambient[2];
if(mat_ambient[2]>1||mat_ambient[2]<0)
{
cout<<"illegal input the ambient for blue will be default"<<endl;
mat_ambient[2]=0.5;
}
////////////////////////////////////////////////////////

cout<<"input specular for red:";
cin>>mat_specular[0];
if(mat_specular[0]>1||mat_specular[0]<0)
{
cout<<"illegal input the specular for red will be default"<<endl;
mat_specular[0]=1.0;
}
cout<<"input specular for greed:";
cin>>mat_specular[1];
if(mat_specular[1]>1||mat_specular[1]<0)
{
cout<<"illegal input the specular for greed will be default"<<endl;
mat_specular[1]=1.0;
}
cout<<"input specular for blue:";
cin>>mat_specular[2];
if(mat_specular[2]>1||mat_specular[2]<0)
{
cout<<"illegal input the specular for blue will be default"<<endl;
mat_specular[2]=1.0;
}
/////////////////////////////////////////////////

cout<<"input diffuse for red:";
cin>>mat_diffuse[0];
if(mat_diffuse[0]>1||mat_diffuse[0]<0)
{
cout<<"illegal input the diffuse for red will be default"<<endl;
mat_diffuse[0]=0.3;
}
cout<<"input diffuse for greed:";
cin>>mat_diffuse[1];
if(mat_diffuse[1]>1||mat_diffuse[1]<0)
{
cout<<"illegal input the diffuse for greed will be default"<<endl;
mat_diffuse[1]=1.0;
}
cout<<"input diffuse for blue:";
cin>>mat_diffuse[2];
if(mat_diffuse[2]>1||mat_diffuse[2]<0)
{
cout<<"illegal input the diffuse for blue will be default"<<endl;
mat_diffuse[2]=0.5;
}

}
else
{
cout<<"illegal input"<<endl;
return;
}
}
int _tmain(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WINDOW_SIZE,WINDOW_SIZE);
glutInitWindowPosition(0,0);
glutCreateWindow("3D");
user();
init();
glutDisplayFunc(mydisplay);
glutMouseFunc(mymouse);
glutMotionFunc(myMotion);
glutKeyboardFunc(mykey);
glutMainLoop();
}
這是我自己的繪制茶壺的opengl,已經編譯好了。
開發環境 vs2008。
實現了顏色拾取,貼圖等操作。

❹ 急求opengl程序代碼

#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>

void drawline(float x1,float y1,float x2,float y2) //The function to draw a line from point(x1,y1) to point (x2,y2)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
}

void _display(void)
{
#define pi 3.1415926
GLfloat theta,r=18;
GLint n=16;
theta=2*pi/n;
GLfloat vertex_list[16][2];
for(int i=0;i<n;i++) //Find the vertex
{
vertex_list[i][0]=25+r*cos(i*theta);
vertex_list[i][1]=25+r*sin(i*theta);

}
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
for(int j=0;j<n;j++)
{

for(int k=j;k<n;k++)
{
drawline(vertex_list[j][0],vertex_list[j][1],vertex_list[k][0],vertex_list[k][1]);

}
}

glEnd();
glFlush ();
}

void _init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0) ;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 50.0, 0.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 0);
glutCreateWindow ("hello");
_init ();
glutDisplayFunc(_display);
glutMainLoop();

return 0;
}

一個正十六邊形 各點相連的圖形
我自己寫的 你看看行不行

❺ OpenGL源代碼的網址是什麼

要獲得GLUT源代碼,請訪問下面的網頁
http://www.opengl.org/developers/documentation/glut/index.html
也可以用ftp
ftp://ftp.sgi.com/opengl/opengl114.tar.z
ftp://ftp.sgi.com/opengl/opengl114.taz
ftp://ftp.sgi.com/opengl/opengl114.zip

❻ 在 android 中怎麼找出 opengl的源碼

區別大了,很多API不見了,功能變少了,版本不兼容,1.x和2.0差別比較大,還有很多,你看看關於ES的簡介就知道了,至於在android上既可以用java直接寫,也可以用本地代碼ndk來實現,就是自己習慣和效率的問題了

❼ 計算機圖形學(openGL)代碼

直接用winAPI寫的可以,用glut等寫的就不行,得安裝其類庫。 追問: 就是想知道怎麼安裝的........ - - 回答: 你用的是glut庫嗎?或者 程序編譯 報什麼錯誤啊 追問: 用dec C ,就是不知道glut 庫函數 在哪有下,下載完了放在哪個目錄.... 回答: http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip 這個是官網最新版本,下好之後 將壓縮包內的.h文件放到...\Microsoft Visual Studio 10\VC\PlatformSDK\Include\gl目錄下 將. lib文件 放到...\Microsoft Visual Studio 10\VC\PlatformSDK\Lib目錄下 將. dll文件 放到C:\windows\system32目錄下

❽ 請大家幫忙寫個OpenGL的代碼(用Dev-C++)

int a=0;
void NormalKeys(unsigned char key,int x,int y)
{
if(key == 'l'|| key=='L') a=1;
else if(key == 'c' || key == 'C') a=2;
else a=0;
glutPostRedisplay();
}
void RenderScene(void)
{

glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
switch(a)
{
case 0:break;
case 1:myDrawCircle();//這兩個函數自己寫吧~
case 2:myDrawLine();//對,就是這兩個~,好久不用了...
//也不知道自己寫的對不對,我原來只在VS,VC上用過OPENGL...^_^加油吧~
}
glutSwapBuffers();

}
void main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(600, 600);
glutInitWindowPosition(0,0);
glutCreateWindow("1");

glutDisplayFunc(RenderScene);
// glutReshapeFunc(ChangeSize);
glutKeyboardFunc(NormalKeys);

SetupRc();
glutMainLoop();
}

❾ opengl這個代碼有啥意義

voidgrab()
{
FILE*pDummyFile;
FILE*pWritingFile;
GLubyte*pPixelData;//用於保存點陣圖像素數據
GLubyteBMP_Header[BMP_Header_Length];//保存點陣圖文件信息頭數據
GLinti,j;
GLintPixelDataLength;//點陣圖像素數據長度

i=WindowWidth*3;//每個像素需要3byte存儲(24位色)
//計算當前窗口(OpenGL)圖像存儲為BMP圖像應該需要的大小
while(i%4!=0)//點陣圖的寬度(像素值)必須為4的倍數,所以這里是取倍數值操作
i++;
PixelDataLength=i*WindowHeight;//寬度需要位數及高度需要位數相乘為總數據需//要的大小

pPixelData=(GLubyte*)malloc(PixelDataLength);//分配內存
if(pPixelData==0)
exit(0);

pDummyFile=fopen("C:\Users\Administrator\mmy.bmp","rb");
if(pDummyFile==0)
exit(0);
pWritingFile=fopen("C:\Users\Administrator\grab.bmp","wb");
if(pWritingFile==0)
exit(0);

glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glReadPixels(0,0,WindowWidth,WindowHeight,GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);//讀取OpenGL窗口圖像緩存的像素數據

fread(BMP_Header,sizeof(BMP_Header),1,pDummyFile);//讀取任意BMP文件的頭部
fwrite(BMP_Header,sizeof(BMP_Header),1,pWritingFile);//把讀取到的頭部寫入
//新建的B點陣圖文件
fseek(pWritingFile,0x0012,SEEK_SET);//流指針偏移到文件頭表示高度和寬度
//數據位置
i=WindowWidth;
j=WindowHeight;
fwrite(&i,sizeof(i),1,pWritingFile);//在新文件中,把新的高度和寬度替換當前//高度值及寬度值
fwrite(&j,sizeof(j),1,pWritingFile);

fseek(pWritingFile,0,SEEK_END);//流指針置於文件末端
fwrite(pPixelData,sizeof(PixelDataLength),1,pWritingFile);//像素數據寫入到
//文件中

fclose(pDummyFile);//關閉文件流並銷毀動態分配的內存
fclose(pWritingFile);
free(pPixelData);

}

❿ 誰有OpenGL源碼,麻煩分享一下

http://oss.sgi.com/

pdf格式的鏈接
NeHe OpenGL Tutorial 中文版(介紹glut,有源碼下載,只到第8課)
http://www.amanpage.com/Articles/opengl/NeHe_OGL_T_TIC.htm
OpenGL電子書籍集[下載]
http://www.86vr.com/teach/cursor/200501/4976.html

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:763
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:662
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:309
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:287
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:815
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:160
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:91
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:505
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:655
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:479