opengl 中的平滑着色 [英] Smooth Shading in opengl
问题描述
我一直在到处寻找如何做到这一点,并查看了 opengl 的红皮书,其中只讨论了对 2d 多边形进行平滑着色.我不确定您将如何为 glutSolidSphere 进行平滑着色.我知道你必须做 glShadeModel.以及当场景没有光线时,我如何区分它是平坦的还是光滑的.
I've been looking everywhere on how to do this and looked in the redbook of opengl and that only talks about smooth shading a 2d polygon. I'm not sure how you would do smooth shading for a glutSolidSphere. I know you have to do glShadeModel. and how can i tell the difference if its flat or smooth when there is no light for the scene.
#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的效果代码>.
You need to enable lighting, enable at least one light, and supply differing per-vertex normals for your geometry to see the effects of GL_SMOOTH
.
...当场景没有光线时,我如何区分它是平坦的还是光滑的.
...how can i tell the difference if its flat or smooth when there is no light for the scene.
您可以通过 glGetIntegerv()
检索当前着色模型:
You can retrieve the current shading model via glGetIntegerv()
:
GLenum shadeModel;
glGetIntegerv( GL_SHADE_MODEL, &shadeModel );
这篇关于opengl 中的平滑着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!