c++ - (q) opengl code won't open window properly - stack overflow
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 enlarge 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 dont 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, cant run window in big size
ogl->MainLoop();
delete ogl;
return 0;
Glckwnsche, 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.