Пытаюсь собрать в netbeans этот пример с помощью mingw. Пишет много ошибок вида
main.cpp:48: undefined reference to `_imp____glewCreateShader'
для всех функций glew, хотя в properties проекта я добавил libraries: freeglut.lib, opengl32.lib, glew32.lib. В чём может быть проблема?
Спасибо за ответы.
upd: на студии попытался добавить цвет вершинам, пишет ошибку при компиляции шейдера
main.cpp:
#include "GL/glew.h"
#ifdef _WIN32
#include "GL/wglew.h"
#else
#include "GL/glxew.h"
#endif
#include "GL/freeglut.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const int NUM_VERTICES = 3;
static const int VERTEX_SIZE = 6 * sizeof(float);
static const int VERTEX_POSITION_OFFSET = 0;
static const int VERTEX_COLOR_OFFSET = 3 * sizeof(float);
GLuint vbo = 0, vao = 0;
GLuint program = 0;
float scale = 0.3;
char *vertexSource = "";
char *fragmentSource = "";
static const float vertices[NUM_VERTICES * 6] =
{
-2.0f, -2.0f, -1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 2.0f, -1.0f, 0.0f, 1.0f, 0.0f,
2.0f, -2.0f, -1.0f, 0.0f, 0.0f, 1.0f,
};
GLuint loadShader(const char *source, GLenum type)
{
GLuint shader = glCreateShader(type);
GLint compileStatus;
glShaderSource(shader, 1, &source, NULL);
// compile the shader
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus == 0) // some error
{
printf("Error compiling shader.\n");
return 0;
}
return shader;
}
char *textFileRead(char *fn) {
FILE *fp;
char *content = NULL;
int count = 0;
if (fn != NULL) {
fp = fopen(fn, "rt");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content, sizeof(char), count, fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
GLuint createProgram()
{
GLuint program = glCreateProgram();
GLuint vs = loadShader(vertexSource, GL_VERTEX_SHADER);
GLuint fs = loadShader(fragmentSource, GL_FRAGMENT_SHADER);
GLint linked;
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked)
{
printf("Error linking program\n");
return 0;
}
return program;
}
bool setAttrPtr(GLuint program, const char *name, int numComponents, GLsizei stride, const GLvoid *ptr, GLenum type = GL_FLOAT, bool normalized = false)
{
int loc = glGetAttribLocation(program, name);
if (loc < 0)
return false;
glVertexAttribPointer(loc, numComponents, type, GL_FALSE,
stride, (const GLvoid *)ptr);
glEnableVertexAttribArray(loc);
return true;
}
bool setUniformFloat(GLuint program, const char *name, float value)
{
int loc = glGetUniformLocation(program, name);
if (loc < 0)
return false;
glUniform1f (loc, value);
return true;
}
void init()
{
glClearColor(0.5, 0.5, 0.5, 1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, NUM_VERTICES);
glBindVertexArray(0);
glUseProgram(0);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport( 0, 0, (GLsizei)w, (GLsizei)h);
}
void key(unsigned char key, int x, int y)
{
if (key == 27 || key == 'q' || key == 'Q') // quit requested
exit(0);
}
void mouseWheel(int wheel, int dir, int mouseX, int mouseY)
{
if (dir > 0)
scale += 0.05;
else
scale -= 0.05;
glUseProgram(program);
setUniformFloat(program, "scale", scale);
glUseProgram(0);
glutPostRedisplay();
}
int main(int argc, char *argv [])
{
// initialize glut
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
// prepare context for 3.3
glutInitContextVersion(3, 3);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
glutInitContextProfile (GLUT_CORE_PROFILE);
// create window
glutCreateWindow ("GL 3.3 demo");
glewExperimental = GL_TRUE;
glewInit();
if (!GLEW_VERSION_3_3)
{
printf("OpenGL 3.3 not supported.\n");
return 1;
}
// register handlers
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMouseWheelFunc(mouseWheel);
wglSwapIntervalEXT(1);
if (!GL_ARB_vertex_array_object)
printf("No VAO support\n");
vertexSource = textFileRead("Shaders/vertex.vs");
fragmentSource = textFileRead("Shaders/fragment.fs");
program = createProgram();
glUseProgram(program);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, NUM_VERTICES * VERTEX_SIZE, vertices, GL_STATIC_DRAW);
setAttrPtr(program, "position", 3, VERTEX_SIZE, (const GLvoid *)VERTEX_POSITION_OFFSET);
setUniformFloat(program, "scale", scale);
setAttrPtr(program, "color", 3, VERTEX_SIZE, (const GLvoid *)VERTEX_COLOR_OFFSET);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
glutMainLoop();
return 0;
}vertex.vs:
#version 330 core
uniform float scale;
in vec3 position;
in vec3 color;
out vec3 fragmentColor;
void main(void)
{
gl_Position = vec4(scale * position, 1.0);
fragmentColor = color;
}fragment.fs:
#version 330 core
in vec3 fragmentColor;
out vec4 color;
void main(void)
{
color = vec4(fragmentcolor, 1.0);
}Отредактировано clman (2011-02-05 21:31:14)