首页 \ 问答 \ 在调整大小之前,无法识别OpenGL视口设置(OpenGL Viewport setting not recognized until resize)

在调整大小之前,无法识别OpenGL视口设置(OpenGL Viewport setting not recognized until resize)

我正在阅读“C#Game Programming for Serious Game Creation”一书中的例子,我看到了一些奇怪的行为。 我设置了一个视口和正交投影,然后尝试绘制一个50px宽的三角形。 我一开始只看到三角形的一小部分,但是当我调整窗口大小(例如,最大化然后恢复)时,三角形突然显示为正确的大小。

这是一个重现问题的简单示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tao.OpenGl;

namespace SimpleExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            _openGLControl.InitializeContexts();
        }

        protected override void OnClientSizeChanged(EventArgs e)
        {
            base.OnClientSizeChanged(e);

            int width = ClientSize.Width;
            int height = ClientSize.Height;
            double halfWidth = (double)width / 2;
            double halfHeight = (double)height / 2;

            Gl.glViewport(0, 0, width, height);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -100, 100);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }

        private void _openGLControl_Paint(object sender, PaintEventArgs e)
        {
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);

            Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
            {
                Gl.glColor4d(1.0, 0.0, 0.0, 0.5);
                Gl.glVertex3d(-50, 0, 0);
                Gl.glColor3d(0.0, 1.0, 0.0);
                Gl.glVertex3d(50, 0, 0);
                Gl.glColor3d(0.0, 0.0, 1.0);
                Gl.glVertex3d(0, 50, 0);

            }
            Gl.glEnd();
            Gl.glFinish();
        }
    }
}

在绘制三角形之前程序启动时会触发OnClientSizeChanged事件,但在调整窗口大小之前,视口/正交似乎没有任何改变。

我正在使用OpenTk库并包含Tao.OpenGL(如您所见)。 我在我的表单上使用SimpleOpenGlControl,其中Dock设置为Fill。 在Windows 7上运行VS2012。我希望尽可能贴近书中的示例,而不是使用过剩或任何额外的库。 谁能指出我做错了什么?


I am going through the examples in the book "C# Game Programming For Serious Game Creation" and am seeing some strange behavior. I set a viewport and the ortho projection and then try to draw a triangle 50px wide. I am only seeing a tiny portion of the triangle at first, but when I resize the window (e.g., maximize and then restore), the triangle suddenly shows as the correct size.

Here is a simple example that reproduces the issue:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tao.OpenGl;

namespace SimpleExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            _openGLControl.InitializeContexts();
        }

        protected override void OnClientSizeChanged(EventArgs e)
        {
            base.OnClientSizeChanged(e);

            int width = ClientSize.Width;
            int height = ClientSize.Height;
            double halfWidth = (double)width / 2;
            double halfHeight = (double)height / 2;

            Gl.glViewport(0, 0, width, height);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -100, 100);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }

        private void _openGLControl_Paint(object sender, PaintEventArgs e)
        {
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);

            Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
            {
                Gl.glColor4d(1.0, 0.0, 0.0, 0.5);
                Gl.glVertex3d(-50, 0, 0);
                Gl.glColor3d(0.0, 1.0, 0.0);
                Gl.glVertex3d(50, 0, 0);
                Gl.glColor3d(0.0, 0.0, 1.0);
                Gl.glVertex3d(0, 50, 0);

            }
            Gl.glEnd();
            Gl.glFinish();
        }
    }
}

The OnClientSizeChanged event does get triggered when the program starts before the triangle is drawn, but the viewport/ortho don't seem to change anything until I resize the window.

I am using the OpenTk library and including Tao.OpenGL (as you can see). I am using a SimpleOpenGlControl on my form which has Dock set to Fill. Running in VS2012 on Windows 7. I would like to stick as close as possible to the book examples, and not use glut or any extra libraries. Can anyone point out what I'm doing wrong?


原文:https://stackoverflow.com/questions/16903234
更新时间:2022-06-21 20:06

最满意答案

而不是重建路径,听起来你可以简化过程如下:

$img = str_replace('images/photo-strip/thumb/th_', 'images/photo-strip/', $originalImage);

要不就:

$img = str_replace('thumb/th_', '', $originalImage);

Instead of rebuilding the path, it sounds like you could simplify the process as follows:

$img = str_replace('images/photo-strip/thumb/th_', 'images/photo-strip/', $originalImage);

Or just:

$img = str_replace('thumb/th_', '', $originalImage);

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)