OpenGL in Visual Studios C++ Edition - c++

Basically, I have included the block of code which Compiles & appears to run for that matter, but the graphics does not display on the menus I have initialized. I know the code should display, because it compiles, runs & displays what it's suppose to on Linux. However, after transferring the code to Visual Studios, & going through a heavy amount of adjustments( glu, glut, & compatability adjustments ) it finally compiled, but now another problem has arised, which is no graphics that clearly displayed on another machine. :(
#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include"SDL.h"
#include"SDL_opengl.h"
#include"glut.h"
#include < tchar.h >
#include < cstdlib >
#include < ctime >
#include < iostream >
#include < fstream >
#include < stdlib.h >
#include < stdio.h >
#include < string >
using namespace std;
#ifdef GL_VERSION_1_1
#define POINTER 1
#define INTERLEAVED 2
#define DRAWARRAY 1
#define ARRAYELEMENT 2
#define DRAWELEMENTS 3
int setupMethod = POINTER;
int derefMethod = DRAWARRAY;
static int legs = 0;
void setupPointers( void );
void init( void );
void display( void );
void reshape( int, int );
void keyboard( unsigned char, int, int );
int _tmain( int argc, char **argv ) {
glutInit( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize ( 500, 500 );
glutInitWindowPosition ( 100, 100 );
glutCreateWindow ( argv[0] );
init( );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutMainLoop( );
return 0;
}
void setupPointers( void ) {
static GLint vertices[ ] = {
//spiderbody
-10, 35,
10, 35,
20, 55,
10, 75,
-10, 75,
-20, 55,
-10,35,
-30,25,
-30,-25,
-10,-35,
10,-35,
30,-25,
30,25,
10,35,
//legs
//leg1 position1
20,30, 40,42.5,32.5,77.5,47.5,42.5,30,25,
//leg2pos1
30,12.5,50,22.5,55,42.5,56,17.5,30,5,
//leg3pos1
30,-12.5,50,-22.5,55,-42.5,56,-17.5,30,-5,
//leg4pos1
20,-30, 40,-42.5,32.5,-77.5,47.5,-42.5,30,-25,
//leg5pos1
-20,30, -40,42.5,-32.5,77.5,-47.5,42.5,-30,25,
//leg6pos1
-30,12.5,-50,22.5,-55,42.5,-56,17.5,-30,5,
//leg7pos1
-30,-12.5,-50,-22.5,-55,-42.5,-56,-17.5,-30,-5,
//leg8pos1
-20,-30, -40,-42.5,-32.5,-77.5,-47.5,-42.5,-30,-25,
//leg1pos2
20,30,45,37.5,50,75,50,32.5,30,25,
//leg2pos2
30,12.5,51,12.5,47.5,22.5,60,5,30,5,
//leg3pos2
30,-12.5,51,-12.5,47.5,-22.5,60,-5,30,-5,
//leg4pos2
20,-30,45,-37.5,50,-75,50,-32.5,30,-25,
//leg5pos2
-20,30,-45,37.5,-50,75,-50,32.5,-30,25,
//leg6pos2
-30,12.5,-51,12.5,-47.5,22.5,-60,5,-30,5,
//leg7pos2
-30,-12.5,-51,-12.5,-47.5,-22.5,-60,-5,-30,-5,
//leg8pos2
-20,-30,-45,-37.5,-50,-75,-50,-32.5,-30,-25
};
static GLfloat colors[ ] = {
0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.75, 0.35, 0.35,
0.5, 0.28, 0.35
}; // COLORS
glEnableClientState ( GL_VERTEX_ARRAY );
glEnableClientState ( GL_COLOR_ARRAY );
glVertexPointer ( 2, GL_INT, 0, vertices );
glColorPointer ( 3, GL_FLOAT, 0, colors );
}
void init(void) {
glColor3f( 1,1,1 );
glutWireSphere( 100,100,100 );
glShadeModel( GL_SMOOTH );
setupPointers ( );
}
void display( void ) {
glClear (GL_COLOR_BUFFER_BIT);
glDrawArrays (GL_POLYGON, 0, 14);
if( legs % 5 == 0 || legs < 5 || legs%5==1 ) {
glDrawArrays (GL_POLYGON,15,4);
glDrawArrays (GL_POLYGON,20,4);
glDrawArrays (GL_POLYGON,25,4);
glDrawArrays (GL_POLYGON,30,4);
glDrawArrays (GL_POLYGON,35,4);
glDrawArrays (GL_POLYGON,40,4);
glDrawArrays (GL_POLYGON,45,4);
glDrawArrays (GL_POLYGON,50,4);
}
//second position of legs
else if( legs % 5 !=0 && legs%5!=1 ) {
glDrawArrays (GL_POLYGON,55,4);
glDrawArrays (GL_POLYGON,60,4);
glDrawArrays (GL_POLYGON,65,4);
glDrawArrays (GL_POLYGON,70,4);
glDrawArrays (GL_POLYGON,75,4);
glDrawArrays (GL_POLYGON,80,4);
glDrawArrays (GL_POLYGON,85,4);
glDrawArrays (GL_POLYGON,90,4);
}
}
void reshape ( int w, int h ) {
glViewport ( 0, 0, (GLsizei) w, (GLsizei) h );
gluOrtho2D ( -60.0, (GLdouble) w, -60.0, (GLdouble) h );
}
void keyboard ( unsigned char key, int x, int y ) {
switch ( key ) {
case 'd':
glTranslatef( 1,1,0 );
glRotatef( -5,0,0,1.0 );
legs++;
break;
case 'a':
glTranslatef( 1,1,0 );
glRotatef( 5,0,0,1.0 );legs++;
break;
case 'w':
glTranslatef( 0,5,0 );legs++;
break;
case 's':
glTranslatef( 0,-5,0 );legs++;
break;
case 27:
exit(0);
break;
default:
break;
}
glutPostRedisplay( );
}
#else
#endif

Related

Can't assign C++ array

Context: Need to set verts in TestTextures3SpriteObj s1 to the verts1 array. Gives me an error "expression must be modifiable lvalue". After its copied the vertices will be sent to the GPU as buffer data with OpenGL and GLUT.
Only relevant excerpts of code included
#pragma once
class TestTextures3SpriteObj
{
public:
int spriteid;
int vao;
int texid;
float verts[];
};
const float verts1[] = { 0.5 ,0.5, 0.0, 0.9, 0.5, 0.3, 0.0, 1.0, 0.0,
0.5, -0.5, 0.0, 0.3, 0.3, 0.9, 1.0, 1.0, 1.0,
-0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
-0.5, 0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
TestTextures3SpriteObj s1;
s1.verts = verts1;
Actually you are not accessing the variable ...
if you want access individual element, use index
s1.verts1[0]
If you want to copy use std::copy
std::copy(verts1, verts1 + 36, s1.verts);
#include <iostream>
using namespace std;
class TestTextures3SpriteObj
{
public:
int spriteid;
int vao;
int texid;
float verts[36]; //assign the size to the array
};
const float verts1[] = { 0.5 ,0.5, 0.0, 0.9, 0.5, 0.3, 0.0, 1.0, 0.0,
0.5, -0.5, 0.0, 0.3, 0.3, 0.9, 1.0, 1.0, 1.0,
-0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
-0.5, 0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
int main()
{
TestTextures3SpriteObj s1;
int len=sizeof(verts1)/sizeof(verts1[0]);
//copies the entire array to the object with member verts
std::copy(verts1, verts1 + 36, s1.verts);
//printing the values in the s1 object
for(int i=0;i<len;i++)
{
cout<<s1.verts[i]<<" ";
}
}
Assign a size to the array in the class and then later perform the std::copy to copy the values in the verts array.

OpenGL how to check if a point is inside a area made of Bernstein curve

I want to check if a specified corrdinate is inside an area closed under curve.
My curve line is generated as follows:
float CtlPoint[] =
{
0, -0.3, 0,
0.15, -0.25, 0,
0.6, -0.2, 0,
0.2, 0.20, 0,
0.10, 0.25, 0,
0.05, 0.5, 0,
0.02, 0.6, 0,
0.0, 0.8, 0,
-0.02, 0.6, 0,
-0.05, 0.5, 0,
-0.10, 0.25, 0,
-0.2, 0.20, 0,
-0.6, -0.2, 0,
-0.15, -0.25, 0,
0, -0.3, 0
};
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 15, CtlPoint);
glEnable(GL_MAP1_VERTEX_3);
glBegin(GL_LINE_STRIP);
for (int t = 0; t <= 100; ++t){
glEvalCoord1f(static_cast<float>(t) / 100);
}
glEnd();
I randomly generate 2 float numbers x and y and want to test a coordinate using those two values is inside the curve line specified above.
Capture the eval output via your favorite method and use a standard point-in-polygon test.

OpenGL rendering causes graphical glitch

Code is below. When the program runs, it generates a more or less correct image, but there are some graphical glitches. Here are some images:. Any ideas on what could be causing this? The weirdest part is that the issue appears to form on triangles, even though the image is being drawn with polygons (quads).
void buildList() {
cubelist = glGenLists(1);
glNewList(cubelist,GL_COMPILE);
// Multi-colored side - FRONT
glBegin(GL_POLYGON);
glColor3b(0,0,0);
glVertex3f( -0.5, -0.5, -0.5);
glVertex3f( -0.5, 0.5, -0.5);
glVertex3f( 0.5, 0.5, -0.5);
glVertex3f( 0.5, -0.5, -0.5);
// Black side - BACK
glBegin(GL_POLYGON);
glColor3b(0,0,0);
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
glEndList();
}
void setupGL() {
glClearColor(1, 1, 1, 0);
glEnable(GL_DEPTH_CLAMP);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glCullFace(GL_FRONT_AND_BACK);
}
int main(int, char const**)
{
sf::Window window(sf::VideoMode(1600, 1200), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
//initWorld();
setupGL();
buildList();
while (running)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Process events
sf::Event event;
handleInput(window);
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
running = false;
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
running = false;
}
}
glCallList(cubelist);
// Update the window (c is a "Camera" object that just sets the perspective. I can provide code if necessary)
c.draw();
window.display();
}
return EXIT_SUCCESS;
}
EDIT: So changing the GL_POLYGONS statements to GL_QUADS seemed to fix the problem, but I'm still somewhat curious as to why.
Your first glBegin doesn't have a matching glEnd, this causes all the other glBegins to be ignored until the next glEnd. This means the front face leaked out to the back face.
That said immediate mode and the related displayList are deprecated, instead look into VBOs to store the vertex data.

