Week #15 Challenge: OpenGL

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 31617 Views - Last Post: 15 May 2011 - 08:53 AM

#16 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Week #15 Challenge: OpenGL

Posted 14 April 2010 - 01:00 PM

View Postgareth00, on 13 April 2010 - 09:30 PM, said:

QT is a great alternative to GLUT in my opinion. It provides great cross-platform UI design and a wealth of useful libraries. There are some openGL examples that are easy to follow. I have tried both, and I will never use GLUT again.

I swear I was not paid to Nokia to say this!

Qt is neat but might be overkill depending on what you're trying to do. GLUT is meant to simplify the platform-dependent intricacies to make examples easier to understand. It's totally usable for games, but you may have to do a little extra work/hacking (keyboard input is useless until you implement some kind of key-state table, joystick support is useless).

Qt gives you real widgets (among other things), and is much better for creating applications that need more than just a bare OpenGL window. :)

Also worth noting that Qt is out of the question if you don't want to use C++.
Was This Post Helpful? 0
  • +
  • -

#17 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Week #15 Challenge: OpenGL

Posted 14 April 2010 - 02:30 PM

View PostTom9729, on 14 April 2010 - 03:00 PM, said:

Also worth noting that Qt is out of the question if you don't want to use C++.


Not really true. I have integrated Qt Jambi with JOGL in java and it seems to work well. I would imagine that other Qt bindings will probably play well with the associated OpenGL binding for other languages.
Was This Post Helpful? 1
  • +
  • -

#18 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Week #15 Challenge: OpenGL

Posted 14 April 2010 - 03:52 PM

View PostNickDMax, on 14 April 2010 - 04:30 PM, said:

View PostTom9729, on 14 April 2010 - 03:00 PM, said:

Also worth noting that Qt is out of the question if you don't want to use C++.


Not really true. I have integrated Qt Jambi with JOGL in java and it seems to work well. I would imagine that other Qt bindings will probably play well with the associated OpenGL binding for other languages.

Whoops! Sorry, I meant to say you can't easily use Qt in C. I realize that there are bindings to many other languages (Java and Python especially).

Edit: Ahhhh sorry, lack of sleep.

This post has been edited by Tom9729: 14 April 2010 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

#19 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Week #15 Challenge: OpenGL

Posted 17 April 2010 - 08:54 PM

I finally got a chance to work on my submission. It's a simplistic Pong game in C!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/freeglut.h>


struct {
    int width;
    int height;
} WindowInfo;

struct PlayerInfo {
    int score;
    int xpos;
    int ypos;
    int width;
    int height;
} player, computer;

struct {
    int xpos;
    int ypos;
    int xmove;
    int ymove;
    int width;
    int height;
} Ball;

void move_ball(void);
void move_computer(void);

