Dean
Dean

Reputation: 517

opengl not drawing anything

Just trying to draw a point using glut and glew for opengl version 4.3

my code

void Renderer(void)
{ 
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    glPointSize(100);
    glColor3f(255, 0, 0);
    glVertex3d(10, 10, 0);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

is not rendering anything, can someone tell me what did i miss? here is full code:

#include <iostream>
using namespace std;

#include "vgl.h"
#include "LoadShaders.h"

enum VAO_IDs { Triangles, NumVAOS };

#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY     10 //ms

//general
long frameCount = 0;

// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;

/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);

void timerEvent(int value)
{
    glutPostRedisplay();
    glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}

void init (void)
{
    // default initialization
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glDisable(GL_DEPTH_TEST);

    // viewport
    glViewport(0, 0, WIDTH, HEIGHT);

    // projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}

void Renderer(void)
{ 
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    glPointSize(100);
    glColor3f(255, 0, 0);
    glVertex3d(10, 10, 0);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitContextVersion(4, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow("The Abyss");
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);

    if (glewInit()) //i guess this is true on failure
    {
        cerr << "Error initializing glew, Program aborted." << endl;
        exit(EXIT_FAILURE);
    }


    //Init First
    init();

    //Init callback for Rendering
    glutDisplayFunc(Renderer);

    //Main Loop
    glutMainLoop();

    exit(EXIT_SUCCESS);
}

////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
    switch (key)
    {
        case (27) :
            exit(EXIT_SUCCESS);
            break;
    }
}

void mouse(int button, int state, int x, int y)
{
    if (state == GLUT_DOWN)
    {
        mouse_buttons |= 1<<button;
    }
    else if (state == GLUT_UP)
    {
        mouse_buttons = 0;
    }

    mouse_old_x = x;
    mouse_old_y = y;
}

void motion(int x, int y)
{
    float dx, dy;
    dx = (float)(x - mouse_old_x);
    dy = (float)(y - mouse_old_y);

    if (mouse_buttons & 1)
    {
        rotate_x += dy * 0.2f;
        rotate_y += dx * 0.2f;
    }
    else if (mouse_buttons & 4)
    {
        translate_z += dy * 0.01f;
    }

    mouse_old_x = x;
    mouse_old_y = y;
}

Upvotes: 0

Views: 684

Answers (1)

genpfault
genpfault

Reputation: 52084

glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);

You have requested a Core context. You need to:

  1. Specify a vertex and fragment shader. There are no freebies in Core.
  2. Stop using deprecated functionality like glBegin() and glMatrixMode().
  3. Start using VBOs to submit your geometry.
  4. Start using glDrawArrays() and friends to draw your geometry.

Upvotes: 3

Related Questions