OPENGL中使用glutPostRedisplay()总是显示未定义

使用单文档+OPENGL,想调用glutPostRedisplay()函数刷新菜单选取后的操作,但是总显示“error C2065: 'glutPostRedisplay' : undeclared identifier”,不加它就没事,这个是怎么回事呢?这个函数使用有什么其他要求吗?我要是不加这个的话,只有响应鼠标或键盘消息时才会更新窗口内容,

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

vs2015环境下OpenGL中怎样实现鼠标右键控制物体旋转

#include "StdAfx.h" #include "CloudPoint.h" #define GLUT_DISABLE_ATEXIT_HACK #include <GL/glut.h> #include <math.h> #include <stdio.h> CCloudPoint::CCloudPoint(void) { } CCloudPoint::~CCloudPoint(void) { } CCloudPoint *CCloudPoint::s_pCurrent = NULL; char *m_cFilename = NULL; //int CCloudPoint::s_argc = 0;//char** CCloudPoint::s_argv = 0; bool mouseLeftDown; bool mouseRightDown; float CCloudPoint::c=3.1415926535/180.0f; //弧度和角度转换参数 int CCloudPoint::du=90,CCloudPoint::oldmy=-1,CCloudPoint::oldmx=-1; //du是视点绕y轴的角度,opengl里默认y轴是上方向 float CCloudPoint::r=0.3f,CCloudPoint::h=0.0f; //r是视点绕y轴的半径,h是视点高度即在y轴上的坐标 int CCloudPoint::scale = 10; int CCloudPoint::iWindowsWidth = 800, CCloudPoint::iWindowHeight = 800; float CCloudPoint::x[1000000], CCloudPoint::y[1000000], CCloudPoint::z[1000000]; int CCloudPoint::iPointNumber = 0; int mButton = -1; void CCloudPoint::setFilename(char * m_cFilename) { this->m_cFilename = m_cFilename; } char* CCloudPoint::getFilename() { return m_cFilename; } void CCloudPoint::MouseCallback(int button, int state, int x, int y) { CCloudPoint::s_pCurrent->Mouse(button, state, x, y); } void CCloudPoint::onMouseMoveCallback( int x,int y) { CCloudPoint::s_pCurrent->onMouseMove( x,y); } void CCloudPoint::reshapeCallback(int w,int h) { CCloudPoint::s_pCurrent->reshape(w,h); } void CCloudPoint::myDisplayCallback(void) { CCloudPoint::s_pCurrent->myDisplay(); } void CCloudPoint::Mouse(int button, int state, int x, int y) //处理鼠标点击 { if (state == GLUT_DOWN) { oldmx = x, oldmy = y; } else { if (state == GLUT_UP && button == GLUT_WHEEL_UP) { if (scale>1) scale -= 1; else scale = 1; //glutPostRedisplay(); } else if (state == GLUT_UP && button == GLUT_WHEEL_DOWN) { scale += 1; //glutPostRedisplay(); } else if (state == GLUT_DOWN && button == GLUT_RIGHT_BUTTON) { mouseRightDown = true; } else if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) { mouseLeftDown = true; } } } void CCloudPoint::onMouseMove(int x,int y) //处理鼠标拖动 { //printf("%d\n",du); if (mouseRightDown) { du += x - oldmx; //鼠标在窗口x轴方向上的增量加到视点绕y轴的角度上,这样就左右转了 h += 0.03f*(y - oldmy); //鼠标在窗口y轴方向上的改变加到视点的y坐标上,就上下转了 if (h > 1.0f) h = 1.0f; //视点y坐标作一些限制,不会使视点太奇怪 else if (h < -1.0f) h = -1.0f; oldmx = x, oldmy = y; //把此时的鼠标坐标作为旧值,为下一次计算增量做准备 } } void CCloudPoint::init() { glEnable(GL_DEPTH_TEST); } void CCloudPoint::reshape(int w,int h) { glViewport( 0, 0, (GLsizei)w, (GLsizei)h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective(90.0f, (GLfloat)w / (GLfloat)h, 1.0f, 1.0f); glMatrixMode( GL_MODELVIEW ); } void CCloudPoint::loadFile(char *cFileName) { FILE* file = fopen(cFileName,"r"); if (!file) { printf("load file %s failed\n",cFileName); } else { /*fseek(file,0,SEEK_END); long fileSize = ftell(file);*/ char buffer[1000]; while(fgets(buffer,300,file) != NULL) { sscanf(buffer,"%f, %f, %f", &x[iPointNumber], &y[iPointNumber], &z[iPointNumber]); iPointNumber++; //printf("x=%f, y=%f, z=%f\n", x, y, z); } } } void CCloudPoint::myDisplay(void) { int i; /*glClear(GL_COLOR_BUFFER_BIT);*/ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //printf("At:%.2f %.2f %.2f\n",r*cos(c*du),h,r*sin(c*du)); //这就是视点的坐标 gluLookAt(r*cos(c*du), h, r*sin(c*du), 0, 0, 0, 0, 1.0f, 0); //从视点看远点,y轴方向(0,1,0)是上方向 glBegin(GL_POINTS); /*glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);*/ for (i = 0; i < iPointNumber; i++) { glVertex3f(x[i]/scale, y[i]/scale, z[i]/scale); } glEnd(); glFlush(); glutSwapBuffers(); } void CCloudPoint::LoadAndShow() { int argc=1; char* argv[]={"good"}; //loadFile(CCloudPoint::getFilename()); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition(100, 0); glutInitWindowSize(iWindowsWidth, iWindowHeight); glutCreateWindow("OpenGL程序"); init(); glutReshapeFunc(CCloudPoint::reshapeCallback); glutDisplayFunc(myDisplayCallback); /*glutDisplayFunc(display);*/ glutIdleFunc(CCloudPoint::myDisplayCallback); //设置不断调用显示函数 glutMouseFunc(CCloudPoint::MouseCallback); glutMotionFunc(CCloudPoint::onMouseMoveCallback); glutMainLoop(); return; } 上面是我的代码,但是没有用,如果将void CCloudPoint::onMouseMove(int x,int y) 中的if语句去掉,就是任意鼠标键移动控制旋转,想问问大神

有关于OpenGL三维变化的问题

用二维矩阵实现一些变换,下面代码有注释。。平移,错切,放大缩小是可以运行的。 但是,旋转就越旋越小,对称点完也没有反应。 请大神看一下哪里有错误.. #include <GL/glut.h> #include <stdlib.h> #include <math.h> //旋转时用的sita double sita1=3.14/3; double sita2=-3.14/3; //这个类定义了点 class Point2{ public: GLfloat x,y,a; }; //四个顶点赋初值 Point2 p[4]={{-0.5,0.5,1},{0.5,0.5,1},{0.5,-0.5,1},{-0.5,-0.5,1}}; //变换矩阵初值 GLfloat varyMatrix[3][3]={{1,0,0},{0,1,0},{0,0,1}}; //每进行一次变换,调用一次这个函数,矩阵相乘。 void VaryMatrix(){ for(int i=0;i<4;i++){ p[i].x=p[i].x*varyMatrix[0][0]+p[i].y*varyMatrix[1][0]+p[i].a*varyMatrix[2][0]; p[i].y=p[i].x*varyMatrix[0][1]+p[i].y*varyMatrix[1][1]+p[i].a*varyMatrix[2][1]; p[i].a=p[i].x*varyMatrix[0][2]+p[i].y*varyMatrix[1][2]+p[i].a*varyMatrix[2][2]; } //恢复变换矩阵初始值 varyMatrix[0][0]=1; varyMatrix[0][1]=0; varyMatrix[0][2]=0; varyMatrix[1][0]=0; varyMatrix[1][1]=1; varyMatrix[1][2]=0; varyMatrix[2][0]=0; varyMatrix[2][1]=0; varyMatrix[2][2]=1; } //每进行一次变化后,调用这个函数,重新绘制图形 void reDraw(){ glClear(GL_COLOR_BUFFER_BIT); //坐标轴 glBegin(GL_LINES); glVertex2i(-1,0); glVertex2i(1,0); glVertex2i(0,-1); glVertex2i(0,1); glEnd(); //绘制矩形 glBegin(GL_LINE_LOOP); glVertex2f(p[0].x,p[0].y); glVertex2f(p[1].x,p[1].y); glVertex2f(p[2].x,p[2].y); glVertex2f(p[3].x,p[3].y); glEnd(); glFlush(); } //键盘响应函数 void keyFunc1(GLubyte key,GLint xMouse,GLint yMouse){ int i; GLfloat t; switch(key) { //旋转 case'q': for(i=0;i<4;i++) { p[i].x=t; p[i].x=p[i].x*cos(sita1)-p[i].y*sin(sita1); p[i].y=t*sin(sita1)-p[i].y*cos(sita1); } glutPostRedisplay(); break; //另一个方向旋转 case'w': varyMatrix[0][0]=cos(sita2); varyMatrix[0][1]=sin(sita2); varyMatrix[0][2]=1; varyMatrix[1][0]=-sin(sita2); varyMatrix[1][1]=cos(sita2); varyMatrix[1][2]=1; VaryMatrix(); glutPostRedisplay(); break; //X轴镜像 case'e': varyMatrix[1][1]=-1; VaryMatrix(); glutPostRedisplay(); break; //Y轴镜像 case'r': varyMatrix[0][0]=-1; VaryMatrix(); glutPostRedisplay(); break; //原点对称 case't': varyMatrix[0][0]=-1; varyMatrix[1][1]=-1; VaryMatrix(); glutPostRedisplay(); break; //错切X轴 可运行!! case'a': varyMatrix[1][0]=-0.5; VaryMatrix(); glutPostRedisplay(); break; //错切Y轴 可运行!! case's': varyMatrix[0][1]=-0.5; VaryMatrix(); glutPostRedisplay(); break; //错切原点 可运行!!! case'd': varyMatrix[1][0]=-1; varyMatrix[0][1]=1; VaryMatrix(); glutPostRedisplay(); break; //放大 可运行!!! case'z': varyMatrix[1][1]=varyMatrix[0][0]=1.2; VaryMatrix(); glutPostRedisplay(); break; //缩小 可运行!!! case'x': varyMatrix[1][1]=varyMatrix[0][0]=0.83; VaryMatrix(); glutPostRedisplay(); break; } } //上下左后键 void keyFunc2(GLint key,GLint xMouse,GLint yMouse){ switch(key){ case GLUT_KEY_UP: varyMatrix[2][1]=0.2; VaryMatrix(); glutPostRedisplay(); break; case GLUT_KEY_DOWN: varyMatrix[2][1]=-0.2; VaryMatrix(); glutPostRedisplay(); break; case GLUT_KEY_LEFT: varyMatrix[2][0]=-0.2; VaryMatrix(); glutPostRedisplay(); break; case GLUT_KEY_RIGHT: varyMatrix[2][0]=0.2; VaryMatrix(); glutPostRedisplay(); break; } } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(50,100); glutInitWindowSize(550, 550); glutCreateWindow("键盘响应!qw(旋转)ert(对称)asd(错切)zx(放大缩小)上下左右键移动方向"); glClearColor(0.0,0.0,0.0,1); glutKeyboardFunc(keyFunc1); glutSpecialFunc(keyFunc2); glutDisplayFunc(reDraw); glutMainLoop(); return 0; } ``` ```

请问如何将OpenGL中的纹理单元传递给片段着色器???

