简体   繁体   中英

(Q) OpenGL Code won't open window properly

I just recently started working with OpenGL, or rather trying to get into it. I found a rather good tutorial, unfortunatly with a very outdated GLFW Version. I'm using Visual Studio 2012, 64bit, glew (64bit files), glfw3 (64bit files) and compile my Project for 64bit.

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.

Code for the OpenGL.cpp file (if any others are requireds I will add them):

#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 . 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.

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.

EDIT: The above was my openGL.cpp The other files:

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):

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

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(). The other window is your OpenGL window "FirstWindow"

Now for the first problem:

include windows header at the top of any header files.

#include < windows.h >

After that change the main() to the following:

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. 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:

EDIT: here is your updated code:

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 <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

#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

I removed the main.h as it served no purpose. The environment is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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