#include "stdafx.h"
#include <Windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void changeSize(int w, int h)
{
float ratio = 1.0 * w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 5, 0, 0, -1, 0, 1, 0);
}
void DrawCube(float xpos = 0.0f, float ypos = 0.0f, float zpos = 0.0f)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPushMatrix();
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f); // top face
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f); // right face
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, 0.0f, 0.0f); // left face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, 0.0f); // bottom face
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -1.0f); // back face
glVertex3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glEnd();
glutSwapBuffers();
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// load the identity matrix (clear to default position and orientation)
glLoadIdentity();
// position and size the cube
glTranslatef(0, 0.0, -5);
/*glRotatef(15.0, 1.0, 0.0, 0.0);
glRotatef(30.0f, 0.0, 1.0, 0.0);
glScalef(0.75, 0.75, 0.75);*/
DrawCube(0.0, 0.0, 0.0);
}
void UpdateProjection(bool toggle)
{
static bool usePerspective = toggle;
// select the projection matrix and clear it out
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// choose the appropriate projection based on the currently toggled mode
if (usePerspective)
{
// set the perspective with the appropriate aspect ratio
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 1000.0);
}
else
{
// set up an orthographic projection with the same near clip plane
glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 1000.0);
}
// select modelview matrix and clear it out
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void ResizeScene(int width, int height)
{
// avoid divide by zero
if (height == 0)
{
height = 1;
}
// reset the viewport to the new dimensions
glViewport(0, 0, width, height);
// set up the projection, without toggling the projection mode
UpdateProjection(false);
}
int _tmain(int argc, char* argv[])
{
glutInit(&argc, argv);
init();
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("hello");
glutDisplayFunc(render);
glutIdleFunc(render);
glutReshapeFunc(ResizeScene);
glutMainLoop();
return 0;
}
OPENGL 游戏编程 第四章 投影 glut
最新推荐文章于 2022-06-07 19:47:41 发布