小白我最近在学习GLSL,绝对的菜鸟级别啊~ 今天遇到了一个问题,我用 *texture[1]* 在程序中存储了一个纹理,然后使用了下面两行代码将纹理传递给片段着色器,程序是可以运行的 *texUnitLoc = glGetUniformLocation(p, "tex"); glUniform1i(texUnitLoc, 0);* 但是当我在程序中存储两个纹理以后,想要将两个纹理都传递到着色器时,应该怎么办啊??将上面代码中的‘0’换做‘texture[0]’也不管用啊,什么都不管用啊,glUniform究竟是怎么一回事啊?求大神指点~~T_T 附代码如下: #include <stdio.h> #include <stdlib.h> #include <GL/glew.h> #include <GL/glut.h> #include <glaux.h> #include "textfile.h" #include "printInfoLog.h" GLfloat LightAmbient0[]= {1, 1, 1, 1.0f }; GLfloat LightDiffuse0[]= {1.0f, 1.0f, 1.0f, 1.0f }; GLfloat LightSpecular0[]={1.0f, 1.0f, 1.0f, 1.0f}; GLfloat LightPosition0[]= {3.0f, 3.0f, 3.0f, 0.0f }; GLfloat MaterialAmbient[] = {0.3, 0.3, 0.3, 1.0f}; GLfloat MaterialDiffuse[] = {0.7, 0.7, 0.7, 1.0f}; GLfloat MaterialSpecular[] ={0.3, 0.3, 0.3, 1.0f}; GLfloat MaterialSe = 64.0f; GLUquadricObj *quadratic; GLuint texture[2]; // 存储一个纹理 GLint texUnitLoc,l3dUnitLoc; float a = 0.0f; GLuint v,f,p; float t = 0; GLint loc; AUX_RGBImageRec *LoadBMP(char *Filename) // 载入位图图象 { FILE *File=NULL; // 文件句柄 if (!Filename) // 确保文件名已提供 { return NULL; // 如果没提供,返回 NULL } File=fopen(Filename,"r"); // 尝试打开文件 if (File) // 文件存在么? { fclose(File); // 关闭句柄 return auxDIBImageLoad(Filename);// 载入位图并返回指针 } return NULL; // 如果载入失败,返回 NULL } int LoadGLTextures() // 载入位图(调用上面的代码)并转换成纹理 { int Status=FALSE; // 状态指示器 AUX_RGBImageRec *TextureImage[2]; // 创建纹理的存储空间 memset(TextureImage,0,sizeof(void *)*1); // 将指针设为 NULL // 载入位图,检查有无错误,如果位图没找到则退出 if (TextureImage[0]=LoadBMP("Data/Crate.bmp")) { Status=TRUE; // 将 Status 设为 TRUE glGenTextures(1, &texture[0]); // 创建纹理 // 创建 MipMapped 纹理 glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); } if (TextureImage[0]) // 纹理是否存在 { if (TextureImage[0]->data) // 纹理图像是否存在 { free(TextureImage[0]->data); // 释放纹理图像占用的内存 } free(TextureImage[0]); // 释放图像结构 } if (TextureImage[1]=LoadBMP("Data/1.bmp")) { Status=TRUE; // 将 Status 设为 TRUE glGenTextures(1, &texture[1]); // 创建纹理 // 创建 MipMapped 纹理 glBindTexture(GL_TEXTURE_2D, texture[1]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data); } if (TextureImage[1]) // 纹理是否存在 { if (TextureImage[1]->data) // 纹理图像是否存在 { free(TextureImage[1]->data); // 释放纹理图像占用的内存 } free(TextureImage[1]); // 释放图像结构 } return Status; // 返回 Status } void changeSize(int w, int h) { float ratio = 1.0* w / h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, w, h); gluPerspective(45,ratio,1,100); glMatrixMode(GL_MODELVIEW); } void init() { glEnable(GL_DEPTH_TEST); glClearColor(0.0,0.0,0.0,0.0); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient0); glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse0); glLightfv(GL_LIGHT0,GL_SPECULAR, LightSpecular0); glLightfv(GL_LIGHT0, GL_POSITION, LightPosition0); glEnable(GL_LIGHT0); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, MaterialAmbient); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialDiffuse); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR, MaterialSpecular); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS, MaterialSe); quadratic=gluNewQuadric(); gluQuadricNormals(quadratic, GLU_SMOOTH); gluQuadricTexture(quadratic, GL_TRUE); LoadGLTextures(); // 调用纹理载入子例程 glEnable(GL_TEXTURE_2D); // 启用纹理映射 } void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0.0,0.0,5.0, 0.0,0.0,-1.0, 0.0f,1.0f,0.0f); a += 0.35f; if(a > 360) a -= 360.0f; glRotatef(a, 0, 1, 0); glRotatef(0, 1, 0, 0); //glBindTexture(GL_TEXTURE_2D, texture[0]); gluSphere(quadratic, 1, 32, 16); t+=0.01; glutPostRedisplay(); glutSwapBuffers(); } int main(int argc, char **argv) { void setShaders(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutCreateWindow("GLSL DEMO"); init(); glutDisplayFunc(renderScene); glutReshapeFunc(changeSize); glewInit(); if (glewIsSupported("GL_VERSION_2_0")) printf("Ready for OpenGL 2.0\n"); else { printf("OpenGL 2.0 not supported\n"); exit(1); } setShaders(); texUnitLoc = glGetUniformLocation(p, "tex"); glUniform1i(texUnitLoc, 0); //l3dUnitLoc = glGetUniformLocation(p, "l3d"); //glUniform1i(l3dUnitLoc, texture[1]); glutMainLoop(); return 0; } void setShaders() { char *vs = NULL,*fs = NULL,*fs2 = NULL; v = glCreateShader(GL_VERTEX_SHADER); f = glCreateShader(GL_FRAGMENT_SHADER); vs = textFileRead("vs.txt"); fs = textFileRead("fs.txt"); const char * vv = vs; const char * ff = fs; glShaderSource(v, 1, &vv,NULL); glShaderSource(f, 1, &ff,NULL); free(vs);free(fs); glCompileShader(v); glCompileShader(f); printShaderInfoLog(v); printShaderInfoLog(f); p = glCreateProgram(); glAttachShader(p,v); glAttachShader(p,f); glLinkProgram(p); printProgramInfoLog(p); glUseProgram(p); //将p改为0表示使用固定的管线 }

OpenGL中glutMouseFunc函数问题

我想在MouseFunc中给一个结构体中成员赋值,但是一直报错Segmentation fault (core dumped),用gdb查了一下是这个赋值语句的问题,以下是代码 ``` #include<GL/glut.h> #include<stdio.h> #include<math.h> #include<malloc.h> #include<stdlib.h> struct PointD { int x; int y; PointD* next; }; struct PointU { int x; int y; PointU* next; }; struct PointD* D=NULL; struct PointU* U=NULL; void MouseClick(int btn,int state ,int x,int y) { x=ReCoordinate(x); y=ReCoordinate(y); x=x-half; y=half-y; if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) { D->x=x; //报错语句 D->y=y;//报错语句 PointD* p=NULL; p=(PointD*)malloc(sizeof(PointD)); D->next=p;//报错语句 D=D->next; pressed=1; } else if(btn==GLUT_LEFT_BUTTON && state==GLUT_UP && pressed==1) { U->x=x; U->y=y; PointU* q=NULL; q=(PointU*)malloc(sizeof(PointU)); U->next=q; U=U->next; pressed=0; glutPostRedisplay; } } int main(int argc,char** argv) { D=(struct PointD*)malloc(sizeof(PointD)); U=(struct PointU*)malloc(sizeof(PointU)); glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE|GLUT_DEPTH); glutInitWindowPosition(100,100); glutInitWindowSize(800,800); int half=400; glutCreateWindow("TEST"); glMatrixMode(GL_PROJECTION); glOrtho(-half,half,-half,half,-half,half); glutDisplayFunc(&display); glutMouseFunc(MouseClick); glutMainLoop(); return 0; } ```

OpenGL 纹理 程序能运行,但是没结果

