I am working on making a tower defense game in C++ using OpenGL. When trying to utilize the "sleep" function from Windows.H it causes my OpenGL window to open the total time of whatever value I put into Sleep and then opens the window. Any suggestions?
#include <GLFW/glfw3.h>
#include <iostream>
#include <Windows.h>
void enemy(int num) //Written By Ryan
{
switch (num)
{
case 1: glBegin(GL_TRIANGLES); //Position 3
glColor3ub(255, 0, 0);
glVertex2f(0.25, 0.9);
glVertex2f(0.1, 0.6);
glVertex2f(0.4, 0.6);
glEnd();
break;
case 2: glBegin(GL_TRIANGLES); //Position 6
glColor3ub(255, 0, 0);
glVertex2f(0.25, 0.4);
glVertex2f(0.1, 0.1);
glVertex2f(0.4, 0.1);
glEnd();
break;
case 3: glBegin(GL_TRIANGLES); //Position 7
glColor3ub(255, 0, 0);
glVertex2f(-0.25, 0.4);
glVertex2f(-0.4, 0.1);
glVertex2f(-0.1, 0.1);
glEnd();
break;
case 4: glBegin(GL_TRIANGLES); //Position 10
glColor3ub(255, 0, 0);
glVertex2f(-0.25, -0.1);
glVertex2f(-0.4, -0.4);
glVertex2f(-0.1, -0.4);
glEnd();
break;
case 5: glBegin(GL_TRIANGLES); //Position 14
glColor3ub(255, 0, 0);
glVertex2f(-0.25, -0.6);
glVertex2f(-0.4, -0.9);
glVertex2f(-0.1, -0.9);
glEnd();
break;
}
}
void map()
{
glColor3ub(0, 128, 0); // set color green
//Position 1
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f); //top left
glVertex3f(-1.0f, 0.5f, 0.0f);//bottom left
glVertex3f(-0.5f, 0.5f, 0.0f); //bottom right
glVertex3f(-0.5f, 1.0f, 0.0f); //top right
glEnd();
//Position 2
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f); //top left
glVertex3f(-0.5f, 0.5f, 0.0f);//bottom left
glVertex3f(0.0f, 0.5f, 0.0f); //bottom right
glVertex3f(0.0f, 1.0f, 0.0f); //top right
glEnd();
//Position 3
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(0.0f, 1.0f, 0.0f); //top left
glVertex3f(0.0f, 0.5f, 0.0f);//bottom left
glVertex3f(0.5f, 0.5f, 0.0f); //bottom right
glVertex3f(0.5f, 1.0f, 0.0f); //top right
glEnd();
//Position 4
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.5f, 1.0f, 0.0f); //top left
glVertex3f(0.5f, 0.5f, 0.0f);//bottom left
glVertex3f(1.0f, 0.5f, 0.0f); //bottom right
glVertex3f(1.0f, 1.0f, 0.0f); //top right
glEnd();
//Position 5
glBegin(GL_QUADS);
glVertex3f(-1.0f, .5f, 0.0f); //top left
glVertex3f(-1.0f, 0.0f, 0.0f);//bottom left
glVertex3f(-0.5f, 0.0f, 0.0f); //bottom right
glVertex3f(-0.5f, 0.5f, 0.0f); //top right
glEnd();
//Position 6
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.5f, 0.0f); //top left
glVertex3f(-0.5f, 0.0f, 0.0f);//bottom left
glVertex3f(0.0f, 0.0f, 0.0f); //bottom right
glVertex3f(0.0f, 0.5f, 0.0f); //top right
glEnd();
//Position 7
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.5f, 0.0f); //top left
glVertex3f(0.0f, 0.0f, 0.0f);//bottom left
glVertex3f(0.5f, 0.0f, 0.0f); //bottom right
glVertex3f(0.5f, 0.5f, 0.0f); //top right
glEnd();
//Position 8
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.5f, 0.5f, 0.0f); //top left
glVertex3f(0.5f, 0.0f, 0.0f);//bottom left
glVertex3f(1.0f, 0.0f, 0.0f); //bottom right
glVertex3f(1.0f, 0.5f, 0.0f); //top right
glEnd();
//Position 9
glBegin(GL_QUADS);
glVertex3f(-1.0f, 0.0f, 0.0f); //top left
glVertex3f(-1.0f, -0.5f, 0.0f);//bottom left
glVertex3f(-0.5f, -0.5f, 0.0f); //bottom right
glVertex3f(-0.5f, 0.0f, 0.0f); //top right
glEnd();
//Position 10
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(-0.5f, 0.0f, 0.0f); //top left
glVertex3f(-0.5f, -0.5f, 0.0f);//bottom left
glVertex3f(0.0f, -0.5f, 0.0f); //bottom right
glVertex3f(0.0f, 0.0f, 0.0f); //top right
glEnd();
//Position 11
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f); //top left
glVertex3f(0.0f, -0.5f, 0.0f);//bottom left
glVertex3f(0.5f, -0.5f, 0.0f); //bottom right
glVertex3f(0.5f, 0.0f, 0.0f); //top right
glEnd();
//Position 12
glBegin(GL_QUADS);
glVertex3f(0.5f, 0.0f, 0.0f); //top left
glVertex3f(0.5f, -0.5f, 0.0f);//bottom left
glVertex3f(1.0f, -0.5f, 0.0f); //bottom right
glVertex3f(1.0f, 0.0f, 0.0f); //top right
glEnd();
//Position 13
glBegin(GL_QUADS);
glVertex3f(-1.0f, -0.5f, 0.0f); //top left
glVertex3f(-1.0f, -1.0f, 0.0f);//bottom left
glVertex3f(-0.5f, -1.0f, 0.0f); //bottom right
glVertex3f(-0.5f, -0.5f, 0.0f); //top right
glEnd();
//Position 14
glColor3ub(139, 69, 19); // set color brown
glBegin(GL_QUADS);
glVertex3f(-0.5f, -0.5f, 0.0f); //top left
glVertex3f(-0.5f, -1.0f, 0.0f);//bottom left
glVertex3f(-0.0f, -1.0f, 0.0f); //bottom right
glVertex3f(-0.0f, -0.5f, 0.0f); //top right
glEnd();
//Position 15
glColor3ub(0, 128, 0); // set color green
glBegin(GL_QUADS);
glVertex3f(0.0f, -0.5f, 0.0f); //top left
glVertex3f(0.0f, -1.0f, 0.0f);//bottom left
glVertex3f(0.5f, -1.0f, 0.0f); //bottom right
glVertex3f(0.5f, -0.5f, 0.0f); //top right
glEnd();
//Position 16
glBegin(GL_QUADS);
glVertex3f(0.5f, -0.5f, 0.0f); //top left
glVertex3f(0.5f, -1.0f, 0.0f);//bottom left
glVertex3f(1.0f, -1.0f, 0.0f); //bottom right
glVertex3f(1.0f, -0.5f, 0.0f); //top right
glEnd();
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(1000, 1000, "Aloha Tower Defence", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
std::cout << "Error!" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
//'The' Loop
for (int x = 0; x <= 5; x++)
{
// glClear(GL_COLOR_BUFFER_BIT);
map();
enemy(x);
Sleep(100);
}
// glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
map();
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
So above causes the window to sit and do nothing for 100 miliseconds (*5 because of the loop value) and then it opens the window and displays Enemy at x value 5. I want to see the enemy "move" down the board from x values 1-5
You have to use the application loop. Add a variable x and increment the variable in the loop:
int x = 0;
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
//'The' Loop
glClear(GL_COLOR_BUFFER_BIT);
map();
enemy(x);
if (x < 5)
x += 1;
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
Sleep(100);
}
Instead of Sleep you can use glfwGetTime, which retrieves the time in seconds, since glfw was initialized:
int x = 0;
double startTime = glfwGetTime();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
double currentTime = glfwGetTime();
/* Render here */
//'The' Loop
glClear(GL_COLOR_BUFFER_BIT);
map();
enemy(x);
if (x < 5 && (currentTime - startTime) > x * 0.1)
x += 1;
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
Related
I'm trying to move the pyramid and cubic "separately" to front-back , up-down and right-left using arrows and page-up, page-down keys but I couldn't make it yet , none of the tow shape are moving my 2 questions are : how can i chose witch one of these shapes i want to move , and why none of them are moving ?
Thanks.
#include <GL/glut.h>
float xmove = 0, ymove = 0, zmove = 0;
float degree = 0;
float xscale = 1, yscale = 1, zscale = 1;
void right(void)
{
glLoadIdentity();//koordinat sistemimizetkilenmesin
xmove += 0.1;
}
void left(void)
{
glLoadIdentity();
xmove -= 0.1;
}
void up(void)
{
glLoadIdentity();
ymove += 0.1;
}
void down(void)
{
glLoadIdentity();
ymove -= 0.1;
}
void front(void)
{
glLoadIdentity();
zmove -= 0.1;
}
void back(void)
{
glLoadIdentity();
zmove += 0.1;
}
void keyboard(int button, int x, int y)
{
switch (button)
{
case GLUT_KEY_LEFT:left(); break;
case GLUT_KEY_RIGHT:right(); break;
case GLUT_KEY_UP:up(); break;
case GLUT_KEY_DOWN:down(); break;
case GLUT_KEY_PAGE_UP:front(); break;
case GLUT_KEY_PAGE_DOWN:back(); break;
glutPostRedisplay();
}
}
void Settings()
{
glClearColor(1,1,1,0);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST);
}
void polygons(void)
{
glColor3f(1.0, 0.0, 0.0);
glRotatef(degree, 0.0, 1.0, 0.0);
glScalef(xscale, yscale, zscale);
glTranslatef(1.5, 0.0, -8.0);
glBegin(GL_TRIANGLES);//pyramid
// fronat face
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
// down face
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
//back
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
//left
glColor3f(1.0f, 0.0f, 0.0f); //reed
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glLoadIdentity();
glTranslatef(-1.5, 0.0, -8.0);
glBegin(GL_QUADS); //cubic
glColor3f(1.0f, 0.0f, 1.0f); //pink
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
//down face
glColor3f(1.0f, 0.5f, 0.0f); //orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
//front face
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
//back
glColor3f(1.0f, 1.0f, 0.0f); //yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
//left
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
//right
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
}
void DrawingFunction()//painting
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
polygons();
glutSwapBuffers();
}
void AppearanceAdjustment(int x, int y)
{
int aspect = x / y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,aspect,0.1,20);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 600);
glutCreateWindow("3d shapes");
glutDisplayFunc(DrawingFunction);
glutReshapeFunc(AppearanceAdjustment);
glutSpecialFunc(keyboard);
Settings();
glutMainLoop();
return 0;
}
glLoadIdentity() has to be called in DrawingFunction:
void DrawingFunction()//painting
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // <----
gluLookAt(0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
polygons();
glutSwapBuffers();
}
Add an array of offsets and an object number:
int object = 0;
float xmove[]{ 0, 0 };
float ymove[]{ 0, 0 };
float zmove[]{ 0, 0 };
Add object selection buttons and change the offset of the selected object.
glutPostRedisplay() has to be executed after the switch statement:
void keyboard(int button, int x, int y)
{
switch (button)
{
case GLUT_KEY_F1: object = 0; break;
case GLUT_KEY_F2: object = 1; break;
case GLUT_KEY_LEFT: xmove[object] -= 0.1; break;
case GLUT_KEY_RIGHT: xmove[object] += 0.1; break;
case GLUT_KEY_DOWN: ymove[object] -= 0.1; break;
case GLUT_KEY_UP: ymove[object] += 0.1; break;
case GLUT_KEY_PAGE_UP: zmove[object] -= 0.1; break;
case GLUT_KEY_PAGE_DOWN: zmove[object] += 0.1; break;
}
glutPostRedisplay();
}
You have to translate the objects object by glTranslatef(xmove[i], ymove[i], zmove[i]). I recommend to use glPushMatrix/glPopMatrix:
void polygons(void)
{
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glRotatef(degree, 0.0, 1.0, 0.0);
glScalef(xscale, yscale, zscale);
glTranslatef(1.5, 0.0, -8.0);
glTranslatef(xmove[0], ymove[0], zmove[0]); // <--- translate pyramid
// draw pyramid
glBegin(GL_TRIANGLES);//pyramid
// [...]
glEnd();
glTranslatef(-1.5, 0.0, -8.0);
glTranslatef(xmove[1], ymove[1], zmove[1]); // <--- translate cube
glBegin(GL_QUADS); //cubic
// [...]
glEnd();
glPopMatrix();
}
Example code:
int object = 0;
float xmove[]{ 0, 0 };
float ymove[]{ 0, 0 };
float zmove[]{ 0, 0 };
float degree = 0;
float xscale = 1, yscale = 1, zscale = 1;
void keyboard(int button, int x, int y)
{
switch (button)
{
case GLUT_KEY_F1: object = 0; break;
case GLUT_KEY_F2: object = 1; break;
case GLUT_KEY_LEFT: xmove[object] -= 0.1; break;
case GLUT_KEY_RIGHT: xmove[object] += 0.1; break;
case GLUT_KEY_DOWN: ymove[object] -= 0.1; break;
case GLUT_KEY_UP: ymove[object] += 0.1; break;
case GLUT_KEY_PAGE_UP: zmove[object] -= 0.1; break;
case GLUT_KEY_PAGE_DOWN: zmove[object] += 0.1; break;
}
glutPostRedisplay();
}
void Settings()
{
glClearColor(1,1,1,0);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST);
}
void polygons(void)
{
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glRotatef(degree, 0.0, 1.0, 0.0);
glScalef(xscale, yscale, zscale);
glTranslatef(1.5, 0.0, -8.0);
glTranslatef(xmove[0], ymove[0], zmove[0]); // <--- translate pyramid
glBegin(GL_TRIANGLES);//pyramid
// fronat face
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
// down face
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
//back
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
//left
glColor3f(1.0f, 0.0f, 0.0f); //reed
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.5, 0.0, -8.0);
glTranslatef(xmove[1], ymove[1], zmove[1]); // <--- translate cube
glBegin(GL_QUADS); //cubic
glColor3f(1.0f, 0.0f, 1.0f); //pink
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
//down face
glColor3f(1.0f, 0.5f, 0.0f); //orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
//front face
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
//back
glColor3f(1.0f, 1.0f, 0.0f); //yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
//left
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
//right
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
glPopMatrix();
}
void DrawingFunction()//painting
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
polygons();
glutSwapBuffers();
}
void AppearanceAdjustment(int x, int y)
{
int aspect = x / y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,aspect,0.1,20);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 600);
glutCreateWindow("3d shapes");
glutDisplayFunc(DrawingFunction);
glutReshapeFunc(AppearanceAdjustment);
glutSpecialFunc(keyboard);
Settings();
glutMainLoop();
return 0;
}
You need to use a key press to pick one of the shapes similar to how you trigger the up, down, left, right ... Etc functions. Say press a for the first shape then set a global int ishape= 1 or ishape=2 if b is pressed. The draw function will then check the value of ishape to see which shape it should move. But really you need a vector of move for each shape else their displacements will be mixed up.Then you need to apply gltranslate on the shape that needs to be displaced using the move vector.
I'm trying to move these tow auto-rotating polygons to the Right-Left , Up-Down and Front-Back using these keys :
for the pyramid its the keys ('a','w','s','d','+','-')
for the cube its the keys ('←','↑','→','↓','PAGE UP','PAGE DOWN')
the polygons do actually move but they are moving in a weird way not as I wanted them to be moved especially when I try to move them Up-Down and Front-Back
I want to know why they moves like and what should I change to make them moves normal .
this is the code :
#include <GL\glew.h>
#include <SOIL.h>
#include <GL/glut.h>
float pyramid_x = 0, pyramid_y = 0, pyramid_z = 0;
float cube_x = 0, cube_y = 0, cube_z = 0;
float pyramid_angle = 0.0;
float cube_angle = 0.0;
int refresh = 10;//ms
float degree = 0;
void cube_right(void)
{
glLoadIdentity();
cube_x += 0.1;
}
void cube_left(void)
{
glLoadIdentity();
cube_x -= 0.1;
}
void cube_up(void)
{
glLoadIdentity();
cube_y += 0.1;
}
void cube_down(void)
{
glLoadIdentity();
cube_y -= 0.1;
}
void cube_front(void)
{
glLoadIdentity();
cube_z -= 0.1;
}
void cube_back(void)
{
glLoadIdentity();
cube_z += 0.1;
}
void pyramid_right(void)
{
glLoadIdentity();
pyramid_x += 0.1;
}
void pyramid_left(void)
{
glLoadIdentity();
pyramid_x -= 0.1;
}
void pyramid_up(void)
{
glLoadIdentity();
pyramid_y += 0.1;
}
void pyramid_down(void)
{
glLoadIdentity();
pyramid_y -= 0.1;
}
void pyramid_front(void)
{
glLoadIdentity();
pyramid_z -= 0.1;
}
void pyramid_back(void)
{
glLoadIdentity();
pyramid_z += 0.1;
}
void keyboard(int buttons, int x, int y)
{
switch (buttons)
{
case GLUT_KEY_LEFT:cube_left(); break;
case GLUT_KEY_RIGHT:cube_right(); break;
case GLUT_KEY_UP:cube_up(); break;
case GLUT_KEY_DOWN:cube_down(); break;
case GLUT_KEY_PAGE_UP:cube_front(); break;
case GLUT_KEY_PAGE_DOWN:cube_back(); break;
}
glutPostRedisplay();
}
void keyboard(unsigned char buttons, int x, int y)
{
switch (buttons)
{
case 'w':pyramid_front(); break;
case'a':pyramid_left(); break;
case's':pyramid_down(); break;
case'd':pyramid_right(); break;
case '+':pyramid_front(); break;
case'-':pyramid_back(); break;
}
glutPostRedisplay();
}
void settings()
{
glClearColor(1, 1, 1, 0);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void drawing_function()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //reset
glTranslatef(2.0f, 0.0f, -7.0f);
glRotatef(pyramid_angle, 1.0f, 0.0f, 0.0f);
glColor3f(1.0, 0.0, 0.0);
glTranslatef(pyramid_x, pyramid_y, pyramid_z);
glBegin(GL_TRIANGLES);
// front
glTexCoord3f(1, 0, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
// right
glTexCoord3f(0, 0, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
// back
glTexCoord3f(0, 1, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// left
glTexCoord3f(1, 1, 0);
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glLoadIdentity();//(Reset model-view matrix)
glTranslatef(-2.0f, 0.0f, -7.0f);
glRotatef(cube_angle, 1.0f, 0.0f, 0.0f);
glTranslatef(cube_x, cube_y, cube_z);
glBegin(GL_QUADS); //cube
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
glutSwapBuffers();
pyramid_angle += 1.0f;
cube_angle -= 0.2f;
}
void timer(int deger)
{
glutPostRedisplay();
glutTimerFunc(refresh, timer, 0);
}
void view_setting(GLsizei x, GLsizei y)
{
if (y == 0) y = 1;
GLfloat aspect = (GLfloat)x / (GLfloat)y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0f, aspect, 1.0f, 20.0f);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 600);
glutCreateWindow("3d");
glutDisplayFunc(drawing_function);
glutReshapeFunc(view_setting);
glutSpecialFunc(keyboard);
glutKeyboardFunc(keyboard);
settings();
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
Change the order of the transformations. Do the rotation then the translation:
glTranslatef(2.0f, 0.0f, -7.0f);
glTranslatef(pyramid_x, pyramid_y, pyramid_z);
glRotatef(pyramid_angle, 1.0f, 0.0f, 0.0f);
glTranslatef(-2.0f, 0.0f, -7.0f);
glTranslatef(cube_x, cube_y, cube_z);
glRotatef(cube_angle, 1.0f, 0.0f, 0.0f);
Matrix multiplication is not Commutative. Operations like glRotate and glTranslate setup a new matrix and multiply the current matrix by the new matrix. Therefore, the transformation that needs to be done first must be the last in code.
Further more there is a mistake in keyboard. When w is pressed the the pyramid has to move up rather than to move to the front:
case 'w':pyramid_front(); break;
case 'w':pyramid_up(); break;
I want to make my shapes to keep rotating by time while I can move them (Left-Right , Down-Up , Front-Back) using keyboard I added a function called timer() that should change the angles of the cube and pyramid by time.
The problem is the code keeps breaking at glutPostOverlayRedisplay(); function and it says :
Exception thrown at 0x10004813 (glut32.dll) in Opengl.exe: 0xC0000005:
Access violation reading location 0x00000020.
how can I fix this problem and why its happening ?
this is the code :
#include <GL/glut.h>
int object = 0;
float xLeftRight[]{ 0, 0 };
float yDownUp[]{ 0, 0 };
float zFrontBack[]{ 0, 0 };
float PyramidAngle = 0.0;
float CupeAngle = 0.0;
int t_refresh = 20;//ms
float degree = 0;
float xscale = 1, yscale = 1, zscale = 1;
void Keyboard(int buttons, int x, int y)
{
switch (buttons)
{
case GLUT_KEY_F1: object = 0; break;
case GLUT_KEY_F2: object = 1; break;
case GLUT_KEY_LEFT: xLeftRight[object] -= 0.1; break;
case GLUT_KEY_RIGHT: xLeftRight[object] += 0.1; break;
case GLUT_KEY_DOWN: yDownUp[object] -= 0.1; break;
case GLUT_KEY_UP: yDownUp[object] += 0.1; break;
case GLUT_KEY_PAGE_UP: zFrontBack[object] -= 0.1; break;
case GLUT_KEY_PAGE_DOWN: zFrontBack[object] += 0.1; break;
}
glutPostRedisplay();
}
void Settings()
{
glClearColor(1, 1, 1, 0);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void DrawingFunction()//painting
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //reset
glTranslatef(1.5f, 0.0f, -6.0f); // rotate left and back by time
glRotatef(PyramidAngle, 1.0f, 1.0f, 0.0f);
glColor3f(1.0, 0.0, 0.0);
glScalef(xscale, yscale, zscale);
glTranslatef(xLeftRight[0], yDownUp[0], zFrontBack[0]); //move the pyramid (if object = 0)
glBegin(GL_TRIANGLES);
// front
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
// right
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
// back
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// left
glColor3f(1.0f, 0.0f, 0.0f); //red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //green
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glLoadIdentity();//(Reset model-view matrix)
glTranslatef(-2.0f, 0.0f, -7.0f); // rotate the cube right and back by time
glRotatef(CupeAngle, 1.0f, 0.0f, 0.0f);
glTranslatef(xLeftRight[1], yDownUp[1], zFrontBack[1]); //move the cube (if object = 1)
glBegin(GL_QUADS); //cube
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
glutSwapBuffers();
PyramidAngle += 1.0f; //changing the angle
CupeAngle -= 0.2f; //changing the angle
glPopMatrix();
}
void timer(int value)
{
glutPostOverlayRedisplay();
glutTimerFunc(t_refresh, timer, 0);
}
void ViewSetting(int x, int y)
{
int aspect = x / y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspect, 0.1, 20);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(50, 50);
glutInitWindowSize(800, 600);
glutCreateWindow("3d shapes");
glutDisplayFunc(DrawingFunction);
glutReshapeFunc(ViewSetting);
glutSpecialFunc(Keyboard);
Settings();
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
glutPostOverlayRedisplay marks the overlay of the current window as needing to be redisplayed (See Overlay Management).
You have to use glutPostRedisplay:
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(t_refresh, timer, 0);
}
I am currently rendering a solar system of planets in my 3d space but every single planet is black even when light hits the sphere. The spheres are rendered last in my render function. Had the colors working when the spheres were being rendered on their own but now i've added my sky box and other quads all the spheres refuse to be colored.
#include "Scene.h"
float rotation;
float rotation2;
int direction;
int speed;
Scene::Scene(Input *in)
{
// Initialise variables
rotation = 20;
rotation2 = 0;
direction = 1;
speed = 5;
myTexture = 0;
skyBox = 0;
// Store pointer for input class
input = in;
// OpenGL settings
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.39f, 0.58f, 93.0f, 1.0f); // Cornflour Blue Background
glClearDepth(1.0f); // Depth Buffer Setup
glClearStencil(0); // Clear stencil buffer
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glEnable(GL_LIGHTING); // Enables Lighting
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
// Other OpenGL / render setting should be applied here.
myTexture = SOIL_load_OGL_texture
(
"gfx/neongrid.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
skyBox = SOIL_load_OGL_texture
(
"gfx/starField.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
camera = new Camera();
}
void Scene::update(float dt)
{
// Update camera position
camera->update(input, dt);
// Handle user input
if (input->isKeyDown(43))
{
direction = 1;
input->SetKeyUp(43);
}
else if (input->isKeyDown(45))
{
direction = -1;
input->SetKeyUp(45);
}
// Update scene related variables
rotation += speed * dt;
rotation2 += (speed *2) * dt;
if (input->isKeyDown('p') && WF == false)
{
WF = true;
input->SetKeyUp('p');
glPolygonMode(GL_FRONT, GL_LINE);
}
if (input->isKeyDown('p') && WF == true)
{
WF = false;
input->SetKeyUp('p');
glPolygonMode(GL_FRONT, GL_FILL);
}
// Calculate FPS for output
calculateFPS();
}
void Scene::render() {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(camera->getPosition().x, camera->getPosition().y, camera->getPosition().z,
camera->getLookAt().x, camera->getLookAt().y, camera->getLookAt().z,
camera->getUp().x, camera->getUp().y, camera->getUp().z);
glutWarpPointer(400, 300);
glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);
// Lighting
GLfloat Light_Ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat Light_Diffuse[] = { 9.0f, 9.0f, 9.0f, 1.0f };
GLfloat Light_Position[] = { 2.0f, 2.0f, 2.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, Light_Ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light_Diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, Light_Position);
glEnable(GL_LIGHT0);
glDisable(GL_DEPTH_TEST);
#pragma region skybox
glPushMatrix();
glTranslatef(camera->getPosition().x, camera->getPosition().y, camera->getPosition().z);
glBindTexture(GL_TEXTURE_2D, skyBox);
glBegin(GL_QUADS);
//Back
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
//front
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
//left
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
//top
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
//bottom
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
#pragma endregion
glEnable(GL_DEPTH_TEST);
glPopMatrix();
glPushMatrix();
#pragma region wall
glBindTexture(GL_TEXTURE_2D, myTexture);
glTranslatef(0.0, 0.0, 0.0);
glScalef(5.0f, 5.0f, 5.0f);
glBegin(GL_QUADS);
// first face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// second face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
// third face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
// fourth face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, myTexture);
glScalef(5.0f, 5.0f, 5.0f);
glBegin(GL_QUADS);
// first face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
// second face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
// third face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, -1.0f);
// fourth face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glEnd();
#pragma endregion
glPopMatrix();
glPushMatrix();
// Render sun
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, NULL);
glTranslatef(0.5, 0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPushMatrix(); // Push for default matrix
// Render Planet 1
glRotatef(rotation, 0.5, 0.5, 0);
glTranslatef(0, 0, 1);
glScalef(1, 1, 1);
glColor3f(0.0f, 2.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 10, 10);
glPopMatrix(); // Pop off stack back to sun matrix
glPushMatrix(); // Push for default matrix
// Render Planet 2
glRotatef(rotation2, 0, 1, 0);
glTranslatef(2, 0, 0);
glScalef(0.5, 0.5, 0.5);
glColor3f(0.0f, 0.0f, 1.0f);
gluSphere(gluNewQuadric(), 0.20, 5, 5);
glPushMatrix(); // Pop back to sun
// Render a moon around Planet 2
glRotatef((rotation*2.0), 0, 1, 0);
glTranslatef(1.5, 0, 0);
glScalef(0.3, 0.3, 0.3);
glColor3f(0.0f, 0.0f, 1.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPopMatrix(); // Pop to planet 2
glPushMatrix(); // Push for default matrix
// Render a SECOND moon around Planet 2
glRotatef((rotation * 2), 0, 0, 1);
glTranslatef(1.5, 0, 0);
glScalef(0.3, 0.3, 0.3);
glColor3f(0.0f, 0.0f, 1.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPopMatrix();
//glPopMatrix();
glPopMatrix(); // Go back to sun!
glPushMatrix();
glTranslatef(2, 2, 2);
glColor3f(1, 0, 0);
gluSphere(gluNewQuadric(), 0.5, 20, 20);
glPopMatrix();
// End render geometry --------------------------------------
// Render text, should be last object rendered.
renderTextOutput();
// Swap buffers, after all objects are rendered.
glutSwapBuffers();
}
// Handles the resize of the window. If the window changes size the perspective matrix requires re-calculation to match new window size.
void Scene::resize(int w, int h)
{
width = w;
height = h;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = (float)w / (float)h;
fov = 45.0f;
nearPlane = 0.1f;
farPlane = 100.0f;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(fov, ratio, nearPlane, farPlane);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
// Calculates FPS
void Scene::calculateFPS()
{
frame++;
time = glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > 1000) {
sprintf_s(fps, "FPS: %4.2f", frame*1000.0 / (time - timebase));
timebase = time;
frame = 0;
}
}
// Compiles standard output text including FPS and current mouse position.
void Scene::renderTextOutput()
{
// Render current mouse position and frames per second.
sprintf_s(mouseText, "Mouse: %i, %i", input->getMouseX(), input->getMouseY());
displayText(-1.f, 0.96f, 1.f, 0.f, 0.f, mouseText);
displayText(-1.f, 0.90f, 1.f, 0.f, 0.f, fps);
}
// Renders text to screen. Must be called last in render function (before swap buffers)
void Scene::displayText(float x, float y, float r, float g, float b, char* string) {
// Get Lenth of string
int j = strlen(string);
// Swap to 2D rendering
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, 5, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Orthographic lookAt (along the z-axis).
gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
// Set text colour and position.
glColor3f(r, g, b);
glRasterPos2f(x, y);
// Render text.
for (int i = 0; i < j; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, string[i]);
}
// Reset colour to white.
glColor3f(1.f, 1.f, 1.f);
// Swap back to 3D rendering.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, ((float)width/(float)height), nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
}
Updated sun code :
// Render sun
glTranslatef(0.5, 0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPushMatrix(); // Push for default matrix
// Render Planet 1
glRotatef(rotation, 0.5, 0.5, 0);
glTranslatef(0, 0, 1);
glScalef(1, 1, 1);
glColor3f(0.0f, 2.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 10, 10);
glPopMatrix(); // Pop off stack back to sun matrix
glPushMatrix(); // Push for default matrix
Found out i was missing a OpenGL setting
glEnable(GL_COLOR_MATERIAL);
Inserted at the start of Scene
Try to end glEnable(GL_TEXTURE_2D); with glDisable(GL_TEXTURE_2D); after you render sun.
If problem appeared after you added skybox. Check if vertices are in right order.
The following code draws a labyrinth or maze in OpenGL, the result is a 2D labyrinth, what I need to do now is to draw cubes instead these quads, how can I do it?
function drawmaze() {
int x,y,dl;
glNewList(dl=glGenLists(1),GL_COMPILE);
glPushAttrib(GL_TEXTURE_BIT | GL_LIGHTING_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
// glPushMatrix();
float i = 0;
for(y=0;y < mazedata.size();y++) {
for(x=0;x < mazedata[y].size();x++) {
bool dibujar = false;
if(wall(x,y)) {
glColor3ub(46,151,208);
drawable = true;
}
else
if (entry(x,y)) {
glColor3f(0.0f,0.184f,0.792f);
drawable = true;
}
else
if (mazexit(x,y)) {
glColor3f(0.811f,0.188f,0.176f);
drawable = true;
}
else
if (thing(x,y)) {
glColor3ub( 151, 204, 0 );
drawable = true;
}
else
if (visited(x,y)) {
glColor3ub( 66, 66, 66 );
drawable = true;
}
if (drawable) {
//glPushMatrix();
/*
This is a try
glTranslatef(1.0,0., 0.0f );
glutSolidCube(0.5);*/
//glPopMatrix();
glVertex3f(x+0.0f ,y+0.0f ,0.0f );
glVertex3f(x+0.0f ,y+1.0f ,0.0f );
glVertex3f(x+1.0f ,y+1.0f ,0.0f );
glVertex3f(x+1.0f ,y+0.0f ,0.0f );
// topside:
glVertex3f(x+0.0f ,y+0.0f ,1.0f );
glVertex3f(x+1.0f ,y+0.0f ,1.0f );
glVertex3f(x+1.0f ,y+1.0f ,1.0f );
glVertex3f(x+0.0f ,y+1.0f ,1.0f );
}
}
i++;
}
glEnd();
// glPopMatrix();
glPopAttrib();
glEndList();
return(dl);
}
glBegin(GL_QUADS);
// front
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// back
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
// right
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
// left
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// top
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
// bottom
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glEnd();
There is no native opengl method that will draw a cube.
So you will have to draw 6 quads to draw a cube.