如何实现通过鼠标的点击进行正交投影和透视投影间的切换
我想实现通过鼠标或者键盘进行正交和透视投影的切换,我的reshape函数如下:
其中正交的和透视的都写出来了,但是不知道如何通过鼠标来切换
下面是源码,在mymouse那个函数里不知道怎么写。。。
opengl新手,求各位爸爸们指点指点
其中正交的和透视的都写出来了,但是不知道如何通过鼠标来切换
void Reshape(int w1, int h1) {
WinWidth = w1;
WinHeight = h1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w1, h1);
gluPerspective(70, 1.0*w1 / h1, 1, 1000);//透视
glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);//正交
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
下面是源码,在mymouse那个函数里不知道怎么写。。。
#include "stdafx.h"
#include <GL/glut.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
int WinWidth, WinHeight;
double posX, posY, posZ, viewX, viewY, viewZ, upX, upY, upZ;
const GLfloat PI = 3.141515926;
void setAll(double px, double py, double pz,
double vx, double vy, double vz,
double ux, double uy, double uz)
{
posX = px;
posY = py;
posZ = pz;
viewX = vx;
viewY = vy;
viewZ = vz;
upX = ux;
upY = uy;
upZ = uz;
}
void setLookAt() {
gluLookAt(posX, posY, posZ,
viewX, viewY, viewZ,
upX, upY, upZ);
}
void setPos(double x, double y, double z) {
posX = x;
posY = y;
posZ = z;
}
void DisplayBox() {
GLfloat x, y, angle;
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 7.5f, 0.0f);
int ipvot = 0;
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot++;
glVertex3f(x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(10.0f, 7.5f, 0.0f);
int ipvot1 = 0;
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot1 % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot1++;
glVertex3f(10.0f+x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(10.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(10.0f+x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(-10.0f, 7.5f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot1 % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot1++;
glVertex3f(-10.0f + x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(-10.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(-10.0f + x, 0.0f, y);
}
glEnd();
}
void DrawGrid() {
glLineWidth(2);
glColor3f(0.0, 0.0, 1.0);
for (int i = -100; i <= 100; i += 10) {
glBegin(GL_LINES);
glVertex3f(i, 0.0f, -100.0f);
glVertex3f(i, 0.0f, 100.f);
glEnd();
glBegin(GL_LINES);
glVertex3f(-100.0f, 0.0f, i);
glVertex3f(100.0f, 0.0f, i);
glEnd();
}
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawGrid();
DisplayBox();
glutSwapBuffers();
}
void update() {
glLoadIdentity();
setLookAt();
glutPostRedisplay();
}
void Reshape(int w1, int h1) {
WinWidth = w1;
WinHeight = h1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w1, h1);
gluPerspective(70, 1.0*w1 / h1, 1, 1000);
//glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
setAll(20, 1.75, 0, 0, 1.75, 0, 0, 1, 0);
}
void ProcessKeyBoard(int key, int x, int y) {
static double delta;
if (key == GLUT_KEY_LEFT) {
delta += 0.03;
setPos(20 * cos(delta), posY, 20 * sin(delta));
}
else if (key == GLUT_KEY_RIGHT) {
delta -= 0.03;
setPos(20 * cos(delta), posY, 20 * sin(delta));
}
else if (key == GLUT_KEY_UP) {
delta += 0.03;
setPos(20 * cos(delta), 40 * sin(delta), posZ);
}
else if (key == GLUT_KEY_DOWN) {
delta -= 0.03;
setPos(20 * cos(delta), 40 * sin(delta), posZ);
}
//setPos(20 * cos(delta), posY, 20 * sin(delta));
}
void myMouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);
}
glutPostRedisplay();
}
int main(int argc, char *argv[]) {
WinWidth = 400;
WinHeight = 400;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(WinWidth, WinHeight);
glutCreateWindow("Rita's OpenGL");
glutReshapeFunc(&Reshape);
//glutMouseFunc(&myMouse);
glutIdleFunc(&update);
glutDisplayFunc(&Display);
glutSpecialFunc(ProcessKeyBoard);//写 char key和int key 的区别 glutKeyboardFunc &
init();
glutMainLoop();
return 0;
}
opengl新手,求各位爸爸们指点指点
...全文
相关推荐
C++三维透视投影源代码
本项目是基于C++实现的三维透视投影,允许用户通过鼠标操作来动态地移动和旋转立方体,提供了一种直观的交互体验。这个项目使用Visual Studio 2008作为开发环境,并包含了源代码和可执行文件。
pyqt opengl 3d渲染源码 正交投影,透视投影
本文介绍了基于PyQt5和OpenGL开发的一个3D图形界面应用程序,主要功能包括渲染3D圆柱体、支持正交和透视投影切换、鼠标交互控制视图旋转和平移、滚轮缩放等功能。程序包含OpenGLWidget_
OpenGL实现三维坐标的鼠标拣选
这个函数需要四个参数:鼠标点击的屏幕坐标(x, y)、深度值(z)以及模型视图矩阵和投影矩阵。其中,深度值通常可以通过读取深度缓冲区获得。
OpenGL绘制3D企鹅
键盘的o和p键用于切换投影模式。在3D渲染中,投影分为正交投影和透视投影。
MFC/C++ 实现OPENGL动态拉框,过程不闪屏
总结来说,本教程的重点在于利用MFC+C++和OpenGL实现动态拉框功能,同时通过双缓冲技术优化用户体验,避免闪烁。此外,还介绍了正交投影、透视投影和正射投影的使用,以满足不同视觉需求。


工具平台和程序库
