网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
09月08日漏签0天
opengl吧 关注:6,859贴子:22,528
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 12回复贴,共1页
<<返回opengl吧
>0< 加载中...

晒晒我刚刚编制的程序

  • 只看楼主
  • 收藏

  • 回复
  • 养过六只猫
  • 贴图
    4
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
用鼠标转换不同视角


  • 晨风起苍穹
  • 顶点
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
楼主,有源码吗


2025-09-08 22:01:38
广告
不感兴趣
开通SVIP免广告
  • 十三队长vision
  • 顶点
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
可以分享源码吗


  • 养过六只猫
  • 贴图
    4
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我源码没什么可保密,只是共享很麻烦,90%以上内容和我专业有关,与opengl绘制无关。如果看源码是出于学习目的,拿我有有简单、更适合于学习源码。我就是看那些源码学会的。


  • 哈哈_410
  • 混合
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
求分享


  • 大宋遗民赵丰年
  • 多边形
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
试着分享吧
我上面图片有张就是圆柱和圆球组成,画一个圆柱很简单。只有两句话
GLUquadric *BondQuad=gluNewQuadric();
gluCylinder( BondQuad, 0.1, 0.1, 1.0, 24,1);
这是起点在原点,沿着z轴长度为1.0,半径0.1的圆柱
图片是是我从网上下载的一个可以用鼠标旋转视角的程序,只有圆柱那句我修改了。另外加光线,放在addlight的子程序,一个没有改几行,一会发出
画圆球的更简单只有一句,
SolidSphere(1.0, 24, 24);,这个圆球只能放在原点上,下面解释以下怎么把这些圆柱和圆球按照需要的方向和位置放好组合成为上面的画面。这里先贴一个这张截图的c代码


  • 大宋遗民赵丰年
  • 多边形
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
下面代码是网上叫如何用鼠标旋转画面的。
我只修改了圆柱那句话,和加入了一小段addlight子程序显示光线效果。
#define PI_OVER_360 3.1415926535897932384626433832795028841971694/360.
#define PI_OVER_180 3.1415926535897932384626433832795028841971694/180.
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include <math.h>
#include <stdio.h>
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <GL/glext.h>
const int MaxNumberOfVertex = 5000;
float vert[MaxNumberOfVertex][3];
int ndata=0;
static int g_xClick, g_yClick,g_sClick;
bool g_Button1Down;
static float ViewMatrx[16]={1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static float ViewMatrx0[16]={1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static float dViewMatrx[16]={1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static float Emtx [16]= {1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1};
static int mouseX0=0, mouseY0=0;
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
void BuildPerspProjMat(float *m, float fov, float aspect,
float znear, float zfar)
{
float xymax = znear * tan(fov * PI_OVER_360);
float ymin = -xymax;
float xmin = -xymax;
float width = xymax - xmin;
float height = xymax - ymin;
float depth = zfar - znear;
float q = -(zfar + znear) / depth;
float qn = -2 * (zfar * znear) / depth;
float w = 2 * znear / width;
w = w / aspect;
float h = 2 * znear / height;
m[0] = w;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = h;
m[6] = 0;
m[7] = 0;
m[8] = 0;
m[9] = 0;
m[10] = q;
m[11] = -1;
m[12] = 0;
m[13] = 0;
m[14] = qn;
m[15] = 0;
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio=float(w)/h;
glOrtho(-1.0*ratio, 1.0*ratio, -1.0, 1.0, -10, 10.0);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
float m[16] = {0};
float fov = 45.f;
// aspect s x/y ratio
float aspect = 1.f;
aspect = float(w)/h;
printf("%i %i,aspect=%f\n",h,w,aspect);
float znear=1.0f, zfar=200.f;
BuildPerspProjMat(m, fov, aspect, znear, zfar);
glLoadMatrixf(m);
}
void addLight() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_LIGHT2); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
//glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 30.0f);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, ambientColor);
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 0.9f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 3.0f, 5.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {1.0f, -0.8f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
return ;
}
float angleX = 0.0f;
float angleY = 0.0f;


  • 大宋遗民赵丰年
  • 多边形
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
GLUquadric *BondQuad=gluNewQuadric();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glPopMatrix(); //
//glPushMatrix(); //Save the current state of transformations
// glTranslatef(0.0f, 0.0f, -3.5f); //Move to the center of the pentagon
// glRotatef(angleX, 1.0f, 0.0f, 0.0f); //Rotate about the x-axis
// glRotatef(angleY, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis
// glTranslatef(0.0f, 0.0f, 3.5f); //Move to the center of the pentagon
glMultMatrixf(ViewMatrx);
glScalef(0.7f, 0.7f, 0.7f); //Scale by 0.7 in the x, y, and z directions
// addLight();
glColor3f(1.f,1.f,0.f);
//gglSolidSphere(1.0, 24, 24);
gluCylinder( BondQuad, 0.1, 0.1, 1.0, 24,1);//opengl产生标准圆柱体
glPopMatrix(); //
//for (int i=0; i<16; i++) printf("%6.3f ",ViewMatrx[i]);
//printf("\n%i data displayed\n",ndata);
glutSwapBuffers(); //Send the 3D scene to the screen
}
void matMultiply(float *mout, const float *m1, const float *m2) {
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
mout[i*4+j]=0;
for (int k=0; k<4; k++) {
mout[i*4+j] += m1[i*4+k]*m2[k*4+j];
}
}
}
}
void RotAboutX(const int DeltaY) {
int i;
float delta=DeltaY;
for (i=0; i<16; i++) dViewMatrx[i]=Emtx[i];
for (i=0; i<16; i++) ViewMatrx0[i]=ViewMatrx[i];
dViewMatrx[5]=cos(DeltaY*PI_OVER_180); dViewMatrx[10]=dViewMatrx[5];
dViewMatrx[6]=sin(DeltaY*PI_OVER_180); dViewMatrx[9]=-dViewMatrx[6];
matMultiply(ViewMatrx,ViewMatrx0,dViewMatrx);
// printf("matrix X %i\n",DeltaY);
// printf("%f %f %f %f\n",ViewMatrx[0],ViewMatrx[4],ViewMatrx[8],ViewMatrx[12]);
// printf("%f %f %f %f\n",ViewMatrx[1],ViewMatrx[5],ViewMatrx[9],ViewMatrx[13]);
// printf("%f %f %f %f\n",ViewMatrx[2],ViewMatrx[6],ViewMatrx[10],ViewMatrx[14]);
// printf("%f %f %f %f\n",ViewMatrx[3],ViewMatrx[7],ViewMatrx[11],ViewMatrx[15]);
}
void RotAboutY(const int DeltaX) {
int i;
float delta=DeltaX;
for (i=0; i<16; i++) dViewMatrx[i]=Emtx[i];
for (i=0; i<16; i++) ViewMatrx0[i]=ViewMatrx[i];
dViewMatrx[0]= cos(delta*PI_OVER_180); dViewMatrx[10]=dViewMatrx[0];
dViewMatrx[8]=sin(delta*PI_OVER_180); dViewMatrx[2]=-dViewMatrx[8];
matMultiply(ViewMatrx,ViewMatrx0,dViewMatrx);
// printf("matrix Y %i\n",DeltaX);
// printf("%f %f %f %f\n",ViewMatrx[0],ViewMatrx[4],ViewMatrx[8],ViewMatrx[12]);
// printf("%f %f %f %f\n",ViewMatrx[1],ViewMatrx[5],ViewMatrx[9],ViewMatrx[13]);
// printf("%f %f %f %f\n",ViewMatrx[2],ViewMatrx[6],ViewMatrx[10],ViewMatrx[14]);
// printf("%f %f %f %f\n",ViewMatrx[3],ViewMatrx[7],ViewMatrx[11],ViewMatrx[15]);
}
void MouseButton(int button, int state, int x, int y)
{
// Respond to mouse button presses.
// If button1 pressed, mark this state so we know in motion function.
if (button == GLUT_LEFT_BUTTON)
{
// g_Button1Down = (state == GLUT_DOWN) ? TRUE : FALSE;
g_Button1Down = (state == GLUT_DOWN );
g_xClick = x;
g_yClick = y;
mouseX0=0;
mouseY0=0;
// printf("click down x=%i,y=%i\n",x,y);
} if (button == GLUT_MIDDLE_BUTTON) {
angleX = 0;
angleY = 0;
mouseX0=0;
mouseX0=0;
for (int i=0; i<16; i++) ViewMatrx[i]=Emtx[i];
glutPostRedisplay();
}
}
void MouseMotion(int x, int y)
{
const int minDelta=2;
int DeltaX, DeltaY;
// printf("mouseX0=%f, Y0=%f\n",)
// If button1 pressed, zoom in/out if mouse is moved up/down.
if (g_Button1Down)
{
DeltaX = float(x - g_xClick);/*if (fabs(DeltaX-mouseX0)>minDelta)*/ RotAboutY(DeltaX-mouseX0);
DeltaY = float(y - g_yClick);/*if (fabs(DeltaY-mouseY0)>minDelta)*/ RotAboutX(DeltaY-mouseY0);
/* if (g_fViewDistance < VIEWING_DISTANCE_MIN)
g_fViewDistance = VIEWING_DISTANCE_MIN;
*/ glutPostRedisplay();
}
mouseX0=DeltaX;
mouseY0=DeltaY;
}
/*
void getData() {
//cout << "draw vertex\n";
float fx=0,fy=0,fz=0;
ndata=0;
FILE *fp=fopen ("icosaedge.dat","r");
//glBegin(GL_TRIANGLES); //Begin triangle coordinate
while (!feof(fp)) {
fscanf(fp,"%f %f %f\n",&vert[ndata][0],&vert[ndata][1],&vert[ndata][2]);
fx+=vert[ndata][0]; fy+=vert[ndata][1]; fz+=vert[ndata][2];
ndata++;
}
fx/=ndata; fy/=ndata; fz/=ndata;
printf("Total %i data (%i vectors) , sum=%f, %f, %f\n",ndata,ndata/2,fx,fy,fz);
fclose(fp);
}
*/
int main(int argc, char *argv[]) {
//Initialize GLUT
//getData();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500); //Set the window size
//Create the window
glutCreateWindow("Icosahedron derivative -- edges");
initRendering(); //Initialize rendering
addLight();
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
//glutReshapeFunc(handleResize);
glutReshapeFunc(reshape);
//glutMouseFunc(mouse);
glutMouseFunc (MouseButton);
glutMotionFunc (MouseMotion);
//glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}


2025-09-08 21:55:38
广告
不感兴趣
开通SVIP免广告
  • 大宋遗民赵丰年
  • 多边形
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
下面的代码是我上面画面关于圆柱的那一段。我的新任物不止把圆柱显示在原点和统一的Z轴方向,而是根据需要显示在不同的位置和方向。我调集这段子程序之前,有了圆柱首段和末端中心坐标放在xyz,我自己在外面编写了几小段子程序进行简单矢量运算、矩阵运算,然后在注释了解释运用这些运算干什么。其实就是计算需要从标准位置旋转的角度和平移的距离。
然后就是opengl语句把这些圆柱旋转平移,放在需要的位置和方向上。
void GraphicsFocus::DrawCylinders() {
//const float z0[]={0,0,1};
float v[3], dir[3], ang, dst;
glColor3fv(piecer->rgb);
for (int i=0; i<piecer->N; i+=2) {
// DrawOneCylnder(cylnders[imol][i],cylnders[imol][i+1]);
arrayMinus(3,&piecer->xyz[3*(i+1)],&piecer->xyz[3*i],v);//末端坐标减去起始坐标得到圆柱体方向矢量v
dst=Length(3,v); //方向矢量长度
Product(z0,v,dir); //z轴叉乘方向矢量是从标准圆柱到实际方向的旋转轴矢量
ang = 180*Angle(z0,v)/PI; //从z轴和圆柱方向矢量点积计算算旋转角度
glPushMatrix(); //opengl从实际视角退回原始视角
glTranslatef(piecer->xyz[3*i],piecer->xyz[3*i+1],piecer->xyz[3*i+2]);///opengl把坐标平移到圆柱起点
glRotatef(ang,dir[0],dir[1],dir[2]);//opengl把坐标沿着上述旋转轴旋转
gluCylinder( BondQuad, piecer->Radius, piecer->Radius, dst, piecer->nSlices,1);//opengl产生标准圆柱体
glPushMatrix();//opengl从实际视角退回原始视角
glTranslatef(0.,0.,dst);//opengl把坐标平移到圆柱末端
gluDisk(BondQuad, 0.f, piecer->Radius, piecer->nSlices, 1);//opengl画圆盘,作圆柱体上方盖子
glPopMatrix();//回到实际视角
glPushMatrix();//opengl从实际视角退回原始视角
glRotatef(180.f,1.f,0.,0.f);//为了发光的缘故下方盖子上下翻转
gluDisk(BondQuad, 0.f, piecer->Radius, piecer->nSlices, 1);//opengl画圆盘,作圆柱体上方盖子
glPopMatrix();//回到标准视角
glPopMatrix();//回到实际视角
}
}


  • 大宋遗民赵丰年
  • 多边形
    3
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
最后,如果你需要对图像频繁变换视角,进行旋转操作等等,我建议到网上寻找和下载QGLViewer程序包。里面繁琐的事情都已经安排好,只要学习那些例子就可以干复杂时期,而那里的例子代码短的出奇。是相当好的学习工具。


  • 其实俄懂你
  • 线段
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
楼主能发opengl 这个软件给我吗


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 12回复贴,共1页
<<返回opengl吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示