C++ OpenGL Draw Pixel - c++

I'm learning how to use OpenGL by doing an online course and the practice problem asks to create a function to draw pixels:
void SetPixel(int x, int y, char r, char g, char b){}
I know I could do something like this to draw one:
glBegin(GL_POINTS);
glColor3f(1.0,1.0,1.0);
glVertex2i(100,100);
glEnd();
However given the context of the problem it's not a valid solution.
The problem also asks to use the SetPixel function by calling it within a loop to randomly place pixels across the screen. I understand how to implement that, but it's the drawing the actual pixel I don't understand, especially within the context of the program given.
The program (using GLUT):
#include <stdlib.h> //- for exit()
#include <stdio.h> //- for sprintf()
#include <string.h> //- for memset()
#ifdef _WIN32
#include "libs/glut.h"
#include <windows.h>
#pragma comment(lib, "winmm.lib") //- not required but have it in anyway
#pragma comment(lib, "libs/glut32.lib")
#elif __APPLE__
#include <GLUT/glut.h>
#elif __unix__
#include <GL/glut.h>
#endif
#define FRAME_WIDE 1000
#define FRAME_HIGH 600
//====== Structs & typedefs =========
typedef unsigned char BYTE;
struct POINT2D {int x, y;};
//====== Global Variables ==========
BYTE pFrameL[FRAME_WIDE * FRAME_HIGH * 3];
BYTE pFrameR[FRAME_WIDE * FRAME_HIGH * 3];
int shade = 0;
POINT2D xypos = {0,0};
int stereo = 0;
int eyes = 10;
//===== Forward Declarations ========
void ClearScreen();
void DrawFrame();
void Interlace(BYTE* pL, BYTE* pR);
void BuildFrame(BYTE *pFrame, int view);
void OnIdle(void);
void OnDisplay(void);
void reshape(int w, int h);
////////////////////////////////////////////////////////
// Program Entry Poinr
////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
//-- setup GLUT --
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //GLUT_3_2_CORE_PROFILE |
glutInitWindowSize(FRAME_WIDE, FRAME_HIGH);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
//--- set openGL state --
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//-- register call back functions --
glutIdleFunc(OnIdle);
glutDisplayFunc(OnDisplay);
glutReshapeFunc(reshape);
//-- run the program
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////
// Event Handers
////////////////////////////////////////////////////////
void OnIdle(void)
{
DrawFrame();
glutPostRedisplay();
}
void OnDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(FRAME_WIDE, FRAME_HIGH, GL_RGB,GL_UNSIGNED_BYTE, (GLubyte*)pFrameR);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
////////////////////////////////////////////////////////
// Utility Functions
////////////////////////////////////////////////////////
void ClearScreen()
{
memset(pFrameL, 0, FRAME_WIDE * FRAME_HIGH * 3);
memset(pFrameR, 0, FRAME_WIDE * FRAME_HIGH * 3);
}
void Interlace(BYTE* pL, BYTE* pR)
{
int rowlen = 3 * FRAME_WIDE;
for (int y = 0; y < FRAME_HIGH; y+=2)
{
for (int x = 0; x < rowlen; x++) *pR++ = *pL++;
pL += rowlen;
pR += rowlen;
}
}
void DrawFrame()
{
ClearScreen();
if (!stereo) BuildFrame(pFrameR, 0);
else {
BuildFrame(pFrameL, -eyes);
BuildFrame(pFrameR, +eyes);
Interlace((BYTE*)pFrameL, (BYTE*)pFrameR);
}
}
////////////////////////////////////////////////////////
// Drawing Function
////////////////////////////////////////////////////////
void BuildFrame(BYTE *pFrame, int view)
{
// create a loop in here that uses SetPixel function to randomly place 10,000 pixels
}
void SetPixel(int x, int y, char r, char g, char b)
{
}
Can someone please help me work this out? How would I implement the SetPixel function elegantly?

Based on your configurations for the pixel format, the red component of the pixel at (x, y) is at index 3 * (y * width + x), and +1 / 2 for green / blue:
void SetPixel(int x, int y, char r, char g, char b)
{
if (x < 0 || x >= FRAME_WIDE || y < 0 || y >= FRAME_HIGH)
return; // out of bounds
int index = 3 * (y * FRAME_WIDE + x);
pFrame[index] = r;
pFrame[index+1] = g;
pFrame[index+2] = b;
}
Note that it is unclear whether SetPixel should write both buffers or there should be a flag indicating which one, so I just wrote pFrame as a placeholder.

Related

invalid use of member ‘xx::x’ in static member function

