Results 1 to 4 of 4

Thread: Multiple Triangles

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2015
    Posts
    4

    Multiple Triangles

    Hello,

    I am trying to display 10 different sized triangles on screen and at the moment nothing appears when I run the code.
    Some help would be great

    #include<ctime>
    #include<cstdlib>
    #include<GL/glut.h>
    #include<cmath>
    using namespace std;

    void start_random()
    {
    static bool seeded = false;
    if (!seeded){
    srand((unsigned)time(NULL));
    seeded = true;
    }
    }

    // Generate a random float in the range -1 to +1...
    float rnd(){
    start_random();
    return (-1) + (float)rand() / 16384;
    }

    class Triangle
    {
    public:
    Triangle(float X, float Y, float width, float height);
    void Show();
    private:
    float x, y, w, h;
    };

    Triangle::Triangle(float X, float Y, float width, float height)
    {
    x = rnd(); y = rnd(); w = rnd(); h = rnd();
    }

    void Triangle::Show()
    {
    glVertex2f(x, y);
    glVertex2f(x - 0.5*w, y - h);
    glVertex2f(x + 0.5*w, y - h);

    }

    void CreateShapes(float x, float y, float w, float h)
    {
    for (int i = 0; i < 100; i++)
    {
    Triangle t(x, y, w, h);
    t.Show();
    }

    }


    void display()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 0);
    void CreateShapes(float x,float y,float w,float h);
    glFlush();


    }

    int main(int argc, char**argv)
    {

    glutInit(&argc, argv);
    glutCreateWindow("Random Shapes");
    glClearColor(1, 1, 1, 1);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
    }

  2. #2
    Junior Member Newbie
    Join Date
    Feb 2015
    Posts
    21
    You are not drawing anything.
    Code :
    void CreateShapes(float x,float y,float w,float h);
    is a function-definition, not a call. I didn't even knew this would compile. What you're looking for is more like
    Code :
    CreateShapes(0,0,100,100);

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2015
    Posts
    4
    Thanks man! That has worked!

    It now display a randomly sized triangle but where shoud I put the for loop so that is does this lets say 10 times?

  4. #4
    Member Contributor
    Join Date
    Oct 2014
    Posts
    55
    Quote Originally Posted by DeclanMcgee11 View Post
    Thanks man! That has worked!

    It now display a randomly sized triangle but where shoud I put the for loop so that is does this lets say 10 times?
    Create 10 triangles once when your program is loading, not in every callback of display().
    Put the triangles in an array or std::vector.
    Put the loop in display and call the show method of every triangle.


    Well, maybe learn C++ first instead of doing random changes at some example code?

Similar Threads

  1. Replies: 9
    Last Post: 11-02-2018, 08:16 AM
  2. Multiple glViewPort calls making triangles glitch out!?
    By gennoveus in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 02-07-2015, 06:19 AM
  3. Extracting triangles of multiple materials
    By Ceashure in forum COLLADA
    Replies: 1
    Last Post: 07-19-2011, 04:05 PM
  4. Error:Multiple triangles inside one glBegin-glEnd
    By Oliver Demetz in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-18-2009, 04:10 AM
  5. Streching a texture over multiple triangles?
    By Kiamo in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 09-25-2001, 06:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean