NURBS表面使用OpenGL和过剩 [英] NURBS surface using OpenGL and glut
问题描述
我正在尝试使用OpenGL和glut库显示一个拉伸圆。最终表面应该看起来像一个完美的圆管。但是,我在显示器上看到的 而是锯齿状的"管"。我为此目的使用的代码是 附上
。我很感激任何提示来解决这个问题。
谢谢!
Hadi
#include" stdafx.h"
#include< stdio.h>
#include< stdlib.h>
#include< GL / freeglut.h>
#include< cstdio>
#include< iostream>
using namespace std;
$
#define S_NUMPOINTS 9
#define S_ORDER 3
#define S_NUMKNOTS (S_NUMPOINTS + S_ORDER)
#define T_NUMPOINTS 2
#define T_ORDER 2
#define T_NUMKNOTS (T_NUMPOINTS + T_ORDER)
$
#define SQRT2 1.414213
$
#define SQRT2HALF 0.707107
$
/ *初始化本地数据* / b
GLfloat sknots [S_NUMKNOTS] =
{0,0.0,0.1,1。,2.,2.,3.,3.,4.,4.,4。};
GLfloat tknots [T_NUMKNOTS] =
{0.0,0.0,1.0,1.0};
GLfloat ctlpoints [S_NUMPOINTS] [T_NUMPOINTS] [4] =
{
{{1.0,0.0,1.0,1.0},{1.0,0.0,0.0,1.0}},
{{1.0,1.0,1.0,SQRT2HALF},{1.0,1.0,0.0,SQRT2HALF}},
{{0.0,1.0,1.0,1.0},{0.0,1.0,0.0,1.0}},
{{-1.0,1.0,1.0,SQRT2HALF},{-1.0,1.0,0.0,SQRT2HALF}},
{{-1.0,0.0,1.0,1.0},{-1.0,0.0,0.0,1.0}},
{{-1.0,-1.0,1.0,SQRT2HALF},{-1.0,-1.0,0.0,SQRT2HALF}},
{{0.0,-1.0,1.0,1.0},{0.0,-1.0,0.0,1.0}},
{{1.0,-1.0,1.0,SQRT2HALF},{1.0,-1.0,0.0,SQRT2HALF}},
{{1.0,0.0,1.0,1.0},{1.0,0.0,0.0,1.0}},
}
GLUnurbsObj * theNurb;
/ * 初始化材料属性,光源,照明模型,
* 和深度缓冲。
* /
$
void myinit(无效)
{
GLfloat mat_ambient [] = {1.0,1.0,1.0,1.0};
GLfloat mat_diffuse [] = {1.0,0.2,1.0,1.0};
GLfloat mat_specular [] = {1.0,1.0,1.0,1.0};
GLfloat mat_shininess [] = {50.0};
GLfloat light0_position [] = {1.0,0.1,1.0,0.0};
GLfloat light1_position [] = {-1.0,0.1,1.0,0.0};
GLfloat lmodel_ambient [] = {0.3,0.3,0.3,1.0};
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_POSITION,light0_position);
glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
theNurb = gluNewNurbsRenderer();
gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,25.0);
gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_FILL);
}
无效显示(无效)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(4。,4.5,2.5);
glRotatef(220.0,1。,0.,0。);
glRotatef(115.0,0.1,1。,0。);
glTranslatef(-4。, - 4.5,-2.5);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb,
S_NUMKNOTS,sknots,
T_NUMKNOTS,tknots,
4 * T_NUMPOINTS,
4,
& ctlpoints [0] [0] [0],
; S_ORDER,T_ORDER,
GL_MAP2_VERTEX_4);
gluEndSurface(theNurb);
glPopMatrix();
glFlush();
}
void myReshape(int w,int h)
{
w = 400;
h = 400;
glViewport(100,100,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.5,0.5,0.8,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(7.0,4.5,4.0,4.5,4.5,2.0,6.0,-3.0,2.0);
}
/ b *&NBSP;主循环
* 打开窗口,初始窗口大小,标题栏,
* RGBA显示模式,并处理输入事件。
* /
$
int main(int argc,char ** argv)
{ ;
glutInit(& argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(argv [0]);
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(显示);
glutMainLoop();
返回0; / * ANSI C要求main返回int。 * / b
}
你好,
也许你应该问这个论坛:
https://www.opengl.org/discussion_boards/
Hi,
I am trying to display an extruded circle using OpenGL and the glut library. The final surface should look like a perfectly round tube. However, what I see on the display rather a jagged "tube". The code, I am using for this purpose, is attached. I appreciate any hint to solve this issue.
Thank you!
Hadi
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/freeglut.h>
#include <cstdio>
#include <iostream>
using namespace std;
#define S_NUMPOINTS 9
#define S_ORDER 3
#define S_NUMKNOTS (S_NUMPOINTS + S_ORDER)
#define T_NUMPOINTS 2
#define T_ORDER 2
#define T_NUMKNOTS (T_NUMPOINTS + T_ORDER)
#define SQRT2 1.414213
#define SQRT2HALF 0.707107
/* initialized local data */
GLfloat sknots[S_NUMKNOTS] =
{ 0., 0., 0., 1., 1., 2., 2., 3., 3., 4., 4., 4. };
GLfloat tknots[T_NUMKNOTS] =
{ 0.0, 0.0, 1.0, 1.0 };
GLfloat ctlpoints[S_NUMPOINTS][T_NUMPOINTS][4] =
{
{ { 1.0, 0.0, 1.0, 1.0 }, { 1.0, 0.0, 0.0, 1.0 } },
{ { 1.0, 1.0, 1.0, SQRT2HALF },{ 1.0, 1.0, 0.0, SQRT2HALF } },
{ { 0.0, 1.0, 1.0, 1.0 },{ 0.0, 1.0, 0.0, 1.0 } },
{ { -1.0, 1.0, 1.0, SQRT2HALF },{ -1.0, 1.0, 0.0, SQRT2HALF } },
{ { -1.0, 0.0, 1.0, 1.0 },{ -1.0, 0.0, 0.0, 1.0 } },
{ { -1.0, -1.0, 1.0, SQRT2HALF },{ -1.0, -1.0, 0.0, SQRT2HALF } },
{ { 0.0, -1.0, 1.0, 1.0 },{ 0.0, -1.0, 0.0, 1.0 } },
{ { 1.0, -1.0, 1.0, SQRT2HALF },{ 1.0, -1.0, 0.0, SQRT2HALF } },
{ { 1.0, 0.0, 1.0, 1.0 },{ 1.0, 0.0, 0.0, 1.0 } },
};
GLUnurbsObj *theNurb;
/* Initialize material property, light source, lighting model,
* and depth buffer.
*/
void myinit(void)
{
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_diffuse[] = { 1.0, 0.2, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light0_position[] = { 1.0, 0.1, 1.0, 0.0 };
GLfloat light1_position[] = { -1.0, 0.1, 1.0, 0.0 };
GLfloat lmodel_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
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_POSITION, light0_position);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
theNurb = gluNewNurbsRenderer();
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(4., 4.5, 2.5);
glRotatef(220.0, 1., 0., 0.);
glRotatef(115.0, 0., 1., 0.);
glTranslatef(-4., -4.5, -2.5);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb,
S_NUMKNOTS, sknots,
T_NUMKNOTS, tknots,
4 * T_NUMPOINTS,
4,
&ctlpoints[0][0][0],
S_ORDER, T_ORDER,
GL_MAP2_VERTEX_4);
gluEndSurface(theNurb);
glPopMatrix();
glFlush();
}
void myReshape(int w, int h)
{
w = 400;
h = 400;
glViewport(100, 100, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.5, 0.5, 0.8, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(7.0, 4.5, 4.0, 4.5, 4.5, 2.0, 6.0, -3.0, 2.0);
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(argv[0]);
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
解决方案Hello,
maybe you should ask in this forum:
https://www.opengl.org/discussion_boards/
这篇关于NURBS表面使用OpenGL和过剩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文