Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C opengl中的平滑着色_C_Opengl - Fatal编程技术网

C opengl中的平滑着色

C opengl中的平滑着色,c,opengl,C,Opengl,我一直在到处寻找如何做到这一点,并在opengl的红皮书中寻找,其中只谈到了平滑着色2d多边形。我不确定如何对球体进行平滑着色。我知道你必须做glShadeModel。在没有灯光的情况下,我如何区分它是平坦的还是平滑的呢 #include <GLUT/glut.h> #include <stdio.h> #include <stdlib.h> //Global variables double sphere = 1, cone = 1, viewy = 2,

我一直在到处寻找如何做到这一点,并在opengl的红皮书中寻找,其中只谈到了平滑着色2d多边形。我不确定如何对球体进行平滑着色。我知道你必须做glShadeModel。在没有灯光的情况下,我如何区分它是平坦的还是平滑的呢

#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
//Global variables
double sphere = 1, cone = 1, viewy = 2, viewx = -10, viewz = 5,headup = 5,headright = 5;
void myinit(){
    glClearColor(0,0,0,1);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,1,1,100);
    //glOrtho(-2,20,-2,20,-10,10);
}
void drawRoom(){
    //floor
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,10,0);
    glVertex3f(10,10,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //wall
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glVertex3f(0,10,0);
    glVertex3f(0,10,10);
    glVertex3f(10,10,10);
    glVertex3f(10,10,0);
    glEnd();
    //wall2
    glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glVertex3f(10,10,0);
    glVertex3f(10,10,10);
    glVertex3f(10,0,10);
    glVertex3f(10,0,0);
    glEnd();
}
void drawObjects(){
    //draw cone
    glColor3f(1,0,1);
    glTranslatef(2,2,0);
    glutSolidCone(cone,5,10,2);
    //draw sphere
    glTranslatef(5,5,0);
    glColor3f(1,0,0);
    glutSolidSphere(sphere,500,500);

}
void move(unsigned char key, int x, int y){
    switch(key){
        case 'y': 
            viewy++;
            glutPostRedisplay();
            break;
        case 'x':
            viewx++;
            glutPostRedisplay();
            break;
        case 'z':
            viewz++;
            glutPostRedisplay();
            break;
        //moves head
        case 'd':
            headup--;
            headright--;
            glutPostRedisplay();
            break;
        case 'a':
            headup++;
            headright++;
            glutPostRedisplay();
            break;
    }
}

void display(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    drawRoom();
    drawObjects();
    glutSwapBuffers();
    glFlush();
}
int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Room");
    myinit();
    glutDisplayFunc(display);
    glutKeyboardFunc(move);
    glutMotionFunc(moveHead);
    glutMainLoop();
    return 0;
}
#包括
#包括
#包括
//全局变量
双球体=1,圆锥体=1,视图Y=2,视图X=-10,视图Z=5,抬头=5,头灯=5;
void myinit(){
glClearColor(0,0,0,1);
glShadeModel(GL_平滑);
glMatrixMode(GL_投影);
glLoadIdentity();
(60,1,1100),;
//格洛托(-2,20,-2,20,-10,10);
}
休息室{
//地板
glBegin(GL_多边形);
gl3f(1,1,1);
glVertex3f(0,0,0);
glVertex3f(0,10,0);
glVertex3f(10,10,0);
glVertex3f(10,0,0);
格伦德(
);
//墙
glBegin(GL_多边形);
gl3f(0,0,1);
glVertex3f(0,10,0);
glVertex3f(0,10,10);
glVertex3f(10,10,10);
glVertex3f(10,10,0);
格伦德();
//墙2
glBegin(GL_多边形);
gl3f(0,1,0);
glVertex3f(10,10,0);
glVertex3f(10,10,10);
glVertex3f(10,0,10);
glVertex3f(10,0,0);
格伦德();
}
void drawObjects(){
//牵引锥
gl3f(1,0,1);
glTranslatef(2,2,0);
圆锥体(圆锥体,5,10,2);
//画球
glTranslatef(5,5,0);
gl3f(1,0,0);
实心球体(球体,500500);
}
无效移动(无符号字符键,整数x,整数y){
开关(钥匙){
案例“y”:
viewy++;
再发现();
打破
案例“x”:
viewx++;
再发现();
打破
案例“z”:
viewz++;
再发现();
打破
//移动头部
案例“d”:
抬头--;
头灯--;
再发现();
打破
案例“a”:
headup++;
头灯++;
再发现();
打破
}
}
无效显示(){
glMatrixMode(GLU模型视图);
glLoadIdentity();
gluLookAt(viewx、viewy、viewz、抬头、前照灯,5,0,0,1);
glClear(GLU颜色缓冲位);
客厅();
drawObjects();
glutSwapBuffers();
glFlush();
}
int main(int argc,字符**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500500);
位置(100100);
窗户(“房间”);
myinit();
glutDisplayFunc(显示器);
键盘功能(移动);
glutMotionFunc(移动头);
glutMainLoop();
返回0;
}

