Hi everyone,
I’ve started working with C++ and OpenGL/GLUT a few weeks ago and I’m having a few probs with GL_LINE_STRIP.
Here’s my code:
#include <stdlib.h>
#include <string.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
// size per char = 1..6 x 1..12, 0 = end of stroke, -1 = end of data
GLfloat AData[] = {1,3, 1,10, 0, 2,11, 4,11, 0, 5,10, 5,3, 0, 2,7,
4,7, -1};
GLfloat BData[] = {4,3, 1,3, 1,11, 4,11, 0, 1,7, 4,7, 0, 5,4, 5,6,
0, 5,8, 5,10, -1};
GLfloat CData[] = {2,3, 5,3, 0, 1,4, 1,10, 0, 2,11, 5,11, -1};
/*
GLbyte DData[] = {};
GLbyte EData[] = {};
GLbyte FData[] = {};
*/
/* drawLetter() interprets the instructions from the array
* for that letter and renders the letter with line segments.
*/
static void drawLetter(GLfloat *charData)
{
glBegin(GL_LINE_STRIP);
for (int ii = 0; charData[ii] != -1;)
{
if (charData[ii] != 0)
{
glVertex2f(charData[ii], charData[ii + 1]);
ii++;ii++;
}
else
{
glEnd();
glBegin(GL_LINE_STRIP);
ii++;
}
}
glEnd();
glTranslatef(8.0, 0.0, 0.0);
}
/* Create a display list for each character */
static void init (void)
{
GLuint base;
glShadeModel (GL_FLAT);
base = glGenLists (128);
glListBase(base);
glNewList(base+'A', GL_COMPILE); drawLetter(AData);
glEndList();
glNewList(base+'B', GL_COMPILE); drawLetter(BData);
glEndList();
glNewList(base+'C', GL_COMPILE); drawLetter(CData);
glEndList();
/*
glNewList(base+'D', GL_COMPILE); drawLetter(DData);
glEndList();
glNewList(base+'E', GL_COMPILE); drawLetter(EData);
glEndList();
glNewList(base+'F', GL_COMPILE); drawLetter(FData);
glEndList();
*/
glNewList(base+' ', GL_COMPILE);
glTranslatef(8.0, 0.0, 0.0); glEndList();
}
char *test1 = "ABC";
char *test2 = "C B A";
static void printStrokedString(char *s)
{
GLsizei len = strlen(s);
glCallLists(len, GL_BYTE, (GLbyte *)s);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
//glScalef(2.0, 2.0, 2.0);
glTranslatef(10.0, 220.0, 0.0);
printStrokedString(test1);
glPopMatrix();
glPushMatrix();
//glScalef(2.0, 2.0, 2.0);
glTranslatef(10.0, 204.0, 0.0);
printStrokedString(test2);
glPopMatrix();
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);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case ' ':
glutPostRedisplay();
break;
case 27:
exit(0);
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
The problem is that seperate line strips are disconnected. There seems to be some imprecision (the specified pixel coordinates in the arrays should be okay?) but I can’t figure out where this comes from.
If I you edit out the code tags before the scaling commands, then it becomes even more apparant.
I tried it with GL_LINES instead of GL_LINE_SRTIP but it got even worse. So I guess my specified coordinates aren’t mapped accurately to the window’s pixels? Is there a way to solve this?