PDA

View Full Version : Problem rendering



Darth_Malloc
06-25-2012, 07:48 PM
I could use a little bit of help. I am new to the principles of OpenGL. I have no trouble with the mathematical aspects of 3D geometry. My difficulty is in actually rendering the image. When I try to execute my program, the window that should be showing the graphic instead simply takes a picture of whatever is on the screen at the time. Below are some parts of the source code, some of which was auto-generated by Code::Blocks. My geometric algorithm shall not be revealed for the time being. If this is a stupid question, then I apologize for wasting your time.


The main function:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <GL/glx.h> /* this includes the necessary X headers */
#include <GL/gl.h>
#include <X11/X.h> /* X11 constant (e.g. TrueColor) */
#include <X11/keysym.h>
#include "double_cube.h"

int main(int argc, char ** argv)
{
static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
Display * dpy;
Window win;
float first;
GLfloat start, xAngle = 42.0, yAngle = 82.0, zAngle = 112.0;
GLboolean doubleBuffer = GL_TRUE;
XVisualInfo * vi;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
int dummy;

/*** (1) open a connection to the X server ***/
std::cout<<"Enter the length of vertex of the cube to be doubled:";
std::cin>>first;
start = (GLfloat)first;
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
{
fatalError("could not open display");
}

/*** (2) make sure OpenGL's GLX extension supported ***/

if(!glXQueryExtension(dpy, &dummy, &dummy))
{
fatalError("X server has no OpenGL GLX extension");
}

/*** (3) find an appropriate visual ***/

/* find an OpenGL-capable RGB visual with depth buffer */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
if (vi == NULL)
{
vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
if (vi == NULL) fatalError("no RGB visual with depth buffer");
doubleBuffer = GL_FALSE;
}
/*** (4) create an OpenGL rendering context ***/

/* create an OpenGL rendering context */

cx = glXCreateContext(dpy, vi, /* no shared dlists */ None, /* direct rendering if possible */ GL_TRUE);
if (cx == NULL)
{
fatalError("could not create rendering context");
}

/*** (5) create an X window with the selected visual ***/

/* create an X colormap since probably not using default visual */
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = KeyPressMask | ExposureMask | ButtonPressMask | StructureNotifyMask;
win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 750, 1500, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap | CWEventMask, &swa);
XSetStandardProperties(dpy, win, "main", "main", None, argv, argc, NULL);
/*** (6) bind the rendering context to the window ***/
glXMakeCurrent(dpy, win, cx);
/*** (7) request the X window to be displayed on the screen ***/

XMapWindow(dpy, win);

/*** (8) configure the OpenGL context for rendering ***/

glEnable(GL_DEPTH_TEST); /* enable depth buffering */
glDepthFunc(GL_LESS); /* pedantic, GL_LESS is the default */
glClearDepth(1.0); /* pedantic, 1.0 is the default */

/* frame buffer clears should be to black */
glClearColor(0.0, 0.0, 0.0, 0.0);

/* set up projection transform */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
/* establish initial viewport */
/* pedantic, full window size is default viewport */
glViewport(0, 0, 750, 1500);

std::cout<<"Press left mouse button to rotate around X axis.\n";
std::cout<<"Press middle mouse button to rotate around Y axis.\n";
std::cout<<"Press right mouse button to rotate around Z axis.\n";
std::cout<<"Press the spacebar to advance to the next stage.\n";
std::cout<<"Press ESC to quit the application\n";
std::cout<<"Please note: due to the limited capacity of OpenGL to display curved lines, a small amount of imprecision may occur in results.\n";
/*** (9) dispatch X events ***/
while (1)
{
do{
XNextEvent(dpy, &event);
switch (event.type)
{
case KeyPress:
{
KeySym keysym;
XKeyEvent * kevent;
char buffer[1];
/* It is necessary to convert the keycode to a
* keysym before checking if it is an escape */
kevent = (XKeyEvent *) &event;
if ((XLookupString((XKeyEvent *)&event, buffer, 1, &keysym, NULL) == 1) && (keysym == (KeySym)XK_Escape))
{
exit(0);
break;
}
}
case ButtonPress:
{
recalcModelView = GL_TRUE;
switch (event.xbutton.button)
{
case 1:
{
xAngle += 10.0;
break;
}
case 2:
{
yAngle += 10.0;
break;
}
case 3:
{
zAngle += 10.0;
break;
}
}
break;
}
case ConfigureNotify:
{
glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
}
/* fall through... */
case Expose:
{
needRedraw = GL_TRUE;
break;
}
}
}while(XPending(dpy)); /* loop to compress events */
if (recalcModelView)
{
glMatrixMode(GL_MODELVIEW);
/* reset modelview matrix to the identity matrix */
glLoadIdentity();
/* move the camera back three units */
glTranslatef(0.0, 0.0, -3.0);
/* rotate by X, Y, and Z angles */
glRotatef(xAngle, 0.1, 0.0, 0.0);
glRotatef(yAngle, 0.0, 0.1, 0.0);
glRotatef(zAngle, 0.0, 0.0, 1.0);
recalcModelView = GL_FALSE;
needRedraw = GL_TRUE;
}
if (needRedraw)
{
double_cube(start, doubleBuffer, dpy, win);
needRedraw = GL_FALSE;
}
}
return 0;
}

The trouble spot:

void double_cube(GLfloat firstNum, GLboolean buff, Display * visual, Window readout)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);
bool align;
char prompt;
int stage, i, j, position = 0;
prompt = 'n';
GLfloat exact = 0.0, cubeRoot = 0.0 , min = 0.0, final = 0.0;
GLfloat ** change;
GLfloat * hypot;
GLfloat * eidos;
....

cireneikual
06-25-2012, 08:49 PM
You are probably not swapping buffers - so the screen is never updated.

BTW, please use code tags!

Dark Photon
06-26-2012, 04:46 AM
In particular, add:



glXSwapBuffers( dpy, win );


when needRedraw is true.

Darth_Malloc
07-10-2012, 09:45 PM
Thank you for that tip. The problem of the window taking a picture of the screen is fixed. Unfortunately, when I run the program, the window simply goes dark, and does not show any images. What must I do to keep the display progressing?

Lowest0ne
07-11-2012, 11:51 AM
/* set up projection transform */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
/* establish initial viewport */
/* pedantic, full window size is default viewport */
glViewport(0, 0, 750, 1500);

// Try adding this:
glMatrixMode (GL_MODELVIEW);


I'm a beginner too, so kindof a shot in the dark.

Darth_Malloc
07-12-2012, 09:38 PM
A well-placed shot nonetheless, even though Han shot Greedo first. Thank you for your help. For those interested, I may post a video showing the final result of my work in action. It is a Pythagorean mathematical concept, but I shall say no more for now.