gpt4 book ai didi

c - opengl中的平滑着色

转载 作者:太空宇宙 更新时间:2023-11-04 03:59:20 26 4
gpt4 key购买 nike

我一直在到处寻找如何做到这一点,并查看了 opengl 的红皮书,其中只讨论了对 2d 多边形进行平滑着色。我不确定如何为 glutSolidSphere 进行平滑着色。我知道你必须做 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;
}

最佳答案

您需要启用照明,至少启用一盏灯,并为您的几何体提供不同的逐顶点法线,以查看 GL_SMOOTH 的效果。


...how can i tell the difference if its flat or smooth when there is no light for the scene.

您可以通过 glGetIntegerv() 检索当前着色模型:

GLenum shadeModel;
glGetIntegerv( GL_SHADE_MODEL, &shadeModel );

关于c - opengl中的平滑着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13480109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com