Here my code, everything is compiling, no errors, but it crash right away (with no errors code, just asking for debugging). If you find what is wrong with my code, let me know! Everything was working correctly with glbegin();…glend();. WHat i changed and added are located in GLHandleARB(for the vbo) and the code in renderScene(where i had the immediate mode drawing before).é
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include "textfile.h"
#include <math.h>
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;
float lastx, lasty;
GLuint v,f,f2,p;
GLfloat colors[]={1,1,1};
float lpos[4] = {1,0.5,1,0};
GLhandleARB InitShaders(const GLchar *p_vertex_shader_src,const GLchar *p_fragment_shader_src)
{
GLint l_size;
// create vertex-shader object
GLhandleARB l_vertex_shader_handle=glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
// assign source code
l_size=::strlen(p_vertex_shader_src);
glShaderSourceARB(l_vertex_shader_handle,1,&p_vertex_shader_src,&l_size);
// compile vertex shader
glCompileShaderARB(l_vertex_shader_handle);
// create fragment-shader object
GLhandleARB l_fragment_shader_handle=glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
// assign source code
l_size=::strlen(p_fragment_shader_src);
glShaderSourceARB(l_fragment_shader_handle,1,&p_fragment_shader_src,&l_size);
// compile fragment shader
glCompileShaderARB(l_fragment_shader_handle);
// create a GLSL program (a combination of vertex and fragment shader)
GLhandleARB l_program_handle=glCreateProgramObjectARB();
// attach the previously create vertex- and fragment-shaders
glAttachObjectARB(l_program_handle,l_vertex_shader_handle);
glAttachObjectARB(l_program_handle,l_fragment_shader_handle);
// link to get a functional GPU program
glLinkProgramARB(l_program_handle);
// delete vertex- and fragment-shader objects.
// They were only needed for setup.
glDeleteObjectARB(l_fragment_shader_handle);
glDeleteObjectARB(l_vertex_shader_handle);
return l_program_handle;
}
void changeSize(int w, int 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 = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
}
void camera (void) {
glRotatef(xrot,1.0,0.0,0.0); //rotate our camera on teh x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis (up and down)
glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
}
void renderScene(void) {
// activate transparency
glEnable(GL_BLEND);
// activate texturing. Not a must, since our shader overrides this settings anyway.
// But don't forget to bind your texture!
glEnable(GL_TEXTURE_2D);
// no depth test, particles should always be visible
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
// activate point-sprites
glEnable(GL_POINT_SPRITE_ARB);
// setup the desired particle size.
// this value should depend on your window-size.
glPointSize(10.0f);
// particle coordinates and colors,
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// tell OpenGL where the coordinates are.
glVertexPointer(4,GL_FLOAT,0,0);
// and here is the RGBA float color info.
glColorPointer(4,GL_FLOAT,0,0);
// activate the shaders
glUseProgramObjectARB(p);
// draw all particles at once
glDrawArrays(GL_POINTS,0,10);
// deactivate our shaders
glUseProgramObjectARB(0);
}
void keyboard (unsigned char key, int x, int y) {
if (key=='q')
{
xrot += 1;
if (xrot >360) xrot -= 360;
}
if (key=='z')
{
xrot -= 1;
if (xrot < -360) xrot += 360;
}
if (key=='w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) ;
zpos -= float(cos(yrotrad)) ;
ypos -= float(sin(xrotrad)) ;
}
if (key=='s')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad)) ;
ypos += float(sin(xrotrad));
}
if (key=='d')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad)) * 0.2;
zpos += float(sin(yrotrad)) * 0.2;
}
if (key=='a')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad)) * 0.2;
zpos -= float(sin(yrotrad)) * 0.2;
}
if (key==27)
{
exit(0);
}
}
void mouseMovement(int x, int y) {
int diffx=x-lastx; //check the difference between the current x and the last x position
int diffy=y-lasty; //check the difference between the current y and the last y position
lastx=x; //set lastx to the current x position
lasty=y; //set lasty to the current y position
xrot += (float) diffy; //set the xrot to xrot with the addition of the difference in the y position
yrot += (float) diffx; //set the xrot to yrot with the addition of the difference in the x position
}
//void processNormalKeys(unsigned char key, int x, int y) {
// if (key == 27)
// exit(0);
//}
void setShaders() {
char *vs = NULL,*fs = NULL,*fs2 = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
f2 = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead("shader.vert");
fs = textFileRead("shader.frag");
fs2 = textFileRead("toon2.frag");
const char * ff = fs;
const char * ff2 = fs2;
const char * vv = vs;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
glShaderSource(f2, 1, &ff2,NULL);
free(vs);free(fs);
glCompileShader(v);
glCompileShader(f);
glCompileShader(f2);
p = glCreateProgram();
glAttachShader(p,f);
glAttachShader(p,f2);
glAttachShader(p,v);
glLinkProgram(p);
glUseProgram(p);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200,200);
glutInitWindowSize(800,600);
glutCreateWindow("OpenGL");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
//glutKeyboardFunc(processNormalKeys);
glutReshapeFunc(changeSize);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
// glEnable(GL_CULL_FACE);
glewInit();
if (glewIsSupported("GL_VERSION_2_0"))
printf("Ready for OpenGL 2.0
");
else {
printf("OpenGL 2.0 not supported
");
exit(1);
}
setShaders();
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}