当前位置:首页 » 操作系统 » 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 浏览:765
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:664
如何更换服务器网站 发布:2024-09-20 08:42:34 浏览:311
子弹算法 发布:2024-09-20 08:41:55 浏览:289
手机版网易我的世界服务器推荐 发布:2024-09-20 08:41:52 浏览:817
安卓x7怎么边打游戏边看视频 发布:2024-09-20 08:41:52 浏览:162
sql数据库安全 发布:2024-09-20 08:31:32 浏览:94
苹果连接id服务器出错是怎么回事 发布:2024-09-20 08:01:07 浏览:507
编程键是什么 发布:2024-09-20 07:52:47 浏览:658
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:481