#include <gl/glew.h> #include <gl/glut.h> #ifdef _DEBUG #include <iostream> #endif #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtx/euler_angles.hpp> #include "imageloader.h" // program static GLuint program, textureId; // width and height of window static GLint width, height; // degree of rotation GLfloat angle = 0.0f; //---------------------------------------------------- // // Shader Source // static const char* vShaderSource = "#version 450 core\n" "layout (location = 0) in vec3 vpos;\n" //"layout (location = 1) in vec4 vcolor;\n" "layout (location = 1) in vec2 ptex;\n" "layout (location = 2) uniform mat4 mvp;\n" "out vec4 color;\n" "out vec2 vtex;\n" "void main()\n" "{\n" " gl_Position = mvp * vec4(vpos,1.0f);\n" //" color = vcolor;\n" " vtex = ptex;\n" "}\n"; static const char* fShaderSource = "#version 450 core\n" //"in vec4 color;\n" "in vec2 vtex;\n" "uniform sampler2D tex;\n" "out vec4 fcolor;\n" "void main()\n" "{\n" " fcolor = texture(tex, vtex);\n" "}\n"; //----------------------------------------------------------- // // vertices (position and color) // static const struct { float x, y, z; }vertices[6] = { // front { -1.0f, 1.0f, 1.0f }, { -1.0f, -1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f }, { 1.0f, -1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f } }; static const struct { float x, y; } texv[6] = { {0.0f,1.0f}, {0.0f,0.0f}, {1.0f,0.0f}, {1.0f,0.0f}, {1.0f,1.0f}, {0.0f,1.0f} }; static GLuint vt; void Init_shader() { program = glCreateProgram(); GLuint vshader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vshader, 1, &vShaderSource, NULL); glCompileShader(vshader); //------------------------------------------------------------------------------{ //check GLint compiled; glGetShaderiv(vshader, GL_COMPILE_STATUS, &compiled); if (!compiled) { #ifdef _DEBUG GLsizei len; glGetShaderiv(vshader, GL_INFO_LOG_LENGTH, &len); GLchar* log = new GLchar[len + 1]; glGetShaderInfoLog(vshader, len, &len, log); std::cerr << "1 Shader compilation failed: " << log << std::endl; delete[] log; #endif return; } //----------------------------------------------------------------------------} glAttachShader(program, vshader); GLuint fshader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fshader, 1, &fShaderSource, NULL); glCompileShader(fshader); //---------------------------------------------------------------------------{ // check glGetShaderiv(fshader, GL_COMPILE_STATUS, &compiled); if (!compiled) { #ifdef _DEBUG GLsizei len; glGetShaderiv(fshader, GL_INFO_LOG_LENGTH, &len); GLchar* log = new GLchar[len + 1]; glGetShaderInfoLog(fshader, len, &len, log); std::cerr << "1 Shader compilation failed: " << log << std::endl; delete[] log; #endif return; } //--------------------------------------------------------------------------} glAttachShader(program, fshader); glLinkProgram(program); //--------------------------------------------------------------------------{ // check GLint linked; glGetProgramiv(program, GL_LINK_STATUS, &linked); if (!linked) { #ifdef _DEBUG GLsizei len; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len); GLchar* log = new GLchar[len + 1]; glGetProgramInfoLog(program, len, &len, log); std::cerr << "Shader linking failed: " << log << std::endl; delete[] log; #endif glDeleteShader(GL_VERTEX_SHADER); glDeleteShader(GL_FRAGMENT_SHADER); return; } //----------------------------------------------------------------------------} } void Init_data() { glGenVertexArrays(1,&vt); glBindVertexArray(vt); GLuint vbuff; glGenBuffers(1, &vbuff); glBindBuffer(GL_ARRAY_BUFFER, vbuff); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 7, (void*)0); Image* image = loadBMP("bird.bmp"); glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_2D, textureId); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels); delete image; GLuint tbuff; glGenBuffers(1, &tbuff); glBindBuffer(GL_ARRAY_BUFFER, tbuff); glBufferData(GL_ARRAY_BUFFER, sizeof(texv), texv, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (void*)0); } void Init_funcSet() { glViewport(0, 0, width, height); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_LINE_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); } void display() { glm::mat4 Projection = glm::perspective(45.0f, (float)(width / height), 0.1f, 100.0f); glm::mat4 View = glm::lookAt( glm::vec3(4, 3, -3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0) ); glm::mat4 Model = glm::mat4(1.0f); glm::mat4 RotationMatrix = glm::eulerAngleY(angle); glm::mat4 mvp = Projection * View * RotationMatrix * Model; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(program); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureId); glUniformMatrix4fv(2, 1, GL_FALSE, &mvp[0][0]); glBindVertexArray(vt); glDrawArrays(GL_TRIANGLES, 0, 6); glFlush(); glutSwapBuffers(); } //------------------------------------ // // Reshape window // void reshape(int x, int y) { glViewport(0, 0, x, y); width = x; height = y; } //--------------------------------------- // // update function // void update(int value) { angle += 0.02f; if (angle > 360) { angle -= 360; } glutPostRedisplay(); glutTimerFunc(20, update, 0); } int main(int argc, char ** argv) { width = height = 500; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(width, height); glutCreateWindow("OpenGL"); if (glewInit()) { #ifdef _DEBUG std::cerr << "Unable to initialize GLEW ... exiting" << std::endl; #endif exit(EXIT_FAILURE); } Init_shader(); Init_funcSet(); Init_data(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutTimerFunc(20, update, 0); glutMainLoop(); //return 0; // never reach } ``` ```

新手问题opengl贴不上图片

这是源码,在一个旋转的正方体上贴图,结果整个正方体变成了白色的 #include <iostream> #include <stdlib.h> #include <GL/glut.h> #include<glaux.h> using namespace std; //纹理数组 GLuint texture[1]; float _angle=0.0f; AUX_RGBImageRec *LoadBMP(char *Filename) { FILE *file=NULL; if(!Filename) { return NULL; } file=fopen(Filename,"r"); if(file) { fclose(file); return auxDIBImageLoad((LPCWSTR )Filename); } return NULL; } int LoadGLTexture() { int Status =FALSE; AUX_RGBImageRec *TextureImage[1]; //创建空间 memset(TextureImage,0,sizeof(void *)*1);//清除图像记录 if(TextureImage[0]=LoadBMP("vtr.bmp")) { Status=TRUE; glGenTextures(1,&texture[0]);//创建纹理 glBindTexture(GL_TEXTURE_2D,texture[0]);//绑定纹理 glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY, 0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data); // 线形滤波 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // 线形滤波 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[0]) // 纹理是否存在 { if (TextureImage[0]->data)// 纹理图像是否存在 { // 释放纹理图像占用的内存 free(TextureImage[0]->data); } free(TextureImage[0]); // 释放图像结构 } return Status; } void handleKeyPressed(unsigned char key,int x,int y)//按键及鼠标坐标 { switch(key) { case 27:exit(0); } } //初始化3D视角 void initRendering() { glEnable(GL_DEPTH_TEST); } //窗口改变大小时调用 void handleResize(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity();//重设视角 gluPerspective(45.0,(double)w/(double)h,1.0,200.0); glEnable(GL_TEXTURE_2D); // 启用纹理映射 glShadeModel(GL_SMOOTH); // 启用阴影平滑 glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // 黑色背景 glClearDepth(1.0f); // 设置深度缓存 glEnable(GL_DEPTH_TEST); // 启用深度测试 glDepthFunc(GL_LEQUAL); // 所作深度测试的类型 // 真正精细的透视修正 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } //画图 void drawScence() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -5.0f); //Move forward 5 units glRotatef(_angle, 0.1f, 0.1f, 0.1f); //Rotate the camera glBindTexture(GL_TEXTURE_2D, texture[0]); // 选择纹理 glBegin(GL_QUADS); // 前面 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // 纹理和四边形的左下 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // 纹理和四边形的右下 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // 纹理和四边形的右上 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // 纹理和四边形的左上 // 后面 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // 纹理和四边形的右下 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // 纹理和四边形的右上 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // 纹理和四边形的左上 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // 纹理和四边形的左下 // 顶面 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // 纹理和四边形的左上 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // 纹理和四边形的左下 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // 纹理和四边形的右下 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // 纹理和四边形的右上 // 底面 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // 纹理和四边形的右上 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // 纹理和四边形的左上 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // 纹理和四边形的左下 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // 纹理和四边形的右下 // 右面 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // 纹理和四边形的右下 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // 纹理和四边形的右上 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // 纹理和四边形的左上 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // 纹理和四边形的左下 // 左面 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // 纹理和四边形的左下 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // 纹理和四边形的右下 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // 纹理和四边形的右上 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // 纹理和四边形的左上 glEnd(); glutSwapBuffers();//将3d图画放到窗口 } void updateAngle(int v) { _angle+=2.0f; if(_angle>360) { _angle-=360; } glutPostRedisplay(); glutTimerFunc(25,updateAngle,0); } int main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,500); //建立窗口 glutCreateWindow("我的三角形"); initRendering(); //gl的函数,实现具体绘制 glutDisplayFunc(drawScence); glutKeyboardFunc(handleKeyPressed); glutReshapeFunc(handleResize); glutTimerFunc(25,updateAngle,0); glutMainLoop(); return 0; }

