Why doesn't OpenGL draw object from std::vector? - c++

Why isn't it drawing object from vector? Where's my mistake?
It shows m_x,m_y position like this object would exist but this object isn't on my screen.
main.cpp:
#include <iostream>
#include <vector>
#define NDEBUG
#include "Figura.h"
#include "Balon.h"
#include <GL/freeglut.h>
using namespace std;
vector<CBalon> wektor_kol;
/* GLUT callback Handlers */
void resize(int width, int height)
{
const float ar = (float)width / (float)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void idle()
{
glutPostRedisplay();
}
void DrawRectangle(double width, double height)
{
glPushMatrix();
// TODO
// test functions below (glTranslated, glRotated, glColor3d) - what happen when you change their arguments?
// does their order change the result?
glTranslated(0.0, 0.0, 0.0);
glRotated(0, 1.0, 0.0, 0.0);
glRotated(0, 0.0, 1.0, 0.0);
glRotated(0, 0.0, 0.0, 1.0);
glColor3d(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
{
glVertex3d(-width / 2, height / 2, 0);
glVertex3d(width / 2, height / 2, 0);
glVertex3d(width / 2, -height / 2, 0);
glVertex3d(-width / 2, -height / 2, 0);
}
glEnd();
glPopMatrix();
}
void display()
{
// clear the scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
for (auto itr = wektor_kol.begin(); itr != wektor_kol.end(); itr++)
{
(*itr).Rysuj();
}
glPopMatrix();
glutSwapBuffers();
}
void InitGLUTScene(char* window_name)
{
glutInitWindowSize(800, 600);
glutInitWindowPosition(40, 40);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow(window_name);
// set white as the clear colour
glClearColor(1, 1, 1, 1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
}
void SetCallbackFunctions()
{
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutIdleFunc(idle);
}
void SetObjectsPositions()
{
CBalon balonik(0.6, 0.1, 0.5, 0.5);
balonik.positionSetter(0, 0);
wektor_kol.push_back(balonik);
}
int main(int argc, char *argv[])
{
// it's still possible to use console to print messages
printf("Hello openGL world!");
// the same can be done with cout / cin
glutInit(&argc, argv);
InitGLUTScene("freeglut template");
SetCallbackFunctions();
SetObjectsPositions();
// start GLUT event loop. It ends when user close the window.
glutMainLoop();
return 0;
}
Balon.cpp
#include "Balon.h"
#include "Figura.h"
#define NDEBUG
#define M_PI 3.14
#include <math.h>
#include <GL/freeglut.h>
CBalon::CBalon()
{
}
CBalon::CBalon(double radius, double red, double green, double blue)
{
m_r = red;
m_g = green;
m_b = blue;
m_radius = radius;
}
void CBalon::Rysuj()
{
glPushMatrix();
{
glTranslated(m_x, m_y, 0.0);
glRotated(m_z, 0.0, 0.0, 1.0);
glColor3d(m_r, m_g, m_b);
glBegin(GL_TRIANGLE_FAN);
{
for (int i = 0; i <= 360; i++)
{
// 180 - pi
// i - degInRad
float degInRad = i*M_PI / 180;
glVertex2f(cos(degInRad)*m_radius, sin(degInRad)*m_radius);
}
}
glEnd();
}
glPopMatrix();
}
double CBalon::redGetter()
{
return m_r;
}
CBalon::~CBalon()
{
}
Balon.h
#pragma once
#include "Figura.h"
class CBalon : public Figura
{
private:
double m_radius;
public:
CBalon();
CBalon(double radius, double red, double green, double blue);
double redGetter();
void Rysuj();
~CBalon();
};
Figura.cpp
#include "Figura.h"
Figura::Figura()
{
}
Figura::Figura(double _x, double _y, double _z, double _r, double _g, double _b, bool _u, double _rotation) : m_x(_x), m_y(_y), m_z(_z), m_r(_r), m_g(_g), m_b(_b), m_ukryj(_u), m_rotation(_rotation)
{
}
Figura::Figura(double red, double green, double blue)
: m_r(red), m_g(green), m_b(blue)
{
m_x = 0.0;
m_y = 0.0;
m_z = 0.0;
m_ukryj = true;
}
void Figura::Przesun(double dx, double dy)
{
m_x += dx;
m_y += dy;
}
bool Figura::GetterUkryj()
{
return m_ukryj;
}
void Figura::ZmienKolor(double _r, double _g, double _b)
{
m_r = _r;
m_g = _g;
m_b = _b;
}
double Figura::xGetter()
{
return m_x;
}
double Figura::yGetter()
{
return m_y;
}
void Figura::positionSetter(double x, double y)
{
m_x = x;
m_y = y;
}
Figura::~Figura()
{
}
Figura.h
#pragma once
class Figura
{
protected:
double m_r, m_g, m_b;
bool m_ukryj;
double m_x, m_y, m_z;
double m_alpha, m_beta, m_gamma;
double m_rotation;
public:
Figura();
Figura(double _x, double _y, double _z, double _r, double _g, double _b, bool _u, double _rotation);
Figura(double red, double green, double blue);
~Figura();
virtual void Rysuj() = 0;
void Ukryj();
void Pokaz();
void Przesun(double dx, double dy);
void Obroc(double dalpha, double dbeta, double dgamma);
void ZmienKolor(double _r, double _g, double _b);
double xGetter();
double yGetter();
void positionSetter(double x, double y);
bool GetterUkryj();
};

The mistake was:
CBalon::CBalon(double radius, double red, double green, double blue): Figura(),m_radius(radius)
{
m_r = red;
m_g = green;
m_b = blue;
}
Changing to:
CBalon::CBalon(double radius, double red, double green, double blue): Figura(red, green, blue),m_radius(radius)
{
}
solved my problem.

Related

How to check whether mouse clicked over a polygon in OpenGL?

I'm trying to make a color picker tool in OpenGL. It works you can use the code below. However there is one glaring issue, if you test the program you will find that if you click the black background, the color selected will become black. So my question is how do I change the code such that it recognizes which polygon I clicked or recognizes that I clicked on something or a polygon.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>
#include <iterator>
//static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mod);
static void mouse_cursor_callback(GLFWwindow* window, double xpos, double ypos);
float randfloat(){
float r = ((float)(rand() % 10))/10;
return r;
}
class RGB{
public:
float r;
float g;
float b;
void setColor(float _r, float _g, float _b){
r = _r;
g = _g;
b = _b;
}
};
RGB selected_col;
RGB mixColor(RGB color1, RGB color2){
float r = 0;
float g = 0;
float b = 0;
r = sqrt((pow(color1.r,2)+pow(color2.r,2))/2);
g = sqrt((pow(color1.g,2)+pow(color2.g,2))/2);
b = sqrt((pow(color1.b,2)+pow(color2.b,2))/2);
RGB a;
a.setColor(r,g,b);
return a;
}
int main() {
int side_count;
std::cout<<"Type the no. of sides: "<<std::endl;
std::cin>>side_count;
if(side_count < 3){
return 1;
}
srand((unsigned)time(0));
float rs[side_count];
float gs[side_count];
float bs[side_count];
RGB colors[side_count];
for (int i=0;i<side_count;i++)
{
rs[i] = randfloat();
gs[i] = randfloat();
bs[i] = randfloat();
colors[i].setColor(rs[i],gs[i],bs[i]);
}
GLFWwindow* window;
if (!glfwInit())
return 1;
window = glfwCreateWindow(400, 400, "Window", NULL, NULL);
//glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCursorPosCallback(window, mouse_cursor_callback);
if (!window) {
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
if(glewInit()!=GLEW_OK)
std::cout<<"Error"<<std::endl;
RGB mcol = colors[1];
for(int i=0;i<side_count-1;i++)
{
mcol = mixColor(mcol, colors[i+1]);
}
while(!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(mcol.r,mcol.g,mcol.b);glVertex2f(0,0);
//glColor3f(1.0f,0.0f,0.0f);glVertex3f(-0.5f,0.0f,0.0f);
for(int i=0; i<=side_count;i++)
{
float r = rs[i%side_count];
float g = gs[i%side_count];
float b = bs[i%side_count];
float x = 0.5f * sin(2.0*M_PI*i/side_count);
float y = 0.5f * cos(2.0*M_PI*i/side_count);
glColor3f(r,g,b);glVertex2f(x,y);
}
glEnd();
glBegin(GL_QUADS);
glColor3f(selected_col.r, selected_col.g, selected_col.b);glVertex2f(-1.0f,0.5f);
glColor3f(selected_col.r, selected_col.g, selected_col.b);glVertex2f(-.5f,0.5f);
glColor3f(selected_col.r, selected_col.g, selected_col.b);glVertex2f(-.5f,1.5f);
glColor3f(selected_col.r, selected_col.g, selected_col.b);glVertex2f(-1.0f,1.5f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
/*static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods){
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);
//std::cout<<"X: "<<xPos<<" Y: "<<yPos<<std::endl;
double x=xPos;
int height,width;
glfwGetWindowSize(window, &width, &height);
double y = height - yPos;
unsigned char pixel[4];
glReadPixels(x, y, 1, 1, GL_RGB,GL_UNSIGNED_BYTE, &pixel);
selected_col.setColor((float)pixel[0]/255,(float)pixel[1]/255,(float)pixel[2]/255);
//std::cout<<"R: "<<selected_col.r<<" G: "<<selected_col.g<<" B: "<<selected_col.b<<std::endl;
}
}*/
void mouse_cursor_callback( GLFWwindow * window, double xpos, double ypos)
{
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE)
{
return;
}
double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);
//std::cout<<"X: "<<xPos<<" Y: "<<yPos<<std::endl;
double x=xPos;
int height,width;
glfwGetWindowSize(window, &width, &height);
double y = height - yPos;
unsigned char pixel[4];
glReadPixels(x, y, 1, 1, GL_RGB,GL_UNSIGNED_BYTE, &pixel);
selected_col.setColor((float)pixel[0]/255,(float)pixel[1]/255,(float)pixel[2]/255);
//std::cout<<"R: "<<selected_col.r<<" G: "<<selected_col.g<<" B: "<<selected_col.b<<std::endl;
}

OpenGL Glut Drawing Points Not Working

Can someone help me out in checking my code? I'm trying to draw a simple dot by clicking in the window, but I can't see any points drawn. The point recording system is copied as an example from one of the stack overflow posts.
#include <stdlib.h>
#include <stdio.h>
//#include <GL\glew.h>
#include <gl/glut.h>
//#include <GL\freeglut.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "draw.h"
//DRAW draw2;
class Point
{
int Xvalue, Yvalue;
std::string Text;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
void text(std::string text)
{
Text = text;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
//return text value
std::string rtext() { return Text; }
};
Point point[30];
int count = 0;
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
//glColor3f(0.0, 0.0, 0.0);
//glPointSize(5);
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glColor3f(1.0, 1.0, 1.0);
//glLoadIdentity();
/*Drawing here*/
//redraw
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
//draw2.Dot(x, y);
std::cout << "drawing dot " << x << " " << y << std::endl;
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f);
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
}
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
//draw2.Dot(x, y);
count++;
}
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv); //initialize toolkit
//Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); //display mode
glutInitWindowSize(1200, 800); //set window size
glutCreateWindow("NURBS Curve"); //open screen window
Init(); //additional initializations
//CALLBACK FUNCTIONS
glutMouseFunc(mouse);
glutDisplayFunc(Display); //send graphics to display window
/*
GLenum err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "GLEW error");
return 1;
}
*/
//pass control to glut for events, loops
glutMainLoop();
return 0;
}
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f); // wat
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
There's a short list of GL functions you can call inside a glBegin()/glEnd() pair and glPointSize() is not on it:
Only a subset of GL commands can be used between glBegin and glEnd.
The commands are
glVertex,
glColor,
glSecondaryColor,
glIndex,
glNormal,
glFogCoord,
glTexCoord,
glMultiTexCoord,
glVertexAttrib,
glEvalCoord,
glEvalPoint,
glArrayElement,
glMaterial, and
glEdgeFlag.
Also,
it is acceptable to use
glCallList or
glCallLists to execute
display lists that include only the preceding commands.
If any other GL command is executed between glBegin and glEnd,
the error flag is set and the command is ignored.
Move the glPointSize() outside the glBegin()/glEnd() pair:
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
All together:
#include <gl/glut.h>
class Point
{
int Xvalue, Yvalue;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
};
Point point[30];
int count = 0;
void Display()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
gluOrtho2D(0.0, w, 0.0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
count++;
}
glutPostRedisplay();
}
int main( int argc, char *argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(1200, 800);
glutCreateWindow("NURBS Curve");
glutMouseFunc(mouse);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
Found out that glcolor3f and glpointsize had to be initialized first in my Init() function.
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0); //this one
glPointSize(5); //and this one
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}

