gpt4 book ai didi

iphone - OpenGL ES 如何正确组合 Orthof 和 Frustum

转载 作者:行者123 更新时间:2023-12-03 17:25:00 24 4
gpt4 key购买 nike

我开始学习适用于 iPhone 的 OpenGL ES 1.1,并且想在一些 3D 对象后面以正交投影方式绘制 2D 图像。使用 Jeff Lamarche 的教程和《Pro OpenGL ES for iPhone》一书,我想出了以下几种方法来尝试做到这一点。如果我禁用对 drawSunInRect 的调用,则 3D 对象渲染得很好,我可以使用触摸控件等移动它们。如果我取消注释该调用并尝试绘制太阳图像,该图像将出现在 CGRect 我提供,但我看不到任何其他 3D 对象 - 屏幕的其余部分是黑色的。我尝试在各个地方禁用/启用深度测试,将不同的参数传递给 glOrthof(),并围绕矩形移动,但只有在 drawSunInRect 时我才不断获取太阳图像code> 方法被调用。我假设它覆盖了我的 3D 对象。

// Draw Sun in Rect and with Depth
- (void)drawSunInRect:(CGRect)rect withDepth:(float)depth {

// Get screen bounds
CGRect frame = [[UIScreen mainScreen] bounds];

// Calculate vertices from passed CGRect and depth
GLfloat vertices[] =
{
rect.origin.x, rect.origin.y, depth,

rect.origin.x + rect.size.width , rect.origin.y, depth,

rect.origin.x, rect.size.height+rect.origin.y , depth,

rect.origin.x + rect.size.width , rect.size.height+rect.origin.y ,depth
};

// Map the texture coords - no repeating
static GLfloat textureCoords[] =
{
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0
};

// Disable DEPTH test and setup Ortho
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000.0);

// Enable blending and configure
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);

// Enable VERTEX and TEXTURE client states
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

// Enable Textures
glEnable(GL_TEXTURE_2D);

// Projection Matrix Mode for ortho and reset
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

// No lighting or Depth Mask for 2D - no Culling
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glDisable(GL_CULL_FACE);
glDisableClientState(GL_COLOR_ARRAY);

// Define ortho
glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000);

// From Jeff Lamarche Tutorials
// glOrthof(-1.0, // Left
// 1.0, // Right
// -1.0 / (rect.size.width / rect.size.height), // Bottom
// 1.0 / (rect.size.width / rect.size.height), // Top
// 0.01, // Near
// 10000.0); // Far

// Setup Model View Matrix
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// Bind and draw the Texture
glBindTexture(GL_TEXTURE_2D,sunInt);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT,0,textureCoords);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

// Re-enable lighting
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDepthMask(GL_TRUE);

}


// Override the draw in rect function
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{

// Initialize and init the rotation stuff for the object
// Identity Matrix
glLoadIdentity();
static GLfloat rot = 0.0;

// Clear any remnants in the drawing buffers
// and fill the background with black
glClearColor(0.0f,0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Render the Sun - 2D Sun Image in CGRect
//[self drawSunInRect:CGRectMake(100, 100, 50, 50) withDepth:-30.0]; // 2D Sun Image

// Render the BattleCruiser - 3D Battlecruiser
[self renderTheBattleCruiser];

// Calculate a time interval to use to rotate the cruiser lives
static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rot += 75 * timeSinceLastDraw;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];

}

更新

我修改了绘制太阳方法,使 GL_PROJECTION 仅有 1 次推送和 1 次弹出。我仍然遇到同样的问题。太阳图像出现,但我看不到 3D 对象。我尝试重新安排对渲染方法的调用,以便首先渲染 3D 对象,但我得到了相同的结果。对于如何同时查看正交纹理和 3D 对象的其他想法,我将不胜感激。

// Draw Sun in Rect and with Depth
- (void)drawSunInRect:(CGRect)rect withDepth:(float)depth {

// Get screen bounds
CGRect frame = [[UIScreen mainScreen] bounds];

// Calculate vertices from passed CGRect and depth
GLfloat vertices[] =
{
rect.origin.x, rect.origin.y, depth,

rect.origin.x + rect.size.width , rect.origin.y, depth,

rect.origin.x, rect.size.height+rect.origin.y , depth,

rect.origin.x + rect.size.width , rect.size.height+rect.origin.y ,depth
};

// Map the texture coords - no repeating
static GLfloat textureCoords[] =
{
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0
};

glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glDisable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisableClientState(GL_COLOR_ARRAY);

glOrthof(0,frame.size.width,frame.size.height,0,0,1000);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glColor4f(1,1,1,1);

glBindTexture(GL_TEXTURE_2D,sunInt);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT,0,textureCoords);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

最佳答案

我意识到我在 viewController viewDidLoad 中只调用了一次 setClipping。我将它移动到我的 glkView 方法中,现在我得到了我的太阳图像和我的 3d 对象。

关于iphone - OpenGL ES 如何正确组合 Orthof 和 Frustum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9133774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com