Evan
Evan

Reputation: 2615

Drawing multiple shapes in OpenGL

#include <Windows.h>
#include <GL\glut.h>

#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

bool init(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glMatrixMode(GL_PROJECTION);
    // Set grid to be from 0 to 1
    gluOrtho2D(0.0, 3.0, 0.0, 3.0);
    return true;
}

void drawline(float from_x, float from_y, float to_x, float to_y)
{
    // From coordinate position
    glVertex2f(from_x, from_y);

    // To coordinate position
    glVertex2f(to_x, to_y);
}

void render(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
    glLineWidth(2.0); // Set line width to 2.0
    glLoadIdentity();

    // Draw line
    glBegin(GL_LINES);
    drawline(0.25, 0.5, 0.4, 0.5);
    drawline(0.4, 0.6, 0.4, 0.5);
    drawline(0.4, 0.4, 0.4, 0.5);
    drawline(0.6, 0.5, 0.75, 0.5);
    glEnd();

    // Draw triangle
    glBegin(GL_TRIANGLES);
    glVertex2f(0.4, 0.5);
    glVertex2f(0.6, 0.6);
    glVertex2f(0.6, 0.4);
    glEnd();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My OpenGL program");

    init();

    // Draw shape one
    glPushMatrix();
    glTranslatef(1.5, 1.5, 0.0);
    glutDisplayFunc(render);
    glPopMatrix();

    // Draw shape two
    glPushMatrix();
    glTranslatef(2.5, 2.5, 0.0);
    glutDisplayFunc(render);
    glPopMatrix();

    glutReshapeFunc(reshape);

    glutMainLoop();
    return 0;
}

I'm working to draw something like this: https://i.sstatic.net/PjEi7.png

I don't need the whole thing, I just want to be able to draw two shapes where I want them to be. However, this isn't working, can anyone point me into the right direction?

Upvotes: 4

Views: 15321

Answers (1)

genpfault
genpfault

Reputation: 52084

You almost had it.

Draw your shape(s) in render(), not main():

#include <GL/glut.h>

void drawline(float from_x, float from_y, float to_x, float to_y)
{
    // From coordinate position
    glVertex2f(from_x, from_y);

    // To coordinate position
    glVertex2f(to_x, to_y);
}

void drawShape()
{
    glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
    glLineWidth(2.0); // Set line width to 2.0

    // Draw line
    glBegin(GL_LINES);
    drawline(0.25, 0.5, 0.4, 0.5);
    drawline(0.4, 0.6, 0.4, 0.5);
    drawline(0.4, 0.4, 0.4, 0.5);
    drawline(0.6, 0.5, 0.75, 0.5);
    glEnd();

    // Draw triangle
    glBegin(GL_TRIANGLES);
    glVertex2f(0.4, 0.5);
    glVertex2f(0.6, 0.6);
    glVertex2f(0.6, 0.4);
    glEnd();
}

void render(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( 0.0, 4.0, 0.0, 4.0, -1, 1 );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // Draw shape one
    glPushMatrix();
    glTranslatef(1.5, 1.5, 0.0);
    drawShape();
    glPopMatrix();

    // Draw shape two
    glPushMatrix();
    glTranslatef(2.5, 2.5, 0.0);
    drawShape();
    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("My OpenGL program");
    glutDisplayFunc(render);
    glutMainLoop();
    return 0;
}

I removed reshape() and init() since the default glutReshapeFunc() already calls glViewport() and you should be resetting your projection and modelview matrices each frame anyway.

Upvotes: 5

Related Questions