How to draw board 3d? GLUT

I know how to draw rectangles, but I want to draw board. It will be part of my map where I wanted to create maze. It should be 3D. So I need to create simple board. I know that I need to create it using polygons. Can someone help?
glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // P1 is red
glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 0.5, 0.5, -0.5 ); // P2 is green
glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -0.5, 0.5, -0.5 ); // P3 is blue
glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( -0.5, -0.5, -0.5 ); // P4 is purple
glEnd();
// White side - BACK
glBegin(GL_POLYGON);
glColor3f( 1.0, 1.0, 1.0 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
It will draw a simple cube, but I need to transform it to the board. How can I do this?
To make your cube appear like the board (or floor) you described, you've got two main options.
The first is simply to change the coordinates of your polygons. At the moment, your cube runs from -0.5 to 0.5 on each axis. You could change the coordinates to run from e.g. -5.0f to 5.0f on X and Z, and -10.0f, to -9.0f on Y.
If you're not sure how to do that, it involves changing the numbers in the glVertex3f() calls. The parameters are in this order: X, Y, Z.
The second option is to use matrix transformations. A translation can move the cube downwards towards the floor, and a scale can stretch it out sideways:
glPushMatrix();
glTranslatef(0.0f, -9.5f, 0.0f); // move downwards
glScalef(10.0f, 1.0f, 10.0f); // stretch on X and Z
// insert your cube drawing code here
glPopMatrix();
The push/pop calls are there to prevent the matrix transformations from affecting other drawing, or from accumulating each time your drawing code is executed.

creating a 3d cube and bouncing balls

I'm writing a program in OpenGL, I'm drawing a simple cube 3D and I want to add bouncing balls so that they bounce inside the walls of the cube, I tried to add a ball class and the following code:
GLfloat WHITE[] = {1, 1, 1};
GLfloat RED[] = {1, 0, 0};
GLfloat GREEN[] = {0, 1, 0};
GLfloat MAGENTA[] = {0, 0, 1};
class Ball {
double radius;
GLfloat* color;
double maximumHeight;
double x;
double y;
double z;
int direction;
public:
Ball(double r, GLfloat* c, double h, double x, double z):
radius(r), color(c), maximumHeight(h), direction(-1),
y(h), x(x), z(z) {
}
void update() {
y += direction * 0.05;
if (y > maximumHeight) {
y = maximumHeight; direction = -1;
} else if (y < radius) {
y = radius; direction = 1;
}
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
glTranslated(x, y, z);
glutSolidSphere(radius, 30, 30);
glPopMatrix();
}
};
Ball balls[] = {
Ball(0.5, GREEN, 7, 6, 1),
Ball(0.5, MAGENTA, 6, 3, 4),
Ball(0.5, WHITE, 5, 1, 7)
};
and in the display function I added this:
for (int i = 0; i < sizeof balls / sizeof(Ball); i++) {
balls[i].update();
}
This is my cube code:
void display();
void specialKeys();
double rotate_y=0;
double rotate_x=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 ); glVertex3f( 0.5, -0.5, -0.5 ); // P1 is red
glColor3f( 0.0, 1.0, 0.0 ); glVertex3f( 0.5, 0.5, -0.5 ); // P2 is green
glColor3f( 0.0, 0.0, 1.0 ); glVertex3f( -0.5, 0.5, -0.5 ); // P3 is blue
glColor3f( 1.0, 0.0, 1.0 ); glVertex3f( -0.5, -0.5, -0.5 ); // P4 is purple
glEnd();
glBegin(GL_POLYGON);
glColor3f( 1.0, 1.0, 1.0 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glEnd();
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();*/
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
glFlush();
glutSwapBuffers();
}
void specialKeys( int key, int x, int y ) {
if (key == GLUT_KEY_RIGHT)
rotate_y += 5;
else if (key == GLUT_KEY_LEFT)
rotate_y -= 5;
else if (key == GLUT_KEY_UP)
rotate_x += 5;
else if (key == GLUT_KEY_DOWN)
rotate_x -= 5;
glutPostRedisplay();
}
int main(int argc, char* argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Awesome Cube");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutMainLoop();
return 0;
}
GLfloat WHITE[] = {1, 1, 1};
...
Ball(0.5, WHITE, 5, 1, 7)
...
color(c)
....
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
glMaterialfv():
GL_AMBIENT: params contains four integer or floating-point values
...
GL_DIFFUSE: params contains four integer or floating-point values
...
GL_AMBIENT_AND_DIFFUSE: Equivalent to calling glMaterial twice with the same parameter values, once with GL_AMBIENT and once with GL_DIFFUSE.
EDIT:
#include <GL/glut.h>
// http://eigen.tuxfamily.org
#include <Eigen/Core>
using namespace Eigen;
struct PointMass
{
PointMass() : pos(0,0,0), vel(0,0,0) {}
PointMass( const Vector3f& pos )
: pos( pos )
, vel( 0, 0, 0 )
{}
void Integrate( float dt )
{
// "gravity" force vector
Vector3f g( 0, 0, -2 );
// semi-implicit euler
vel = vel + g * dt;
pos = pos + vel * dt;
// collision detection/response
if( pos.z() < 0 )
{
pos.z() = -pos.z();
vel.z() = -vel.z();
}
}
Vector3f pos;
Vector3f vel;
};
GLfloat WHITE[] = {1, 1, 1, 1};
GLfloat RED[] = {1, 0, 0, 1};
GLfloat GREEN[] = {0, 1, 0, 1};
GLfloat MAGENTA[] = {0, 0, 1, 1};
class Ball
{
double radius;
GLfloat* color;
PointMass pm;
public:
Ball(double r, GLfloat* c, const Vector3f& pos )
: radius(r)
, color(c)
, pm( pos )
{ }
void Integrate( float dt )
{
pm.Integrate( dt );
}
void Draw()
{
glPushMatrix();
glColor4fv( color );
glTranslatef( pm.pos.x(), pm.pos.y(), pm.pos.z() );
glutSolidSphere(radius, 30, 30);
glPopMatrix();
}
};
Ball balls[] =
{
Ball( 0.1, GREEN, Vector3f( 1, 1, 2 ) ),
Ball( 0.1, MAGENTA, Vector3f( -1, 1, 1 ) ),
Ball( 0.1, WHITE, Vector3f( 0, -1, 1.5 ) ),
};
double rotate_x = 55;
double rotate_z = 25;
void display()
{
static int last = glutGet(GLUT_ELAPSED_TIME);
int cur = glutGet(GLUT_ELAPSED_TIME);
float dt = ( cur - last ) / 1000.0f;
last = cur;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective( 60, w / h, 0.1, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0, 0, -5 );
glRotatef( -rotate_x, 1.0, 0.0, 0.0 );
glRotatef( -rotate_z, 0.0, 0.0, 1.0 );
// ground
glBegin(GL_QUADS);
glColor3f( 1.0, 0.0, 0.0 );
glVertex2f( -2, -2 );
glColor3f( 0.0, 1.0, 0.0 );
glVertex2f( 2, -2 );
glColor3f( 0.0, 0.0, 1.0 );
glVertex2f( 2, 2 );
glColor3f( 1.0, 0.0, 1.0 );
glVertex2f( -2, 2 );
glEnd();
for (int i = 0; i < sizeof balls / sizeof(Ball); i++)
{
balls[i].Integrate( dt );
balls[i].Draw();
}
glutSwapBuffers();
}
void specialKeys( int key, int x, int y )
{
if (key == GLUT_KEY_RIGHT)
rotate_z += 5;
if (key == GLUT_KEY_LEFT)
rotate_z -= 5;
if (key == GLUT_KEY_UP)
rotate_x += 5;
if (key == GLUT_KEY_DOWN)
rotate_x -= 5;
}
void timer( int extra )
{
// run display() every 16ms or so
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Awesome Cube");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}