(Q) OpenGL 代码无法正确打开窗口
(Q) OpenGL Code won't open window properly
我最近才开始使用OpenGL,或者更确切地说是尝试进入它。我找到了一个相当不错的教程,不幸的是,它有一个非常过时的 GLFW 版本。我正在使用Visual Studio 2012,64位,glew(64位文件),glfw3(64位文件)并将我的项目编译为64位。
到目前为止,由于函数现在具有不同的名称等,我不得不更改代码的某些部分。
我目前的问题是,我确实打开了两扇窗户......一个将我的项目目录作为标题,另一个称为"第一个窗口",因为我在我的创建代码中有它(见下文)。两个窗口都没有按应有的方式呈现三角形,加上"第一个窗口"窗口似乎使整个事情卡住了。它只是无休止地加载。
我不得不承认,到目前为止,我对OpenGL知之甚少,这就是为什么我在这里问出了什么问题。
OpenGL.cpp 文件的代码(如果需要任何其他文件,我将添加它们):
#include "OpenGL.h"
// put that globaly cause functions outside of Init require the pointer but won't
// take it otherwise for me
GLFWwindow* windowOne;
OpenGL::OpenGL(int w, int h)
{
width = w;
height = h;
Init();
}
OpenGL::~OpenGL()
{
glfwTerminate();
}
void OpenGL::Init()
{
glfwInit();
// Window should be created here
windowOne = glfwCreateWindow(width,height,"FirstWindow",NULL,NULL);
running = true;
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
}
void OpenGL::MainLoop()
{
do
{
glfwGetWindowSize(windowOne, &width, &height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Update();
Draw();
glFlush();
glfwSwapBuffers(windowOne);
}
while(running);
}
void OpenGL::Update()
{
if(glfwGetKey(windowOne, GLFW_KEY_ESCAPE) || !glfwGetWindowAttrib(windowOne, GLFW_FOCUSED))
{
running = false;
}
}
void OpenGL::Draw()
{
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}
有问题的教程是 http://www.hightech-journal.net/opengl-tutorial-02-das-erste-polygon。它是德语的,所以我不知道它是否对每个人都有很大帮助,特别是因为 glfw 版本已经过时了,正如我上面提到的。
我很乐意在需要时提供任何进一步的信息。
我可以想象我对指针的全局定义正在造成麻烦。问题是,在 Init 之外需要指针的其他函数会将其称为未声明(奇怪的是不是全部),所以因为我不想对函数进行太多返工(并且可能破坏)我将其声明为全局。
编辑:以上是我的开放GL.cpp其他文件:
openGL.h:
#include "main.h"
class OpenGL
{
public:
OpenGL(int w, int h);
~OpenGL();
void MainLoop();
private:
void Init();
void Update();
void Draw();
bool running;
int width;
int height;
};
main.h(一个简短的):
#include <stdlib.h>
#include "GL/glfw3.h"
主.cpp:
#include "main.h"
#include "OpenGL.h"
int main(int argc, char **argv)
{
OpenGL* ogl = new OpenGL(800,600);
ogl->MainLoop();
delete ogl;
return 0;
}
希望这有助于解决它。
由于它是一个控制台应用程序,因此将出现两个窗口,这是正常行为。第一个窗口是程序在处理 main() 时创建的控制台窗口。另一个窗口是您的OpenGL窗口"FirstWindow"
现在第一个问题:
在任何头文件的顶部包括 Windows 标头。
#include < windows.h >
之后,将 main() 更改为以下内容:
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpComand, int nShow)
然后添加以下行:
HWND hWnd = GetConsoleWindow();
if(IsWindow(hWnd)) ShowWindow( hWnd, SW_HIDE );
这将完全隐藏您的控制台窗口,并且只会显示第一个窗口。至于你的第二个问题,你一定在两个函数中的任何一个中犯了错误:Init() & Update() 检查代码以查看您犯错的地方:
编辑:这是您更新的代码:
OpenGL.h
#pragma once
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "opengl32.lib")
class OpenGL
{
public:
OpenGL(int w, int h);
~OpenGL();
void MainLoop();
private:
void Update();
void Prepare();
void Draw();
bool running;
int width, height;
};
OpenGL.CPP
#include "OpenGL.h"
#include <GLFWglfw3.h>
GLFWwindow* windowOne;
OpenGL::OpenGL(int w, int h): running(true), width(w), height(h)
{
glfwInit(); //MER [Sep 20, 2013] Removed teh init() as it made no sense to have sep
windowOne = glfwCreateWindow(width,height,"FirstWindow",NULL,NULL);
glfwMakeContextCurrent(windowOne);
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
}
OpenGL::~OpenGL()
{
glfwTerminate();
glfwDestroyWindow(windowOne); // MER [Sep 20, 2013] window destruction
}
void OpenGL::MainLoop()
{
do
{
Update();
Prepare();
Draw(); //glFlush(); MER [Sep 20, 2013] No need to call
glfwSwapBuffers(windowOne);
}
while(running);
}
void OpenGL::Update()
{
if(glfwGetKey(windowOne, GLFW_KEY_ESCAPE) || !glfwGetWindowAttrib(windowOne, GLFW_FOCUSED))
running = false;
}
void OpenGL::Prepare()
{
float ratio=0.0f;
//MER [Sep 20, 2013] You need to set the viewport
glfwGetFramebufferSize(windowOne, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // MER [Sep 20, 2013] Clear depth buffer too.
//MER [Sep 20, 2013] if you don't do this, triangle would be there but not visible.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
}
void OpenGL::Draw()
{
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}
主.CPP
#include <stdlib.h>
#include <windows.h>
#include "OpenGL.h"
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpComand, int nShow)
{
HWND hWnd = GetConsoleWindow(); //MER [Sep 20, 2013] This hides the window
if(IsWindow(hWnd)) ShowWindow( hWnd, SW_HIDE );
OpenGL* ogl = new OpenGL(200,200); //MER [Sep 20, 2013] sitting in ma office, can't run window in big size
ogl->MainLoop();
delete ogl;
return 0;
}
Glückwünsche, Sie haben Ihre rotierende Dreieck
我删除了main.h,因为它没有任何用途。环境是VS2012 x32位,Windows 7-32位。添加了 glfw 和 openGl32.h 的附加目录(平台 SDK 7.1A)链接到gl32.lib(平台SDK 7.1A)和glfw库。
- 在 opengl 中渲染 obj 文件时出现黑窗口
- 无法启动 OpenGL 窗口
- C++ 如何在 OpenGL 程序中远程调用控制台窗口
- OpenGL C++(制作简单窗口时抛出错误)
- 如何防止opengl绘图拉伸到窗口大小?
- 文本在窗口 Opengl 上不可见
- OpenGL 纹理会导致 ImGUI 窗口永久失焦
- 我如何将Win32窗口渲染到OpenGL纹理
- 如何将 Unity 上下文导入 OpenGL 窗口
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- SDL2 OpenGL窗口立即关闭
- OpenGL纹理闪烁/有时在窗口大小上移动
- 如何在窗口外绘制图形(OpenGL GLFW)
- OpenGL 不会创建所需大小的窗口
- 使用 OpenGL 窗口(如 GLFW)或周围的窗口(如 GTK 或 SDL)是否存在性能差异?
- 在窗口大小上绘制的OpenGL消失
- 在无窗口 GLX 程序中读取 OpenGL 的默认帧缓冲
- 异步渲染到 Qt OpenGL 窗口
- openGL PBO 使用 wglcontext,而不是创建窗口
- 使用64位 自定义窗口形状(ETC平滑的角落不是矩形)在Windows中使用C 创建OpenGL窗口