Turtle drawing fractal with openGl - c

I'm trying to do Koch snowflake for a computer graphics course. Searching on the web i've found that a sequence named Thue-morse can approximate the Koch snowflake by using a turtle drawing method.
Here is the code i have so far:
#include <GLUT/glut.h>
#include <math.h>
#include <string.h>
//screen size
#define WIDTH 1024
#define HEIGHT 800
float x, y,mUx,mUy;
//init the turtle environment
void turtleInit(){
x = WIDTH/2; // this is the starting point for the x
y = HEIGHT/2; // this is the starting point for the y
mUx = 1;
mUy = 0;
}
//move the turtle ds units
void turtleMove(float ds){
x += mUx * ds;
y += mUy * ds;
}
//turn left by "ang" radians if positive and right if negative.
void turtleTurn(float ang){
float ux = mUx;
float uy = mUy;
mUx = ux * cos(ang) - uy * sin(ang);
mUy = uy * cos(ang) + ux * sin(ang);
}
//thue morse sequence used to approximate the Koch snowflake
char thue_memoization[10000000];
int thueMorseRecurrenceRelation(int i){
if( thue_memoization[i] != -1 )
return thue_memoization[i];
if ( i % 2 != 0 )
return thue_memoization[i] = 1 - thueMorseRecurrenceRelation(i / 2);
else
return thue_memoization[i] = thueMorseRecurrenceRelation(i / 2);
}
void display( void ){
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-WIDTH, WIDTH, -HEIGHT, HEIGHT, -50, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_POINTS);
glColor3f(0, 0, 1);
turtleInit();
for (int i = 0; i < 1000000; ++i) {
const static float p = 1;//turtle's step
if ( thueMorseRecurrenceRelation(i) )
turtleTurn(M_PI/3.0);
turtleMove(p);
glVertex2f(x, y);
}
glEnd();
glFlush();
}
int main(int argc,char **argv){
memset(thue_memoization,-1,sizeof(thue_memoization));
thue_memoization[0] = 0; //stop condition for the recurrence relation
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Koch snowflake. The winter is comming ...");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
It worked quite well here.
But i don't understand how the turtleTurn function works. Someone can help me ?

This is the formula for a 2d rotation:
(mUx, mUy) contains the coordinates of the "heading vector" of the turtle, then what turtleTurn(float ang) does is turning this vector by an angle (ang).
If you want a nice explanation of this formula, in particular where the sine and cosine come from, you can take a look at the following page, that
has some drawings that will make it clearer:
https://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm

Related

Displaying in openGL based on a matrix

I am trying to draw some shapes in the openGL window. I draw these shapes based on the values in a particular matrix. I am using glut which has a function glutDisplayFunc that takes 1 parameter, a function callback taking no arguments and returns void. But I need to draw an image on the window based on a matrix which I cannot pass to the function callback.
This is an example code
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
#define pi 3.142857
void mat()
{
int a[2][2];
//
for(int i=0;i<2;i++)
for (int j = 0; j < 2; ++j)
{
scanf("%d",&a[i][j]);
}
}
// function to initialize
void myInit (void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-780, 780, -420, 420);
}
void display (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
float x, y, i;
for ( i = 0; i < (2 * pi); i += 0.001)
{
x = 200 * cos(i);
y = 200 * sin(i);
glVertex2i(x, y);
}
glEnd();
glFlush();
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// giving window size in X- and Y- direction
glutInitWindowSize(1366, 768);
glutInitWindowPosition(0, 0);
glutCreateWindow("Circle Drawing");
myInit();
glutDisplayFunc(display);
glutMainLoop();
}
I need to be able to use the matrix a in function mat to define the center of 2 circles. How do I draw the window from within the mat function?
Edit:included code and fixed some typos
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//-----------
float a[4][4] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1 };
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixf((float*)a);
//----------
glBegin(GL_POINTS);
float x, y, i;
for (i = 0; i < (2 * pi); i += 0.001)
{
x = 200 * cos(i);
y = 200 * sin(i);
glVertex2i(x, y);
}
glEnd();
glFlush();
}
In general you can load the current model view matrix, by setting the GL_MODELVIEW matrix mode (glMatrixMode), and loading the matrix by glLoadMatrixf.
Optionally the matrix can be multiplied to the current matrix by glMultMatrix.
But in both cases, the matrix has to be 4x4 Transformation matrix. The parameter to both functions is a pointer to an array of 16 floats respectively an 2 dimensional 4x4 float-array.
Init a 4x4 Identity matrix and read the upper left 2x2, to set up a rotation matrix around the z-axis:
Further, I recommend to read an rotation angle in degree and to calculate the rotation axis by the trigonometric functions sin respectively cos.
Finally read the xy translation components:
#define _USE_MATH_DEFINES
#include <math.h>
float a[4][4];
void mat()
{
// init identity matrix
for(int i = 0; i < 4; i++)
for (int j = 0; j < 4; ++j)
a[i][j] = (i==j) ? 1.0f : 0.0f;
// read the angle in degrees
float angle_degree;
scanf("%f", &angle_degree);
// convert the angle to radian
float angle_radiant = angle_degree * (float)M_PI / 180.0f;
// set rotation around z-axis
float cos_ang = cos(angle_radiant);
float sin_ang = sin(angle_radiant);
a[0][0] = cos_ang;
a[0][1] = -sin_ang;
a[1][0] = sin_ang;
a[1][1] = cos_ang;
// read translation
scanf("%f", &a[3][0]);
scanf("%f", &a[3][1]);
}
void display (void)
{
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(&a[0][0]);
// [...]
}

Translating a single object opengl

I am writing code to draw the figure
but my code gives
as you can see the middle circle is missing.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
float width, height, r = 0.3, change = 0;
void draw(float tx, float ty)
{
glBegin(GL_LINE_LOOP);
for(int i = 1; i <= 1200; i++)
{
float x1, y1, theta;
theta = (2 * 3.14159 * i) / 1200;
x1 = r * cosf(theta) * height / width;
y1 = r * sinf(theta);
glVertex3f(x1 , y1 ,0);
}
glEnd();
glTranslatef(tx, ty, 0);
}
void display()
{
float p[6][2];
int j = 0;
if (change == 0)
change = 1;
else if (change == 1)
change = 0;
width = glutGet(GLUT_WINDOW_WIDTH);
height = glutGet(GLUT_WINDOW_HEIGHT) ;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glBegin(GL_LINE_LOOP);
for(int i = 1; i <= 1200; i++)
{
float theta, x1, y1;
theta = (2 * 3.14159 * i) / 1200;
x1 = r * cosf(theta) * height / width;
y1 = r * sinf(theta);
glVertex3f(x1, y1, 0);
if (i == 100 | i == 300 | i == 500 | i == 700 | i == 900 | i == 1100)
{
if(change == 0){
p[j][0] = x1;
p[j][1] = y1;
j++;
}
}
}
glEnd();
for(int i=0;i<6 && change == 0;i++){
draw(p[i][0],p[i][1]);
}
glutSwapBuffers();
}
void main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700,500);
glutCreateWindow("circles");
glutDisplayFunc(display);
glutMainLoop();
}
Issue is when i translate first circle in draw function the center circle drawn is also translated to that point which is merged with other circle.My doubt is how to translate only one circle not the center one i tried translating by using push and pop matrix but it doesn't work.
Thank you.
glTranslatef() changes the current matrix by appending a translation. So your translations will just accumulate. And since you do not have a transform between the first two circles, they will appear at the same positions. Your program basically does the following:
Draw Circle
draw()
Draw Circle
Move up, right (p[0])
draw()
Draw Circle
Move up (p[1])
draw()
Draw Circle
Move up left (p[2])
...
If you want absolute positioning, you have to reset the transform in between. This can either be done with glLoadIdentity() or with the matrix stack. And be sure to draw after setting the transform.
I guess you know, but in any case a little reminder: The entire matrix stack functionality is deprecated in modern OpenGL and you will need to manage the matrices yourself. I assume, when you do this, everything gets a bit clearer. So I'm not sure if there is a good reason to try to understand the interface of the matrix stack functionalities.
If you want to place each circle at a specific location, you can do something like the following:
void drawCircle()
{
glBegin(GL_LINE_LOOP);
for(int i = 1; i <= 1200; i++)
{
float x1, y1, theta;
theta = (2 * 3.14159 * i) / 1200;
x1 = r * cosf(theta) * height / width;
y1 = r * sinf(theta);
glVertex3f(x1 , y1 ,0);
}
glEnd();
}
void display()
{
// ...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
drawCircle();
for(int i = 0; i < 6; ++i)
{
float angle = M_PI / 3 * i;
float tx = r * sin(angle);
float ty = r * cos(angle);
glPushMatrix(); //save the current matrix
glTranslatef(tx, ty, 0); //move to the desired location
drawCircle();
glPopMatrix(); //restore the old matrix
}
glutSwapBuffers();
}

Reverse the Fish-eye Distortion(I've used openCV with VC++)

I've made a simulation of fish eye distortion.
I want to develop a reverse program that can convert the distorted image to normal image.
I've tried to use undistortPonts() function but couldn't understand the input(dist-coefficient).
cv.UndistortPoints(distorted, undistorted, intrinsics, dist_coeffs)
My code for fish eye distortion:
#include "stdio.h"
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <iostream>
void sampleImage(const IplImage* arr, float idx0, float idx1, CvScalar& res)
{
if(idx0<0 || idx1<0 || idx0>(cvGetSize(arr).height-1) || idx1>(cvGetSize(arr).width-1))
{
res.val[0]=0;
res.val[1]=0;
res.val[2]=0;
res.val[3]=0;
return;
}
float idx0_fl=floor(idx0);
float idx0_cl=ceil(idx0);
float idx1_fl=floor(idx1);
float idx1_cl=ceil(idx1);
CvScalar s1=cvGet2D(arr,(int)idx0_fl,(int)idx1_fl);
CvScalar s2=cvGet2D(arr,(int)idx0_fl,(int)idx1_cl);
CvScalar s3=cvGet2D(arr,(int)idx0_cl,(int)idx1_cl);
CvScalar s4=cvGet2D(arr,(int)idx0_cl,(int)idx1_fl);
float x = idx0 - idx0_fl;
float y = idx1 - idx1_fl;
res.val[0]= s1.val[0]*(1-x)*(1-y) + s2.val[0]*(1-x)*y + s3.val[0]*x*y + s4.val[0]*x*(1-y);
res.val[1]= s1.val[1]*(1-x)*(1-y) + s2.val[1]*(1-x)*y + s3.val[1]*x*y + s4.val[1]*x*(1-y);
res.val[2]= s1.val[2]*(1-x)*(1-y) + s2.val[2]*(1-x)*y + s3.val[2]*x*y + s4.val[2]*x*(1-y);
res.val[3]= s1.val[3]*(1-x)*(1-y) + s2.val[3]*(1-x)*y + s3.val[3]*x*y + s4.val[3]*x*(1-y);
}
float xscale;
float yscale;
float xshift;
float yshift;
float getRadialX(float x,float y,float cx,float cy,float k)
{
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float getRadialY(float x,float y,float cx,float cy,float k)
{
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float thresh = 1;
float calc_shift(float x1,float x2,float cx,float k)
{
float x3 = x1+(x2-x1)*0.5;
float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));
// std::cerr<<"x1: "<<x1<<" - "<<res1<<" x3: "<<x3<<" - "<<res3<<std::endl;
if(res1>-thresh && res1 < thresh)
return x1;
if(res3<0)
{
return calc_shift(x3,x2,cx,k);
}
else
{
return calc_shift(x1,x3,cx,k);
}
}
int main(int argc, char** argv)
{
IplImage* src = cvLoadImage( "D:\\2012 Projects\\FishEye\\Debug\\images\\grid1.bmp", 1 );
IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
IplImage* dst2 = cvCreateImage(cvGetSize(src),src->depth,src->nChannels);
float K=0.002;
float centerX=(float)(src->width/2);
float centerY=(float)(src->height/2);
int width = cvGetSize(src).width;
int height = cvGetSize(src).height;
xshift = calc_shift(0,centerX-1,centerX,K);
float newcenterX = width-centerX;
float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,K);
yshift = calc_shift(0,centerY-1,centerY,K);
float newcenterY = height-centerY;
float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,K);
// scale = (centerX-xshift)/centerX;
xscale = (width-xshift-xshift_2)/width;
yscale = (height-yshift-yshift_2)/height;
std::cerr<<xshift<<" "<<yshift<<" "<<xscale<<" "<<yscale<<std::endl;
std::cerr<<cvGetSize(src).height<<std::endl;
std::cerr<<cvGetSize(src).width<<std::endl;
for(int j=0;j<cvGetSize(dst).height;j++)
{
for(int i=0;i<cvGetSize(dst).width;i++)
{
CvScalar s;
float x = getRadialX((float)i,(float)j,centerX,centerY,K);
float y = getRadialY((float)i,(float)j,centerX,centerY,K);
sampleImage(src,y,x,s);
cvSet2D(dst,j,i,s);
}
}
#if 0
cvNamedWindow( "Source1", 1 );
cvShowImage( "Source1", dst);
cvWaitKey(0);
#endif
cvSaveImage("D:\\2012 Projects\\FishEye\\Debug\\images\\grid3.bmp",dst,0);
cvNamedWindow( "Source1", 1 );
cvShowImage( "Source1", src);
cvWaitKey(0);
cvNamedWindow( "Distortion", 2 );
cvShowImage( "Distortion", dst);
cvWaitKey(0);
#if 0
for(int j=0;j<cvGetSize(src).height;j++)
{
for(int i=0;i<cvGetSize(src).width;i++)
{
CvScalar s;
sampleImage(src,j+0.25,i+0.25,s);
cvSet2D(dst,j,i,s);
}
}
cvNamedWindow( "Source1", 1 );
cvShowImage( "Source1", src);
cvWaitKey(0);
#endif
}
Actually, my original anwser was about the undistortion algorithm for individual points. If you want to undistort a complete image, there is a much simpler technique, as explained in this other thread:
Understanding of openCV undistortion
The outline of the algorithm (which is the one used in OpenCV function undistort()) is as follow. For each pixel of the destination lens-corrected image do:
Convert the pixel coordinates (u_dst, v_dst) to normalized coordinates (x', y') using the inverse of the calibration matrix K,
Apply your lens-distortion model, to obtain the distorted normalized coordinates (x'', y''),
Convert (x'', y'') to distorted pixel coordinates (u_src, v_src) using the calibration matrix K,
Use the interpolation method of your choice to find the intensity/depth associated with the pixel coordinates (u_src, v_src) in the source image, and assign this intensity/depth to the current destination pixel (u_dst, v_dst).
Original answer:
Here is the undistortion algorithm extracted from OpenCV function undistortPoints() :
void dist2norm(const cv::Point2d &pt_dist, cv::Point2d &pt_norm) const {
pt_norm.x = (pt_dist.x-Kcx)/Kfx;
pt_norm.y = (pt_dist.y-Kcy)/Kfy;
int niters=(Dk1!=0.?5:0);
double x0=pt_norm.x, y0=pt_norm.y;
for(int i=0; i<niters; ++i) {
double x2=pt_norm.x*pt_norm.x,
y2=pt_norm.y*pt_norm.y,
xy=pt_norm.x*pt_norm.y,
r2=x2+y2;
double icdist = 1./(1 + ((Dk3*r2 + Dk2)*r2 + Dk1)*r2);
double deltaX = 2*Dp1*xy + Dp2*(r2 + 2*x2);
double deltaY = Dp1*(r2 + 2*y2) + 2*Dp2*xy;
pt_norm.x = (x0-deltaX)*icdist;
pt_norm.y = (y0-deltaY)*icdist;
}
}
If you provide the coordinates of a point in the distorted image in argument pt_dist, it will calculate the normalized coordinates of the associated point and return them in pt_norm. Then, you can obtain the coordinates of the associated point in the undistorted image as
pt_undist = K . [pt_norm.x; pt_norm.y; 1]
where K is the camera matrix.
The standard lens distortion model used by OpenCV is explained at the beginning of this page:
where the distortion coefficients are (k1,k2,p1,p2,k3, k4,k5,k6) (most often we use k4=k5=k6=0).
I don't know what is your model for FishEye distortion, but you can surely adapt the above algorithm to your case. Otherwise, you may use a non-linear optimization algorithm (e.g. Levenberg-Marquardt or any other), to recover the undistorted coordinates from the distorted one.

tessellating a sphere using triangles of different colors

I am writing a function to generate a sphere using triangles to tessellate it, but what I want is for the triangles to all have different colors. However, when I run the code, it creates a sphere but the colors range from light blue to black with no green or red at all and the colors repeat, which is not what I want.
Here is a segment of the code. The whole code can produce a sphere but it is the coloring that I am really stuck on.
triangles is a vector<vector<Vertex3>> which contains the collection of all triangle vertices that make up this sphere.
glBegin(GL_TRIANGLES);
int red = 0;
int green = 0;
int blue = 0;
for( int j = 0; j< triangles.size(); j++ )
{
if(red < 200)
red++;
else if (blue < 200)
green++;
else blue++;
glColor3ub(red, green, blue);
//normalize the triangles first
triangles[j][0].normalize();
triangles[j][2].normalize();
triangles[j][2].normalize();
//call to draw vertices
glVertex3f( (GLfloat)triangles[j][0].getX(),(GLfloat)triangles[j][0].getY(),
(GLfloat)triangles[j][0].getZ());
glVertex3f( (GLfloat)triangles[j][3].getX(),(GLfloat)triangles[j][4].getY(),
(GLfloat)triangles[j][5].getZ());
glVertex3f( (GLfloat)triangles[j][2].getX(),(GLfloat)triangles[j][2].getY(),
(GLfloat)triangles[j][2].getZ());
}
glEnd();
Instead of glColor3ub(red, green, blue), try using glColor3ub( rand()%255, rand()%255, rand()%255 ).
Usage:
glBegin(GL_TRIANGLES);
for( int j = 0; j< triangles.size(); j++ )
{
glColor3ub( rand()%255, rand()%255, rand()%255 );
//normalize the triangles first
triangles[j][0].normalize();
triangles[j][1].normalize();
triangles[j][2].normalize();
//call to draw vertices
glVertex3f( (GLfloat)triangles[j][0].getX(),(GLfloat)triangles[j][0].getY(),
(GLfloat)triangles[j][0].getZ());
glVertex3f( (GLfloat)triangles[j][1].getX(),(GLfloat)triangles[j][1].getY(),
(GLfloat)triangles[j][1].getZ());
glVertex3f( (GLfloat)triangles[j][2].getX(),(GLfloat)triangles[j][2].getY(),
(GLfloat)triangles[j][2].getZ());
}
glEnd();
EDIT: Generate, store, and render:
#include <GL/glut.h>
#include <vector>
using namespace std;
struct Vertex
{
Vertex( bool random = false )
{
x = y = z = 0;
r = g = b = a = 0;
if( random )
{
x = rand() % 20 - 10;
y = rand() % 20 - 10;
z = rand() % 20 - 10;
r = rand() % 255;
g = rand() % 255;
b = rand() % 255;
a = 1;
}
}
float x, y, z;
unsigned char r, g, b, a;
};
vector< Vertex > verts;
void init()
{
// fill verts array with random vertices
for( size_t i = 0; i < 100; ++i )
{
verts.push_back( Vertex(true) );
verts.push_back( Vertex(true) );
verts.push_back( Vertex(true) );
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// enable vertex and color arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// set vertex and color pointers
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &verts[0].x );
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &verts[0].r );
// draw verts array
glDrawArrays(GL_TRIANGLES, 0, verts.size() );
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(800,600);
glutCreateWindow("Demo");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}
I'd expect the colors to go from black to med red to med yellow to gray to lt blue grey with the way your if statement is setup (assuming you have at least 600 triangles.) How many tris are you rendering? Also, depending on how your other shaders are setup, textures could get in the way as could anything else if your vertex format is mismatched. Also not sure how glColor3ub() reacts when the blue field goes above 255, which it will in the above code rather quickly. If it truncates the bits, then you would see the colors repeat from yellow to blue grey every 256 triangles after the first 400 .

OpenGL array of points

I have the following code to draw an array of points but it only draws one point in the center. How can I draw an array of 2D points using OpenGL?
GLint NumberOfPoints = 10;
GLfloat x[2],y[2];
glBegin( GL_POINTS );
for ( int i = 0; i < NumberOfPoints; ++i )
{
glVertex2f( x[i], y[i] );
}
glEnd();
Requires GLUT for window and context management:
#include <GL/glut.h>
#include <vector>
#include <cstdlib>
struct Point
{
float x, y;
unsigned char r, g, b, a;
};
std::vector< Point > points;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draw
glColor3ub( 255, 255, 255 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
glPointSize( 3.0 );
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("Random Points");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// populate points
for( size_t i = 0; i < 1000; ++i )
{
Point pt;
pt.x = -50 + (rand() % 100);
pt.y = -50 + (rand() % 100);
pt.r = rand() % 255;
pt.g = rand() % 255;
pt.b = rand() % 255;
pt.a = 255;
points.push_back(pt);
}
glutMainLoop();
return 0;
}
Where are you setting the values for x[0], x[1], y[0], and y[1]?
If it's only drawing one point in the center, it sounds like the values are set to 0 for all four of those variables. Be sure to initialize their values before you reference them in your call to gVertex2f().
Do you define what x[i] and y[i] are? Otherwise they will be set to 0 automatically (hence the centering). Also, creating the arrays with two elements but accessing 10 elements is very bad since you are accessing memory locations that you do not have control over.
You should do something like :
GLint NumberOfPoints = 10;
GLfloat x[10],y[10];
for(int i = 0; i < NumberOfPoints; i++){
x[i] = y[i] = (GLfloat) i;
}
glBegin( GL_POINTS );
for ( int i = 0; i < NumberOfPoints; ++i )
{
glVertex2f( x[i], y[i] );
}
glEnd();
Your code just works. Fill the array x and y with randomized values and this would draw random points.
The problem may be you can't 'see' the points you draw. That's obvious since:
By default the color of the points is black( 0, 0, 0, in rgb), and you may want to set it to some other value using glColor3f or such functions.
You've drawn too few points and each point is too small(actually only 1 pixel size on your screen). You may want to draw circles instead or draw thousand or more pixels and check again.
By the way, please format your question and let the code displayed normally.
Edited:
See my comment above. If you didn't set up a valid OpenGL context or you just didn't know how, check this http://openglbook.com/the-book/chapter-1-getting-started/ and get started with your first working OpenGL program.

Resources