Related
As the title says I'm tyring to model a simple giraffe out of arraycubes in open GL wiht C++, now I got the concepts done, but ran into an issue, when I start on the neck for some reaosn I lose 5 out of the 6 faces of my cube, the example I'm following doesn't result in this. I linked a small video below to show the visual result and I'm wondering what might be causing this. If there's an easier way to go about this as well please do let me know.
Visual Result
Code Sample
#include <glut.h>
float angle[4];
GLfloat corners[8][3] = { {-0.5,0.5,-0.5},{0.5,0.5,-0.5},
{0.5,-0.5,-0.5},{-0.5,-0.5,-0.5},
{-0.5,0.5,0.5},{0.5,0.5,0.5},
{0.5,-0.5,0.5},{-0.5,-0.5,0.5} };
void drawFace(int a, int b, int c, int d) {
glBegin(GL_POLYGON);
glVertex3fv(corners[a]);
glVertex3fv(corners[b]);
glVertex3fv(corners[c]);
glVertex3fv(corners[d]);
glEnd();
}
void ArrayCube() {
glColor3f(1.0, 1.0, 1.0);
drawFace(0, 3, 2, 1);
glColor3f(1.0, 1.0, 1.0);
drawFace(3, 0, 4, 7);
glColor3f(1.0, 1.0, 1.0);
drawFace(2, 3, 7, 6);
glColor3f(1.0, 1.0, 1.0);
drawFace(1, 2, 6, 5);
glColor3f(1.0, 1.0, 1.0);
drawFace(4, 5, 6, 7);
glColor3f(1.0, 1.0, 1.0);
drawFace(5, 4, 0, 1);
}
void LowerNeck()
{
glPushMatrix();
glTranslatef(0.5, 0.25, -0.125);
glScalef(0.0, 0.5, 0.25);
ArrayCube();
glPopMatrix();
}
void MainBody()
{
glPushMatrix();
glScalef(1.25, 0.25, 0.5);
ArrayCube();
glPopMatrix();
}
void DrawGiraffe()
{
glRotatef(angle[0], 0.0, 1.0, 0.0);
MainBody();
LowerNeck();
}
void rotate() {
angle[0] += 1.0;
if (angle[0] > 360) angle[0] -= 360;
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.6, 0.6, 0.6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
DrawGiraffe();
glutSwapBuffers();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Basic 3D");
glutDisplayFunc(display);
init();
glutIdleFunc(rotate);
glutMainLoop();
}
For the second object (the neck) you apply a scale transformation on x that scales the x component of all the following drawn vertices to 0.0:
glScalef(0.0, 0.5, 0.25);
That 0.0 should've probably been a 1.0.
That's the reason you only see one quad in the render video: That's the quad/face (actually two faces) which still have a dimension in Y and Z. The faces that have a dimension on x are squished to degenerate quads and not displayed at all.
Currently creating a small animation of Neptune and its moon's revolving around the sun. With some help I managed to get a background of stars but instead of being white, they're now black. I've tried putting glColor3f(1.0, 1.0, 1.0) inside and outside of the matrix consisting the background and none of it seems to be working. Any solutions?
Background declaration: Display()
Background call: End of Display()
int triton = 0;
int proteus = 0;
int neptune = 0;
int sun = 0;
GLint buf, sbuf;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
// Kind of Useless rn
glGetIntegerv(GL_SAMPLE_BUFFERS, &buf);
printf("Number of sample Buffers: %d\n", buf);
glGetIntegerv(GL_SAMPLES, &sbuf);
printf("Number of samples: %d\n", sbuf);
printf("Controls: \n A = Orbit Left. \n D = Orbit Right. \n S = Stop. \n (,) = Move Left. \n (.) = Move right.");
glShadeModel(GL_SMOOTH);
// Material Specs
GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
GLfloat mat_shininess[] = { 40.0 };
GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };
// Light 0 Initialized.
GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
// Spotlight (Sun)
// Mat Specs Implmentations.
glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// Light 0 implementation
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0);
// Ambient surrounding light on object.
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// Antialias 3D Shape (In Progress)
/*glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);*/
// Enable Lighting and Depth
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void orbit(void)
{
triton = (triton - 2) % 360;
proteus = (proteus - 5) % 360;
neptune = (neptune - 1) % 360;
glutPostRedisplay();
}
void backorbit(void)
{
triton = (triton + 2) % 360;
proteus = (proteus + 5) % 360;
neptune = (neptune + 1) % 360;
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glNewList(1, GL_COMPILE);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < 300; i++)
{
for (int j = 0; j < 300; j++)
{
if (((i + j) % 2) == 0)
{
glVertex2f(30 * i, 30 * j);
}
}
}
glEnd();
glEndList();
// Sun
glPushMatrix();
glColor3f(1.0, 0.35, 0.1);
glTranslatef(0.0, 0.0, 0.0);
glutSolidSphere(2.0, 100, 100);
// Neptune
glPushMatrix();
glRotatef((GLfloat)neptune, 0.0, 1.0, 0.0);
glTranslatef(3.0, 0.0, 0.0);
glColor3f(0.1, 0.1, 0.3);
glutSolidSphere(0.3, 100, 100);
// Triton
glPushMatrix();
glColor3f(0.85, 0.7, 0.8);
glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glutSolidSphere(0.05, 100, 100);
glPopMatrix(); // Ends Triton
// Proteus
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glRotatef((GLfloat)proteus, 0.0, 1.0, 0.0);
glTranslatef(1.0, 0.0, 0.0);
glutSolidSphere(0.02, 100, 100);
glPopMatrix(); // Ends Proteus
glPopMatrix(); // Ends Neptune
glPopMatrix(); // Ends Sun
glEnable(GL_MULTISAMPLE);
// Stars
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 1000, 0, 1000);
glColor3f(1.0, 1.0, 1.0);
glCallList(1);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix(); // Ends Solar System
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
// Triton + Proteus Orbit.
case 'a':
glutIdleFunc(orbit);
break;
case 'd':
glutIdleFunc(backorbit);
break;
case 's':
glutIdleFunc(NULL);
break;
case ',':
glTranslatef(-0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case '.':
glTranslatef(0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Neptune and Space");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
I'm trying to illustrate orbits of Triton and Proteus around Neptune with a background of stars. I decided to sketch a template background by multisampling an array of white dots ( stars ) as the background. Could someone explain why my array isn't displaying?
*Background is called in Display() created in Init().
Full Code here:
int triton = 0;
int proteus = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
// Material Specs
GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
GLfloat mat_shininess[] = { 40.0 };
GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };
// Light 0 Initialized.
GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
// Mat Specs Implmentations.
glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// Light 0 implementation
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0);
// Ambient surrounding light on object.
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// Enable Lighting and Depth
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
// Background (Stars: In Progress)
glNewList(1, GL_COMPILE);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 1000; j++)
{
if (((i + j) % 2) == 0)
{
glVertex2f(2*i, 2*j);
}
}
}
glEnd();
glEndList();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
// Neptune
glColor3f(0.1, 0.1, 0.3);
glutSolidSphere(2.0, 100, 100);
// Triton
glPushMatrix();
glColor3f(0.9, 0.7, 0.8);
glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
glTranslatef(2.5, 0.0, 0.0);
glRotatef((GLfloat)triton, 1.0, 0.0, 0.0);
glutSolidSphere(0.35, 100, 100);
glPopMatrix();
// Proteus
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glRotatef((GLfloat)proteus, 0.7, -0.4, 1.0);
glTranslatef(3.5, 0.0, 0.0);
glRotatef((GLfloat)proteus, 1.0, 0.0, 0.0);
glutSolidSphere(0.1, 100, 100);
glPopMatrix();
// Stars Background
glEnable(GL_MULTISAMPLE);
glPushMatrix();
glCallList(1); // Not Coding
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
// Triton + Proteus Orbit.
case 'a':
triton = (triton - 2) % 360;
proteus = (proteus - 5) % 360;
glutPostRedisplay();
break;
case 'd':
triton = (triton + 2) % 360;
proteus = (proteus + 5) % 360;
glutPostRedisplay();
break;
case ',':
glTranslatef(- 0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case '.':
glTranslatef(0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Neptune and Space");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
You you draw the start, then the perspective projection matrix and the view matrix are stills set. The "stars" are not on the viewport.
Use a scale of about 1/500 to put the points into clip space:
glVertex2f(2*i / 500.0f, 2*j / 500.0f);
Or setup an orthographic projection, before you draw the stars:
// Stars Background
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 1000, 0, 1000 );
glCallList(1);
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
So I am still a newbie with opengl and I used this tutorial as a reference
https://www.youtube.com/watch?v=8p76pJsUP44
I am now trying to draw a set of 4 triangles that made up a square (it's my project assignment) and this is my code
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
using namespace std;
void changeViewPort(int w, int h)
{
glViewport(0, 0, w, h);
}
void draw() {
glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 255.0);
glVertex3f(0.0, 0.0, 0.0); // top triangle
glVertex3f(-1.0,1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glColor3f(50.0, 50.0, 255.0);
glVertex3f(0.0, 0.0, 0.0); // right triangle
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glColor3f(75.0, 75.0, 255.0);
glVertex3f(0.0, 0.0, 0.0);//bottom triangle
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(50.0, 50.0, 255.0);
glVertex3f(0.0, 0.0, 0.0); //left triangle
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
glutSwapBuffers();
}
int main(int argc, char* argv[]) {
// Initialize GLUT
glutInit(&argc, argv);
// Set up some memory buffers for our display
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// Set the window size
glutInitWindowSize(350, 300);
// Create the window with the title "Hello,GL"
glutCreateWindow("Hello, GL");
// Bind the two functions (above) to respond when necessary
glutReshapeFunc(changeViewPort);
glutDisplayFunc(render);
//glutDisplayFunc(draw);
// Very important! This initializes the entry points in the OpenGL driver so we can
// call all the functions in the API.
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW error");
return 1;
}
glutMainLoop();
return 0;
}
notice that in my draw function, I am just trying to create 4 different triangles with different shades of blue.
However, whenever this code is executed, only the top triangle is blue while the rest stays white
Does anyone know why this wouldnt work ? I even tried to create a for loop version of the draw function but it also didnt work
void draw() {
double x = 1.0;
double y = 1.0;
double coords[8] = { -x,y,x,y,x,-y,-x,-y };
for (int i = 0; i < 7; i=i+2) { // i = [0,2,4,6]
switch (i) {
case 0:
glColor3f(0, 0, 255);
break;
case 2:
glColor3f(50, 50, 255);
break;
case 4:
glColor3f(75, 75, 255);
break;
case 6:
glColor3f(50, 50, 255);
break;
}
if (i == 6) { //4th and 1st coordinate
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(coords[i], coords[i + 1], 0.0);
glVertex3f(coords[0], coords[1], 0.0);
glEnd();
}
else {
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(coords[i], coords[i + 1], 0.0);
glVertex3f(coords[i + 2], coords[i + 3], 0.0);
glEnd();
}
}
}
glColor3f takes color components in the [0, 1] range as input and not [0, 255] as currently used.
Colors in OpenGL are stored in [0, 1] range. Unsigned ints map onto that range linearly automatically, but floats are just straight up stored.
See https://msdn.microsoft.com/en-us/library/windows/desktop/dd318399(v=vs.85).aspx
I've written a simple opengl running in my ubuntu laptop. It is a small solar system including the sun and the earth, the earth rotates around the sun. The problem with my program is the screen keep blinking continuously every time I try to run it.
#include <GL/glut.h>
#define SUN_RADIUS 0.4
#define EARTH_RADIUS 0.06
#define MOON_RADIUS 0.016
GLfloat EARTH_ORBIT_RADIUS = 0.9;
GLfloat year = 0.0;
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void renderScene() {
gluLookAt(
0.0, 0.0, -4.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
glColor3f(1.0, 1.0, 0.7);
glutWireSphere(SUN_RADIUS, 50, 50);
glPushMatrix();
glRotatef(year, 0.0, 1.0, 0.0);
glTranslatef(EARTH_ORBIT_RADIUS, 0.0, 0.0);
glColor3f(0.0, 0.7, 1.0);
glutWireSphere(EARTH_RADIUS, 10, 10);
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
renderScene();
glFlush();
glutSwapBuffers();
}
void idle() {
year += 0.2;
display();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Solar System");
init();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
}
gluLookAt() multiplies by the current matrix, it does not load a new one. Multiple gluLookAt()s multiplied together aren't very meaningful.
Reload proj/modelview matrices each frame, helps prevent matrix oddities.
Let GLUT do it's job, don't call display() from idle(), use glutPostRedisplay() instead. That way GLUT knows to call display() the next time through the event loop.
All together:
#include <GL/glut.h>
#define SUN_RADIUS 0.4
#define EARTH_RADIUS 0.06
#define MOON_RADIUS 0.016
GLfloat EARTH_ORBIT_RADIUS = 0.9;
GLfloat year = 0.0;
void renderScene()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -1, 1, -1, 1, -100, 100 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
0.0, 0.0, -4.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
glColor3f(1.0, 1.0, 0.7);
glutWireSphere(SUN_RADIUS, 50, 50);
glPushMatrix();
glRotatef(year, 0.0, 1.0, 0.0);
glTranslatef(EARTH_ORBIT_RADIUS, 0.0, 0.0);
glColor3f(0.0, 0.7, 1.0);
glutWireSphere(EARTH_RADIUS, 10, 10);
glPopMatrix();
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(10.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderScene();
glutSwapBuffers();
}
void idle()
{
year += 0.2;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Solar System");
glutDisplayFunc(display);
glutIdleFunc( idle );
glutMainLoop();
}
This may be due to tearing if you aren't doing any kind of v-sync (Which it doesn't look like your code is). Try adding a sleep time to your display method (like sleep(500)). This isn't the correct way to fix this, but this will allow you to verify that it is the issue. If it is, look into adding v-sync to your application.