El_Loco
El_Loco

Reputation: 1866

Can't draw in opengl in visual studio 2012

I am trying to figure out how to make opengl work in Windows 8.1 using Visual Studio 2012.

My program compiles and runs, but nothing happens in the windows which is created, I can't even change background color or see the mouse.

My program looks as follows:

main.cpp

#include <cstdio>
#include <iostream>
#include "simpleViewer.h"

using namespace std;

int main(int argc, char **argv){
simpleViewer Viewer;
Viewer.initOpenGL(argc, argv);
Viewer.run();
}

simpleViewer.h

#pragma once
#ifndef _SIMPLEVIEWER_H_
#define _SIMPLEVIEWER_H_
#include "GL\GL\glut.h"
#include <iostream>
enum DisplayModes {
DISPLAY_MODE_OVERLAY,
DISPLAY_MODE_DEPTH,
DISPLAY_MODE_IMAGE
};


class simpleViewer 
{
public:
simpleViewer(void);
~simpleViewer(void);
virtual void run();
virtual void initOpenGL(int argc, char **argv);
virtual void initOpenGLHooks();
virtual void display();
virtual void displayPostDraw(){};
DisplayModes m_eViewState;
private:
static simpleViewer* ms_self;
static void glutIdle();
static void glutDisplay();

};
#endif

and simpleViewer.cpp

#include "simpleViewer.h"

#define GL_WIN_SIZE_X   1280
#define GL_WIN_SIZE_Y   1024

// Undeprecate CRT functions
#ifndef _CRT_SECURE_NO_DEPRECATE 
#define _CRT_SECURE_NO_DEPRECATE 1
#endif

simpleViewer* simpleViewer::ms_self;
simpleViewer::simpleViewer(void)
{
ms_self = this;
}


simpleViewer::~simpleViewer(void)
{
}

void simpleViewer::initOpenGL(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow ("Test");
//  glutFullScreen();
glutSetCursor(GLUT_CURSOR_NONE);
initOpenGLHooks();
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glClearColor(1.0f,0.0f,0.0f,1.0f);
}
void simpleViewer::initOpenGLHooks()
{
glutDisplayFunc(glutDisplay);
glutIdleFunc(glutIdle);
}
void simpleViewer::run(){
glutMainLoop();
}

void simpleViewer::display(){
glBegin(GL_TRIANGLES);
glColor3f(1.0,0,0);
glVertex3f(0.1,0,0);
glVertex3f(0,0,0);
glVertex3f(0,0,0.1);
glEnd();
glFlush();

}
void simpleViewer::glutIdle(){
glutPostRedisplay();
}
void simpleViewer::glutDisplay(){
        simpleViewer::ms_self->display();
}

I have checked so that it really goes into display(), but nothing happens. The background is totally white even if it should be red.

Upvotes: 1

Views: 324

Answers (1)

genpfault
genpfault

Reputation: 52084

You've requested double-buffering via GLUT_DOUBLE.

Use glutSwapBuffers() instead of glFlush() at the end of simpleViewer::display().

glClearColor() only latches some state. glClear() actually does the clear. Add a glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) at the top of simpleViewer::display().

That triangle is wonky for the default projection/modelview matrices. Try this instead:

glBegin( GL_TRIANGLES );
glVertex2i( 0, 0 );
glVertex2i( 1, 0 );
glVertex2i( 1, 1 );
glEnd();

You'll probably want to change the triangle color too. Red on red is pretty hard to see :)

Upvotes: 1

Related Questions