opengl not drawing anything

Just trying to draw a point using glut and glew for opengl version 4.3
my code
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
is not rendering anything, can someone tell me what did i miss? here is full code:
#include <iostream>
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOS };
#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY 10 //ms
//general
long frameCount = 0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
void timerEvent(int value)
{
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}
void init (void)
{
// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, WIDTH, HEIGHT);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("The Abyss");
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);
if (glewInit()) //i guess this is true on failure
{
cerr << "Error initializing glew, Program aborted." << endl;
exit(EXIT_FAILURE);
}
//Init First
init();
//Init callback for Rendering
glutDisplayFunc(Renderer);
//Main Loop
glutMainLoop();
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case (27) :
exit(EXIT_SUCCESS);
break;
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
mouse_buttons |= 1<<button;
}
else if (state == GLUT_UP)
{
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
}
void motion(int x, int y)
{
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
if (mouse_buttons & 1)
{
rotate_x += dy * 0.2f;
rotate_y += dx * 0.2f;
}
else if (mouse_buttons & 4)
{
translate_z += dy * 0.01f;
}
mouse_old_x = x;
mouse_old_y = y;
}
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
You have requested a Core context. You need to:
Specify a vertex and fragment shader. There are no freebies in Core.
Stop using deprecated functionality like glBegin() and glMatrixMode().
Start using VBOs to submit your geometry.
Start using glDrawArrays() and friends to draw your geometry.