反应扩散Gray-Scott模型问题(OpenGL纹理)

![图片说明](https://img-ask.csdn.net/upload/201703/31/1490936767_681612.png) **谁能告诉我什么问题?以上是运行结果,一下是代码。** #include "GrayScott.h" #include "math.h" laplace GS_Grid[WIDTH][HEIGHT] = {}; GLubyte GS_Pix[WIDTH][HEIGHT][4]; //纹理数据 GLubyte GS_test[WIDTH][HEIGHT][4]; float GS_LAPLACIAN[3][3] = { { 0.05,0.2,0.05 }, { 0.2, -1, 0.2 }, { 0.05,0.2,0.05 } }; //初始化 void GS_Initialize() { glClearColor(0.0, 0.0, 0.0, 0.0); for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { GS_Grid[i][j].a = 1;//rand()/100000.0; GS_Grid[i][j].b = 0.5;// rand() / 100000.0; GS_test[i][j][0] = (GLubyte)100; GS_test[i][j][1] = (GLubyte)100; GS_test[i][j][2] = (GLubyte)100; GS_test[i][j][3] = (GLubyte)255; } } for (int i = 1; i < 30; i++) { for (int j = 1; j < 30; j++) { //GS_Grid[i][j].b = 1.0; } } GS_PixelUpdate(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //设置纹理参数 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GS_test); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //设置纹理环境 glEnable(GL_TEXTURE_2D); } //交换 void GS_Swap() { } //更新 void GS_PixelUpdate() { int i, j, c; GS_ReactionDiffusion(); //glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGR_EXT, GL_UNSIGNED_BYTE, &pix); for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { //c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;//黑白方块交替 //int c = max(min(texNext[i][j].a - texNext[i][j].b, 1), 0); c = floor((GS_Grid[i][j].a - GS_Grid[i][j].b) * 255); GS_Pix[i][j][0] = (GLubyte)c; GS_Pix[i][j][1] = (GLubyte)GS_Grid[i][j].a*255;// floor(GS_Grid[i][j].a * 255); GS_Pix[i][j][2] = (GLubyte)GS_Grid[i][j].b * 255;// floor(GS_Grid[i][j].b * 255); GS_Pix[i][j][3] = (GLubyte)255; } } } //反应扩散 void GS_ReactionDiffusion() { //反应扩散 for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { float a = GS_Grid[x][y].a; float b = GS_Grid[x][y].b; //更新 GS_Grid[x][y].a = a + ((GS_DA*GS_Laplacian(x, y, GS_U)) - (a*b*b) + (GS_FEED*(1 - a)))*GS_DT; GS_Grid[x][y].b = b + ((GS_DB*GS_Laplacian(x, y, GS_V)) + (a*b*b) - ((GS_KILL + GS_FEED)*b))*GS_DT; } } } //显示纹理 void GS_Display() { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GS_test); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glPushMatrix(); glColor4f(0, 0, 1, 1); glTranslatef(0.0, 0.0, -7.0); //glRotatef(xRot, 1, 0, 0); glBegin(GL_QUADS); //指定纹理坐标 glTexCoord2f(0.0, 0.0); glVertex3f(-3.0, -3.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-3.0, 3.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(3.0, 3.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(3.0, -3.0, 0.0); glEnd(); glPopMatrix(); glFlush(); } //拉普拉斯算子 float GS_Laplacian(int x, int y, int target) { GLfloat sum = 0.0; if (target == GS_U) { sum += GS_LAPLACIAN[1][1] * GS_Grid[x][y].a; sum += GS_LAPLACIAN[1][1 - 1] * GS_Grid[x][y - 1].a; sum += GS_LAPLACIAN[1][1 + 1] * GS_Grid[x][y + 1].a; sum += GS_LAPLACIAN[1 - 1][1] * GS_Grid[x - 1][y].a; sum += GS_LAPLACIAN[1 + 1][1] * GS_Grid[x + 1][y].a; sum += GS_LAPLACIAN[1 - 1][1 - 1] * GS_Grid[x - 1][y - 1].a; sum += GS_LAPLACIAN[1 + 1][1 - 1] * GS_Grid[x + 1][y - 1].a; sum += GS_LAPLACIAN[1 + 1][1 + 1] * GS_Grid[x + 1][y + 1].a; sum += GS_LAPLACIAN[1 - 1][1 + 1] * GS_Grid[x - 1][y + 1].a; } else if (GS_V == target) { sum += GS_LAPLACIAN[1][1] * GS_Grid[x][y].b; sum += GS_LAPLACIAN[1][1 - 1] * GS_Grid[x][y - 1].b; sum += GS_LAPLACIAN[1][1 + 1] * GS_Grid[x][y + 1].b; sum += GS_LAPLACIAN[1 - 1][1] * GS_Grid[x - 1][y].b; sum += GS_LAPLACIAN[1 + 1][1] * GS_Grid[x + 1][y].b; sum += GS_LAPLACIAN[1 - 1][1 - 1] * GS_Grid[x - 1][y - 1].b; sum += GS_LAPLACIAN[1 + 1][1 - 1] * GS_Grid[x + 1][y - 1].b; sum += GS_LAPLACIAN[1 + 1][1 + 1] * GS_Grid[x + 1][y + 1].b; sum += GS_LAPLACIAN[1 - 1][1 + 1] * GS_Grid[x - 1][y + 1].b; } return sum; } void GS_Reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 30.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //计时器 void GS_OnTimer(int value) { GS_PixelUpdate(); glutPostRedisplay(); glutTimerFunc(500, GS_OnTimer, 1); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); GS_Initialize(); glutDisplayFunc(GS_Display); glutReshapeFunc(GS_Reshape); glutTimerFunc(100, GS_OnTimer, 1); glutMainLoop(); } ``` ```

程序能编译正确连接,但是不能把旋转的正方形画出来

#include "stdafx.h" #include<math.h> #include<gl/glut.h> #define DEG_TO_RAD 0.017453 GLfloat theta = 0.0; void mydisplay() { glClearColor(0.0, 0.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(1.0, 0.0, 0.0); glVertex2f(sin(DEG_TO_RAD*theta), cos(DEG_TO_RAD*theta)); glVertex2f(-sin(DEG_TO_RAD*theta), cos(DEG_TO_RAD*theta)); glVertex2f(-sin(DEG_TO_RAD*theta), -cos(DEG_TO_RAD*theta)); glVertex2f(sin(DEG_TO_RAD*theta), -cos(DEG_TO_RAD*theta)); glEnd(); glFlush(); } void myidle() { glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(450, 400); glutCreateWindow("sample"); glutDisplayFunc(mydisplay); glutIdleFunc(myidle); glutMainLoop(); return 0; } 这是我根据书上写的一个程序,功能说能画一个旋转的正方形,但是我在vs上运行时什么都没有,求大家帮忙解决一下哈。。

OpenGL 用三角形扇绘制圆 报错GLUT Fatal Error: pixel format with necessary capabilities not

#include<GLTools.h> #include<GLShaderManager.h> #include<GL/glut.h> int g_width = 800; int g_height = 600; int nStep = 200; const GLfloat a = 0.6f, b = 0.3f; const GLfloat Pi = 3.1415926536f; GLShaderManager shaderManager; GLBatch pointBatch; GLBatch lineBatch; GLBatch lineStripBatch; GLBatch lineLoopBatch; GLBatch triangleBatch; GLBatch triangleStripBatch; GLBatch triangleFanBatch; void initialize(); void uninitialize(); void display_func(void); void reshape_func(int w, int h); void key_func(unsigned char key, int x, int y); void idle_func(); void initialize() { glClearColor(0.0, 0.0, 0.0, 1.0); glClearDepth(1.0); shaderManager.InitializeStockShaders(); GLfloat vCoast[24][3] = { {2.80,1.20,0.0},{2.0,1.20,0.0}, {2.0,1.08,0.0},{2.0,1.08,0.0}, {0.0,0.80,0.0},{-.40,0.40,0.0}, {-.48,0.2,0.0},{-.40,0.0,0.0}, {-.60,-.40,0.0},{-.80,-.80,0.0}, {-.80,-1.4,0.0},{-.40,-1.60,0.0}, {0.0,-1.20,0.0},{.2,-.80,0.0}, {.48,-.40,0.0},{.2,-.80,0.0}, {.48,.20,0.0},{.80,.40,0.0}, {1.20,.80,0.0},{1.60,.60,0.0}, {2.0,.60,0.0},{2.2,.80,0.0}, {2.40,1.0,0.0},{2.80,1.0,0.0} }; for (int i = 0; i < 24; i++) { vCoast[i][0] *= 0.3; vCoast[i][1] *= 0.3; vCoast[i][2] *= 0.3; } //Load point batch pointBatch.Begin(GL_POINT, 24); pointBatch.CopyVertexData3f(vCoast); pointBatch.End(); //Load as a bunch of line segments lineBatch.Begin(GL_LINES, 24); lineBatch.CopyVertexData3f(vCoast); lineBatch.End(); //Load as a single line segment lineStripBatch.Begin(GL_LINE_STRIP, 24); lineStripBatch.CopyVertexData3f(vCoast); lineStripBatch.End(); //Single line,connect first and last points lineLoopBatch.Begin(GL_LINE_LOOP, 24); lineLoopBatch.CopyVertexData3f(vCoast); lineLoopBatch.End(); GLfloat vPyramid[16][3] = {0}; float angle = 0; int i = 0; for (; angle < (2.0f*Pi)&&i<16; angle += (Pi / 8.0f),i++) { int x = 50.0f*sin(angle); int y = 50.f*cos(angle); vPyramid[i][1] = x; vPyramid[i][2] = y; vPyramid[i][3] = 0.0f; } //Load as a triangle fan triangleFanBatch.Begin(GL_TRIANGLE_FAN,16); triangleFanBatch.CopyVertexData3f(vPyramid); triangleFanBatch.End(); } void display_func(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSwapBuffers(); } void idle_func() { glutPostRedisplay(); } void reshape_func(int w, int h) { glViewport(0, 0, w, h); } void key_func(unsigned char key, int x, int y) { switch (key) { case 49: case 50: glLineWidth(2.0f); lineStripBatch.Draw(); glLineWidth(1.0f); break; case 51: glLineWidth(2.0f); lineLoopBatch.Draw(); glLineWidth(1.0f); break; case 52: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); triangleBatch.Draw(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; case 53: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); triangleFanBatch.Draw(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; case 54: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); triangleStripBatch.Draw(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; } } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(g_width, g_height); glutInitDisplayMode(GLUT_DEPTH || GLUT_DOUBLE || GLUT_RGB); glutCreateWindow("MyShapes"); initialize(); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "GLEW Error: $s\n", glewGetErrorString(err)); return 1; } glutDisplayFunc(display_func); glutReshapeFunc(reshape_func); glutKeyboardFunc(key_func); glutIdleFunc(idle_func); glutMainLoop(); return 0; }

代码正常运行一会后出现 0xC0000005: 读取位置 0x00000010 时发生访问冲突

#include <gl\glut.h> #include<iostream> #include<fstream> #include<sstream> #include<stdio.h> #include<stdlib.h> #include<math.h> using namespace std; typedef struct V { GLdouble x; GLdouble y; GLdouble z; GLdouble X; GLdouble Y; GLdouble Z; GLdouble XN; GLdouble YN; GLdouble ZN; }V;//记录每个顶点窗口坐标、三维坐标以及法向量 V *vertex; int v_num[2]={0,0}; //记录点的数量 int vn_num[2]={0,0};//记录法线的数量 int f_num[2]={0,0}; //记录面的数量 GLdouble **vArr; //存放点的二维数组 GLdouble **vnArr;//存放法线的二维数组 int **fvArr; //存放面顶点的二维数组 int **fnArr; //存放面法线的二维数组 char s1[100];//读取行的数组 GLfloat f2,f3,f4;//读取每行内容的变量 int a=0; char showv[25]=""; char showf[25]=""; char space[5]=" "; char showv1[25]=""; char showvn[25]=""; char vx[25]=""; char vy[25]=""; char vz[25]=""; char vnx[25]=""; char vny[25]=""; char vnz[25]="";//输出字符组 const int SIZE=256; float movex=0; float movey=0; float len=4; int x=0,y=0,z=0; int xRange=0; int yRange=0; //运动变量 GLfloat LightAmbient[]= { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat LightDiffuse[]= { 1.0f, 1.0f, 8.0f, 10.0f }; GLfloat LightPosition[]= { 1.0f, 1.0f, 1.0f, 1.0f }; //灯光属性 int startx; int starty;//鼠标点击坐标记录 int getLineNum(string addrstr)//读取文件的顶点数和面数 { v_num[a]=0; vn_num[a]=0; f_num[a]=0; ifstream infile(addrstr.c_str()); char sline[100]; while(infile.getline(sline,100)) { if(sline[0]=='v') { if(sline[1]=='n') vn_num[a]++; else v_num[a]++; } else if (sline[0]=='f') f_num[a]++; } itoa(v_num[a],showv,10); itoa(f_num[a],showf,10); return 0; } int readfile(string addrstr) //将文件内容读到数组中去 { vertex=new V[v_num[a]]; vArr=new GLdouble*[v_num[a]]; for (int i=0;i<v_num[a];i++) { vArr[i]=new GLdouble[3]; } vnArr=new GLdouble*[vn_num[a]]; for (int i=0;i<vn_num[a];i++) { vnArr[i]=new GLdouble[3]; } fvArr=new int*[f_num[a]]; fnArr=new int*[f_num[a]]; for (int i=0;i<f_num[a];i++) { fvArr[i]=new int[3]; fnArr[i]=new int[3]; } ifstream infile(addrstr.c_str()); int ii=0,jj=0,kk=0; char sline[100]; while(infile.getline(sline,100)) { if(sline[0]=='v') { if(sline[1]=='n')//vn { istringstream sin(sline); sin>>s1>>f2>>f3>>f4; vnArr[ii][0]=f2; vnArr[ii][1]=f3; vnArr[ii][2]=f4; vertex[ii].XN=f2; vertex[ii].YN=f3; vertex[ii].ZN=f4; ii++; } else//v { istringstream sin(sline); sin>>s1>>f2>>f3>>f4; vArr[jj][0]=f2; vArr[jj][1]=f3; vArr[jj][2]=f4; vertex[jj].X=f2; vertex[jj].Y=f3; vertex[jj].Z=f4; jj++; } } if (sline[0]=='f') //读取面 { istringstream in(sline); GLfloat a; in>>s1;//去掉前缀f int i,k; for(i=0;i<3;i++) { in>>s1; //取得顶点索引和法线索引 a=0; for(k=0;s1[k]!='/';k++) { a=a*10+(s1[k]-48); } fvArr[kk][i]=a; a=0; for(k=k+2;s1[k];k++) { a=a*10+(s1[k]-48);; } fnArr[kk][i]=a; } kk++; } } return 0; } void drawbunny() { glBegin(GL_TRIANGLES); for (int i=0;i<f_num[a];i++) { glNormal3f(vnArr[fnArr[i][0]-1][0], vnArr[fnArr[i][0]-1][1], vnArr[fnArr[i][0]-1][2]); glVertex3f(vArr[fvArr[i][0]-1][0], vArr[fvArr[i][0]-1][1], vArr[fvArr[i][0]-1][2]); glNormal3f(vnArr[fnArr[i][1]-1][0], vnArr[fnArr[i][1]-1][1], vnArr[fnArr[i][1]-1][2]); glVertex3f(vArr[fvArr[i][1]-1][0], vArr[fvArr[i][1]-1][1], vArr[fvArr[i][1]-1][2]); glNormal3f(vnArr[fnArr[i][2]-1][0], vnArr[fnArr[i][2]-1][1], vnArr[fnArr[i][2]-1][2]); glVertex3f(vArr[fvArr[i][2]-1][0], vArr[fvArr[i][2]-1][1], vArr[fvArr[i][2]-1][2]); } glEnd(); GLuint base;/*显示顶点数,面数,以及鼠标点击顶点坐标,顶点法向量*/ base=glGenLists(256); for(int i=0;i<256;++i) { glNewList(base+i,GL_COMPILE); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,i); glEndList(); } glColor3f(1.0,1.0,1.0); glRasterPos2f(0.0,-0.1); glListBase(base); glCallLists((GLint)strlen("V"),GL_UNSIGNED_BYTE,"V"); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(showv),GL_UNSIGNED_BYTE,showv); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen("F"),GL_UNSIGNED_BYTE,"F"); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(showf),GL_UNSIGNED_BYTE,showf); glRasterPos2f(-0.2,-0.2); glCallLists((GLint)strlen("v"),GL_UNSIGNED_BYTE,"v"); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(vx),GL_UNSIGNED_BYTE,vx); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(vy),GL_UNSIGNED_BYTE,vy); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(vz),GL_UNSIGNED_BYTE,vz); glRasterPos2f(-0.2,-0.15); glCallLists((GLint)strlen("vn"),GL_UNSIGNED_BYTE,"vn"); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(vnx),GL_UNSIGNED_BYTE,vnx); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(vny),GL_UNSIGNED_BYTE,vny); glCallLists((GLint)strlen(space),GL_UNSIGNED_BYTE,space); glCallLists((GLint)strlen(vnz),GL_UNSIGNED_BYTE,vnz); glFlush(); glutSwapBuffers(); } void myDisplay() { } void IdleDisplay() { glEnable(GL_DEPTH_TEST); glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // 设置环境光 glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // 设置漫射光 glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // 设置光源位置 glEnable(GL_LIGHT1); glClearColor(0.0, 0.0, 0.0, 0.0); glViewport(0,0,800,800); GLint viewport[4]; glGetIntegerv( GL_VIEWPORT , viewport ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1, 1, -1, 1, -10, 10); GLdouble projectionMatrix[16]; glGetDoublev( GL_PROJECTION_MATRIX , projectionMatrix ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(movex,movey,0); glScalef(len,len,len); glRotatef(xRange, 1, 0, 0); glRotatef(yRange, 0, 1, 0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f(1.0,0.0,0.0); GLdouble modelMatrix[16]; glGetDoublev( GL_MODELVIEW_MATRIX , modelMatrix ); for(int i=0;i<v_num[a];i++) gluProject(vertex[i].X,vertex[i].Y,vertex[i].Z,modelMatrix,projectionMatrix,viewport,&vertex[i].x,&vertex[i].y,&vertex[i].z);//计算每个顶点的窗口坐标 drawbunny(); } //控制 void moveright() { movex+=0.2; if (movex>5.0) movex=0.0; glutPostRedisplay(); } void moveleft() { movex-=0.2; if (movex<-5.0) movex=0.0; glutPostRedisplay(); } void moveup() { movey+=0.2; if (movey>5.0) movey=0.0; glutPostRedisplay(); } void movedown() { movey-=0.2; if (movey<-5.0) movey=0.0; glutPostRedisplay(); } void shrink() { len-=0.1; if(len<-10) len=-10; glutPostRedisplay(); } void zoom() { len+=0.1; if(len>10) len=10; glutPostRedisplay(); } void specialKeyFunc(int key,int x,int y) { switch(key) { case GLUT_KEY_RIGHT: moveright(); break; case GLUT_KEY_LEFT: moveleft(); break; case GLUT_KEY_DOWN: movedown(); break; case GLUT_KEY_UP: moveup(); break; case GLUT_KEY_F1: zoom(); break; case GLUT_KEY_F2: shrink(); break; default: break; } } void processmenu(int value) { if(value==1) { a=0; getLineNum("bunny.obj"); readfile("bunny.obj"); glutIdleFunc(&IdleDisplay); } if(value==2) { a=1; getLineNum("mycube.obj"); readfile("mycube.obj"); glutIdleFunc(&IdleDisplay); } } void mouse(int button,int state,int x,int y) { if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; if(state==GLUT_DOWN) startx=x,starty=y; GLdouble temp; GLdouble min=100000; for(int i=0;i<v_num[a];i++) { if(vertex[i].x>x-5&&vertex[i].x<x+5&&vertex[i].y>(800-y)-5&&vertex[i].y<800-y+5)//顶点的窗口坐标在鼠标点击范围内 { temp=sqrt(pow(x-vertex[i].x,2)+pow((800-y)-vertex[i].y,2)); if(temp<min)//取距离鼠标点击位置最近的顶点 min=temp; } } int flag=0; GLdouble v[2][4]; GLdouble vn[2][4]; GLdouble zmin; for(int i=0;i<v_num[a];i++) if(sqrt(pow(x-vertex[i].x,2)+pow((800-y)-vertex[i].y,2))==min)//根据获得的最小值依次查询相应顶点 { if(flag==0) { flag++; v[0][0]=vertex[i].X; v[0][1]=vertex[i].Y; v[0][2]=vertex[i].Z; v[0][3]=1; vn[0][0]=vertex[i].XN; vn[0][1]=vertex[i].YN; vn[0][2]=vertex[i].ZN; zmin=vertex[i].z; continue; } if(flag==1)//避免两点恰好重合的情况,如果两点恰好重合,取前点 { v[1][0]=vertex[i].X; v[1][1]=vertex[i].Y; v[1][2]=vertex[i].Z; v[1][3]=1; vn[1][0]=vertex[i].XN; vn[1][1]=vertex[i].YN; vn[1][2]=vertex[i].ZN; if(vertex[i].z>zmin) { v[1][3]=1; v[0][3]=0; } else { v[0][3]=1; v[1][3]=0; } } } if(v[0][3]==1) { sprintf(vx,"%lf",v[0][0]); sprintf(vy,"%lf",v[0][1]); sprintf(vz,"%lf",v[0][2]); sprintf(vnx,"%lf",vn[0][0]); sprintf(vny,"%lf",vn[0][1]); sprintf(vnz,"%lf",vn[0][2]); } if(v[1][3]==1) { sprintf(vx,"%lf",v[1][0]); sprintf(vy,"%lf",v[1][1]); sprintf(vz,"%lf",v[1][2]); sprintf(vnx,"%lf",vn[1][0]); sprintf(vny,"%lf",vn[1][1]); sprintf(vnz,"%lf",vn[1][2]); } glutPostRedisplay(); } void onMouseMove(int x,int y) { xRange += 3.14159265 * (y - starty) / 5; if(xRange >= 360 || xRange <= -360) xRange = 0; yRange += 3.14159265 * (x - startx) / 5; if(yRange >= 360 || yRange <= -360) yRange = 0; startx = x; //把此时的鼠标坐标作为旧值,为下一次计算增量做准备 starty = y; } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowPosition(100, 100); glutInitWindowSize(800, 800); glutCreateWindow("OBJ"); int id=glutCreateMenu(processmenu); glutAddMenuEntry("model1",1); glutAddMenuEntry("model2",2); glutAttachMenu(GLUT_RIGHT_BUTTON); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glutDisplayFunc(&myDisplay); glutSpecialFunc(specialKeyFunc); glutMouseFunc(mouse); glutMotionFunc(onMouseMove); glutMainLoop(); return 0; }

2019 Python开发者日-培训

2019 Python开发者日-培训

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

设计模式(JAVA语言实现)--20种设计模式附带源码

设计模式(JAVA语言实现)--20种设计模式附带源码

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

java后台+微信小程序 实现完整的点餐系统

java后台+微信小程序 实现完整的点餐系统

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

2019 AI开发者大会

2019 AI开发者大会

玩转Linux:常用命令实例指南

玩转Linux:常用命令实例指南

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

4小时玩转微信小程序——基础入门与微信支付实战

4小时玩转微信小程序——基础入门与微信支付实战

Git 实用技巧

Git 实用技巧

Python数据清洗实战入门

Python数据清洗实战入门

使用TensorFlow+keras快速构建图像分类模型

使用TensorFlow+keras快速构建图像分类模型

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

程序员的算法通关课:知己知彼(第一季)

程序员的算法通关课:知己知彼(第一季)

MySQL数据库从入门到实战应用

MySQL数据库从入门到实战应用

机器学习初学者必会的案例精讲

机器学习初学者必会的案例精讲

手把手实现Java图书管理系统(附源码)

手把手实现Java图书管理系统(附源码)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

.net core快速开发框架

.net core快速开发框架

玩转Python-Python3基础入门

玩转Python-Python3基础入门

Python数据挖掘简易入门

Python数据挖掘简易入门

微信公众平台开发入门

微信公众平台开发入门

程序员的兼职技能课

程序员的兼职技能课

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

HoloLens2开发入门教程

HoloLens2开发入门教程

微信小程序开发实战

微信小程序开发实战

Java8零基础入门视频教程

Java8零基础入门视频教程

Python可以这样学(第一季:Python内功修炼)

Python可以这样学(第一季:Python内功修炼)

C++语言基础视频教程

C++语言基础视频教程

Python可以这样学(第四季:数据分析与科学计算可视化)

Python可以这样学(第四季:数据分析与科学计算可视化)

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

Python数据分析与挖掘

Python数据分析与挖掘

微信小程序开发实战之番茄时钟开发

微信小程序开发实战之番茄时钟开发

软件测试2小时入门

软件测试2小时入门

相关热词 c# 局部 截图 页面 c#实现简单的文件管理器 c# where c# 取文件夹路径 c# 对比 当天 c# fir 滤波器 c# 和站 队列 c# txt 去空格 c#移除其他类事件 c# 自动截屏
立即提问