您需要启用照明,至少启用一个灯光,并为几何体提供不同的逐顶点法线,以查看
GL\u平滑的效果


…在没有灯光的情况下,我如何区分它是平坦的还是平滑的

#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
//Global variables
double sphere = 1, cone = 1, viewy = 2, viewx = -10, viewz = 5,headup = 5,headright = 5;
void myinit(){
    glClearColor(0,0,0,1);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,1,1,100);
    //glOrtho(-2,20,-2,20,-10,10);
}
void drawRoom(){
    //floor
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,10,0);
    glVertex3f(10,10,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //wall
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glVertex3f(0,10,0);
    glVertex3f(0,10,10);
    glVertex3f(10,10,10);
    glVertex3f(10,10,0);
    glEnd();
    //wall2
    glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glVertex3f(10,10,0);
    glVertex3f(10,10,10);
    glVertex3f(10,0,10);
    glVertex3f(10,0,0);
    glEnd();
}
void drawObjects(){
    //draw cone
    glColor3f(1,0,1);
    glTranslatef(2,2,0);
    glutSolidCone(cone,5,10,2);
    //draw sphere
    glTranslatef(5,5,0);
    glColor3f(1,0,0);
    glutSolidSphere(sphere,500,500);

}
void move(unsigned char key, int x, int y){
    switch(key){
        case 'y': 
            viewy++;
            glutPostRedisplay();
            break;
        case 'x':
            viewx++;
            glutPostRedisplay();
            break;
        case 'z':
            viewz++;
            glutPostRedisplay();
            break;
        //moves head
        case 'd':
            headup--;
            headright--;
            glutPostRedisplay();
            break;
        case 'a':
            headup++;
            headright++;
            glutPostRedisplay();
            break;
    }
}

void display(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    drawRoom();
    drawObjects();
    glutSwapBuffers();
    glFlush();
}
int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Room");
    myinit();
    glutDisplayFunc(display);
    glutKeyboardFunc(move);
    glutMotionFunc(moveHead);
    glutMainLoop();
    return 0;
}
您可以通过
glGetIntegerv()
检索当前着色模型:


您需要启用照明,至少启用一个灯光,并为几何体提供不同的逐顶点法线,以查看
GL\u平滑的效果


…在没有灯光的情况下,我如何区分它是平坦的还是平滑的

#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
//Global variables
double sphere = 1, cone = 1, viewy = 2, viewx = -10, viewz = 5,headup = 5,headright = 5;
void myinit(){
    glClearColor(0,0,0,1);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,1,1,100);
    //glOrtho(-2,20,-2,20,-10,10);
}
void drawRoom(){
    //floor
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,10,0);
    glVertex3f(10,10,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //wall
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glVertex3f(0,10,0);
    glVertex3f(0,10,10);
    glVertex3f(10,10,10);
    glVertex3f(10,10,0);
    glEnd();
    //wall2
    glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glVertex3f(10,10,0);
    glVertex3f(10,10,10);
    glVertex3f(10,0,10);
    glVertex3f(10,0,0);
    glEnd();
}
void drawObjects(){
    //draw cone
    glColor3f(1,0,1);
    glTranslatef(2,2,0);
    glutSolidCone(cone,5,10,2);
    //draw sphere
    glTranslatef(5,5,0);
    glColor3f(1,0,0);
    glutSolidSphere(sphere,500,500);

}
void move(unsigned char key, int x, int y){
    switch(key){
        case 'y': 
            viewy++;
            glutPostRedisplay();
            break;
        case 'x':
            viewx++;
            glutPostRedisplay();
            break;
        case 'z':
            viewz++;
            glutPostRedisplay();
            break;
        //moves head
        case 'd':
            headup--;
            headright--;
            glutPostRedisplay();
            break;
        case 'a':
            headup++;
            headright++;
            glutPostRedisplay();
            break;
    }
}

void display(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    drawRoom();
    drawObjects();
    glutSwapBuffers();
    glFlush();
}
int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Room");
    myinit();
    glutDisplayFunc(display);
    glutKeyboardFunc(move);
    glutMotionFunc(moveHead);
    glutMainLoop();
    return 0;
}
您可以通过
glGetIntegerv()
检索当前着色模型:


不要使用固定函数管道(1992年发布!),使用着色器、顶点缓冲区和其他新东西。OpenGL 2.0于2004年发布,每个图形卡都支持着色器。A.e.我帮不了你,因为我从未使用过固定函数管道。你说的“平滑着色”是什么意思?不要使用固定函数管道(1992年发布!),使用着色器、顶点缓冲区和其他新东西。OpenGL 2.0于2004年发布,每个图形卡都支持着色器。我帮不了你,因为我从来不使用固定功能管道。你说的“平滑着色”是什么意思?