gluProject() 在 OpenGL VC++ 初学者中设置
gluProject() setup in OpenGL VC++ beginner
我正在尝试将 3D 坐标转换为 2D 坐标。 我知道我需要使用gluProject()
.但是我在设置它时遇到了麻烦。我需要使用像素坐标
我需要帮助InitGL()
如何正确初始化它。 同样在DrawGLScene()
如何使用它?我需要保留用于制作图层的 z
变量,所以我不想禁用它。
这是我的代码:
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/gl.h> // Header File For The OpenGL32 Library
#include <OpenGL/glu.h> // Header File For The GLu32 Library
#else
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#endif
#include "SDL.h" // SDL library for managing input and window management (multipltform)
#include "SOIL.h" // SOIL library for loading textures (multiplatform)
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "RB Game Engine";
float position=0;
GLuint texture[1]; // number of texture = 1
/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
LoadGLTextures(); // Jump To Texture Loading Routine ( NEW )
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glViewport(0, 0, Width, Height);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}
/* The main drawing function. */
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-6.0f); // Move Right 3 Units
glBindTexture(GL_TEXTURE_2D, texture[0]);
// draw a square (quadrilateral)
glBegin(GL_QUADS); // start drawing a polygon (4 sided)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f+position, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f+position, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f+position, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f+position, 1.0f, 1.0f);
glEnd(); // done with the polygon
// swap buffers to display, since we're double buffered.
SDL_GL_SwapBuffers();
}
您可以在 DrawGLScene()
中使用下面的代码,在 InitGL()
之后。
它不需要任何其他初始化。
但请确保在对投影进行任何更改之前执行它(例如,2D 投影用于在您返回coords
的位置上绘制)。
GLdouble x, y, z; // target 2d coords (z is 'unused')
GLdouble coords[] = { 1.0, 1.0, 1.0 }; // current 3d coords
GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
// get window coords based on 3D coordinates
gluProject( coords[0], coords[1], coords[2],
model_view, projection, viewport,
&x, &y, &z);
胶粘剂项目参数说明
gluUnproject的一个示例,其中我有代码
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- C++错误消息*成员参考.**初学者*
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何在24位SDL_Surface上设置像素的颜色
- std::设置自定义比较器
- 如何设置一个范围来提取我想要获得的信息
- gluProject() 在 OpenGL VC++ 初学者中设置
- 设置Visual Studio for Windows初学者教程
- 在Visual Studio Express 2013中为初学者设置c++