当前位置:网站首页>OpenGL 3D graphics rendering

OpenGL 3D graphics rendering

2022-07-07 08:54:00 Baldness is impossible in this life


#include "stdafx.h"
#include<GL\glut.h>
#include<gl\glut.h>
#include<GL\glaux.h>

static GLsizei iMode = 1;
static GLfloat xRot = 0;
static GLfloat yRot = 0;
GLUquadricObj *obj;

void Initial(){
	glClearColor(1, 1, 1, 1);
	glColor3f(0, 0, 0);
	obj = gluNewQuadric();
	gluQuadricDrawStyle(obj, GLU_LINE);
}

void ChangeSize(int w, int h){
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-1.5, 1.5, -1.5, 1.5);
}
void Display(){
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glRotatef(xRot, 1, 0, 0);
	glRotatef(yRot, 0, 1, 0);
	switch (iMode){
	case 1:
		glutWireTetrahedron(); break;
	case 2:
		glutSolidTetrahedron(); break;
	case 3:
		glutWireOctahedron(); break;
	case 4:
		glutSolidOctahedron(); break;
	case 5:
		glutWireSphere(1, 15, 15); break;
	case 6:
		glutSolidSphere(1, 15, 15); break;
	case 7:
		glutWireTeapot(1); break;
	case 8:
		glutSolidTeapot(1); break;
	case 9:
		gluSphere(obj, 1, 15, 15); break;
	case 10:
		gluCylinder(obj, 1, 0, 1, 15, 15); break;
	case 11:
		gluPartialDisk(obj, 0.3, 0.8, 15, 15, 30, 260); break;
	default: break;
	}
	glFlush();
}

void ProcessMenu(int value){
	iMode = value;
	glutPostRedisplay();
}
void SpecialKeys(int key, int x, int y){
	if (key == GLUT_KEY_UP) xRot -= 5;
	if (key == GLUT_KEY_DOWN) xRot += 5;
	if (key == GLUT_KEY_LEFT) yRot -= 5;
	if (key == GLUT_KEY_RIGHT) yRot += 5;
	if (xRot > 356) xRot = 0;
	if (xRot < -1) xRot = 355;
	if (yRot > 356) yRot = 0;
	if (yRot < -1) yRot = 355;
	glutPostRedisplay();
}

int main(int argc, char** argv){
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(400, 400);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Hello");

	int nGlutPolyMenu = glutCreateMenu(ProcessMenu);
	glutAddMenuEntry(" Wire frame tetrahedron ", 1);
	glutAddMenuEntry(" Solid tetrahedron ", 2);
	glutAddMenuEntry(" Wireframe regular octahedron ", 3);
	glutAddMenuEntry(" Solid regular octahedron ", 4);

	int nGlutCurveMenu = glutCreateMenu(ProcessMenu);
	glutAddMenuEntry(" Wireframe sphere ", 5);
	glutAddMenuEntry(" Solid sphere ", 6);
	glutAddMenuEntry(" Wireframe teapot ", 7);
	glutAddMenuEntry(" Solid teapot ", 8);

	int nGluCurveMenu = glutCreateMenu(ProcessMenu);
	glutAddMenuEntry(" Wireframe sphere ", 9);
	glutAddMenuEntry(" Wireframe cone ", 10);
	glutAddMenuEntry(" Wireframe torus ", 11);

	int nMainMenu = glutCreateMenu(ProcessMenu);
	glutAddSubMenu("GLUT Polyhedron ", nGlutPolyMenu);
	glutAddSubMenu("GLUT Surfaces ", nGlutCurveMenu);
	glutAddSubMenu("GLU Surfaces ", nGluCurveMenu);
	glutAttachMenu(GLUT_RIGHT_BUTTON);

	glutDisplayFunc(Display);
	glutReshapeFunc(ChangeSize);
	glutSpecialFunc(SpecialKeys);
	Initial();
	glutMainLoop();
}

use vs2013 To write , After running, right-click the stand-alone to expand the menu

原网站

版权声明
本文为[Baldness is impossible in this life]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070618425921.html