[英](Q) OpenGL Code won't open window properly
我最近才开始使用OpenGL,或者只是尝试使用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之外需要指针的其他函数将其声明为未声明的(奇怪的是不是全部),因此因为我不想对声明为global的函数进行大量修改(可能会破坏)。
编辑:上面是我的openGL.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"
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;
}
希望这有助于解决它。
由于它是一个控制台应用程序,因此将出现两个窗口,这是正常现象。 第一个窗口是您的程序在处理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 );
这将完全隐藏您的控制台窗口,并且只会显示FirstWindow。 至于第二个问题,您必须在以下两个函数之一中犯了一个错误: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 <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
我删除了main.h,因为它没有任何作用。 环境是VS2012 x32bit,Windows 7-32bit。 添加了glfw和openGl32.h(平台sdk 7.1A)的附加目录,该目录链接到gl32.lib(平台SDK 7.1A)和glfw库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.