GeatMaster

02-11-2017, 08:08 AM

I wrote my game in openGL 2.0 because I found it much easier to understand, and now that I've gotten past the proof-of-concept stage I'm trying to switch it to openGL 4.5. So I've been looking at tutorials for a few days and I'm just not getting it.

So far I've written a class to make, and handle errors for glPrograms and load shaders from files, and my own matrix/matrix stack class, then got confused on what's anything, and realized the book I was using was from 2009 so I'm not sure if it's actually good for anything.

The first source of confusion is from my onResize() function:

glViewport(0, 0, screenSize.x, screenSize.y);

double width = screenSize.x*devicePixelRatio.x;

double height = screenSize.y*devicePixelRatio.y;

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(-width/2, width/2, -height/2, height/2, .999999, 8.0);

glTranslatef(0, 0, -1);

glScalef(1, -1, 8.0/4096);

glMatrixMode(GL_MODELVIEW);

I've read that GL_PROJECTION, GL_MODELVIEW and the matrix functions are deprecated, so I've added the frustrum/transform/scale etc functions to my own matrix class, but I'm very unclear on what to do with it, or how the matrix modes really related to each other. Are the GL_PROJECTION matrices essentially at the bottom of the matrix stack or something?

The second is in my sprite rendering function, which is around 90% of rendering:

const GLfloat * getTexCoordinates(int _mirror)

{

static const GLfloat texCoord[] = { 0, 1, 1, 1, 1, 0, 0, 0 };

static const GLfloat xMirrorTexCoord[] = { 1, 1, 0, 1, 0, 0, 1, 0 };

static const GLfloat yMirrorTexCoord[] = { 0, 0, 1, 0, 1, 1, 0, 1 };

static const GLfloat MirrorTexCoord[] = { 1, 0, 0, 0, 0, 1, 1, 1 };

switch(_mirror & 0x03)

{

default: return texCoord;

case 0x01: return xMirrorTexCoord;

case 0x02: return yMirrorTexCoord;

case 0x03: return MirrorTexCoord;

}

}

void SpriteComponent::render(position_type camera)

{

if(!bind())

{

return;

}

glPushMatrix();

glTranslatef(pos.x - camera.x, pos.y - camera.y, 0);

if(drawOrder < 0)

{

glTranslatef(0, 0, drawOrder);

double scale = 1 + (drawOrder << 3)/-4096.0;

glScaled(scale, scale, 1);

}

Point2F s = size();

Point2F p = self()->pivotPoint();

glRotatef(-self()->rot(), 0, 0, 1);

glTranslatef(-pivot().x, pivot().y, 0);

glScalef(size().x, size().y, 1);

static const GLfloat vertices[] = { 0, 1, 1, 1, 1, 0, 0, 0 };

static const GLubyte indices[] = { 0, 1, 2, 0, 2, 3 };

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, vertices);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glTexCoordPointer(2, GL_FLOAT, 0, getTexCoordinates(attr._mirror));

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

glDisable(GL_TEXTURE_2D);

glPopMatrix();

}

I'm pretty unclear on what to do to convert it other than make the vertices and indices into a VBO. I've read that I can do... something, to render objects in batches and reduce calls to the graphics card, but I can't discern what that means exactly, as in batches with a single texture? or are the textures and texture coordinates also included in the batches? are texture coordinates also a VBO?

So far I've written a class to make, and handle errors for glPrograms and load shaders from files, and my own matrix/matrix stack class, then got confused on what's anything, and realized the book I was using was from 2009 so I'm not sure if it's actually good for anything.

The first source of confusion is from my onResize() function:

glViewport(0, 0, screenSize.x, screenSize.y);

double width = screenSize.x*devicePixelRatio.x;

double height = screenSize.y*devicePixelRatio.y;

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(-width/2, width/2, -height/2, height/2, .999999, 8.0);

glTranslatef(0, 0, -1);

glScalef(1, -1, 8.0/4096);

glMatrixMode(GL_MODELVIEW);

I've read that GL_PROJECTION, GL_MODELVIEW and the matrix functions are deprecated, so I've added the frustrum/transform/scale etc functions to my own matrix class, but I'm very unclear on what to do with it, or how the matrix modes really related to each other. Are the GL_PROJECTION matrices essentially at the bottom of the matrix stack or something?

The second is in my sprite rendering function, which is around 90% of rendering:

const GLfloat * getTexCoordinates(int _mirror)

{

static const GLfloat texCoord[] = { 0, 1, 1, 1, 1, 0, 0, 0 };

static const GLfloat xMirrorTexCoord[] = { 1, 1, 0, 1, 0, 0, 1, 0 };

static const GLfloat yMirrorTexCoord[] = { 0, 0, 1, 0, 1, 1, 0, 1 };

static const GLfloat MirrorTexCoord[] = { 1, 0, 0, 0, 0, 1, 1, 1 };

switch(_mirror & 0x03)

{

default: return texCoord;

case 0x01: return xMirrorTexCoord;

case 0x02: return yMirrorTexCoord;

case 0x03: return MirrorTexCoord;

}

}

void SpriteComponent::render(position_type camera)

{

if(!bind())

{

return;

}

glPushMatrix();

glTranslatef(pos.x - camera.x, pos.y - camera.y, 0);

if(drawOrder < 0)

{

glTranslatef(0, 0, drawOrder);

double scale = 1 + (drawOrder << 3)/-4096.0;

glScaled(scale, scale, 1);

}

Point2F s = size();

Point2F p = self()->pivotPoint();

glRotatef(-self()->rot(), 0, 0, 1);

glTranslatef(-pivot().x, pivot().y, 0);

glScalef(size().x, size().y, 1);

static const GLfloat vertices[] = { 0, 1, 1, 1, 1, 0, 0, 0 };

static const GLubyte indices[] = { 0, 1, 2, 0, 2, 3 };

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, vertices);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glTexCoordPointer(2, GL_FLOAT, 0, getTexCoordinates(attr._mirror));

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

glDisable(GL_TEXTURE_2D);

glPopMatrix();

}

I'm pretty unclear on what to do to convert it other than make the vertices and indices into a VBO. I've read that I can do... something, to render objects in batches and reduce calls to the graphics card, but I can't discern what that means exactly, as in batches with a single texture? or are the textures and texture coordinates also included in the batches? are texture coordinates also a VBO?