I want to apply selection operation in a 3d viewer (using qt-creator)
any.h file:
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
class any
{
public:
any();
int board[3][3];
void init(void);
static void drawSquares(GLenum mode);
static void processHits (GLint hits, GLuint buffer[]);
static void pickSquares(int button, int state, int x, int y);
static void display(void);
static void reshape(int w, int h);
};
any.ccp file :
#include "any.h"
any::any()
{
}
void any:: init(void)
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j ++)
board[i][j] = 0;
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void any:: drawSquares(GLenum mode)
{
GLuint i, j;
for (i = 0; i < 3; i++) {
if (mode == GL_SELECT)
glLoadName (i);
for (j = 0; j < 3; j ++) {
if (mode == GL_SELECT)
glPushName (j);
glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0,(GLfloat) board[i][j]/3.0);
glRecti (i, j, i+1, j+1);
if (mode == GL_SELECT)
glPopName ();
}
}
}
/* processHits prints out the contents of the
* selection array.
*/
void any:: processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint ii, jj, names, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
names = *ptr;
printf (" number of names for this hit = %d\n", names);
ptr++;
printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
printf (" names are ");
for (j = 0; j < names; j++) { /* for each name */
printf ("%d ", *ptr);
if (j == 0) /* set row and column */
ii = *ptr;
else if (j == 1)
jj = *ptr;
ptr++;
}
printf ("\n");
board[ii][jj] = (board[ii][jj] + 1) % 3;
}
}
#define BUFSIZE 512
void any:: pickSquares(int button, int state, int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
return;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
(void) glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
/* create 5x5 pixel picking region near cursor location */
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
5.0, 5.0, viewport);
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
drawSquares (GL_SELECT);
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();
hits = glRenderMode (GL_RENDER);
processHits (hits, selectBuf);
glutPostRedisplay();
}
void any:: display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
drawSquares (GL_RENDER);
glFlush();
}
void any:: reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
main file:
#include <any.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (100, 100);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
any* an=new any();
an->init();
glutMouseFunc (an->pickSquares);
glutReshapeFunc (an->reshape);
glutDisplayFunc(an->display);
glutMainLoop();
return app.exec();
}
the error :
../untitled/any.cpp:60:24: error: invalid use of member ‘any::board’ in static member function
when I try to make board static it shows a lot of errors about :
26: undefined reference to `any::board'
Static function cannot access non-static variables
In your source code, compiler will flash an error i.e. illegal reference to non-static member any::board.
Hence, accessing non-static members from static method in C++ programming is not possible.
static method memories will be create once without creating an instance of class. so you cannot use of a non-static class members in a static method, because non-static members has no memory already in static methods.
Please define your methods as non-static or use of static members in your class.
Please check out this link.
board is an instance member. You can't use it in a static method like drawSquares. You either need to make board a static member too, or, more likely, make drawSquares non-static.

glutKeyboardUpFunc get's called without key release

Im having a problem with glutKeyboardUpFunc. Everytime I press a key and don't release it, the callback of glutKeyboardUpFunc stills gets called, the longer I hold the key the more times the callback executes.
Here is a quick example I put up:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <GL/glut.h>
#define DEBUG 1
/* VARI�VEIS GLOBAIS */
typedef struct {
GLboolean doubleBuffer;
GLint delay;
} Estado;
typedef struct {
GLfloat x;
GLfloat y;
GLfloat height;
GLfloat width;
GLfloat y_accelaration;
} Plataforma;
//save stage coordinates
typedef struct {
GLfloat top;
GLfloat bottom;
GLfloat right;
GLfloat left;
} Screen;
Estado estado;
Screen ecra;
Plataforma plataforma;
GLfloat tab_speed = 2.0 / 20.0;
GLfloat y_accelaration = 0.001;
void Init(void) {
struct tm *current_time;
time_t timer = time(0);
estado.delay = 42;
estado.doubleBuffer = GL_TRUE;
plataforma.height = 0.3;
plataforma.width = 0.05;
plataforma.y_accelaration = 0;
plataforma.y = 0;
// L� hora do Sistema
current_time = localtime(&timer);
glClearColor(0.3, 0.3, 0.3, 0.0);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
}
void Reshape(int width, int height) {
GLint size;
GLfloat ratio = (GLfloat) width / height;
GLfloat ratio1 = (GLfloat) height / width;
if (width < height)
size = width;
else
size = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width < height) {
ecra.left = -1;
ecra.right = 1;
ecra.bottom = -1 * ratio1;
ecra.top = 1 * ratio1;
gluOrtho2D(-1, 1, -1 * ratio1, 1 * ratio1);
} else {
ecra.left = -1 * ratio;
ecra.right = 1 * ratio;
ecra.bottom = -1;
ecra.top = 1;
gluOrtho2D(-1 * ratio, 1 * ratio, -1, 1);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void desenhar_plataforma(Plataforma pf) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(pf.x - pf.width / 2, pf.y - pf.height / 2);
glVertex2f(pf.x + pf.width / 2, pf.y - pf.height / 2);
glVertex2f(pf.x + pf.width / 2, pf.y + pf.height / 2);
glVertex2f(pf.x - pf.width / 2, pf.y + pf.height / 2);
glEnd();
}
void Draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
plataforma.x = ecra.left + 0.1;
desenhar_plataforma(plataforma);
glFlush();
if (estado.doubleBuffer)
glutSwapBuffers();
}
void Key(unsigned char key, int x, int y) {
switch (key) {
case 'a':
plataforma.y += tab_speed;
glutPostRedisplay();
break;
case 's':
plataforma.y -= tab_speed;
glutPostRedisplay();
break;
}
}
void KeyUp(unsigned char key, int x, int y) {
if (DEBUG)
printf("Key Up %c\n", key);
}
int main(int argc, char **argv) {
estado.doubleBuffer = GL_TRUE;
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 600);
glutInitDisplayMode(((estado.doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) | GLUT_RGB);
if (glutCreateWindow("No Man's Pong") == GL_FALSE)
exit(1);
Init();
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
// Callbacks de teclado
glutKeyboardFunc(Key);
glutKeyboardUpFunc(KeyUp);
glutMainLoop();
return 0;
}
Here's the ouput when pressing 'a':
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
......
Needless to say im not releasing 'a'.
Im using linux mint 17.3 and g++, shutting off key repeat or setting glutIgnoreKeyRepeat(true), stops this issue, but also makes glutKeyboardFunc execute only once per key press, and I realy don't want that.
Does anyone know how to fix this, or whats causing this?
I also compiled the same code under windows 8.1 and the keyUp only fires when there's an actual key release.

I am trying to use structure in c++ but its not working

in this method
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
that uses the structure i made
struct GLintPoint {
GLint x,y;
};
it says that there is an error and that "no operator "[]" matches these operands, operator types are GLintPoint[int]" where i try to assign a value from points to point. Well i did use the right brackets and it is an int in there so what is the problem? FYI this code is to draw the Sierpinski gasket with the user making the initial 3 points by clicking the screen with the mouse. Just in case you would like to see it here is the whole program.
#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
struct GLintPoint {
GLint x,y;
};
void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();
}
void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void myMouse(int button, int state, int x, int y){
static GLintPoint corner[2];
static int numCorners = 0;
if(state == GLUT_DOWN){
if(button == GLUT_LEFT_BUTTON){
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y;
if(++numCorners ==2 ){
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0;
glFlush();
}
}
else if(button == GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,150);
glutCreateWindow("mouse dots");
glutDisplayFunc(display);
glutPostRedisplay();
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
I'm inferring from the name of the function argument, points, that you intend the function to accept an array of points. But it's actually just written to take one.
So when you write points[index] the compiler is looking for operator[] in your GLintPoint struct (and there isn't one).
If you change the function prototype to take an array of GLintPoints I think you'll have better luck.

OpenGL Not Drawing Anything

Just trying to draw a line between two points using glut for openGL version 3.0. Here's the code.
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
void init(void) {
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,30.0,0.0,30.0);
}
void setPixel(GLint x, GLint y) {
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,30.0,0.0,30.0);
}
float roundValue(float v) {
return floor(v + 0.5);
}
void lineDDA(int xa, int ya, int xb, int yb) {
double dx = xb-xa, dy = yb-ya, steps;
float xInc, yInc, x = xa, y = ya;
if (abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xInc = dx/(float)steps;
yInc = dy/(float)steps;
setPixel(x, y);
int k;
for (k = 0; k < steps; ++k) {
/* code */
x += xInc;
y += yInc;
setPixel(roundValue(x), roundValue(y));
}
}
void update() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0f);
glColor3f(1, 0, 0);
lineDDA(1, 1, 8, 7);
lineDDA(1, 1, 8, 2);
glFlush();
}
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(200,100);
glutInitWindowSize(640,480);
glutCreateWindow("Tutorial-1: Q4");
glLoadIdentity();
init();
glutDisplayFunc(update);
glutMainLoop();
return 0;
}
But this is not drawing anything on the window. It just remains white. Can anyone please suggest a solution?
Your setPixel function doesn't use its parameters x and y. You are not enabling line rendering mode or adding vertices to be drawn.
Fixing would consist of at least these steps:
Call glBegin(GL_LINES); after glPointSize(5.0f);
Call glEnd() before glFlush();
Modify setPixel to look like this:
void setPixel(GLint x, GLint y)
{
glVertex2f(x, y);
}

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.