Draw a 9 point circle in openGL?

I'm trying to draw a 9 point circle for my openGL class project, the problem? My teacher has not taught us how to do hardly anything with circles. He gave us code on how to 'draw' a triangle and told us what the formula to find vertex and center points for normal circles. But never once even touched on how to use them for coding, especially with his way he wants us to do it. He told us to use
class GLintPoint {
public:
GLint x, y;
};
and we could name them A, B, C then find the vertex say a = B - A. I assume this would be a.x = B.x - A.x and same for why, but I don't know for sure. He had to leave for a medical thing(I assume surgery) so I can't ask him and he still has it due at the end of the week.
He gave us this code before he left.
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include "canvas.h"
const int screenWidth = 640;
const int screenHeight = 480;
Canvas cvs(screenWidth, screenHeight, "Relative Drawing Example", 1);
class GLintPoint {
public:
GLint x, y;
};
#define NUM 3
static GLintPoint List[NUM];
// myInit
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble )screenWidth, 0.0, (GLdouble)screenHeight);
}
// myDisplay
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void mykey(unsigned char key, int x, int y)
{
if (key == 'Q' || key == 'q') exit(0);
}
// draw arc
void draw_arc(GLdouble cx, GLdouble cy, GLdouble r, GLdouble startAngle,
GLdouble sweepAngle)
{
glBegin(GL_LINE_STRIP);
for (GLdouble t = startAngle; t < startAngle+sweepAngle; t += 0.001)
{
GLdouble x = cx + r*cos(DEG2RAD*t);
GLdouble y = cy + r*sin(DEG2RAD*t);
glVertex2d(x, y);
}
glEnd();
}
// myMouse
void myMouse(int button, int state, int x, int y)
{
static int last = -1;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && last < (NUM -1))
{
List[++last].x = x;
List[ last].y = screenHeight - y;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= last; i++) {
if(last <=0){
cvs.moveTo(List[i].x, List[i].y);
}
else
cvs.lineTo(List[i].x, List[i].y);
}
int i = 0;
cvs.lineTo(List[i].x, List[i].y);
glEnd();
glFlush();
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
last = -1;
GLintPoint a.x =
}
// main
int main(int argc, char ** argv)
{
//glutInit(&argc, argv);
//glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB);
//glutInitWindowSize(screenWidth, screenHeight);
//glutInitWindowPosition(100, 150);
//glutCreateWindow("case study 4.2");
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutKeyboardFunc(mykey);
myInit();
glutMainLoop();
return 0;
}
and
#include <math.h>
// define some ascii key names
#define GLUT_KEY_ESC 27
// Keeps track of a single point ( turtle position )
class Point2d {
public:
Point2d() { x = y = 0.0f; }
Point2d( float xx, float yy ) { x = xx; y = yy; }
void set( float xx, float yy ) { x = xx; y = yy; }
void setX( float xx ) { x = xx; }
void setY( float yy ) { y = yy; }
float getX() { return x; }
float getY() { return y; }
void draw( void ) { glBegin( GL_POINTS );
glVertex2f( (GLfloat)x, (GLfloat)y );
glEnd();
}
private:
float x, y;
};
//Data type for an array of points
class Point2dArray {
static const int MAX_NUM = 100;
public:
int num;
Point2d pt[MAX_NUM];
};
class IntRect {
public:
IntRect() { l = 0; r = 100; b = 0; t = 100; }
IntRect( int left, int right, int bottom, int top )
{ l = left; r = right; b = bottom; t = top; }
void set( int left, int right, int bottom, int top )
{ l = left; r = right; b = bottom; t = top; }
void draw( void ) { glRecti( l, b, r, t ); }
int getL() { return l; }
int getR() { return r; }
int getT() { return t; }
int getB() { return b; }
private:
int l, r, b, t;
};
class RealRect {
public:
RealRect() { l = 0; r = 100; b = 0; t = 100; }
RealRect( float left, float right, float bottom, float top )
{ l = left; r = right; b = bottom; t = top; }
void set( float left, float right, float bottom, float top )
{ l = left; r = right; b = bottom; t = top; }
void draw( void ) { glRectf( l, b, r, t ); }
float ratio( void ) { return (r - l)/(t - b); }
float getL() { return l; }
float getR() { return r; }
float getT() { return t; }
float getB() { return b; }
private:
float l, r, b, t;
};
class Canvas {
public:
Canvas( int width, int height, char* windowTitle, int buffer );
void setWindow( float l, float r, float b, float t );
void setViewport( int w, int h );
void autoSetWindow(void);
float getWindowAspectRatio( void );
void lineTo( float x, float y );
void lineTo( Point2d p );
void moveTo( float x, float y );
void moveTo( Point2d p );
void turnTo( float angle );
void turn( float angle );
void forward( float dist, int visible );
int getWinId(void);
void initCT(void);
void scale2D(double, double);
void translate2D(double, double);
void rotate2D(double);
private:
Point2d CP;
float CD;
IntRect viewport;
RealRect window;
int winId;
float delx, dely;
char code1, code2;
char formCode(Point2d p);
void chopLine(Point2d &p, char c);
int clipSegment(Point2d &p1, Point2d &p2);
};
Canvas::Canvas( int width, int height, char* windowTitle, int buffer = 1 ) {
char* argv[1];
char dummyString[1];
argv[0] = dummyString;
int argc = 1;
glutInit( &argc, argv );
glutInitDisplayMode( ((buffer == 1) ? GLUT_SINGLE : GLUT_DOUBLE) | GLUT_RGB );
glutInitWindowSize( width, height );
glutInitWindowPosition( (1024 - width) / 2, (768 - height) / 2 );
winId = glutCreateWindow( windowTitle );
setWindow( 1000.0f, -1000.0f, 1000.0f, -1000.0f );
CP.set( 0.0f, 0.0f );
CD = 0.0f;
}
int Canvas::getWinId(void)
{
return winId;
}
float Canvas::getWindowAspectRatio(void)
{
return (window.getR() - window.getL())/(window.getT() - window.getB());
}
void Canvas::setWindow( float l, float r, float b, float t ) {
window.set( l, r, b, t );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( (GLdouble) l, (GLdouble) r, (GLdouble) b, (GLdouble) t );
}
void Canvas::autoSetWindow(void) {
float l = window.getL();
float r = window.getR();
float b = window.getB();
float t = window.getT();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( (GLdouble) l, (GLdouble) r, (GLdouble) b, (GLdouble) t);
}
void Canvas::setViewport( GLsizei width, GLsizei height) {
float aspectRatio = getWindowAspectRatio();
GLint l, b;
GLsizei w, h;
if ((float) width / (float) height >= aspectRatio) {
l = (GLint) ((width - height * aspectRatio)/2.0);
b = 0;
w = (GLsizei) (height * aspectRatio);
h = height;
}
else {
l = 0;
b = (int) ((height - width / aspectRatio)/2.0);
w = width;
h = (GLsizei) (width / aspectRatio);
}
glViewport( l, b, w, h );
}
void Canvas::lineTo( float x, float y ) {
glBegin( GL_LINES );
glVertex2f( (GLfloat) CP.getX(), (GLfloat) CP.getY() );
glVertex2f( (GLfloat) x, (GLfloat) y );
glEnd();
CP.set( x, y );
glFlush();
}
void Canvas::lineTo( Point2d p ) {
glBegin( GL_LINES );
glVertex2f( (GLfloat) CP.getX(), (GLfloat) CP.getY() );
glVertex2f( (GLfloat) p.getX(), (GLfloat) p.getY() );
glEnd();
CP.set( p.getX(), p.getY() );
glFlush();
}
void Canvas::moveTo( float x, float y ) {
CP.set( x, y );
}
void Canvas::moveTo( Point2d p ) {
CP.set( p.getX(), p.getY() );
}
void Canvas::turn(float angle) {
CD += angle;
}
void Canvas::turnTo(float angle) {
CD = angle;
}
void Canvas::forward( float dist, int visible ) {
const float RadPerDeg=0.017453393;
float x = CP.getX()+dist*cos(RadPerDeg*CD);
float y = CP.getY()+dist*sin(RadPerDeg*CD);
float l = window.getL();
float r = window.getR();
float b = window.getB();
float t = window.getT();
l = (x < l)?x:l;
r = (x > r)?x:r;
b = (y < b)?y:b;
t = (y > t)?y:t;
window.set(l, r, b, t);
if (visible)
lineTo(x, y);
else
moveTo(x, y);
}
And these formulas. Written just as the piece of paper he gave me reads
a = B -A
b = C - B
c - A - C
c = A = 1/2((a + ((b(dot product) c)/(matrix of a (dotproduct)c)) * (matrix of a))
r = (magnitude of a)/2 squareroot(((b(dot product) c)/(matrix of a (dotproduct)c))^2 + 1)
I hate to ask, but I know when I'm in over my head, can anyone make heads or tails of what hes asking me to do? Or can anyone lead me in the right direction for how to make a 9 point circle? Its worth 10% of my final grade, so I'm not asking for the code to just copy/paste just "wtf do I do?" is all I'm asking.
You can't really draw curves in OpenGL, just polygons. So if someone asked me to draw a nine point circle in OpenGL, I would assume they meant a polygon with 9 sides, where each each point is evenly spaced along the perimeter of a circle.

How to get color from the pixel? OpenGL

I would like to get color from the pixel in my game and save this color in variable. How to do it in opengl?
glReadPixels():
#include <GL/glut.h>
#include <iostream>
using namespace std;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glScalef(5,5,5);
glBegin(GL_TRIANGLES);
glColor3ub(255,0,0);
glVertex2f(-1,-1);
glColor3ub(0,255,0);
glVertex2f(1,-1);
glColor3ub(0,0,255);
glVertex2f(1,1);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
unsigned char pixel[4];
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
cout << "R: " << (int)pixel[0] << endl;
cout << "G: " << (int)pixel[1] << endl;
cout << "B: " << (int)pixel[2] << endl;
cout << endl;
}
int main(int argc, char **argv)
{
glutInitWindowSize(640,480);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("glReadPixels()");
glutDisplayFunc(display);
glutPassiveMotionFunc(motion);
glutMainLoop();
return 0;
}
struct{ GLubyte red, green, blue; } pixel;
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixel);
#include <math.h>
#include <gl/glut.h>
struct Point {
GLint x;
GLint y;
};
struct Color {
GLfloat r;
GLfloat g;
GLfloat b;
};
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
//this function is used for getting color of pixel
Color getPixelColor(GLint x, GLint y) {
Color color;
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, &color);
return color;
}
void setPixelColor(GLint x, GLint y, Color color) {
glColor3f(color.r, color.g, color.b);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void floodFill(GLint x, GLint y, Color oldColor, Color newColor) {
Color color;
color = getPixelColor(x, y);
if(color.r == oldColor.r && color.g == oldColor.g && color.b == oldColor.b)
{
setPixelColor(x, y, newColor);
floodFill(x+1, y, oldColor, newColor);
floodFill(x, y+1, oldColor, newColor);
floodFill(x-1, y, oldColor, newColor);
floodFill(x, y-1, oldColor, newColor);
}
return;
}
void onMouseClick(int button, int state, int x, int y)
{
Color newColor = {1.0f, 0.0f, 0.0f};
Color oldColor = {1.0f, 1.0f, 1.0f};
floodFill(320, 240, oldColor, newColor);
}
void draw_circle(Point pC, GLfloat radius) {
GLfloat step = 1/radius;
GLfloat x, y;
for(GLfloat theta = 0; theta <= 360; theta += step) {
x = pC.x + (radius * cos(theta));
y = pC.y + (radius * sin(theta));
glVertex2i(x, y);
}
}
void display(void) {
Point pt = {320, 240};
GLfloat radius = 50;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
draw_circle(pt, radius);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Open GL");
init();
glutDisplayFunc(display);
glutMouseFunc(onMouseClick);
glutMainLoop();
return 0;
}