Getting to OpenGL?

Getting to OpenGL?
0.0 0


I’d like to start learning more OpenGL. I thought that since Cocos3d-X is based upon OpenGL that I could start with seeing how Cocos2d-x interacts wit OpenGL to do things.

Say, a sprite. If I look at CCSprite.h and CCSprite.cpp, where is the openGL? Where can I see how Cocos2d-x uses OpenGL to place a sprite on the screen, as an example.

What step am I missing?


What you are looking for is in the CCSprite::draw() method:

Before you get into OpenGL drawing, keep in mind that there are 2 separate memory spaces involved in this whole process. The CPU memory and the GPU memory. OpenGL is a bridge that transfers commands from the CPU to the GPU. So in order to draw a sprite, that is a rectangle (4 edges) with an image (texture) and possibly color, you need the following in general:

First load the image data from disk into memory. That is a 2 step process:

  1. Load the image bytes into CPU memory by using an appropriate library. For example with libpng you can load png files into a class that will hold the image: bytes, width, height and format.
  2. Transfer the image bytes from CPU into GPU memory and keep a handle as a reference:
class Bitmap {
  int32_t width;
  int32_t height;
  int32_t format;
  uint8_t* pixels;

// STEP 1

// ...

// STEP 2

uint32_t mTextureId;

// Generate new texture id
glGenTextures(1, &mTextureId);

// Make the texture id "active"
glBindTexture(GL_TEXTURE_2D, mTextureId);

// Upload image data into GPU memory.
glTexImage2D(GL_TEXTURE_2D, 0, bitmap->format, bitmap->width, bitmap->height, 0, bitmap->format, GL_UNSIGNED_BYTE, bitmap->pixels);

You continue with a similar process to define and upload the sprite data.

GLfloat vertices[] = {
  // sprite vertices, texture and color

uint32_t mVerticesId;

// Generate new buffer id
glGenBuffers(1, &mVerticesId);

// Make the buffer id "active"
glBindBuffer(GL_ARRAY_BUFFER, mVerticesId);

// Upload the buffer data into GPU memory
glBufferData(GL_ARRAY_BUFFER, dataBytes, data, GL_STATIC_DRAW);

Then setup the rendering properties and finally initiate the rendering process.

GLfloat* offsetPtr = mVertices;

// Tell OpenGL we are going to render vertices
glVertexPointer(2, GL_FLOAT, mVertexBytes, offsetPtr);
// Update offsetPtr to point into the color data ...

// Tell OpenGL we will use colors
glColorPointer(4, GL_FLOAT, mVertexBytes, offsetPtr);
// Update offsetPtr to point into the texture data ...

// Tell OpenGL we will use texture
glTexCoordPointer(2, GL_FLOAT, mVertexBytes, offsetPtr);

// Tell OpenGL to start rendering!
glDrawArrays(GL_TRIANGLES, offset, length);


Not sure where you got the idea that OpenGL is somehow specific to Cocos3d-X.

This is all OpenGL based, as you can see the link above is to 2.1.5 of Cocos2d-x.

OpenGL is just low level way to draw things to the screen and there are lots of simpler, easier to understand tutorials and materials on the web (compared to trying to learn OpenGL concepts through Cocos2d.)