Hello,
Can someone tell me what is wrong in my code below. My other project mates have the same exact code but it works fine for them. When i run this, i just get a black background and i cannot see if i’m drawing anything or not. They are using windows and I’m using mac.
//here is a part of my code//
//Draw the cicle with given parameters (size, color, position)
void drawCircle(float cx, float cy, float r, int num_segments) {
glBegin(GL_LINE_LOOP);
glColor3ub(colors[0],colors[1],colors[2]);
glLineWidth(circleSize);
for(int ii = 0; ii < num_segments; ii++) {
float theta = 2.0f * 3.1415926f * (float)(ii) / (float)(num_segments);//get the current angle
float x = r * cosf(theta);//calculate the x component
float y = r * sinf(theta);//calculate the y component
glVertex2f(x + cx, y + cy);//output vertex
}
glColor3f(1.0f, 1.0f, 1.0f);
glEnd();
}
bool isInit = false;
void init() {
glMatrixMode(GL_PROJECTION);
float aspect = (float)windowWidth / (float)windowHeight;
glOrtho(-aspect, aspect, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
bool pixelsLoaded = false;
GLuint pixelsTex;
void display() {
if (!isInit) {
init();
isInit = true;
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
if (reset) {
glClear(GL_COLOR_BUFFER_BIT);
}
reset = false;
if (menuMode) {
if (!pixelsLoaded) {
glGenTextures(1, &pixelsTex);
glBindTexture(GL_TEXTURE_2D, pixelsTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, windowWidth, windowHeight, 0);
glBindTexture(GL_TEXTURE_2D, 0);
pixelsLoaded = true;
}
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, pixelsTex);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(-1.0f, -1.0f);
glTexCoord2i(0, 1); glVertex2i(-1.0f, 1.0f);
glTexCoord2i(1, 1); glVertex2i(1.0f, 1.0f);
glTexCoord2i(1, 0); glVertex2i(1.0f, -1.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)windowWidth / (float)windowHeight;
glOrtho(-aspect, aspect, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} else if (pixelsLoaded) {
//reset texture
pixelsLoaded = false;
}
bool redraw = callHeldInputs();
/*glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(-squareSize[0] + offsetsUpRight[1], -squareSize[1] + offsetsUpRight[0]);
glVertex2f(squareSize[0] + offsetsUpRight[1], -squareSize[1] + offsetsUpRight[0]);
glVertex2f(squareSize[0] + offsetsUpRight[1], squareSize[1] + offsetsUpRight[0]);
glVertex2f(-squareSize[0] + offsetsUpRight[1], squareSize[1] + offsetsUpRight[0]);
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);*/
drawCircle(0.0f + offsetsUpRight[1], 0.0f + offsetsUpRight[0], circleSize, 20);
glFlush();
glutPostRedisplay();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
screenWidth = glutGet(GLUT_SCREEN_WIDTH);
screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
if (argc > 1) {
glutInitWindowSize(320, 320);
moveSpeed = moveSpeed / 10;
} else {
glutInitWindowSize(screenWidth, screenHeight);
}
glutCreateWindow("OpenGL Test");
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
glutInitWindowPosition(50, 50);
glutKeyboardFunc(readKeyboard);
glutSpecialFunc(readSpecialKeyboard);
glutSpecialUpFunc(readSpecialKeyboardUp);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
//end of code//