void render(void) {
    move_ball();
    move_computer();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glShadeModel(GL_FLAT);

    //draw player 1
    glPushMatrix();
    glColor3ub(255, 0, 0);
    glTranslatef(0.0f, player.ypos , 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(20, (-player.height));
    glVertex2f(20, (player.height));
    glVertex2f(20 + player.width, (player.height));
    glVertex2f(20 + player.width, (-player.height));
    glEnd();
    glPopMatrix();

    //draw player 2
    glPushMatrix();
    glColor3ub(0, 0,255);
    glTranslatef(0.0f, computer.ypos , 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(computer.xpos, (-computer.height));
    glVertex2f(computer.xpos, (computer.height));
    glVertex2f(computer.xpos + computer.width, (computer.height));
    glVertex2f(computer.xpos + computer.width, (-computer.height));
    glEnd();
    glPopMatrix();

    //draw ball
    glPushMatrix();
    glColor3ub(255, 255, 255);
    glTranslatef(Ball.xpos, Ball.ypos , 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(-10, (-Ball.height));
    glVertex2f(-10, (Ball.height));
    glVertex2f(10, (Ball.height));
    glVertex2f(10, (-Ball.height));
    glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

void idle(void) {
    glutPostRedisplay();
}
void resize(int w, int h) {
    WindowInfo.width = h;
    WindowInfo.height = h;

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, w, 0, h);
    glMatrixMode(GL_MODELVIEW);
}
#define Y_MOVE 10
#define Y_MOVE_COMP 10

void reset_ball(void) {
    Ball.ypos = WindowInfo.height / 2;
    Ball.xpos = WindowInfo.width  / 2;
    Ball.xmove = rand() % 5 + 10;
    Ball.ymove = rand() % 5 + 10;
}
int collides_with(struct PlayerInfo p) {
    if (Ball.ypos + Ball.height >= p.ypos - 30 && Ball.ypos <= p.ypos + p.height) {
        return 1;
    }
    return 0;
}

void check_bounds(struct PlayerInfo* p) {
    if (p->ypos > WindowInfo.height - p->height) {
        p->ypos = WindowInfo.height - p->height;
    } else if (p->ypos <  p->height ) {
        p->ypos = p->height ;
    }
}

void move_player(struct PlayerInfo* p, int y_chg) {
    p->ypos += y_chg;
    check_bounds(p);
}
void move_computer(void) {
    if (Ball.ypos - computer.ypos > Y_MOVE_COMP ) {
        move_player(&computer, Y_MOVE_COMP);
    } else if (Ball.ypos < computer.ypos) {
        move_player(&computer, -Y_MOVE_COMP);
    }
}
void check_ball_bounds(void) {
    if (Ball.ypos + Ball.ymove > WindowInfo.height) {
        Ball.ymove = -Ball.ymove;
    } else if (Ball.ypos + Ball.ymove < Ball.height) {
        Ball.ymove = -Ball.ymove;
    }

    if (Ball.xpos < 50) {
        if (!collides_with(player)) {
            computer.score++;
            printf("Computer scored!\n");
            reset_ball();
        } else {
            Ball.xmove = -Ball.xmove;
        }
    } else if (Ball.xpos > WindowInfo.width - 50) {
        if (!collides_with(computer)) {
            printf("Player Scored!\n");
            reset_ball();
        } else {
            Ball.xmove = -Ball.xmove;
        }
    }
}

void move_ball(void) {
    check_ball_bounds();
    Ball.xpos += Ball.xmove;
    Ball.ypos += Ball.ymove;

}
void init_components(void) {
    srand(time(NULL));

    WindowInfo.width = glutGet(GLUT_WINDOW_WIDTH);
    WindowInfo.height = glutGet(GLUT_WINDOW_HEIGHT);

    player.score = 0;
    player.width = 20;
    player.height = 75;
    player.ypos = WindowInfo.height / 2 + player.height;
    player.xpos = WindowInfo.width + 50;

    computer.score = 0;
    computer.width = 20;
    computer.height = 50;
    computer.ypos = WindowInfo.height / 2 + computer.height;
    computer.xpos =  WindowInfo.width - 50;

    Ball.ypos = WindowInfo.height / 2;
    Ball.xpos = WindowInfo.width  / 2;
    Ball.width = 10;
    Ball.height = 10;
    Ball.xmove = rand() % 5 + 10;
    Ball.ymove = rand() % 5 + 10;
}

void keyboard (unsigned char key, int x, int y) {
    switch (key) {
    case 'w':
        move_player(&player, Y_MOVE);
        break;
    case 's':
        move_player(&player, -Y_MOVE);
        break;
    case 27: //escape
        exit(0);
    default:
        break;
    }
}

void init(int argc, char ** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(800, 800);

    glutCreateWindow("GLPong");


    glutDisplayFunc(render);
    glutReshapeFunc(resize);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);
    init_components();
}

int main(int argc, char** argv) {
    init(argc, argv);
    glutMainLoop();
    return EXIT_SUCCESS;
}



It's been a long time since I've used OpenGL, so I'm pretty happy that it's actually functional

It was spread across several headers if you were wondering. I hate cramming everything into main (but it's easier like this) ;)


I'll also give the Clojure bindings a shot, but can't promise anything more impressive than a spinning triangle :)
Was This Post Helpful? 2
  • +
  • -

#20 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Week #15 Challenge: OpenGL

Posted 19 April 2010 - 12:48 AM

Been messing around with F# since the recent Visual Studio 2010 release and wrote this little bit of code to render a simple OpenGL window and display a white square in the center. Nothing fantastic, but I only just started learning F# and I think it's pretty good for a week of learning. I used the Tao Framework to work with OpenGL, which is worth a look at if you want to mess around with OpenGL in other .NET languages like C#.

open System
open Tao.OpenGl
open Tao.FreeGlut

// function to render the screen content
let display () =
    Gl.glClear Gl.GL_COLOR_BUFFER_BIT
    Gl.glColor3f (1.0f, 1.0f, 1.0f)

    Gl.glBegin Gl.GL_POLYGON

    Gl.glVertex3f (-0.5f, -0.5f, 0.0f)
    Gl.glVertex3f (0.5f, -0.5f, 0.0f)
    Gl.glVertex3f (0.5f, 0.5f, 0.0f)
    Gl.glVertex3f (-0.5f, 0.5f, 0.0f)

    Gl.glEnd ()
    Gl.glFlush ()

// function to check for escape press
let keyboard (key:byte) x y =
    let keyChar = char key
    if keyChar = '\x1B' then exit 0

// it's the main function
let main =
    printfn "Initializing OpenGL..."
    Glut.glutInit ()
    Glut.glutInitDisplayMode (Glut.GLUT_SINGLE ||| Glut.GLUT_RGB)
    Glut.glutInitWindowSize (200, 200)
    Glut.glutInitWindowPosition (200, 200)

    Gl.glClearColor (0.0f, 0.0f, 0.0f, 1.0f)
    Gl.glMatrixMode Gl.GL_PROJECTION
    Gl.glLoadIdentity ()
    Gl.glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)

    ignore (Glut.glutCreateWindow ("OpenGL F# Sample"))

    Glut.glutDisplayFunc (new Glut.DisplayCallback (display))
    Glut.glutKeyboardFunc (new Glut.KeyboardCallback (keyboard))

    printfn "Now entering the main loop..."
    Glut.glutMainLoop ()

[<STAThread>]
do main


This post has been edited by Skaggles: 19 April 2010 - 12:59 AM

Was This Post Helpful? 2
  • +
  • -

#21 JesseLord  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 24-May 10

Re: Week #15 Challenge: OpenGL

Posted 17 June 2010 - 10:05 PM

The poly count on a droid moto is what 27,000 ish at 30 frames. Would it be possible to get OpenGL 1.3 to render Asheron's Call client on a Driod? ac.turbine.com. I would think that very interesting what do you guys think?
Was This Post Helpful? 0
  • +
  • -

#22 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: Week #15 Challenge: OpenGL

Posted 05 July 2010 - 02:21 AM

hi
I wanna use 3D effects one C# forms, is PoenGL appropriate?
if not, how I can learn about 3D effects on C#
for example creating a cube that user can see all sides of cube by clicking in cube
Was This Post Helpful? -1
  • +
  • -

#23 kr4m0llz27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-August 10

Re: Week #15 Challenge: OpenGL

Posted 18 August 2010 - 10:13 PM

Excuse me guys. Is this challenge requires to USE PC in br0wsing and in j0ining? Sory to ask this. Because im only using cell ph0ne br0wser. And don't have flash. I cant even see the videos. :(
Was This Post Helpful? 0
  • +
  • -

#24 Guest_xenoslash*


Reputation:

Re: Week #15 Challenge: OpenGL

Posted 13 December 2010 - 10:32 PM

Thanks for the very detailed instructions!
However, I can't find constants for glPolygonMode (e.g GL_POINT, GL_LINE , etc) in the JOGL 2.0 library
I am using Eclipse, and when I type GL.GL_LINE, it says "no suggestions available."
Should I re-download the whole library, or is it that JOGL 2.0 doesn't have these constants yet?
Oh, and other constants, like GL.GL_TRIANGLES can be found, so far I only have problems with the glPolygonMode constants.

Thanks!
Was This Post Helpful? 0

#25 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Week #15 Challenge: OpenGL

Posted 15 May 2011 - 08:53 AM

I guess this might be a slight interest. A while back I decided to write a model loader capable of loading DFF models. DFF is a model file format devised by Renderware and is known to be used in the GTA series of games. Unfortunately I never really finished the thing but it does load most models. You may recognise this model if you're familier with GTA San Andreas (Hydra).

Posted Image

The graphics are rendered using OpenGL of course.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2