Related
This is my fist time studying Computer Graphics and i was given the code below from my professor. I think i have understood how the algorithm is implemented in general, but what i fail to understand is in what way the incx,incy,inc1,inc2 lines work. I obviously know they are there to increase something but since i haven't fully understood the algorithm i'm kinda baffled. This is the full code:
#include <gl/glut.h>
#include <stdio.h>
int xstart, ystart, xend, yend;
void myInit() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
void draw_pixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void draw_line(int xstart, int xend, int ystart, int yend) {
int dx, dy, i, e;
int incx, incy, inc1, inc2;
int x,y;
dx = xend-xstart;
dy = yend-ystart;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
incx = 1;
if (xend < xstart) incx = -1;
incy = 1;
if (yend < ystart) incy = -1;
x = xstart;
y = ystart;
if (dx > dy) {
draw_pixel(x, y);
e = 2 * dy-dx;
inc1 = 2*(dy-dx);
inc2 = 2*dy;
for (i=0; i<dx; i++) {
if (e >= 0) {
y += incy;
e += inc1;
}
else
e += inc2;
x += incx;
draw_pixel(x, y);
}
} else {
draw_pixel(x, y);
e = 2*dx-dy;
inc1 = 2*(dx-dy);
inc2 = 2*dx;
for (i=0; i<dy; i++) {
if (e >= 0) {
x += incx;
e += inc1;
}
else
e += inc2;
y += incy;
draw_pixel(x, y);
}
}
}
void myDisplay() {
draw_line(xstart, xend, ystart, yend);
glFlush();
}
void main(int argc, char **argv) {
printf( "Enter (xstart, ystart, xend, yend)\n");
scanf("%d %d %d %d", &xstart, &ystart, &xend, ¥d);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Algorithm Visualization");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
In this code, I'm trying to shade a surface properly based on the position of a light which can be moved. So when you move the light, the surface updates. In addition, I'm looking to have two lights of different colors both shading the same surface. Unfortunately, the surface color remains static.
What I'd like:
1) Have the surface update when the light is moved, and a vector that will use both colors(I'm not 100% on how to do this).
2) Have the lights and normals remain a static color regardless of shading/light.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#ifdef MAC
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
//Camera variables
int xangle = -270;
int yangle = 0;
//Control Modes (Rotate mode by default)
int mode = 0;
int lightmode = 0;
//Player Position (Y offset so it would not be straddling the grid)
float cubeX = 0;
float cubeY = 0.5;
float cubeZ = 0;
//Vertex arrays for surface
float surfaceX [12][12];
float surfaceY [12][12];
float surfaceZ [12][12];
//Surface Normal arrays
float Nx[11][11];
float Ny[11][11];
float Nz[11][11];
//Color arrays
float R[11][11];
float G[11][11];
float B[11][11];
//Material properties
float Ka = 0.2;
float Kd = 0.4;
float Ks = 0.4;
float Kp = 0.5;
//Light position and color variables
float Light1x = 0;
float Light1y = 5;
float Light1z = 0;
float Light1r = 1;
float Light1g = 0;
float Light1b = 0;
float Light2x = -5;
float Light2y = 5;
float Light2z = -5;
float Light2r = 0;
float Light2g = 1;
float Light2b = 0;
//Random number generator
float RandomNumber(float Min, float Max)
{
return ((float(rand()) / float(RAND_MAX)) * (Max - Min)) + Min;
}
//---------------------------------------
// Initialize material properties
//---------------------------------------
void init_material(float Ka, float Kd, float Ks, float Kp,
float Mr, float Mg, float Mb)
{
// Material variables
float ambient[] = { Ka * Mr, Ka * Mg, Ka * Mb, 1.0 };
float diffuse[] = { Kd * Mr, Kd * Mg, Kd * Mb, 1.0 };
float specular[] = { Ks * Mr, Ks * Mg, Ks * Mb, 1.0 };
// Initialize material
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, Kp);
}
//---------------------------------------
// Initialize light source
//---------------------------------------
void init_light(int light_source, float Lx, float Ly, float Lz,
float Lr, float Lg, float Lb)
{
// Light variables
float light_position[] = { Lx, Ly, Lz, 0.0 };
float light_color[] = { Lr, Lg, Lb, 1.0 };
// Initialize light source
glEnable(GL_LIGHTING);
glEnable(light_source);
glLightfv(light_source, GL_POSITION, light_position);
glLightfv(light_source, GL_AMBIENT, light_color);
glLightfv(light_source, GL_DIFFUSE, light_color);
glLightfv(light_source, GL_SPECULAR, light_color);
glLightf(light_source, GL_CONSTANT_ATTENUATION, 1.0);
glLightf(light_source, GL_LINEAR_ATTENUATION, 0.0);
glLightf(light_source, GL_QUADRATIC_ATTENUATION, 0.0);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
}
//---------------------------------------
// Initialize surface
//---------------------------------------
void init_surface()
{
//Initialize X, select column
for (int i = 0; i < 12; i++)
{
//Select row
//Surface is +1 so the far right normal will be generated correctly
for (int j = 0; j < 12; j++)
{
//-5 to compensate for negative coordinate values
surfaceX[i][j] = i-5;
//Generate random surface height
surfaceY[i][j] = RandomNumber(5, 7) - 5;
//surfaceY[i][j] = 0;
surfaceZ[i][j] = j-5;
}
}
}
void define_normals()
{
//Define surface normals
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
//Get two tangent vectors
float Ix = surfaceX[i+1][j] - surfaceX[i][j];
float Iy = surfaceY[i+1][j] - surfaceY[i][j];
float Iz = surfaceZ[i+1][j] - surfaceZ[i][j];
float Jx = surfaceX[i][j+1] - surfaceX[i][j];
float Jy = surfaceY[i][j+1] - surfaceY[i][j];
float Jz = surfaceZ[i][j+1] - surfaceZ[i][j];
//Do cross product, inverted for upward normals
Nx[i][j] = - Iy * Jz + Iz * Jy;
Ny[i][j] = - Iz * Jx + Ix * Jz;
Nz[i][j] = - Ix * Jy + Iy * Jx;
//Original vectors
//Nx[i][j] = Iy * Jz - Iz * Jy;
//Ny[i][j] = Iz * Jx - Ix * Jz;
//Nz[i][j] = Ix * Jy - Iy * Jx;
float length = sqrt(
Nx[i][j] * Nx[i][j] +
Ny[i][j] * Ny[i][j] +
Nz[i][j] * Nz[i][j]);
if (length > 0)
{
Nx[i][j] /= length;
Ny[j][j] /= length;
Nz[i][j] /= length;
}
}
}
}
void calc_color()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
//Calculate light vector
//Light position, hardcoded for now 0,1,1
float Lx = Light1x - surfaceX[i][j];
float Ly = Light1y - surfaceY[i][j];
float Lz = Light1z - surfaceZ[i][j];
//std::cout << "Lx: " << Lx << std::endl;
//std::cout << "Ly: " << Ly << std::endl;
//std::cout << "Lz: " << Lz << std::endl;
//Grab surface normals
//These are Nx,Ny,Nz due to compiler issues
float Na = Nx[i][j];
float Nb = Ny[i][j];
float Nc = Nz[i][j];
//std::cout << "Na: " << Na << std::endl;
//std::cout << "Nb: " << Nb << std::endl;
//std::cout << "Nc: " << Nc << std::endl;
//Do cross product
float Color = (Na * Lx) + (Nb * Ly) + (Nc * Lz);
//std::cout << "Color: " << Color << std::endl;
R[i][j] = Color;
G[i][j] = Color;
B[i][j] = Color;
}
}
}
//---------------------------------------
// Init function for OpenGL
//---------------------------------------
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Viewing Window Modified
glOrtho(-7.0, 7.0, -7.0, 7.0, -7.0, 7.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Rotates camera
//glRotatef(30.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
//Project 3 code
init_surface();
define_normals();
//Shading code
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
//X,Y,Z - R,G,B
init_light(GL_LIGHT1, Light1x, Light1y, Light1z, Light1r, Light1g, Light1b);
//init_light(GL_LIGHT2, Light2x, Light2y, Light2z, Light2r, Light2g, Light2b);
//init_light(GL_LIGHT2, 0, 1, 0, 0.5, 0.5, 0.5);
}
void keyboard(unsigned char key, int x, int y)
{
///TODO: allow user to change color of light
//Controls
//Toggle Mode
if (key == 'q')
{
if(mode == 0)
{
mode = 1;
std::cout << "Switched to Light mode (" << mode << ")" << std::endl;
}
else if(mode == 1)
{
mode = 0;
std::cout << "Switched to Rotate mode (" << mode << ")" << std::endl;
}
}
//Toggle light control
else if (key == 'e')
{
if(lightmode == 0)
{
lightmode = 1;
std::cout << "Switched to controlling light 2 (" << lightmode << ")" << std::endl;
}
else if(lightmode == 1)
{
lightmode = 0;
std::cout << "Switched to controlling light 1 (" << lightmode << ")" << std::endl;
}
}
////Rotate Camera (mode 0)
//Up & Down
else if (key == 's' && mode == 0)
xangle += 5;
else if (key == 'w' && mode == 0)
xangle -= 5;
//Left & Right
else if (key == 'a' && mode == 0)
yangle -= 5;
else if (key == 'd' && mode == 0)
yangle += 5;
////Move Light (mode 1)
//Forward & Back
else if (key == 'w' && mode == 1)
{
if (lightmode == 0)
{
Light1z = Light1z - 1;
//init_surface();
//define_normals();
calc_color();
glutPostRedisplay();
}
else if (lightmode == 1)
Light2z = Light2z - 1;
//init_surface();
}
else if (key == 's' && mode == 1)
{
if (lightmode == 0)
Light1z = Light1z + 1;
else if (lightmode == 1)
Light2z = Light2z + 1;
}
//Strafe
else if (key == 'd' && mode == 1)
{
if (lightmode == 0)
Light1x = Light1x + 1;
else if (lightmode == 1)
Light2x = Light2x + 1;
}
else if (key == 'a' && mode == 1)
{
if (lightmode == 0)
Light1x = Light1x - 1;
else if (lightmode == 1)
Light2x = Light2x - 1;
}
//Up & Down (Cube offset by +0.5 in Y)
else if (key == 'z' && mode == 1)
{
if (lightmode == 0)
Light1y = Light1y + 1;
else if (lightmode == 1)
Light2y = Light2y + 1;
}
else if (key == 'x' && mode == 1)
{
if (lightmode == 0)
Light1y = Light1y - 1;
else if (lightmode == 1)
Light2y = Light2y - 1;
}
//Redraw objects
glutPostRedisplay();
}
//---------------------------------------
// Display callback for OpenGL
//---------------------------------------
void display()
{
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Rotation Code
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(xangle, 1.0, 0.0, 0.0);
glRotatef(yangle, 0.0, 1.0, 0.0);
//Light Code
init_material(Ka, Kd, Ks, 100 * Kp, 0.8, 0.6, 0.4);
//Color Code
calc_color();
//Draw the squares, select column
for (int i = 0; i <= 9; i++)
{
//Select row
for (int j = 0; j <= 9; j++)
{
glBegin(GL_POLYGON);
//Surface starts at top left
//Counter clockwise
glColor3f(R[i][j], G[i][j], B[i][j]);
glNormal3f(Nx[i][j], Ny[i][j], Nz[i][j]);
glVertex3f(surfaceX[i][j], surfaceY[i][j], surfaceZ[i][j]);
glColor3f(R[i][j+1], G[i][j+1], B[i][j+1]);
glNormal3f(Nx[i][j+1], Ny[i][j+1], Nz[i][j+1]);
glVertex3f(surfaceX[i][j+1], surfaceY[i][j+1], surfaceZ[i][j+1]);
glColor3f(R[i+1][j+1], G[i+1][j+1], B[i+1][j+1]);
glNormal3f(Nx[i+1][j+1], Ny[i+1][j+1], Nz[i+1][j+1]);
glVertex3f(surfaceX[i+1][j+1], surfaceY[i+1][j+1], surfaceZ[i+1][j+1]);
glColor3f(R[i+1][j], G[i+1][j], B[i+1][j]);
glNormal3f(Nx[i+1][j], Ny[i+1][j], Nz[i+1][j]);
glVertex3f(surfaceX[i+1][j], surfaceY[i+1][j], surfaceZ[i+1][j]);
glEnd();
}
}
//Draw the normals
for (int i = 0; i <= 10; i++)
{
for (int j = 0; j <= 10; j++)
{
glBegin(GL_LINES);
//glColor3f(0.0, 1.0, 1.0);
float length = 1;
glVertex3f(surfaceX[i][j], surfaceY[i][j], surfaceZ[i][j]);
glVertex3f(surfaceX[i][j]+length*Nx[i][j],
surfaceY[i][j]+length*Ny[i][j],
surfaceZ[i][j]+length*Nz[i][j]);
glEnd();
}
}
//Marking location of lights
glPointSize(10);
glBegin(GL_POINTS);
glColor3f(Light1r, Light1g, Light1b);
glVertex3f(Light1x, Light1y, Light1z);
glEnd();
glPointSize(10);
glBegin(GL_POINTS);
glColor3f(Light2r, Light2g, Light2b);
glVertex3f(Light2x, Light2y, Light2z);
glEnd();
//+Z = Moving TOWARD camera in opengl
//Origin point for reference
glPointSize(10);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(0, 0, 0);
glEnd();
//Assign Color of Lines
float R = 1;
float G = 1;
float B = 1;
glBegin(GL_LINES);
glColor3f(R, G, B);
////Drawing the grid
//Vertical lines
for (int i = 0; i < 11; i++)
{
int b = -5 + i;
glVertex3f(b, 0, -5);
glVertex3f(b, 0, 5);
}
//Horizontal lines
for (int i = 0; i < 11; i++)
{
int b = -5 + i;
glVertex3f(-5,0,b);
glVertex3f(5,0,b);
}
glEnd();
glFlush();
}
//---------------------------------------
// Main program
//---------------------------------------
int main(int argc, char *argv[])
{
srand(time(NULL));
//Print Instructions
std::cout << "Project 3 Controls: " << std::endl;
std::cout << "q switches control mode" << std::endl;
std::cout << "w,a,s,d for camera rotation" << std::endl;
//Required
glutInit(&argc, argv);
//Window will default to a different size without
glutInitWindowSize(500, 500);
//Window will default to a different position without
glutInitWindowPosition(250, 250);
//
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
//Required
glutCreateWindow("Project 3");
//Required, calls display function
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
//Required
init();
glutMainLoop();
return 0;
}
1) Have the surface update when the light is moved
You missed to update the position of the light, when the light was moved. Set the light position at the begin of the function display.
Note, when the light position is set by glLightfv(GL_LIGHT0, GL_POSITION, pos), then
pos is multiplied by the current model view matrix.
So the light has to be set after the model view matrix was "cleared" by glLoadIdentity:
void display()
{
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Rotation Code
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// init light
init_light(GL_LIGHT1, Light1x, Light1y, Light1z, Light1r, Light1g, Light1b);
init_light(GL_LIGHT2, Light2x, Light2y, Light2z, Light2r, Light2g, Light2b);
glRotatef(xangle, 1.0, 0.0, 0.0);
glRotatef(yangle, 0.0, 1.0, 0.0);
// [...]
}
2) Have the lights and normals remain a static color regardless of shading/light.
Enable lighting before drawing the surface, but disable lighting before drawing the lines and points:
void display()
{
// [...]
// switch on lighting
glEnable(GL_LIGHTING);
//Draw the squares, select column
for (int i = 0; i <= 9; i++)
{
// [...]
}
// switch off lighting
glDisable(GL_LIGHTING);
//Draw the normals
for (int i = 0; i <= 10; i++)
{
// [...]
}
// [...]
}
I am trying to draw a Bezier curve in OpenGL using floating point values. I have tried using many different code examples. My current code below, runs ,but does not show the curve on screen. The usual way to draw Bezier curves are with integer values, which means using the GLUORTHO2D() function for drawing the curve. But I want to draw a curve using floating point values. Such as x range(-1,1) and y range(-1,1).
like if x=(500) then consider it (-1 to 1) and if y=(800) then consider it (-1,1).
I have already tried using integer values and it worked for me. my code using integer values is below:
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#define CTRL_COUNT 100
int ctrlPointsCount;
int ctrlPointsX[CTRL_COUNT], ctrlPointsY[CTRL_COUNT];
int X1[3]={20,25,20}, Y1[3]={5,24,38}; //first point(x1[0],y1[0]) second(x1[1],y1[1]) third(x1[2],y1[2])
void myInit()
{
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,0.0,0.0);
glPointSize(8.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,128.0,0.0,96.0);
}
//p(t)=(1-t)^3*p0+3t(1-t)^2*p1+3t^2(1-t)p2+t^3p3
float getNextBezierPointX(float t)
{
float x=0.0;
for(int i=0; i<ctrlPointsCount; i++)
{
int c;
if(i==0 || i==ctrlPointsCount-1)
c = 1;
else
{
c = ctrlPointsCount-1;
}
x += c * pow(t, i) * pow(1-t, ctrlPointsCount-1-i) * ctrlPointsX[i];
}
return x;
}
float getNextBezierPointY(float t)
{
float y=0.0;
for(int i=0; i<ctrlPointsCount; i++)
{
int c;
if(i==0 || i==ctrlPointsCount-1)
c = 1;
else
{
c = ctrlPointsCount-1;
}
y += c * pow(t, i) * pow(1-t, ctrlPointsCount-1-i) * ctrlPointsY[i];
}
return y;
}
void drawline()
{
// draw control points using red color
for(int i=0; i < 3; i++)
{
glBegin(GL_POINTS);
glVertex2i(ctrlPointsX[i], ctrlPointsY[i]);
glEnd();
glFlush();
}
// draw bezier curve using control poitns by calculating next points using cubic bezier curve formula
float oldX=ctrlPointsX[0], oldY=ctrlPointsY[0];
for(double t = 0.0;t <= 1.0; t += 0.01) {
float x = getNextBezierPointX(t);
float y = getNextBezierPointY(t);
//glColor3f(1.0,t,1.0);
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2f(oldX, oldY);
glVertex2f(x, y);
glEnd();
glFlush();
oldX = x;
oldY = y;
}
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
ctrlPointsCount=3;
for(int i=0;i<3;i++)
{
ctrlPointsX[i] = X1[i];
ctrlPointsY[i] = Y1[i];
}
drawline();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Bezier Curve");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
But when i tried using floating point values , it does not work for me. It does not show the curved line on screen. My code using floating point values is below:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
using namespace std;
#define CTRL_COUNT 100
int ctrlPointsCount;
int ctrlPointsX[CTRL_COUNT], ctrlPointsY[CTRL_COUNT];
double X1[3] = { 0.26015037593985, 0.43609022556391, 0.6 }, Y1[3] = { 0.946875, 0.884375, 0.946875 };
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
float getNextBezierPointX(float t)
{
float x = 0.0;
for (int i = 0; i<ctrlPointsCount; i++)
{
int c;
if (i == 0 || i == ctrlPointsCount - 1)
c = 1;
else
{
c = ctrlPointsCount - 1;
}
x += c * pow(t, i) * pow(1 - t, ctrlPointsCount - 1 - i) * ctrlPointsX[i];
}
return x;
}
float getNextBezierPointY(float t)
{
float y = 0.0;
for (int i = 0; i<ctrlPointsCount; i++)
{
int c;
if (i == 0 || i == ctrlPointsCount - 1)
c = 1;
else
{
c = ctrlPointsCount - 1;
}
y += c * pow(t, i) * pow(1 - t, ctrlPointsCount - 1 - i) * ctrlPointsY[i];
}
return y;
}
void drawline()
{
// draw control points using red color
for (int i = 0; i < 3; i++)
{
glBegin(GL_POINTS);
glVertex2i(ctrlPointsX[i], ctrlPointsY[i]);
glEnd();
glFlush();
}
// draw bezier curve using control poitns by calculating next points using cubic bezier curve formula
float oldX = ctrlPointsX[0], oldY = ctrlPointsY[0];
for (double t = 0.0; t <= 1.0; t += 0.01)
{
float x = getNextBezierPointX(t);
float y = getNextBezierPointY(t);
//glColor3f(1.0,t,1.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2f(oldX, oldY);
glVertex2f(x, y);
glEnd();
glFlush();
oldX = x;
oldY = y;
}
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = 0.0;
float _cameraAngle = 0.0;
float _ang_tri = 0.0;
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //Reset the drawing perspective
ctrlPointsCount = 3;
for (int i = 0; i<3; i++)
{
ctrlPointsX[i] = X1[i];
ctrlPointsY[i] = Y1[i];
}
drawline();
glutSwapBuffers();
}
void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 360;
}
_ang_tri += 2.0f;
if (_ang_tri > 360) {
_ang_tri -= 360;
}
glutPostRedisplay(); //Tell GLUT that the display has changed
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1331, 641);
glutInitWindowPosition(0, 0);
//Create the window
glutCreateWindow("Our cg project");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0); //Add a timer
glClearColor(0.0, 0.7, 1.5,0.0);
glutMainLoop();
return 0;
}
The problem is this here:
int ctrlPointsX[CTRL_COUNT], ctrlPointsY[CTRL_COUNT];
double X1[3] = { 0.26015037593985, 0.43609022556391, 0.6 }, Y1[3] = {0.946875, 0.884375, 0.946875 };
for (int i = 0; i<3; i++)
{
ctrlPointsX[i] = X1[i];
ctrlPointsY[i] = Y1[i];
}
ctrlPointsX and ctrlPointsYcan only hold integer values. So when you do ctrlPointsX[i] = X1[i] and ctrlPointsY[i] = Y1[i] you are converting the floats to integers, which will round them down. So all your controlPoints will be 0.
You have to declare the controlPoints arrays as type double too:
double ctrlPointsX[CTRL_COUNT], ctrlPointsY[CTRL_COUNT];
double X1[3] = { 0.26015037593985, 0.43609022556391, 0.6 }, Y1[3] = {0.946875, 0.884375, 0.946875 };
This should fix your problem.
I'm having trouble getting my yoyo program to work; I have a growing and shrinking string to go along with the yoyo moving and spinning, but I've got the string moving along with the yoyo. I need to have the string staying stationary while the yoyo spins up and down.I have to have the string and yoyo's movements allocated onto one button as well. Mind helping me with my code? (also please excuse the lackluster yoyo; I wanted to finish the programming before designing it)
#include "stdafx.h"
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
void firstDisplay();
void secondDisplay();
void movement();
void init();
void drawButton();
void handleButton(int button, int state, int x, int y);
const int Width=720;
const int Height = 480;
static float PI = 3.14159;
static float radius = 25;
static float INC= (PI/30);
static GLfloat spin = 0.0;
static GLfloat deltaz = .001;
static GLfloat deltax = 0.0;
static GLfloat deltay = 0.0;
//######################################################### Main #########################################################
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE | GLUT_DEPTH);
glutInitWindowSize(Width,Height);
glutInitWindowPosition(0,0);
glutCreateWindow("Yoyo");
glutDisplayFunc(firstDisplay);
glutMouseFunc(handleButton);
init();
glutMainLoop();
return;
}
//######################################################### Draw Button #########################################################
void drawButton()
{
glBegin(GL_POLYGON);
float theta;
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,0,1);
glVertex2f(40 + radius * cos(theta), 40 + radius*sin(theta));
}
glEnd();
}
//######################################################### Second Display #########################################################
void secondDisplay()
{
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,1,1);
float radius1 = 75;
glVertex2f(360 + radius1 * cos(theta), 80 + radius1*sin(theta));
}
glEnd();
glBegin(GL_POLYGON); //Here is the yoyo design
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(0,1,1);
float radius1 = 55;
glVertex2f(350 + radius1 * cos(theta), 90 + radius1*sin(theta));
}
glEnd();
glFlush();
return;
}
//######################################################### First Display #########################################################
void firstDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
float theta;
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,1,1);
float radius1 = 75;
glVertex2f(360 + radius1 * cos(theta), 80 + radius1*sin(theta));
}
glEnd();
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(0,1,1);
float radius1 = 55;
glVertex2f(350 + radius1 * cos(theta), 90 + radius1*sin(theta));
}
glEnd();
drawButton();
glutSwapBuffers();
glFlush();
return;
}
//######################################################### Draw String #########################################################
void draw_string()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(363, 8.25 - deltay, 0.0);
glVertex3f(357, 8.25 - deltay, 0);
glVertex3f(357, 6, 0);
glVertex3f(363, 6, 0);
glEnd();
}
//######################################################### Button Controls #########################################################
void handleButton(int button, int state, int x, int y)
{
static int index =-1;
if(button == GLUT_LEFT_BUTTON)
{
if(y>= 10 && y<= 100)
{
if (state == GLUT_DOWN)
{
glutIdleFunc(movement);
}
if (state == GLUT_UP)
{
glutIdleFunc(NULL);
}
}
glutSwapBuffers();
}
}
//######################################################### Yoyo Movement #########################################################
void movement()
{
static int goingup = 0;
glClear(GL_COLOR_BUFFER_BIT);
if(goingup==1)
{
deltay -= 6;
if(deltay <= 0)
{
goingup = 0;
}
spin = spin -5;
if(spin < 360)
{
spin = spin + 360;
}
}
if(goingup == 0)
{
deltay += 6;
if (deltay >= 315)
{
goingup = 1;
}
spin = spin +5;
if(spin < 360)
{
spin = spin + 360;
}
}
glPushMatrix();
glTranslatef(360+deltax, 80+deltay, 0.0);
glRotatef(spin,0.0,0.0,1.0);
glTranslatef(-360,-80, 0.0);
secondDisplay();
draw_string();
glPopMatrix();
drawButton();
glutSwapBuffers();
}
//######################################################### Init #########################################################
void init()
{
glClearColor(0, 0, 0, 0.0);
glColor3f(1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,Width,Height,0);
return;
}
I solved my problem. I went separated the draw_string() and seconddisplay() functions within the movement function like so:
}
glPushMatrix();
glTranslatef(360+deltax, 80+deltay, 0.0);
glRotatef(spin,0.0,0.0,1.0);
glTranslatef(-360,-80, 0.0);
secondDisplay();
glPopMatrix();
glPushMatrix();
glTranslatef(360+deltax, 80+deltay, 0.0);
glRotatef(0,0.0,0.0,1.0);
glTranslatef(-360,-80, 0.0);
draw_string();
glPopMatrix();
drawButton();
glutSwapBuffers();
}
I removed the spin from draw_string()'s glRotatef.
I've written a simple brick breaker game in OpenGL. The ball is a 2D circle drawn using :
for (float angle = 0; angle < (10); angle+=0.01)
{
glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS));
}
This causes distortion when the game is changed to fullscreen. RADIUS is defined as 0.025 .
I need help in making the paddle movements smooth as well.
Also, if you play the game a couple of times, you'll notice that towards the extreme left, when the ball hits the paddle, it rises to a certain height and then bounces back down.
Full code:
#include <GL/openglut.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#define RADIUS 0.025
#define SPEED 0.001
int WIDTH = 900;
int HEIGHT = 650;
int RATIO = WIDTH/HEIGHT;
bool show[5][10];
float x_brick[4][9];
float y_brick[4][9];
float P_XPOS = 0;
float P_YPOS = -0.8;
bool phit_center = false , phit_corner = false;
bool game_over = false;
bool RIGHT = 1,LEFT = 0,UP = 1,DOWN = 0;
bool started = false;
float x_pos = 0,y_pos = -0.75;
bool hit = false;
int lives = 3;
using namespace std;
void b_draw()
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
for(int a = 0; a < 9; a++)
{
for(int b = 0; b < 4; b++)
{
if(show[b][a] == 1)
{
glVertex2f(x_brick[b][a],y_brick[b][a]);
glVertex2f(x_brick[b][a],y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a]+0.2,y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a]+0.2,y_brick[b][a]);
}
}
}
glEnd();
}
void c_draw()
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x_pos,y_pos);
for (float angle = 0; angle < (10); angle+=0.01)
{
glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS));
}
glEnd();
}
bool b_hit()
{
hit = false;
int flag = 1;
for(int a = 0; a < 10; a++)
{
for(int b =0; b < 4; b++)
{
if(x_pos >= x_brick[b][a] && x_pos <= x_brick[b][a] + 0.2)
{
if(y_pos <= y_brick[b][a] && y_pos >= y_brick[b][a] - 0.1)
{
if(show[b][a] == 1)
{
show[b][a] = 0;
flag = 0;
hit = true;
break;
}
}
}
}
if(flag == 0)
break;
}
return hit;
}
bool crashed()
{
if(y_pos < P_YPOS - 0.05)
return true;
else return false;;
}
void p_hit()
{
phit_corner = false;
phit_center = false;
if(x_pos <= P_XPOS + 0.13 && x_pos >= P_XPOS - 0.13)
{
if(y_pos <= P_YPOS)
{
phit_center = true;
}
}
else if((x_pos >= P_XPOS + 0.13 && x_pos <= P_XPOS + 0.2) || (x_pos <= P_XPOS - 0.13 && x_pos >= P_XPOS - 0.2))
{
if(y_pos <= P_YPOS)
{
phit_corner = true;
}
}
}
void c_move()
{
if(UP && RIGHT)
{
x_pos += (SPEED);
y_pos += (SPEED);
}
if(UP && LEFT)
{
x_pos -= (SPEED);
y_pos += (SPEED);
}
if(DOWN && RIGHT)
{
x_pos += (SPEED);
y_pos -= (SPEED);
}
if(DOWN && LEFT)
{
x_pos -= (SPEED);
y_pos -= (SPEED);
}
b_hit();
if(x_pos >= (RATIO-RADIUS))
{
RIGHT = 0;
LEFT = 1;
}
else if(x_pos <= (-RATIO+RADIUS))
{
RIGHT = 1;
LEFT = 0;
}
if(y_pos >= (RATIO-RADIUS) || hit )
{
UP = 0;
DOWN = 1;
}
else if(y_pos <= (-RATIO+RADIUS) || hit )
{
UP = 1;
DOWN = 0;
}
p_hit();
if(phit_center)
{
DOWN = 0;
UP = 1;
}
if(phit_corner)
{
if(LEFT)
{
LEFT = 0;
RIGHT = 1;
}
else
{
RIGHT = 0;
LEFT = 1;
}
UP = 1;
DOWN = 0;
}
}
void p_draw()
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(P_XPOS-0.2,P_YPOS);
glVertex2f(P_XPOS+0.2,P_YPOS);
glVertex2f(P_XPOS+0.2,P_YPOS-0.05);
glVertex2f(P_XPOS-0.2,P_YPOS-0.05);
glEnd();
}
void BallLoop()
{
glClearColor(1.0,1.0,1.0,0);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
c_draw();
b_draw();
p_draw();
glFlush();
if(started)
c_move();
if(crashed())
{
x_pos = 0;
y_pos = -0.7;
started = 0;
UP = 1;
RIGHT = 1;
DOWN = 0;
LEFT = 0;
}
glutSwapBuffers();
glutPostRedisplay();
}
void user_input(unsigned char key, int x, int y)
{
if(key == 13)
started = true;
}
void ArrowKeys(int key, int x, int y)
{
if(key==GLUT_KEY_LEFT && P_XPOS >= -0.8)
for(float a = 0; a < 0.05; a+= 0.001)
{
P_XPOS -=0.003;
BallLoop();
}
if(key==GLUT_KEY_RIGHT && P_XPOS <= 0.8)
{
for(float a = 0; a < 0.05; a+= 0.001)
{
P_XPOS +=0.003;
BallLoop();
}
}
}
void set_xy()
{
for(int a = 0; a < 5; a++)
{
for(int b = 0; b < 10; b++)
{
show[a][b] = 1;
}
}
int c = 0;
for(float a = -0.94; c <= 8; a+=0.21)
{
for(int b = 0; b <= 5; b++)
{
x_brick[b][c] = a;
}
c++;
}
int d = 0;
for(float s = 0.99; d <= 3; s-=0.11)
{
for(int r = 0; r < 9; r++)
{
y_brick[d][r] = s;
}
d++;
}
}
void changeSize(int w, int h)
{
if(h == 0)
h = 1;
RATIO = w/h;
float ratio = 1.0* w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
BallLoop();
}
int main(int argc, char **argv)
{
set_xy();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(WIDTH,HEIGHT);
glutCreateWindow("Brick Breaker - By Viraj");
glutReshapeFunc(changeSize);
glutDisplayFunc(BallLoop);
glutKeyboardFunc(user_input);
glutSpecialFunc(ArrowKeys);
glutMainLoop();
return 0;
}
A lot of people run into this kind of issue, because they base their code on badly written tutorials. One key mistake is to do the projection matrix setup in the reshape handler. In any serious OpenGL application, games including, you will switch the projection several times during rendering – for HUDs, minimaps, GUI elements etc.
Instead you set projection in the display handler, right before you need that projection. Also you missed to set the projection matrix at all, you only set the viewport - close, but not enough. I'd not call the display handler Ball_Loop, but well, here's how I'd modify it:
void BallLoop()
{
const int win_width = glutGet(GLUT_WINDOW_WIDTH);
const int win_height = glutGet(GLUT_WINDOW_HEIGHT);
const float win_aspect = (float)win_width/(float)win_height;
glClearColor(1.0,1.0,1.0,0);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, win_width, win_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-aspect, aspect, -1, 1, -1, 1); /* those ortho limits should match your game logic */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
c_draw();
b_draw();
p_draw();
glFlush();
if(started)
c_move();
if(crashed())
{
x_pos = 0;
y_pos = -0.7;
started = 0;
UP = 1;
RIGHT = 1;
DOWN = 0;
LEFT = 0;
}
glutSwapBuffers();
glutPostRedisplay();
}
EDIT fully fixed and playable brickbreaker.cc source code
#include <GL/glut.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
#define RADIUS 0.025
#define RATIO (4./3.)
bool show[5][10];
float x_brick[4][9];
float y_brick[4][9];
float paddle_x = 0;
float paddle_y = -0.8;
float paddle_speed = 0;
const float PaddleSpeedFactor = 3.;
bool phit_center = false , phit_corner = false;
bool game_over = false;
float speed_x = 0.;
float speed_y = 0.;
float x_pos = 0,y_pos = -0.75;
int lives = 3;
float T_last_frame = 0.;
void draw_bricks()
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
for(int a = 0; a < 9; a++)
{
for(int b = 0; b < 4; b++)
{
if(show[b][a] == 1)
{
glVertex2f(x_brick[b][a],y_brick[b][a]);
glVertex2f(x_brick[b][a],y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a]+0.2,y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a]+0.2,y_brick[b][a]);
}
}
}
glEnd();
}
void ball_draw()
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x_pos,y_pos);
for (float angle = 0; angle < (10); angle+=0.01)
{
glVertex2f((x_pos + sin(angle) * RADIUS), (y_pos + (cos(angle)) * RADIUS));
}
glEnd();
}
bool brick_hit()
{
bool hit = false;
int flag = 1;
for(int a = 0; a < 10; a++)
{
for(int b =0; b < 4; b++)
{
if(x_pos >= x_brick[b][a] && x_pos <= x_brick[b][a] + 0.2)
{
if(y_pos <= y_brick[b][a] && y_pos >= y_brick[b][a] - 0.1)
{
if(show[b][a] == 1)
{
show[b][a] = 0;
flag = 0;
hit = true;
break;
}
}
}
}
if(flag == 0)
break;
}
return hit;
}
bool crashed()
{
if(y_pos < paddle_y - 0.05)
return true;
return false;
}
void paddle_hit()
{
phit_corner = false;
phit_center = false;
if(x_pos <= paddle_x + 0.13 && x_pos >= paddle_x - 0.13)
{
if(y_pos <= paddle_y)
{
phit_center = true;
}
}
else if( (x_pos >= paddle_x + 0.13 && x_pos <= paddle_x + 0.2) ||
(x_pos <= paddle_x - 0.13 && x_pos >= paddle_x - 0.2))
{
if(y_pos <= paddle_y)
{
phit_corner = true;
}
}
}
void paddle_move(float dT)
{
if(paddle_x < RATIO && paddle_x > -RATIO)
paddle_x += paddle_speed * PaddleSpeedFactor * dT;
if( paddle_x > 0.95) {
paddle_x = 0.95;
paddle_speed = 0.;
}
if( paddle_x < -0.95) {
paddle_x = -0.95;
paddle_speed = 0.;
}
paddle_speed *= (1. - 0.05);
if( fabs(paddle_speed) < 0.01 )
paddle_speed = 0.;
}
void ball_move(float dT)
{
x_pos += speed_x * dT;
y_pos += speed_y * dT;
if( brick_hit() ) {
speed_y *= -1;
}
if( x_pos >= (RATIO-RADIUS) || x_pos <= (-RATIO+RADIUS ) )
{
speed_x *= -1;
}
if( y_pos >= (1.-RADIUS) )
{
speed_y = -1;
}
paddle_hit();
if(phit_center)
{
speed_y = 1;
}
if(phit_corner)
{
speed_x *= -1;
speed_y = 1;
}
}
void paddle_draw()
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(paddle_x - 0.2, paddle_y);
glVertex2f(paddle_x + 0.2, paddle_y);
glVertex2f(paddle_x + 0.2, paddle_y - 0.05);
glVertex2f(paddle_x - 0.2, paddle_y - 0.05);
glEnd();
}
void step_game()
{
paddle_move(T_last_frame);
ball_move(T_last_frame);
if(crashed())
{
speed_x = 0;
speed_y = 0;
x_pos = 0;
y_pos = -0.7;
paddle_speed = 0;
paddle_x = 0;
}
glutPostRedisplay();
}
void launch_ball()
{
speed_y = 1.;
speed_x = 1.;
}
void user_input(unsigned char key, int x, int y)
{
if(key == 13)
launch_ball();
}
void ArrowKeys(int key, int x, int y)
{
if(key==GLUT_KEY_LEFT)
paddle_speed = -1.;
if(key==GLUT_KEY_RIGHT)
paddle_speed = +1.;
}
void set_xy()
{
for(int a = 0; a < 5; a++)
{
for(int b = 0; b < 10; b++)
{
show[a][b] = 1;
}
}
int c = 0;
for(float a = -0.94; c <= 8; a+=0.21)
{
for(int b = 0; b <= 5; b++)
{
x_brick[b][c] = a;
}
c++;
}
int d = 0;
for(float s = 0.99; d <= 3; s-=0.11)
{
for(int r = 0; r < 9; r++)
{
y_brick[d][r] = s;
}
d++;
}
}
void display()
{
const int win_width = glutGet(GLUT_WINDOW_WIDTH);
const int win_height = glutGet(GLUT_WINDOW_HEIGHT);
const float win_aspect = (float)win_width / (float)win_height;
glViewport(0, 0, win_width, win_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(win_aspect > RATIO) {
glOrtho(-win_aspect, win_aspect, -1., 1., -1., 1.);
} else {
glOrtho(-RATIO, RATIO, -RATIO/win_aspect, RATIO/win_aspect, -1., 1.);
}
glMatrixMode(GL_MODELVIEW);
glClearColor(0., 0., 1., 1.);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1,1,1);
glVertex2f(-RATIO, -1);
glVertex2f(RATIO, -1);
glVertex2f(RATIO, 1);
glVertex2f(-RATIO, 1);
glEnd();
draw_bricks();
paddle_draw();
ball_draw();
glutSwapBuffers();
// GLUT doesn't offer cross plattform timing
// assume 60Hz refresh rate
T_last_frame = 1./60.;
}
int main(int argc, char **argv)
{
set_xy();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(800, 600);
glutCreateWindow("Brick Breaker - By Viraj");
glutDisplayFunc(display);
glutKeyboardFunc(user_input);
glutSpecialFunc(ArrowKeys);
glutIdleFunc(step_game);
glutMainLoop();
return 0;
}
EDIT 2 and just for the sake of completeness, here's a GLFW version
#include <GL/glfw.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define RADIUS 0.025
#define RATIO (4./3.)
bool show[5][10];
float x_brick[4][9];
float y_brick[4][9];
const float SpeedFactor = 10.;
float paddle_x = 0;
float paddle_y = -0.8;
float paddle_speed = 0;
const float PaddleSpeedFactor = 3.;
bool phit_center = false, phit_corner = false;
bool game_over = false;
float speed_x = 0.;
float speed_y = 0.;
float x_pos;
float y_pos;
int lifes = 0;
void draw_bricks()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
for (int a = 0; a < 9; a++) {
for (int b = 0; b < 4; b++) {
if (show[b][a] == 1) {
glVertex2f(x_brick[b][a], y_brick[b][a]);
glVertex2f(x_brick[b][a], y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a] + 0.2,
y_brick[b][a] - 0.10);
glVertex2f(x_brick[b][a] + 0.2, y_brick[b][a]);
}
}
}
glEnd();
}
void ball_draw()
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x_pos, y_pos);
for (float angle = 0; angle < (10); angle += 0.01) {
glVertex2f((x_pos + sin(angle) * RADIUS),
(y_pos + (cos(angle)) * RADIUS));
}
glEnd();
}
bool brick_hit()
{
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 4; b++) {
if (x_pos >= x_brick[b][a]
&& x_pos <= x_brick[b][a] + 0.2) {
if (y_pos <= y_brick[b][a]
&& y_pos >= y_brick[b][a] - 0.1) {
if (show[b][a] == 1) {
show[b][a] = 0;
return true;
}
}
}
}
}
return false;
}
bool crashed()
{
if (y_pos < paddle_y - 0.05)
return true;
return false;
}
void paddle_hit()
{
phit_corner = false;
phit_center = false;
if (x_pos <= paddle_x + 0.13 && x_pos >= paddle_x - 0.13) {
if (y_pos <= paddle_y) {
phit_center = true;
}
} else if ((x_pos >= paddle_x + 0.13 && x_pos <= paddle_x + 0.2) ||
(x_pos <= paddle_x - 0.13 && x_pos >= paddle_x - 0.2)) {
if (y_pos <= paddle_y) {
phit_corner = true;
}
}
}
void paddle_move(float dT)
{
if (paddle_x < RATIO && paddle_x > -RATIO)
paddle_x += paddle_speed * PaddleSpeedFactor * dT;
if (paddle_x > 1.) {
paddle_x = 1.;
paddle_speed = 0.;
}
if (paddle_x < -1.) {
paddle_x = -1.;
paddle_speed = 0.;
}
}
void ball_move(float dT)
{
x_pos += speed_x * dT;
y_pos += speed_y * dT;
if (brick_hit()) {
speed_y *= -1;
}
if (x_pos >= (RATIO - RADIUS) || x_pos <= (-RATIO + RADIUS)) {
speed_x *= -1;
}
if (y_pos >= (1. - RADIUS)) {
speed_y = -1;
}
paddle_hit();
if (phit_center) {
speed_y = 1;
}
if (phit_corner) {
speed_x *= -1;
speed_y = 1;
}
}
void paddle_draw()
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(paddle_x - 0.2, paddle_y);
glVertex2f(paddle_x + 0.2, paddle_y);
glVertex2f(paddle_x + 0.2, paddle_y - 0.05);
glVertex2f(paddle_x - 0.2, paddle_y - 0.05);
glEnd();
}
void reset_game()
{
lifes = 3;
speed_x = 0;
speed_y = 0;
x_pos = 0;
y_pos = -0.7;
paddle_speed = 0;
paddle_x = 0;
}
void step_game(float dT)
{
if(!lifes)
return;
paddle_move(dT * SpeedFactor);
ball_move(dT * SpeedFactor);
if (crashed()) {
lifes--;
speed_x = 0;
speed_y = 0;
x_pos = 0;
y_pos = -0.7;
}
}
void launch_ball()
{
if(!lifes)
return;
speed_y = 1.;
speed_x = 1.;
}
void keyboard(int key, int action)
{
switch(key)
{
case GLFW_KEY_ENTER:
launch_ball();
break;
case GLFW_KEY_ESC:
reset_game();
break;
case GLFW_KEY_LEFT:
switch(action) {
case GLFW_PRESS:
paddle_speed = -1.;
break;
case GLFW_RELEASE:
paddle_speed = 0;
break;
} break;
case GLFW_KEY_RIGHT:
switch(action) {
case GLFW_PRESS:
paddle_speed = 1.;
break;
case GLFW_RELEASE:
paddle_speed = 0;
break;
} break;
}
}
void set_xy()
{
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 10; b++) {
show[a][b] = 1;
}
}
int c = 0;
for (float a = -0.94; c <= 8; a += 0.21) {
for (int b = 0; b <= 5; b++) {
x_brick[b][c] = a;
}
c++;
}
int d = 0;
for (float s = 0.99; d <= 3; s -= 0.11) {
for (int r = 0; r < 9; r++) {
y_brick[d][r] = s;
}
d++;
}
}
float display()
{
int win_width;
int win_height;
glfwGetWindowSize(&win_width, &win_height);
const float win_aspect = (float)win_width / (float)win_height;
glfwSetTime(0.);
glViewport(0, 0, win_width, win_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (win_aspect > RATIO) {
glOrtho(-win_aspect, win_aspect, -1., 1., -1., 1.);
} else {
glOrtho(-RATIO, RATIO, -RATIO / win_aspect, RATIO / win_aspect,
-1., 1.);
}
glMatrixMode(GL_MODELVIEW);
glClearColor(0., 0., 1., 1.);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glVertex2f(-RATIO, -1);
glVertex2f(RATIO, -1);
glVertex2f(RATIO, 1);
glVertex2f(-RATIO, 1);
glEnd();
draw_bricks();
paddle_draw();
ball_draw();
glfwSwapBuffers();
return glfwGetTime();
}
int main(int argc, char **argv)
{
set_xy();
if( GL_FALSE == glfwInit() )
return -1;
if( GL_FALSE == glfwOpenWindow(800, 600, 8, 8, 8, 8, 0, 0, GLFW_WINDOW) )
return -2;
glfwSetWindowTitle("Viraj's Brick Breaker - GLFW version by datenwolf");
glfwSetKeyCallback(keyboard);
reset_game();
while( glfwGetWindowParam(GLFW_OPENED) ) {
glfwPollEvents();
float const dT = display();
step_game(dT);
}
glfwTerminate();
return 0;
}
i think your distortion is due to your changeSize(...) function: you calculate the aspect ratio of of your window, but you do not use it. Have a look at the code where you stole this snippet from :)
Do you change resolution of your screen when switching to fullscreen mode? If you do, it is very likely that a pixel is not square anymore, so you will need the actual aspect ratio of your screen.