简体   繁体   English

(Q)OpenGL代码无法正确打开窗口

[英](Q) OpenGL Code won't open window properly

I just recently started working with OpenGL, or rather trying to get into it. 我最近才开始使用OpenGL,或者只是尝试使用OpenGL。 I found a rather good tutorial, unfortunatly with a very outdated GLFW Version. 我找到了一个很好的教程,不幸的是使用了非常过时的GLFW版本。 I'm using Visual Studio 2012, 64bit, glew (64bit files), glfw3 (64bit files) and compile my Project for 64bit. 我正在使用Visual Studio 2012、64位,glew(64位文件),glfw3(64位文件),并为64位编译我的项目。

So far I had to change a few parts of the code because of functions having different names now, etc. 到目前为止,由于函数现在具有不同的名称,因此我不得不更改部分代码,等等。

My current problem is, I do get two windows open... one that has the Directory of my Project as title and one that is called "First Window" as I have it in my creation code (see below). 我当前的问题是,我确实打开了两个窗口……一个窗口具有我的项目目录作为标题,而另一个窗口则被称为“第一个窗口”,就像我在创建代码中所看到的那样(请参见下文)。 None of the two windows renders the triangle as it should, plus the "First Window" window seems to make the whole Thing stuck. 这两个窗口均未呈现应有的三角形,再加上“第一个窗口”窗口似乎会使整个物件卡住。 It just loads endlessly. 它只是不断地加载。

I have to admit I don't have much knowledge on OpenGL so far, that's why I am asking here what is going wrong. 我必须承认,到目前为止,我对OpenGL的了解并不多,这就是为什么我在这里问出了什么问题。

Code for the OpenGL.cpp file (if any others are requireds I will add them): 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();
}

The Tutorial in question is http://www.hightech-journal.net/opengl-tutorial-02-das-erste-polygon . 有问题的教程是http://www.hightech-journal.net/opengl-tutorial-02-das-erste-polygon It's in german, so I don't know if it's of much help for everyone, especially since the glfw Version is outdated, as I mentioned above. 它是德语的,所以我不知道它是否对每个人都有很大帮助,特别是因为如上所述,glfw版本已经过时了。

I'll gladly provide any further Information when needed. 如有需要,我将很乐意提供任何进一步的信息。

I can imagine that my global definition of the pointer is causing trouble. 我可以想象我的指针全局定义引起麻烦。 Thing is, before other functions outside of Init that require the pointer would call it as undeclared (strangely not all of them), so because I didn't want to rework (and possibly break) to much on the functions I declared it globaly. 事情是,在Init之外需要指针的其他函数将其声明为未声明的(奇怪的是不是全部),因此因为我不想对声明为global的函数进行大量修改(可能会破坏)。

EDIT: The above was my openGL.cpp The other files: 编辑:上面是我的openGL.cpp其他文件:

openGL.h: 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 (a short one): main.h(简短的一个):

#include <stdlib.h>
#include "GL/glfw3.h"

main.cpp: main.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;
} 

Hope this helps with solving it. 希望这有助于解决它。

Since it is a console application the two windows will appear which is normal behavior. 由于它是一个控制台应用程序,因此将出现两个窗口,这是正常现象。 The first window is the console window that your program creates as it processes main(). 第一个窗口是您的程序在处理main()时创建的控制台窗口。 The other window is your OpenGL window "FirstWindow" 另一个窗口是您的OpenGL窗口“ FirstWindow”

Now for the first problem: 现在针对第一个问题:

include windows header at the top of any header files. 在任何头文件的顶部包括Windows头。

#include < windows.h >

After that change the main() to the following: 之后,将main()更改为以下内容:

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpComand, int nShow)

Then add the following lines: 然后添加以下行:

HWND hWnd = GetConsoleWindow();
if(IsWindow(hWnd)) ShowWindow( hWnd, SW_HIDE );

This will hide your console window completely and only FirstWindow will appear. 这将完全隐藏您的控制台窗口,并且只会显示FirstWindow。 As for your second problem, you must be making a mistake in either of the two function: Init() & Update() Check the code to see where you are making mistake: 至于第二个问题,您必须在以下两个函数之一中犯了一个错误:Init()和Update()检查代码以查看您在哪里犯错:

EDIT: here is your updated code: 编辑:这是您的更新代码:

OpenGL.h 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 OpenGL.Cpp

 #include "OpenGL.h"
 #include <GLFW\glfw3.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();
}

Main.Cpp Main.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 Glückwünsche,Sie haben,Ihre rotierende Dreieck

I removed the main.h as it served no purpose. 我删除了main.h,因为它没有任何作用。 The environment is VS2012 x32bit, Windows 7-32bit. 环境是VS2012 x32bit,Windows 7-32bit。 added the additional directory for glfw and openGl32.h (platform sdk 7.1A) linked to gl32.lib (platform SDK 7.1A) and glfw library. 添加了glfw和openGl32.h(平台sdk 7.1A)的附加目录,该目录链接到gl32.lib(平台SDK 7.1A)和glfw库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM