I wanna create a program with OpenGL in my PC. I use this code
#include "stdafx.h"
#include <GL/glut.h>
bool bFullsreen = false;
int nWindowID;
void display(void)
{
//Clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
//draw white polygon (rectangle) with corners at
// (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
// Don't wait start processing buffered OpenGL routines)
//glFlush();
glutSwapBuffers();
}
void init(void)
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);
//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void keyboardFunc(unsigned char key, int x, int y) {
switch (key) {
case 'f':
bFullsreen = !bFullsreen;
if (bFullsreen)
glutFullScreen();
else {
glutSetWindow(nWindowID);
glutPositionWindow(100, 100);
glutReshapeWindow(640, 480);
}
break;
}
}
void idleFunc(void) {
glutPostRedisplay();
}
int _tmain(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
nWindowID = glutCreateWindow("Hello World");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboardFunc);
glutIdleFunc(idleFunc);
glutMainLoop();
return 0;
}
when i run it in my PC it coesn't show anything and only show background. like this picture:
But when I switch to FullScreen mode, it shows correctly. I searched about it and I found changing GLUT_Single to GLUT_DOUBLE and glFlush() to glutSwapBuffers() but it doesn't work for me.
I have windows 10 with NVIDIA GForce 7300 LE. when I run my program in other PC it runs correctly. so
What is my problem? Is it about my graphic card or my code is incorrect? and
How can I fixed it?
Related
I have written this code for displaying the line segment but I am not able to understand why the line is not getting displayed. Can someone please help?
#include<GL/glut.h>
#include<iostream>
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void line_segment()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE);
glVertex2i(180, 15);
glVertex2i(10, 145);
glEnd();
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
// optional
glutInitWindowSize(400, 300);
glutInitWindowPosition(50, 100);
// Till here
glutCreateWindow("Window.....");
init();
glutDisplayFunc(line_segment);
// without infinite loop window onl displayed for a very short time
glutMainLoop();
}
OUTPUT
GL_LINE is not a valid line primitive type. GL_LINE is a polygon mode (see glPolygonMode). The primitive type you want to use is GL_LINES:
glBegin(GL_LINE);
glBegin(GL_LINES);
I am developing a project and after I added menu for changing color,the object or shape in window changes color but keep moving other way or out of range every time an item in menu selected such as color. I also tried changing GLUT_DOUBLE to GLUT_SINGLE but still no luck.
#include "stdafx.h"
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4 //white colour for sphere
float red =1.0,green =1.0,blue=0.0,white = 0.0;
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glFlush();
}
void reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}
void mouse(int option){
switch(option){
case RED:
red = 1.0,green = 0.0,blue = 0.0;
break;
case GREEN:
red = 0.0,green = 1.0,blue = 0.0;
break;
case BLUE:
red = 0.0,green = 0.0,blue = 1.0;
break;
case WHITE:
red = 1.0,green = 1.0,blue = 1.0;
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (700, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Testing");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
int sphere = glutCreateMenu(mouse); //change colour of pacman
glutAddMenuEntry("Red",RED);
glutAddMenuEntry("Green",GREEN);
glutAddMenuEntry("Blue",BLUE);
glutAddMenuEntry("White",WHITE);
glutCreateMenu(mouse);
glutAddSubMenu("Pacman",sphere);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init ();
glutMainLoop();
return 0;
}
You have to do one glPopMatrix for each glPushMatrix. Ther is missing 1 glPopMatrix in the function display:
void display(void){
// Sphere
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(red,green,blue);
glPushMatrix();
glTranslatef (-3.5, -1.5, 0.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (3.0, 3.0, 0.0);
glutSolidSphere(0.4,40,40);
glPopMatrix();
//Cone
glColor3f(0.0,0.0,0.0);
glTranslatef (0.95,-0.2, 0.0);
glPushMatrix();
glRotated(300,1.0,4.0,1.0);
glutSolidCone(0.6, 0.9, 30, 30);
glPopMatrix();
//Sphere(eye)
glColor3f(0.0,0.0,0.0);
glTranslatef (-0.75,0.8, 0.0);
glPushMatrix();
glScalef(0.6,0.6,0.0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
glPopMatrix(); // <--------------------- this is missing
glFlush();
}
Since the pop is missing, the translation of the first rendering is kept on the top of the matrix stack. When the rendering is done a 2nd time with the new color, translation is applied again. This causes a progressive change of position.
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.
#include <Windows.h>
#include <GL\glut.h>
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
bool init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glMatrixMode(GL_PROJECTION);
// Set grid to be from 0 to 1
gluOrtho2D(0.0, 3.0, 0.0, 3.0);
return true;
}
void drawline(float from_x, float from_y, float to_x, float to_y)
{
// From coordinate position
glVertex2f(from_x, from_y);
// To coordinate position
glVertex2f(to_x, to_y);
}
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
glLineWidth(2.0); // Set line width to 2.0
glLoadIdentity();
// Draw line
glBegin(GL_LINES);
drawline(0.25, 0.5, 0.4, 0.5);
drawline(0.4, 0.6, 0.4, 0.5);
drawline(0.4, 0.4, 0.4, 0.5);
drawline(0.6, 0.5, 0.75, 0.5);
glEnd();
// Draw triangle
glBegin(GL_TRIANGLES);
glVertex2f(0.4, 0.5);
glVertex2f(0.6, 0.6);
glVertex2f(0.6, 0.4);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("My OpenGL program");
init();
// Draw shape one
glPushMatrix();
glTranslatef(1.5, 1.5, 0.0);
glutDisplayFunc(render);
glPopMatrix();
// Draw shape two
glPushMatrix();
glTranslatef(2.5, 2.5, 0.0);
glutDisplayFunc(render);
glPopMatrix();
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
I'm working to draw something like this: http://i.imgur.com/JJpbJ7M.png
I don't need the whole thing, I just want to be able to draw two shapes where I want them to be. However, this isn't working, can anyone point me into the right direction?
You almost had it.
Draw your shape(s) in render(), not main():
#include <GL/glut.h>
void drawline(float from_x, float from_y, float to_x, float to_y)
{
// From coordinate position
glVertex2f(from_x, from_y);
// To coordinate position
glVertex2f(to_x, to_y);
}
void drawShape()
{
glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
glLineWidth(2.0); // Set line width to 2.0
// Draw line
glBegin(GL_LINES);
drawline(0.25, 0.5, 0.4, 0.5);
drawline(0.4, 0.6, 0.4, 0.5);
drawline(0.4, 0.4, 0.4, 0.5);
drawline(0.6, 0.5, 0.75, 0.5);
glEnd();
// Draw triangle
glBegin(GL_TRIANGLES);
glVertex2f(0.4, 0.5);
glVertex2f(0.6, 0.6);
glVertex2f(0.6, 0.4);
glEnd();
}
void render(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, 4.0, 0.0, 4.0, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Draw shape one
glPushMatrix();
glTranslatef(1.5, 1.5, 0.0);
drawShape();
glPopMatrix();
// Draw shape two
glPushMatrix();
glTranslatef(2.5, 2.5, 0.0);
drawShape();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("My OpenGL program");
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
I removed reshape() and init() since the default glutReshapeFunc() already calls glViewport() and you should be resetting your projection and modelview matrices each frame anyway.
I read the "Red book"(OpenGL Programming Guide) and tried the first program in the book under windows 7 with VS2010.
The result can appear normally, but when the program returns, it crashes with the following information:
Unhandled exception at 0x6992c660 in first.exe: 0xC0000005: Access violation.
I have tried some other programs, which issued the same problem.
The following is the code:
#include <stdio.h>
#include <gl/glut.h>
void display(void)
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT);
/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
*/
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush();
}
void init(void)
{
/* select clearing (background) color */
glClearColor(0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("hello");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}